+def run_subcmd(subcmd):
+
+ c = (do_profile and [sys.executable, '-m', 'cProfile'] or []) + subcmd
+ if not (fix_stdout or fix_stderr):
+ os.execvp(c[0], c)
+
+ p = None
+ try:
+ p = subprocess.Popen(c,
+ stdout=PIPE if fix_stdout else sys.stdout,
+ stderr=PIPE if fix_stderr else sys.stderr,
+ preexec_fn=force_tty,
+ bufsize=4096,
+ close_fds=True)
+ # Assume p will receive these signals and quit, which will
+ # then cause us to quit.
+ for sig in (signal.SIGINT, signal.SIGTERM, signal.SIGQUIT):
+ signal.signal(sig, signal.SIG_IGN)
+
+ filter_output(fix_stdout and p.stdout.fileno() or None,
+ fix_stderr and p.stderr.fileno() or None,
+ fix_stdout and sys.stdout.fileno() or None,
+ fix_stderr and sys.stderr.fileno() or None)
+ return p.wait()
+ except BaseException as ex:
+ add_ex_tb(ex)
+ try:
+ if p and p.poll() == None:
+ os.kill(p.pid, signal.SIGTERM)
+ p.wait()
+ except BaseException as kill_ex:
+ raise chain_ex(add_ex_tb(kill_ex), ex)
+ raise ex
+
+wrap_main(lambda : run_subcmd(subcmd))