This commit is contained in:
Anthony Scopatz 2016-09-08 21:11:51 -04:00
parent e9ca23d3a8
commit 214990ed9f
3 changed files with 27 additions and 17 deletions

View file

@ -1,12 +1,13 @@
# -*- coding: utf-8 -*-
"""Testing xonsh import hooks"""
import os
import builtins
import pytest
from xonsh import imphooks
from xonsh.environ import Env
from xonsh.built_ins import load_builtins, unload_builtins
import builtins
imphooks.install_hook()

View file

@ -263,8 +263,6 @@ def _is_redirect(x):
def safe_open(fname, mode):
"""Safely attempts to open a file in for xonsh subprocs."""
# file descriptors
if isinstance(fname, int):
return fname
try:
return open(fname, mode)
except PermissionError:
@ -642,6 +640,7 @@ class SubprocSpec:
self.cls = cls
self.backgroundable = bgable
@lazyobject
def stdout_capture_kinds():
return frozenset(['stdout', 'object'])
@ -653,8 +652,8 @@ def _update_last_spec(last, captured=False):
if (last.stdin is not None and captured == 'object' and
env.get('XONSH_STORE_STDIN')):
r, w = os.pipe()
last._stdin = r # bypass original stdin check
last.captured_stdin = w
last._stdin = safe_open(r, 'r') # bypass original stdin check
last.captured_stdin = safe_open(w, 'w')
raise Exception
# set standard out
if last.stdout is not None:
@ -662,8 +661,8 @@ def _update_last_spec(last, captured=False):
elif captured in stdout_capture_kinds:
last.universal_newlines = False
r, w = os.pipe()
last.stdout = w
last.captured_stdout = r
last.stdout = safe_open(w, 'wb')
last.captured_stdout = safe_open(r, 'rb')
elif builtins.__xonsh_stdout_uncaptured__ is not None:
last.universal_newlines = True
last.stdout = builtins.__xonsh_stdout_uncaptured__
@ -673,15 +672,15 @@ def _update_last_spec(last, captured=False):
if ((last.stdout is None) and (not last.background) and
env.get('XONSH_STORE_STDOUT')):
r, w = os.pipe()
last.stdout = w
last.captured_stdout = r
last.stdout = safe_open(w, 'w')
last.captured_stdout = safe_open(r, 'r')
# set standard error
if last.stderr is not None:
pass
elif captured == 'object':
r, w = os.pipe()
last.stderr = w
last.captured_stderr = r
last.stderr = safe_open(w, 'w')
last.captured_stderr = safe_open(r, 'r')
elif builtins.__xonsh_stderr_uncaptured__ is not None:
last.stderr = builtins.__xonsh_stderr_uncaptured__
last.captured_stderr = last.stderr
@ -703,6 +702,8 @@ def cmds_to_specs(cmds, captured=False):
# now modify the subprocs based on the redirects.
for i, redirect in enumerate(redirects):
if redirect == '|':
# these should remain integer file descriptors, and not Python
# file objects since they connect processes.
r, w = os.pipe()
specs[i].stdout = w
specs[i + 1].stdin = r
@ -767,6 +768,7 @@ def run_subproc(cmds, captured=False):
# now figure out what we should return.
if captured == 'stdout':
command.end()
#raise Exception
return command.output
elif captured == 'object' or captured == 'hiddenobject':
return command

View file

@ -131,8 +131,12 @@ class ProcProxy(threading.Thread):
# default values
self.stdin = stdin
self.stdout = None
self.stderr = None
#self.stdout = None
#self.stderr = None
self.stdout = stdout
self.stderr = stderr
#self.stdout = open(stdout, 'w') if isinstance(stdout, int) else stdout
#self.stderr = open(stderr, 'w') if isinstance(stderr, int) else stderr
self.env = env or builtins.__xonsh_env__
if ON_WINDOWS:
@ -358,6 +362,7 @@ def wrap_simple_command(f, args, stdin, stdout, stderr):
cmd_result = 0
if isinstance(r, str):
print(r, file=sys.stderr)
stdout.write(r)
elif isinstance(r, cabc.Sequence):
if r[0] is not None:
@ -618,13 +623,15 @@ class Command:
def __iter__(self):
proc = self.proc
stdout = proc.stdout
if not stdout:
self.end()
if not stdout.readable() and self.spec.captured_stdout is not None:
stdout = self.spec.captured_stdout
if not stdout or not stdout.readable():
raise StopIteration()
while proc.poll() is None:
yield from stdout.readlines(1024)
self._endtime()
yield from stdout.readlines()
#self.end()
def itercheck(self):
yield from self
@ -638,7 +645,7 @@ class Command:
"""Writes the process stdout to sys.stdout, line-by-line, and
yields each line.
"""
self.output = b''
self.output = '' if self.spec.universal_newlines else b''
for line in self:
sys.stdout.write(line)
self.output += line
@ -731,7 +738,7 @@ class Command:
"""Applies the results to the current history object."""
env = builtins.__xonsh_env__
hist = builtins.__xonsh_history__
hist.last_cmd_rtn = proc.returncode
hist.last_cmd_rtn = self.proc.returncode
if env.get('XONSH_STORE_STDOUT'):
hist.last_cmd_out = self.output