[pypy-svn] r72892 - in pypy/trunk/pypy: tool tool/test translator/platform

arigo at codespeak.net arigo at codespeak.net
Fri Mar 26 12:44:38 CET 2010


Author: arigo
Date: Fri Mar 26 12:44:36 2010
New Revision: 72892

Added:
   pypy/trunk/pypy/tool/runsubprocess.py   (contents, props changed)
   pypy/trunk/pypy/tool/test/test_runsubprocess.py   (contents, props changed)
Modified:
   pypy/trunk/pypy/translator/platform/__init__.py
Log:
Workaround for the issue that os.fork() sometimes runs out of memory.


Added: pypy/trunk/pypy/tool/runsubprocess.py
==============================================================================
--- (empty file)
+++ pypy/trunk/pypy/tool/runsubprocess.py	Fri Mar 26 12:44:36 2010
@@ -0,0 +1,50 @@
+"""Run a subprocess.  Wrapper around the 'subprocess' module
+with a hack to prevent bogus out-of-memory conditions in os.fork()
+if the current process already grew very large.
+"""
+
+import sys, os
+from subprocess import PIPE, Popen
+
+def run_subprocess(executable, args, env=None, cwd=None):
+    return _run(executable, args, env, cwd)
+
+
+def _run(executable, args, env, cwd):   # unless overridden below
+    if isinstance(args, str):
+        args = str(executable) + ' ' + args
+        shell = True
+    else:
+        if args is None:
+            args = [str(executable)]
+        else:
+            args = [str(executable)] + args
+        shell = False
+    pipe = Popen(args, stdout=PIPE, stderr=PIPE, shell=shell, env=env, cwd=cwd)
+    stdout, stderr = pipe.communicate()
+    return pipe.returncode, stdout, stderr
+
+
+if __name__ == '__main__':
+    while True:
+        operation = sys.stdin.readline()
+        if not operation:
+            sys.exit()
+        assert operation.startswith('(')
+        args = eval(operation)
+        results = _run(*args)
+        sys.stdout.write('%r\n' % (results,))
+        sys.stdout.flush()
+
+
+if sys.platform != 'win32' and hasattr(os, 'fork'):
+    # do this at import-time, when the process is still tiny
+    _child_stdin, _child_stdout = os.popen2(
+        "'%s' '%s'" % (sys.executable, os.path.abspath(__file__)))
+
+    def _run(*args):
+        _child_stdin.write('%r\n' % (args,))
+        _child_stdin.flush()
+        results = _child_stdout.readline()
+        assert results.startswith('(')
+        return eval(results)

Added: pypy/trunk/pypy/tool/test/test_runsubprocess.py
==============================================================================
--- (empty file)
+++ pypy/trunk/pypy/tool/test/test_runsubprocess.py	Fri Mar 26 12:44:36 2010
@@ -0,0 +1,26 @@
+import py, os
+from pypy.tool.runsubprocess import run_subprocess
+
+def test_echo():
+    if not os.path.exists('/bin/echo'):
+        py.test.skip("there is no /bin/echo")
+    returncode, stdout, stderr = run_subprocess('/bin/echo', 'FooBar')
+    assert returncode == 0
+    assert stdout == 'FooBar\n'
+    assert stderr == ''
+
+def test_false():
+    if not os.path.exists('/bin/false'):
+        py.test.skip("there is no /bin/false")
+    returncode, stdout, stderr = run_subprocess('/bin/false', [])
+    assert returncode == 1
+    assert stdout == ''
+    assert stderr == ''
+
+def test_cat_fail():
+    if not os.path.exists('/bin/cat'):
+        py.test.skip("there is no /bin/cat")
+    returncode, stdout, stderr = run_subprocess('/bin/cat', 'no/such/filename')
+    assert returncode == 1
+    assert stdout == ''
+    assert 'no/such/filename' in stderr

Modified: pypy/trunk/pypy/translator/platform/__init__.py
==============================================================================
--- pypy/trunk/pypy/translator/platform/__init__.py	(original)
+++ pypy/trunk/pypy/translator/platform/__init__.py	Fri Mar 26 12:44:36 2010
@@ -10,21 +10,7 @@
 log = py.log.Producer("platform")
 py.log.setconsumer("platform", ansi_log)
 
-from subprocess import PIPE, Popen
-
-def _run_subprocess(executable, args, env=None, cwd=None):
-    if isinstance(args, str):
-        args = str(executable) + ' ' + args
-        shell = True
-    else:
-        if args is None:
-            args = [str(executable)]
-        else:
-            args = [str(executable)] + args
-        shell = False
-    pipe = Popen(args, stdout=PIPE, stderr=PIPE, shell=shell, env=env, cwd=cwd)
-    stdout, stderr = pipe.communicate()
-    return pipe.returncode, stdout, stderr
+from pypy.tool.runsubprocess import run_subprocess as _run_subprocess
 
 class CompilationError(Exception):
     def __init__(self, out, err):



More information about the Pypy-commit mailing list