Skip to content

Commit

Permalink
Merge pull request ipython#1879 from takluyver/i1878
Browse files Browse the repository at this point in the history
Correct stack depth for variable expansion in !system commands

Closes ipython#1878.
  • Loading branch information
fperez committed Jun 8, 2012
2 parents 09ae0f3 + 9e68e27 commit 93e6672
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
6 changes: 3 additions & 3 deletions IPython/core/interactiveshell.py
Expand Up @@ -2179,7 +2179,7 @@ def system_piped(self, cmd):
# we explicitly do NOT return the subprocess status code, because
# a non-None value would trigger :func:`sys.displayhook` calls.
# Instead, we store the exit_code in user_ns.
self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=2))
self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=1))

def system_raw(self, cmd):
"""Call the given cmd in a subprocess using os.system
Expand All @@ -2189,7 +2189,7 @@ def system_raw(self, cmd):
cmd : str
Command to execute.
"""
cmd = self.var_expand(cmd, depth=2)
cmd = self.var_expand(cmd, depth=1)
# protect os.system from UNC paths on Windows, which it can't handle:
if sys.platform == 'win32':
from IPython.utils._process_win32 import AvoidUNCPath
Expand Down Expand Up @@ -2229,7 +2229,7 @@ def getoutput(self, cmd, split=True):
if cmd.rstrip().endswith('&'):
# this is *far* from a rigorous test
raise OSError("Background processes not supported.")
out = getoutput(self.var_expand(cmd, depth=2))
out = getoutput(self.var_expand(cmd, depth=1))
if split:
out = SList(out.splitlines())
else:
Expand Down
18 changes: 18 additions & 0 deletions IPython/core/tests/test_interactiveshell.py
Expand Up @@ -239,6 +239,24 @@ def test_var_expand(self):
# This should not raise any exception:
ip.var_expand(u'echo $f')

def test_var_expand_local(self):
"""Test local variable expansion in !system and %magic calls"""
# !system
ip.run_cell('def test():\n'
' lvar = "ttt"\n'
' ret = !echo {lvar}\n'
' return ret[0]\n')
res = ip.user_ns['test']()
nt.assert_in('ttt', res)

# %magic
ip.run_cell('def makemacro():\n'
' macroname = "macro_var_expand_locals"\n'
' %macro {macroname} codestr\n')
ip.user_ns['codestr'] = "str(12)"
ip.run_cell('makemacro()')
nt.assert_in('macro_var_expand_locals', ip.user_ns)

def test_bad_var_expand(self):
"""var_expand on invalid formats shouldn't raise"""
# SyntaxError
Expand Down

0 comments on commit 93e6672

Please sign in to comment.