The Artima Developer Community
Sponsored Link

Python Answers Forum
getting values from an "exec" statement

2 replies on 1 page. Most recent reply: Jun 16, 2004 10:56 AM by Greg Jorgensen

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Toby Donaldson

Posts: 2
Nickname: toby
Registered: Jan, 2003

getting values from an "exec" statement Posted: Sep 25, 2003 5:21 PM
Reply to this message Reply
Advertisement
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


Evil Mr Henry

Posts: 1
Nickname: evilmrhenr
Registered: Jun, 2004

Re: getting values from an "exec" statement Posted: Jun 11, 2004 5:42 PM
Reply to this message Reply
Try this on for size:

the external file code.py:
for i in xrange(1, 5):
print i


the file that checks the returns:
import os
output = []

child_stdin, child_stdout, child_stderr = os.popen3("python code.py")
output = child_stdout.read()

for i in output:
print i



The only problem is that each character is in a separate array element in output[], so you would need to recombine them. This should be fairly easy. (You may also need to close the io streams opened)

I'm not quite sure how to give a timeout, but it would involve threads. Take a look at:
http://www.python.org/doc/current/lib/module-thread.html
and play around with the "sleep" command.

Greg Jorgensen

Posts: 65
Nickname: gregjor
Registered: Feb, 2004

Re: getting values from an "exec" statement Posted: Jun 16, 2004 10:56 AM
Reply to this message Reply
You can point standard output and standard error at a file-like string object, execute your code, then examine the captured strings:


import sys
import StringIO

# create file-like string to capture output
codeOut = StringIO.StringIO()
codeErr = StringIO.StringIO()

code = """
for i in xrange(0,15):
print i
"""

# capture output and errors
sys.stdout = codeOut
sys.stderr = codeErr

exec code

# restore stdout and stderr
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__

s = codeErr.getvalue()
if s:
print "error:\n%s\n" % s

s = codeOut.getvalue()
if s:
print "output:\n%s" % s

codeOut.close()
codeErr.close()


You can't time out code executed with the exec statement. You can wrap the above code in a function and execute it in a separate thread, but Python doesn't have any way for one thread to kill another. There is some commentary on that, and at least one threading library add-on that includes a kill function: use Google to search for "python kill thread" in Usenet.

Perhaps you can pre-process the code entered by the students and insert checkpoints inside loops and at other places, which will let the executed code check if it's supposed to terminate or not.

I thought about doing something like this -- allowing students to enter and execute Python code on a web page -- but decided it was too risky and hard to control. In the end my students installed Python on their own PCs and then uploaded their code for me and other students to look at.

Greg Jorgensen
PDXperts LLC - Portland, Oregon USA

Flat View: This topic has 2 replies on 1 page
Topic: Need someone to help me write a program Previous Topic   Next Topic Topic: profile

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use