Merge branch 'subproc_return_values' of https://github.com/adqm/xonsh into subproc_return_values

This commit is contained in:
adam j hartz 2016-03-06 11:58:19 -05:00
commit c9eb179ec3
4 changed files with 59 additions and 12 deletions

View file

@ -34,7 +34,7 @@ class DummyBaseShell(BaseShell):
class DummyShell:
def settitle():
def settitle(self):
pass
_shell = None

View file

@ -468,7 +468,7 @@ def _redirect_io(streams, r, loc=None):
raise XonshError('Unrecognized redirection command: {}'.format(r))
def run_subproc(cmds, captured=True):
def run_subproc(cmds, captured=False):
"""Runs a subprocess, in its many forms. This takes a list of 'commands,'
which may be a list of command line arguments or a string, representing
a special connecting character. For example::
@ -637,15 +637,41 @@ def run_subproc(cmds, captured=True):
hist.last_cmd_out = output
if not prev_is_proxy and hist.last_cmd_rtn > 0 and ENV.get('RAISE_SUBPROC_ERROR'):
raise CalledProcessError(hist.last_cmd_rtn, aliased_cmd, output=output)
if captured:
if captured=='stdout':
return output
elif captured=='object':
return CompletedProcess(prev_proc, output)
def subproc_captured(*cmds):
class CompletedProcess:
def __init__(self, proc, output=''):
self.output = output
self.proc = proc
def __bool__(self):
return self.proc.returncode == 0
def __eq__(self, other):
return self.proc.returncode == other
def __getattr__(self, attr):
if hasattr(self.proc, attr):
return getattr(self.proc, attr)
raise AttributeError('CompletedProcess instance has no attribute: %r' % attr)
def subproc_captured_stdout(*cmds):
"""Runs a subprocess, capturing the output. Returns the stdout
that was produced as a str.
"""
return run_subproc(cmds, captured=True)
return run_subproc(cmds, captured='stdout')
def subproc_captured_object(*cmds):
"""Runs a subprocess, capturing the output. Returns the stdout
that was produced as a str.
"""
return run_subproc(cmds, captured='object')
def subproc_uncaptured(*cmds):
@ -688,7 +714,8 @@ def load_builtins(execer=None, config=None):
if hasattr(builtins, 'quit'):
builtins.__xonsh_pyquit__ = builtins.quit
del builtins.quit
builtins.__xonsh_subproc_captured__ = subproc_captured
builtins.__xonsh_subproc_captured_stdout__ = subproc_captured_stdout
builtins.__xonsh_subproc_captured_object__ = subproc_captured_object
builtins.__xonsh_subproc_uncaptured__ = subproc_uncaptured
builtins.__xonsh_execer__ = execer
builtins.__xonsh_all_jobs__ = {}
@ -737,7 +764,8 @@ def unload_builtins():
'__xonsh_stderr_uncaptured__',
'__xonsh_pyexit__',
'__xonsh_pyquit__',
'__xonsh_subproc_captured__',
'__xonsh_subproc_captured_stdout__',
'__xonsh_subproc_captured_object__',
'__xonsh_subproc_uncaptured__',
'__xonsh_execer__',
'evalx',

View file

@ -211,6 +211,11 @@ def handle_question(state, token, stream):
n.string == '?' and n.start == token.end:
state['last'] = n
yield _new_token('DOUBLE_QUESTION', '??', token.start)
elif (n is not None and n.type == tokenize.OP
and n.string == '(' and n.start == token.end):
state['pymode'].append((False, '?(', ')', token.start))
state['last'] = n
yield _new_token('QUESTION_LPAREN', '?(', token.start)
else:
state['last'] = token
yield _new_token('QUESTION', '?', token.start)
@ -517,6 +522,7 @@ class Lexer(object):
'DOUBLE_QUESTION', # ??
'AT_LPAREN', # @(
'DOLLAR_NAME', # $NAME
'QUESTION_LPAREN', # ?(
'DOLLAR_LPAREN', # $(
'DOLLAR_LBRACE', # ${
'DOLLAR_LBRACKET', # $[

View file

@ -237,7 +237,8 @@ class BaseParser(object):
'at', 'lshift', 'rshift', 'pipe', 'xor', 'ampersand',
'for', 'colon', 'import', 'except', 'nonlocal', 'global',
'yield', 'from', 'raise', 'with', 'dollar_lparen',
'dollar_lbrace', 'dollar_lbracket', 'try']
'dollar_lbrace', 'dollar_lbracket', 'try',
'question_lparen']
for rule in tok_rules:
self._tok_rule(rule)
@ -1727,9 +1728,10 @@ class BaseParser(object):
"""atom : DOLLAR_NAME"""
p[0] = self._envvar_by_name(p[1][1:], lineno=self.lineno, col=self.col)
def p_atom_fisful_of_dollars(self, p):
def p_atom_fistful_of_dollars(self, p):
"""atom : dollar_lbrace_tok test RBRACE
| dollar_lparen_tok subproc RPAREN
| question_lparen_tok subproc RPAREN
| dollar_lbracket_tok subproc RBRACKET
"""
p[0] = self._dollar_rules(p)
@ -2037,7 +2039,10 @@ class BaseParser(object):
p0 = ast.Subscript(value=xenv, slice=idx, ctx=ast.Load(),
lineno=lineno, col_offset=col)
elif p1 == '$(':
p0 = xonsh_call('__xonsh_subproc_captured__', p2,
p0 = xonsh_call('__xonsh_subproc_captured_stdout__', p2,
lineno=lineno, col=col)
elif p1 == '?(':
p0 = xonsh_call('__xonsh_subproc_captured_object__', p2,
lineno=lineno, col=col)
elif p1 == '$[':
p0 = xonsh_call('__xonsh_subproc_uncaptured__', p2,
@ -2150,10 +2155,18 @@ class BaseParser(object):
p0._cliarg_action = 'splitlines'
p[0] = p0
def p_subproc_atom_captured(self, p):
def p_subproc_atom_captured_stdout(self, p):
"""subproc_atom : dollar_lparen_tok subproc RPAREN"""
p1 = p[1]
p0 = xonsh_call('__xonsh_subproc_captured__', args=p[2],
p0 = xonsh_call('__xonsh_subproc_captured_stdout__', args=p[2],
lineno=p1.lineno, col=p1.lexpos)
p0._cliarg_action = 'splitlines'
p[0] = p0
def p_subproc_atom_captured_object(self, p):
"""subproc_atom : question_lparen_tok subproc RPAREN"""
p1 = p[1]
p0 = xonsh_call('__xonsh_subproc_captured_object__', args=p[2],
lineno=p1.lineno, col=p1.lexpos)
p0._cliarg_action = 'splitlines'
p[0] = p0