コード例 #1
0
ファイル: shell.py プロジェクト: aig787/xonsh
 def default(self, line):
     """Implements code execution."""
     line = line if line.endswith('\n') else line + '\n'
     code = self.push(line)
     if code is None:
         return
     try:
         # Temporarily redirect stdout and stderr to save results in
         # history.
         with redirect_stdout(self.stdout), redirect_stderr(self.stderr):
             self.execer.exec(code, mode='single', glbs=self.ctx)  # no locals
         self.stdout.seek(0)
         self.stderr.seek(0)
         sys.stdout.write(self.stdout.read())
         sys.stderr.write(self.stderr.read())
         cmd = {}
         cmd['cmd']  = self.last
         self.stdout.seek(0)
         cmd['stdout'] = self.stdout.read()
         self.stderr.seek(0)
         cmd['stderr'] = self.stderr.read()
         self.stdout.seek(0)
         self.stdout.truncate()
         self.stderr.seek(0)
         self.stderr.truncate()
         builtins.__history__.add(cmd)
     except XonshError as e:
         print(e.args[0], file=sys.stderr, end='')
     except:
         traceback.print_exc()
     if builtins.__xonsh_exit__:
         return True
コード例 #2
0
    def wrapped_simple_command(args, stdin, stdout, stderr):
        try:
            i = stdin.read()
            if bgable:
                with redirect_stdout(stdout), redirect_stderr(stderr):
                    r = f(args, i)
            else:
                r = f(args, i)

            cmd_result = 0
            if isinstance(r, str):
                stdout.write(r)
            elif isinstance(r, Sequence):
                if r[0] is not None:
                    stdout.write(r[0])
                if r[1] is not None:
                    stderr.write(r[1])
                if len(r) > 2 and r[2] is not None:
                    cmd_result = r[2]
            elif r is not None:
                stdout.write(str(r))
            return cmd_result
        except Exception:
            print_exception()
            return 1  # returncode for failure
コード例 #3
0
ファイル: proc.py プロジェクト: AndreaCrotti/xonsh
    def wrapped_simple_command(args, stdin, stdout, stderr):
        try:
            i = stdin.read()
            if bgable:
                with redirect_stdout(stdout), redirect_stderr(stderr):
                    r = f(args, i)
            else:
                r = f(args, i)

            cmd_result = 0
            if isinstance(r, str):
                stdout.write(r)
            elif isinstance(r, abc.Sequence):
                if r[0] is not None:
                    stdout.write(r[0])
                if r[1] is not None:
                    stderr.write(r[1])
                if len(r) > 2 and r[2] is not None:
                    cmd_result = r[2]
            elif r is not None:
                stdout.write(str(r))
            return cmd_result
        except Exception:
            print_exception()
            return 1  # returncode for failure
コード例 #4
0
ファイル: kernel.py プロジェクト: Calysto/xonsh_kernel
 def _do_execute_direct(self, code):
     shell = builtins.__xonsh_shell__
     env = builtins.__xonsh_env__
     out = io.StringIO()
     err = io.StringIO()
     enc = env.get('XONSH_ENCODING')
     out = SpooledTemporaryFile(max_size=MAX_SIZE, mode='w+t',
                                encoding=enc, newline='\n')
     err = SpooledTemporaryFile(max_size=MAX_SIZE, mode='w+t',
                                encoding=enc, newline='\n')
     try:
         with redirect_stdout(out), redirect_stderr(err), \
              swap(builtins, '__xonsh_stdout_uncaptured__', out), \
              swap(builtins, '__xonsh_stderr_uncaptured__', err), \
              env.swap({'XONSH_STORE_STDOUT': False}):
             shell.default(code)
         interrupted = False
     except KeyboardInterrupt:
         interrupted = True
     output, error = '', ''
     if out.tell() > 0:
         out.seek(0)
         output = out.read()
     if err.tell() > 0:
         err.seek(0)
         error = err.read()
     out.close()
     err.close()
     return output, error, interrupted
コード例 #5
0
 def _do_execute_direct(self, code):
     shell = builtins.__xonsh_shell__
     env = builtins.__xonsh_env__
     out = io.StringIO()
     err = io.StringIO()
     enc = env.get('XONSH_ENCODING')
     out = SpooledTemporaryFile(max_size=MAX_SIZE,
                                mode='w+t',
                                encoding=enc,
                                newline='\n')
     err = SpooledTemporaryFile(max_size=MAX_SIZE,
                                mode='w+t',
                                encoding=enc,
                                newline='\n')
     try:
         with redirect_stdout(out), redirect_stderr(err), \
              swap(builtins, '__xonsh_stdout_uncaptured__', out), \
              swap(builtins, '__xonsh_stderr_uncaptured__', err), \
              env.swap({'XONSH_STORE_STDOUT': False}):
             shell.default(code)
         interrupted = False
     except KeyboardInterrupt:
         interrupted = True
     output, error = '', ''
     if out.tell() > 0:
         out.seek(0)
         output = out.read()
     if err.tell() > 0:
         err.seek(0)
         error = err.read()
     out.close()
     err.close()
     return output, error, interrupted
コード例 #6
0
ファイル: jupyter_kernel.py プロジェクト: mattdanbrown/xonsh
    def do_execute(self,
                   code,
                   silent,
                   store_history=True,
                   user_expressions=None,
                   allow_stdin=False):
        """Execute user code."""
        if len(code.strip()) == 0:
            return {
                'status': 'ok',
                'execution_count': self.execution_count,
                'payload': [],
                'user_expressions': {}
            }

        shell = builtins.__xonsh_shell__
        hist = builtins.__xonsh_history__
        out = io.StringIO()
        err = io.StringIO()
        try:
            with redirect_stdout(out), redirect_stderr(err):
                shell.default(code)
            interrupted = False
        except KeyboardInterrupt:
            interrupted = True

        if not silent:  # stdout response
            if out.tell() > 0:
                out.seek(0)
                response = {'name': 'stdout', 'text': out.read()}
                self.send_response(self.iopub_socket, 'stream', response)
            if err.tell() > 0:
                err.seek(0)
                response = {'name': 'stderr', 'text': err.read()}
                self.send_response(self.iopub_socket, 'stream', response)
            if len(hist) > 0 and out.tell() == 0 and err.tell() == 0:
                response = {'name': 'stdout', 'text': hist.outs[-1]}
                self.send_response(self.iopub_socket, 'stream', response)

        if interrupted:
            return {'status': 'abort', 'execution_count': self.execution_count}

        rtn = 0 if len(hist) == 0 else hist.rtns[-1]
        if 0 < rtn:
            message = {
                'status': 'error',
                'execution_count': self.execution_count,
                'ename': '',
                'evalue': str(rtn),
                'traceback': []
            }
        else:
            message = {
                'status': 'ok',
                'execution_count': self.execution_count,
                'payload': [],
                'user_expressions': {}
            }
        return message
コード例 #7
0
ファイル: jupyter_kernel.py プロジェクト: BlaXpirit/xonsh
    def do_execute(self, code, silent, store_history=True, user_expressions=None,
                   allow_stdin=False):
        """Execute user code."""
        if len(code.strip()) == 0:
            return {'status': 'ok', 'execution_count': self.execution_count,
                    'payload': [], 'user_expressions': {}}
        env = builtins.__xonsh_env__
        shell = builtins.__xonsh_shell__
        hist = builtins.__xonsh_history__
        enc = env.get('XONSH_ENCODING')
        out = SpooledTemporaryFile(max_size=MAX_SIZE, mode='w+t',
                                   encoding=enc, newline='\n')
        err = SpooledTemporaryFile(max_size=MAX_SIZE, mode='w+t',
                                   encoding=enc, newline='\n')
        try:
            with redirect_stdout(out), redirect_stderr(err), \
                 swap(builtins, '__xonsh_stdout_uncaptured__', out), \
                 swap(builtins, '__xonsh_stderr_uncaptured__', err), \
                 env.swap({'XONSH_STORE_STDOUT': False}):
                shell.default(code)
            interrupted = False
        except KeyboardInterrupt:
            interrupted = True

        if not silent:  # stdout response
            if out.tell() > 0:
                out.seek(0)
                self._respond_in_chunks('stdout', out.read())
            if err.tell() > 0:
                err.seek(0)
                self._respond_in_chunks('stderr', err.read())
            if hasattr(builtins, '_') and builtins._ is not None:
                # rely on sys.displayhook functionality
                self._respond_in_chunks('stdout', pformat(builtins._))
                builtins._ = None
            if len(hist) > 0 and out.tell() == 0 and err.tell() == 0:
                self._respond_in_chunks('stdout', hist.outs[-1])

        out.close()
        err.close()

        if interrupted:
            return {'status': 'abort', 'execution_count': self.execution_count}

        rtn = 0 if len(hist) == 0 else hist.rtns[-1]
        if 0 < rtn:
            message = {'status': 'error', 'execution_count': self.execution_count,
                       'ename': '', 'evalue': str(rtn), 'traceback': []}
        else:
            message = {'status': 'ok', 'execution_count': self.execution_count,
                       'payload': [], 'user_expressions': {}}
        return message
コード例 #8
0
ファイル: jupyter_kernel.py プロジェクト: sharondevop/xonsh
    def do_execute(self, code, silent, store_history=True, user_expressions=None,
                   allow_stdin=False):
        """Execute user code."""
        if len(code.strip()) == 0:
            return {'status': 'ok', 'execution_count': self.execution_count,
                    'payload': [], 'user_expressions': {}}
        env = builtins.__xonsh_env__
        shell = builtins.__xonsh_shell__
        hist = builtins.__xonsh_history__
        enc = env.get('XONSH_ENCODING')
        out = SpooledTemporaryFile(max_size=MAX_SIZE, mode='w+t',
                                   encoding=enc, newline='\n')
        err = SpooledTemporaryFile(max_size=MAX_SIZE, mode='w+t',
                                   encoding=enc, newline='\n')
        try:
            with redirect_stdout(out), redirect_stderr(err), \
                 swap(builtins, '__xonsh_stdout_uncaptured__', out), \
                 swap(builtins, '__xonsh_stderr_uncaptured__', err), \
                 env.swap({'XONSH_STORE_STDOUT': False}):
                shell.default(code)
            interrupted = False
        except KeyboardInterrupt:
            interrupted = True

        if not silent:  # stdout response
            if out.tell() > 0:
                out.seek(0)
                self._respond_in_chunks('stdout', out.read())
            if err.tell() > 0:
                err.seek(0)
                self._respond_in_chunks('stderr', err.read())
            if hasattr(builtins, '_') and builtins._ is not None:
                # rely on sys.displayhook functionality
                self._respond_in_chunks('stdout', pformat(builtins._))
                builtins._ = None
            if hist is not None and len(hist) > 0 and out.tell() == 0 and err.tell() == 0:
                self._respond_in_chunks('stdout', hist.outs[-1])

        out.close()
        err.close()

        if interrupted:
            return {'status': 'abort', 'execution_count': self.execution_count}

        rtn = 0 if (hist is None or len(hist) == 0) else hist.rtns[-1]
        if 0 < rtn:
            message = {'status': 'error', 'execution_count': self.execution_count,
                       'ename': '', 'evalue': str(rtn), 'traceback': []}
        else:
            message = {'status': 'ok', 'execution_count': self.execution_count,
                       'payload': [], 'user_expressions': {}}
        return message
コード例 #9
0
ファイル: jupyter_kernel.py プロジェクト: takluyver/xonsh
    def do_execute(self, code, silent, store_history=True, user_expressions=None, allow_stdin=False):
        """Execute user code."""
        if len(code.strip()) == 0:
            return {"status": "ok", "execution_count": self.execution_count, "payload": [], "user_expressions": {}}
        env = builtins.__xonsh_env__
        shell = builtins.__xonsh_shell__
        hist = builtins.__xonsh_history__
        enc = env.get("XONSH_ENCODING")
        out = SpooledTemporaryFile(max_size=MAX_SIZE, mode="w+t", encoding=enc, newline="\n")
        err = SpooledTemporaryFile(max_size=MAX_SIZE, mode="w+t", encoding=enc, newline="\n")
        try:
            with redirect_stdout(out), redirect_stderr(err), swap(builtins, "__xonsh_stdout_uncaptured__", out), swap(
                builtins, "__xonsh_stderr_uncaptured__", err
            ), env.swap({"XONSH_STORE_STDOUT": False}):
                shell.default(code)
            interrupted = False
        except KeyboardInterrupt:
            interrupted = True

        if not silent:  # stdout response
            if out.tell() > 0:
                out.seek(0)
                self._respond_in_chunks("stdout", out.read())
            if err.tell() > 0:
                err.seek(0)
                self._respond_in_chunks("stderr", err.read())
            if hasattr(builtins, "_") and builtins._ is not None:
                # rely on sys.displayhook functionality
                self._respond_in_chunks("stdout", pformat(builtins._))
                builtins._ = None
            if len(hist) > 0 and out.tell() == 0 and err.tell() == 0:
                self._respond_in_chunks("stdout", hist.outs[-1])

        out.close()
        err.close()

        if interrupted:
            return {"status": "abort", "execution_count": self.execution_count}

        rtn = 0 if len(hist) == 0 else hist.rtns[-1]
        if 0 < rtn:
            message = {
                "status": "error",
                "execution_count": self.execution_count,
                "ename": "",
                "evalue": str(rtn),
                "traceback": [],
            }
        else:
            message = {"status": "ok", "execution_count": self.execution_count, "payload": [], "user_expressions": {}}
        return message
コード例 #10
0
def _run_callable_subproc(alias,
                          args,
                          captured=True,
                          prev_proc=None,
                          stdout=None):
    """Helper for running callables as a subprocess."""
    # compute stdin for callable
    if prev_proc is None:
        stdin = None
    elif isinstance(prev_proc, ProcProxy):
        stdin = prev_proc.stdout
    else:
        stdin = StringIO(prev_proc.communicate()[0].decode(), None)
        stdin.seek(0)
        stdin, _ = stdin.read(), stdin.close()
    # Redirect the output streams temporarily. merge with possible
    # return values from alias function.
    if stdout is PIPE:
        # handles captured mode
        new_stdout, new_stderr = StringIO(), StringIO()
        with redirect_stdout(new_stdout), redirect_stderr(new_stderr):
            rtn = alias(args, stdin=stdin)
        proxy_stdout = new_stdout.getvalue()
        proxy_stderr = new_stderr.getvalue()
        if isinstance(rtn, str):
            proxy_stdout += rtn
        elif isinstance(rtn, Sequence):
            if rtn[0]:  # not None nor ''
                proxy_stdout += rtn[0]
            if rtn[1]:
                proxy_stderr += rtn[1]
        return ProcProxy(proxy_stdout, proxy_stderr)
    else:
        # handles uncaptured mode
        rtn = alias(args, stdin=stdin)
        rtnout, rtnerr = None, None
        if isinstance(rtn, str):
            rtnout = rtn
            sys.stdout.write(rtn)
        elif isinstance(rtn, Sequence):
            if rtn[0]:
                rtnout = rtn[0]
                sys.stdout.write(rtn[0])
            if rtn[1]:
                rtnerr = rtn[1]
                sys.stderr.write(rtn[1])
        return ProcProxy(rtnout, rtnerr)
コード例 #11
0
ファイル: proc.py プロジェクト: rbrewer123/xonsh
 def wrapped_simple_command(args, stdin, stdout, stderr):
     try:
         i = stdin.read()
         with redirect_stdout(stdout), redirect_stderr(stderr):
             r = f(args, i)
         if isinstance(r, str):
             stdout.write(r)
         elif isinstance(r, Sequence):
             if r[0] is not None:
                 stdout.write(r[0])
             if r[1] is not None:
                 stderr.write(r[1])
         elif r is not None:
             stdout.write(str(r))
         return True
     except:
         return False
コード例 #12
0
ファイル: proc.py プロジェクト: dboyliao/xonsh
 def wrapped_simple_command(args, stdin, stdout, stderr):
     try:
         i = stdin.read()
         with redirect_stdout(stdout), redirect_stderr(stderr):
             r = f(args, i)
         if isinstance(r, str):
             stdout.write(r)
         elif isinstance(r, Sequence):
             if r[0] is not None:
                 stdout.write(r[0])
             if r[1] is not None:
                 stderr.write(r[1])
         elif r is not None:
             stdout.write(str(r))
         return True
     except:
         return False
コード例 #13
0
ファイル: built_ins.py プロジェクト: chenesan/xonsh
def _run_callable_subproc(alias, args,
                          captured=True,
                          prev_proc=None,
                          stdout=None):
    """Helper for running callables as a subprocess."""
    # compute stdin for callable
    if prev_proc is None:
        stdin = None
    elif isinstance(prev_proc, ProcProxy):
        stdin = prev_proc.stdout
    else:
        stdin = StringIO(prev_proc.communicate()[0].decode(), None)
        stdin.seek(0)
        stdin, _ = stdin.read(), stdin.close()
    # Redirect the output streams temporarily. merge with possible
    # return values from alias function.
    if stdout is PIPE:
        # handles captured mode
        new_stdout, new_stderr = StringIO(), StringIO()
        with redirect_stdout(new_stdout), redirect_stderr(new_stderr):
            rtn = alias(args, stdin=stdin)
        proxy_stdout = new_stdout.getvalue()
        proxy_stderr = new_stderr.getvalue()
        if isinstance(rtn, str):
            proxy_stdout += rtn
        elif isinstance(rtn, Sequence):
            if rtn[0]:  # not None nor ''
                proxy_stdout += rtn[0]
            if rtn[1]:
                proxy_stderr += rtn[1]
        return ProcProxy(proxy_stdout, proxy_stderr)
    else:
        # handles uncaptured mode
        rtn = alias(args, stdin=stdin)
        rtnout, rtnerr = None, None
        if isinstance(rtn, str):
            rtnout = rtn
            sys.stdout.write(rtn)
        elif isinstance(rtn, Sequence):
            if rtn[0]:
                rtnout = rtn[0]
                sys.stdout.write(rtn[0])
            if rtn[1]:
                rtnerr = rtn[1]
                sys.stderr.write(rtn[1])
        return ProcProxy(rtnout, rtnerr)
コード例 #14
0
ファイル: proc.py プロジェクト: ArRolin/gitsome
 def wrapped_simple_command(args, stdin, stdout, stderr):
     try:
         i = stdin.read()
         with redirect_stdout(stdout), redirect_stderr(stderr):
             r = f(args, i)
         if isinstance(r, str):
             stdout.write(r)
         elif isinstance(r, Sequence):
             if r[0] is not None:
                 stdout.write(r[0])
             if r[1] is not None:
                 stderr.write(r[1])
         elif r is not None:
             stdout.write(str(r))
         return 0  # returncode for succees
     except Exception:
         print_exception()
         return 1  # returncode for failure
コード例 #15
0
 def wrapped_simple_command(args, stdin, stdout, stderr):
     try:
         i = stdin.read()
         with redirect_stdout(stdout), redirect_stderr(stderr):
             r = f(args, i)
         if isinstance(r, str):
             stdout.write(r)
         elif isinstance(r, Sequence):
             if r[0] is not None:
                 stdout.write(r[0])
             if r[1] is not None:
                 stderr.write(r[1])
         elif r is not None:
             stdout.write(str(r))
         return 0  # returncode for succees
     except Exception:
         print_exception()
         return 1  # returncode for failure
コード例 #16
0
ファイル: jupyter_kernel.py プロジェクト: blink1073/xonsh
    def do_execute(self, code, silent, store_history=True, user_expressions=None,
                   allow_stdin=False):
        """Execute user code."""
        if len(code.strip()) == 0:
            return {'status': 'ok', 'execution_count': self.execution_count,
                    'payload': [], 'user_expressions': {}}

        shell = builtins.__xonsh_shell__
        hist = builtins.__xonsh_history__
        out = io.StringIO()
        err = io.StringIO()
        try:
            with redirect_stdout(out), redirect_stderr(err):
                shell.default(code)
            interrupted = False
        except KeyboardInterrupt:
            interrupted = True

        if not silent:  # stdout response
            if out.tell() > 0:
                out.seek(0)
                response = {'name': 'stdout', 'text': out.read()}
                self.send_response(self.iopub_socket, 'stream', response)
            if err.tell() > 0:
                err.seek(0)
                response = {'name': 'stderr', 'text': err.read()}
                self.send_response(self.iopub_socket, 'stream', response)
            if len(hist) > 0 and out.tell() == 0 and err.tell() == 0:
                response = {'name': 'stdout', 'text': hist.outs[-1]}
                self.send_response(self.iopub_socket, 'stream', response)

        if interrupted:
            return {'status': 'abort', 'execution_count': self.execution_count}

        rtn = 0 if len(hist) == 0 else hist.rtns[-1]
        if 0 < rtn:
            message = {'status': 'error', 'execution_count': self.execution_count,
                       'ename': '', 'evalue': str(rtn), 'traceback': []}
        else:
            message = {'status': 'ok', 'execution_count': self.execution_count,
                       'payload': [], 'user_expressions': {}}
        return message
コード例 #17
0
    def run(self):
        arguments = self.arguments
        lines = ['.. code-block:: none', '']
        m, f = arguments[0].rsplit('.', 1)
        mod = importlib.import_module(m)
        func = getattr(mod, f)
        args = ['--help'] if len(arguments) == 1 else arguments[1:]
        stdout = io.StringIO()
        stderr = io.StringIO()
        with redirect_stdout(stdout), redirect_stderr(stderr):
            try:
                func(args)
            except SystemExit:
                pass
        stdout.seek(0)
        s = stdout.read()
        lines += textwrap.indent(s, '    ').splitlines()

        # hook to docutils
        src, lineno = self.state_machine.get_source_and_line(self.lineno)
        vl = ViewList(lines, source=src)
        node = nodes.paragraph()
        nested_parse_with_titles(self.state, vl, node)
        return node.children
コード例 #18
0
ファイル: cmdhelp.py プロジェクト: CJ-Wright/xonsh
    def run(self):
        arguments = self.arguments
        lines = ['.. code-block:: none', '']
        m, f = arguments[0].rsplit('.',  1)
        mod = importlib.import_module(m)
        func = getattr(mod, f)
        args = ['--help'] if len(arguments) == 1 else arguments[1:]
        stdout = io.StringIO()
        stderr = io.StringIO()
        with redirect_stdout(stdout), redirect_stderr(stderr):
            try:
                func(args)
            except SystemExit:
                pass
        stdout.seek(0)
        s = stdout.read()
        lines += textwrap.indent(s, '    ').splitlines()

        # hook to docutils
        src, lineno = self.state_machine.get_source_and_line(self.lineno)
        vl = ViewList(lines, source=src)
        node = nodes.paragraph()
        nested_parse_with_titles(self.state, vl, node)
        return node.children
コード例 #19
0
    def run(self):
        """Set up input/output streams and execute the child function in a new
        thread.  This is part of the `threading.Thread` interface and should
        not be called directly.
        """
        if self.f is None:
            return
        spec = self._wait_and_getattr("spec")
        last_in_pipeline = spec.last_in_pipeline
        if last_in_pipeline:
            capout = spec.captured_stdout  # NOQA
            caperr = spec.captured_stderr  # NOQA
        env = builtins.__xonsh__.env
        enc = env.get("XONSH_ENCODING")
        err = env.get("XONSH_ENCODING_ERRORS")
        if xp.ON_WINDOWS:
            if self.p2cread != -1:
                self.p2cread = xli.msvcrt.open_osfhandle(
                    self.p2cread.Detach(), 0)
            if self.c2pwrite != -1:
                self.c2pwrite = xli.msvcrt.open_osfhandle(
                    self.c2pwrite.Detach(), 0)
            if self.errwrite != -1:
                self.errwrite = xli.msvcrt.open_osfhandle(
                    self.errwrite.Detach(), 0)
        # get stdin
        if self.stdin is None:
            sp_stdin = None
        elif self.p2cread != -1:
            sp_stdin = io.TextIOWrapper(io.open(self.p2cread, "rb", -1),
                                        encoding=enc,
                                        errors=err)
        else:
            sp_stdin = sys.stdin
        # stdout
        if self.c2pwrite != -1:
            sp_stdout = io.TextIOWrapper(io.open(self.c2pwrite, "wb", -1),
                                         encoding=enc,
                                         errors=err)
        else:
            sp_stdout = sys.stdout
        # stderr
        if self.errwrite == self.c2pwrite:
            sp_stderr = sp_stdout
        elif self.errwrite != -1:
            sp_stderr = io.TextIOWrapper(io.open(self.errwrite, "wb", -1),
                                         encoding=enc,
                                         errors=err)
        else:
            sp_stderr = sys.stderr
        # run the function itself
        try:
            alias_stack = builtins.__xonsh__.env.get("__ALIAS_STACK", "")
            if self.env.get("__ALIAS_NAME"):
                alias_stack += ":" + self.env["__ALIAS_NAME"]

            with STDOUT_DISPATCHER.register(
                    sp_stdout
            ), STDERR_DISPATCHER.register(sp_stderr), xt.redirect_stdout(
                    STDOUT_DISPATCHER), xt.redirect_stderr(
                        STDERR_DISPATCHER), builtins.__xonsh__.env.swap(
                            __ALIAS_STACK=alias_stack):
                r = self.f(self.args, sp_stdin, sp_stdout, sp_stderr, spec,
                           spec.stack)
        except SystemExit as e:
            r = e.code if isinstance(e.code, int) else int(bool(e.code))
        except OSError:
            status = still_writable(self.c2pwrite) and still_writable(
                self.errwrite)
            if status:
                # stdout and stderr are still writable, so error must
                # come from function itself.
                xt.print_exception()
                r = 1
            else:
                # stdout and stderr are no longer writable, so error must
                # come from the fact that the next process in the pipeline
                # has closed the other side of the pipe. The function then
                # attempted to write to this side of the pipe anyway. This
                # is not truly an error and we should exit gracefully.
                r = 0
        except Exception:
            xt.print_exception()
            r = 1
        safe_flush(sp_stdout)
        safe_flush(sp_stderr)
        self.returncode = parse_proxy_return(r, sp_stdout, sp_stderr)
        if not last_in_pipeline and not xp.ON_WINDOWS:
            # mac requires us *not to* close the handles here while
            # windows requires us *to* close the handles here
            return
        # clean up
        # scopz: not sure why this is needed, but stdin cannot go here
        # and stdout & stderr must.
        handles = [self.stdout, self.stderr]
        for handle in handles:
            safe_fdclose(handle, cache=self._closed_handle_cache)