Skip to content

Instantly share code, notes, and snippets.

@sdiehl
Created April 21, 2011 04:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdiehl/933721 to your computer and use it in GitHub Desktop.
Save sdiehl/933721 to your computer and use it in GitHub Desktop.
exec_sandbox.py
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