pypy / pypy / tool /

"""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, gc
import 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
        if args is None:
            args = [str(executable)]
            args = [str(executable)] + args
        shell = False
    # Just before spawning the subprocess, do a gc.collect().  This
    # should help if we are running on top of PyPy, if the subprocess
    # is going to need a lot of RAM and we are using a lot too.
    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__':
    import gc
    while True:
        operation = sys.stdin.readline()
        if not operation:
        assert operation.startswith('(')
        args = eval(operation)
            results = _run(*args)
        except EnvironmentError, e:
            results = (None, str(e))
        sys.stdout.write('%r\n' % (results,))

if sys.platform != 'win32' and hasattr(os, 'fork'):
    # do this at import-time, when the process is still tiny
    _source = os.path.dirname(os.path.abspath(__file__))
    _source = os.path.join(_source, '')   # and not e.g. '.pyc'

    def spawn_subprocess():
        global _child
        _child = Popen([sys.executable, _source], bufsize=0,
                       stdin=PIPE, stdout=PIPE, close_fds=True)

    def cleanup_subprocess():
        global _child
        _child = None
    import atexit; atexit.register(cleanup_subprocess)

    def _run(*args):
            _child.stdin.write('%r\n' % (args,))
        except (OSError, IOError):
            # lost the child.  Try again...
            _child.stdin.write('%r\n' % (args,))
        results = _child.stdout.readline()
        assert results.startswith('(')
        results = eval(results)
        if results[0] is None:
            raise OSError('%s: %s\nargs=%r' % (args[0], results[1], args))
        return results
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.