| 1 |
"""Run a subprocess. Wrapper around the 'subprocess' module |
| 2 |
with a hack to prevent bogus out-of-memory conditions in os.fork() |
| 3 |
if the current process already grew very large. |
| 4 |
""" |
| 5 |
|
| 6 |
import sys |
| 7 |
import gc |
| 8 |
import os |
| 9 |
from subprocess import PIPE, Popen |
| 10 |
|
| 11 |
PY2 = (sys.version_info.major == 2) |
| 12 |
if PY2: |
| 13 |
text = unicode |
| 14 |
else: |
| 15 |
text = str |
| 16 |
|
| 17 |
def run_subprocess(executable, args, env=None, cwd=None): |
| 18 |
if isinstance(args, list) and sys.platform != 'win32': |
| 19 |
args = [a.encode('latin1') if isinstance(a, text) else a |
| 20 |
for a in args] |
| 21 |
return _run(executable, args, env, cwd) |
| 22 |
|
| 23 |
shell_default = False |
| 24 |
if sys.platform == 'win32': |
| 25 |
shell_default = True |
| 26 |
|
| 27 |
def _run(executable, args, env, cwd): |
| 28 |
|
| 29 |
|
| 30 |
if sys.platform == 'win32': |
| 31 |
executable = executable.replace('/','\\') |
| 32 |
if isinstance(args, str): |
| 33 |
args = str(executable) + ' ' + args |
| 34 |
shell = True |
| 35 |
else: |
| 36 |
if args is None: |
| 37 |
args = [str(executable)] |
| 38 |
else: |
| 39 |
args = [str(executable)] + args |
| 40 |
|
| 41 |
|
| 42 |
shell = shell_default |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
gc.collect() |
| 48 |
|
| 49 |
pipe = Popen(args, stdout=PIPE, stderr=PIPE, shell=shell, env=env, cwd=cwd) |
| 50 |
stdout, stderr = pipe.communicate() |
| 51 |
if (sys.platform == 'win32' and pipe.returncode == 1 and |
| 52 |
b'is not recognized' in stderr): |
| 53 |
|
| 54 |
raise EnvironmentError(stderr) |
| 55 |
return pipe.returncode, stdout, stderr |
| 56 |
|
| 57 |
|
| 58 |
if __name__ == '__main__': |
| 59 |
while True: |
| 60 |
gc.collect() |
| 61 |
operation = sys.stdin.readline() |
| 62 |
if not operation: |
| 63 |
sys.exit() |
| 64 |
assert operation.startswith('(') |
| 65 |
args = eval(operation) |
| 66 |
try: |
| 67 |
results = _run(*args) |
| 68 |
except EnvironmentError as e: |
| 69 |
results = (None, str(e)) |
| 70 |
sys.stdout.write('%r\n' % (results,)) |
| 71 |
sys.stdout.flush() |
| 72 |
|
| 73 |
|
| 74 |
if sys.platform != 'win32' and hasattr(os, 'fork') and not os.getenv("PYPY_DONT_RUN_SUBPROCESS", None): |
| 75 |
|
| 76 |
_source = os.path.dirname(os.path.abspath(__file__)) |
| 77 |
_source = os.path.join(_source, 'runsubprocess.py') |
| 78 |
|
| 79 |
def spawn_subprocess(): |
| 80 |
global _child, child_stdin, child_stdout |
| 81 |
_child = Popen([sys.executable, _source], bufsize=0, |
| 82 |
stdin=PIPE, stdout=PIPE, close_fds=True) |
| 83 |
if PY2: |
| 84 |
child_stdin = _child.stdin |
| 85 |
child_stdout = _child.stdout |
| 86 |
else: |
| 87 |
|
| 88 |
|
| 89 |
from io import TextIOWrapper |
| 90 |
child_stdin = TextIOWrapper(_child.stdin, |
| 91 |
newline=sys.stdin.newlines, |
| 92 |
write_through=True) |
| 93 |
child_stdout = TextIOWrapper(_child.stdout, |
| 94 |
newline=sys.stdout.newlines) |
| 95 |
spawn_subprocess() |
| 96 |
|
| 97 |
def cleanup_subprocess(): |
| 98 |
global _child, child_stdin, child_stdout |
| 99 |
_child = None |
| 100 |
child_stdin = None |
| 101 |
child_stdout = None |
| 102 |
import atexit; atexit.register(cleanup_subprocess) |
| 103 |
|
| 104 |
def _run(*args): |
| 105 |
try: |
| 106 |
child_stdin.write('%r\n' % (args,)) |
| 107 |
except (OSError, IOError): |
| 108 |
|
| 109 |
spawn_subprocess() |
| 110 |
child_stdin.write('%r\n' % (args,)) |
| 111 |
results = child_stdout.readline() |
| 112 |
assert results.startswith('(') |
| 113 |
results = eval(results) |
| 114 |
if results[0] is None: |
| 115 |
raise OSError('%s: %s\nargs=%r' % (args[0], results[1], args)) |
| 116 |
return results |