[pypy-svn] r45465 - pypy/dist/pypy/translator/sandbox

arigo at codespeak.net arigo at codespeak.net
Thu Aug 2 19:35:30 CEST 2007


Author: arigo
Date: Thu Aug  2 19:35:30 2007
New Revision: 45465

Added:
   pypy/dist/pypy/translator/sandbox/interact.py   (contents, props changed)
Modified:
   pypy/dist/pypy/translator/sandbox/sandlib.py
Log:
A wrapper tool for CPython to run a sandboxed subprocess: stdin, stdout,
stderr are redirected from the parent to the subprocess, but all other
external functions are forbidden.

A sandboxed pyrolog-c can be run in this way.


Added: pypy/dist/pypy/translator/sandbox/interact.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/sandbox/interact.py	Thu Aug  2 19:35:30 2007
@@ -0,0 +1,17 @@
+#! /usr/bin/env python
+
+"""Interacts with a subprocess translated with --sandbox.
+The subprocess is only allowed to use stdin/stdout/stderr.
+
+Usage:
+    interact.py <executable> <args...>
+"""
+
+import sys
+from pypy.translator.sandbox.sandlib import SimpleIOSandboxedProc
+
+if __name__ == '__main__':
+    if len(sys.argv) < 2:
+        print >> sys.stderr, __doc_
+        sys.exit(2)
+    SimpleIOSandboxedProc(sys.argv[1:]).interact()

Modified: pypy/dist/pypy/translator/sandbox/sandlib.py
==============================================================================
--- pypy/dist/pypy/translator/sandbox/sandlib.py	(original)
+++ pypy/dist/pypy/translator/sandbox/sandlib.py	Thu Aug  2 19:35:30 2007
@@ -92,6 +92,18 @@
         self._error = None
         return (output, error)
 
+    def interact(self, stdin=None, stdout=None, stderr=None):
+        """Interact with the subprocess.  By default, stdin, stdout and
+        stderr are set to the ones from 'sys'."""
+        import sys
+        self._input  = stdin  or sys.stdin
+        self._output = stdout or sys.stdout
+        self._error  = stderr or sys.stderr
+        self.handle_forever()
+        self._input = None
+        self._output = None
+        self._error = None
+
     def do_read(self, fd, size):
         if fd == 0:
             if self._input is None:


More information about the pypy-svn mailing list