I have some buggy OCaml programs in strings, and I am using Python subprocess to check the feedback from the compiler. However, because there are some programs contain infinite loops, subprocess stuck even I set timeout field.
Environment: Windows 10, Python 3.5.1
This is the code I have tried:
annotated_prog = "let rec ww : ('a -> 'a * bool) * 'a -> 'a = fun (f,b) -> let (b',c') = f b in if c' then ww (f, b') else b';;\n let _ = let f x = let xx = (x * x) * x in (xx, (xx < 100)) in ww (f, 1);;"
try:
error_output = subprocess.run(["ocaml"], input = annotated_prog,
stdout=subprocess.PIPE,universal_newlines = True, timeout=1)
except TimeoutExpired:
error_output = None
However, the code still goes to infinite loop and cannot work.
Another way I have tried:
def run(cmd, timeout_sec):
proc = subprocess.Popen(["ocaml"], stdout=subprocess.PIPE,
stderr=subprocess.PIPE,universal_newlines = True)
proc.communicate(timeout = timeout_sec)
print(proc.communicate("let a = b;;\n")[0])
This doesn't even give a correct output:
what I expect: Characters 8-9: Error: Unbound value b
what I got: OCaml version 4.02.3
In this way how can I even get the correct output from python?
def run(timeout_sec):
proc = subprocess.Popen(["python"], stdout=subprocess.PIPE,
stderr=subprocess.PIPE,universal_newlines = True)
print(cmd)
print(proc.communicate("while True: print('hello')\n", timeout = timeout_sec)[0])
try:
error_output =run(1)
except subprocess.TimeoutExpired:
print('timeout')
print('error_output')
What I expect: 'time out' with or w/o a bunch of hellos What I got: None