getting values from an "exec" statement
Toby Donaldson
tjd at sfu.ca
Tue Sep 30 19:13:09 EDT 2003
Hi all,
I'm designing an educational application that will run Python code and
check the output against a pre-define answer. I want to use the "exec"
statement to run the code, but I don't know how to get output from it.
For instance, exec works like this:
>>> code = """
for i in xrange(1, 5):
print i
"""
>>> exec code
1
2
3
4
I want to store the values output by the print statement in a list. Is
there anyway to re-direct the output of the exec statement?
Also, it would be nice if exec had a timeout that automatically
haulted code that ran for too long. Is there a standard trick for
this? I expect I would have to run it in its own thread and kill the
thread when it takes too long (which reminds me I don't know anything
about Python threads!).
Toby
More information about the Python-list
mailing list