use*_*178 2 python infinite-loop halting-problem
我正在尝试创建一个函数来识别python文件中的代码是否会通过无限循环.这是我到目前为止:
def reader(filename):
myfile = open(filename)
counter = 0
#counters the number of lines in the file
for line in myfile:
counter +=1
print line
#print number of lines in file
print counter
#execute the code in file
execution = execfile(filename)
Run Code Online (Sandbox Code Playgroud)
我要做的是执行文件,并尝试计算执行的行数,并将其与前一个计数器中的任何数字进行比较.例如,如果计数器> lines_exected,则返回True,代码中存在无限循环.这会有用吗?或者我还需要尝试别的吗?