Here's my situation:
I have a program generator that generates a lot of random programs (these programs run on a single thread so nothing fancy) with loops. I need to quickly check them against a few test-cases for correctness.
However, since these programs are random, they can be mal-formed and contain infinite loops, so I need a timeout mechanism to timeout and fail these bad programs quickly so I can continue testing the next program.
I am using the timeout.py decorator in this post: How to timeout function in python, timeout less than a second
I ran into issue with this particular program that refused to timeout, here's a script:
from timeout import *
@timeout(0.1)
def f ( x0 ) :
x1 = [ ]
x2 = [ 0.0 ]
while x1 < x2 :
if x1 == 0 :
x3 [ 0 ] = 0.0
elif x1 == 1 :
x3 . append ( x0 [ x1 ] )
else :
for x2 in range ( 1 , len ( x0 ) ) :
x3 . append ( x0 [ x1 ] * ( x1 - 1 ) )
x1 = x1 + 1
return x3
f([1,2,3,4,5])
I have no idea what is going on, but it appears the program got stuck in some part of the process that become unresponsive to the timeout signal mechanisms.
I have found a very strange manual work-around to get this particular program to timeout:
@timeout(0.1)
def f1 ( x0 ) :
print "1"
x1 = [ ]
print "2"
x2 = [ 0.0 ]
print "3"
while x1 < x2 :
print "4"
if x1 == 0 :
print "5"
x3 [ 0 ] = 0.0
print "6"
elif x1 == 1 :
print "7"
x3 . append ( x0 [ x1 ] )
print "8"
else :
print "9"
for x2 in range ( 1 , len ( x0 ) ) :
print "10"
x3 . append ( x0 [ x1 ] * ( x1 - 1 ) )
print "11"
x1 = x1 + 1
print "12"
return x3
Again, I am uncertain why did the print statement forced it to timeout properly, but it is impractical to manually insert these print statements and it is way too troublesome to edit either the string form of the program or the AST of the program to insert these prints just for the sake of timeout.
In conclusion, given the fact that I need to quickly test these programs with timeout, what is the best solution? Thanks in advance.
I'm running ubuntu 14.04 (as I understand these signals are system specific)