Created
April 21, 2011 04:32
-
-
Save sdiehl/933721 to your computer and use it in GitHub Desktop.
exec_sandbox.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from cStringIO import StringIO | |
sys.path = [ | |
'/opt/pypy-sandbox', | |
'/opt/pypy-sandbox/lib_pypy', | |
'/opt/pypy-sandbox/lib-python/modified-2.5.2', | |
'/opt/pypy-sandbox/lib-python/2.5.2', | |
'/opt/pypy-sandbox/lib-python/2.5.2/plat-linux2' | |
] | |
from pypy.translator.sandbox import pypy_interact | |
TIMEOUT = 5 | |
SANDBOX_BIN = 'pypy-sandbox' | |
def exec_sandbox(code): | |
sandproc = pypy_interact.PyPySandboxedProc( | |
SANDBOX_BIN, | |
['-c', code,'--timeout',str(TIMEOUT)] | |
) | |
try: | |
code_output = StringIO() | |
sandproc.interact(stdout=code_output, stderr=code_output) | |
return code_output.getvalue() | |
except: | |
sandproc.kill() | |
finally: | |
sandproc.kill() | |
return 'Error, could not evaluate' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment