[pypy-svn] r46388 - in pypy/dist/pypy/translator: goal sandbox

arigo at codespeak.net arigo at codespeak.net
Fri Sep 7 11:58:52 CEST 2007


Author: arigo
Date: Fri Sep  7 11:58:51 2007
New Revision: 46388

Modified:
   pypy/dist/pypy/translator/goal/targetpypystandalone.py
   pypy/dist/pypy/translator/sandbox/pypy_interact.py
Log:
Add a --heapsize option to pypy_interact that
uses a kind-of-hidden --heapsize option of the
sandboxed subprocess.


Modified: pypy/dist/pypy/translator/goal/targetpypystandalone.py
==============================================================================
--- pypy/dist/pypy/translator/goal/targetpypystandalone.py	(original)
+++ pypy/dist/pypy/translator/goal/targetpypystandalone.py	Fri Sep  7 11:58:51 2007
@@ -31,6 +31,13 @@
         #debug("entry point starting") 
         #for arg in argv: 
         #    debug(" argv -> " + arg)
+        if len(argv) > 2 and argv[1] == '--heapsize':
+            # Undocumented option, handled at interp-level.
+            # It has silently no effect in non-Boehm translations.
+            # At the moment this option exists mainly to support sandboxing.
+            from pypy.rlib import rgc
+            rgc.set_max_heap_size(int(argv[2]))
+            argv = argv[:1] + argv[3:]
         try:
             try:
                 space.call_function(w_run_toplevel, w_call_startup_gateway)

Modified: pypy/dist/pypy/translator/sandbox/pypy_interact.py
==============================================================================
--- pypy/dist/pypy/translator/sandbox/pypy_interact.py	(original)
+++ pypy/dist/pypy/translator/sandbox/pypy_interact.py	Fri Sep  7 11:58:51 2007
@@ -8,6 +8,9 @@
 Options:
     --tmp=DIR     the real directory that corresponds to the virtual /tmp,
                   which is the virtual current dir (always read-only for now)
+    --heapsize=N  limit memory usage to N bytes, or kilo- mega- giga-bytes
+                  with the 'k', 'm' or 'g' suffix respectively.
+                  ATM this only works if the sandboxed executable uses Boehm.
 """
 
 import sys, os
@@ -53,8 +56,10 @@
 
 if __name__ == '__main__':
     from getopt import getopt      # and not gnu_getopt!
-    options, arguments = getopt(sys.argv[1:], 't:h', ['tmp=', 'help'])
+    options, arguments = getopt(sys.argv[1:], 't:h', 
+                                ['tmp=', 'heapsize=', 'help'])
     tmpdir = None
+    extraoptions = []
 
     def help():
         print >> sys.stderr, __doc__
@@ -66,6 +71,21 @@
             if not os.path.isdir(value):
                 raise OSError("%r is not a directory" % (value,))
             tmpdir = value
+        elif option == '--heapsize':
+            value = value.lower()
+            if value.endswith('k'):
+                bytes = int(value[:-1]) * 1024
+            elif value.endswith('m'):
+                bytes = int(value[:-1]) * 1024 * 1024
+            elif value.endswith('g'):
+                bytes = int(value[:-1]) * 1024 * 1024 * 1024
+            else:
+                bytes = int(value)
+            if bytes <= 0:
+                raise ValueError
+            if bytes > sys.maxint:
+                raise OverflowError("--heapsize maximum is %d" % sys.maxint)
+            extraoptions[:0] = ['--heapsize', str(bytes)]
         elif option in ['-h', '--help']:
             help()
         else:
@@ -74,5 +94,6 @@
     if len(arguments) < 1:
         help()
 
-    sandproc = PyPySandboxedProc(arguments[0], arguments[1:], tmpdir=tmpdir)
+    sandproc = PyPySandboxedProc(arguments[0], extraoptions + arguments[1:],
+                                 tmpdir=tmpdir)
     sandproc.interact()



More information about the Pypy-commit mailing list