diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index 9b20a90..3a3e885 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -2036,7 +2036,7 @@ class InteractiveShell(SingletonConfigurable): # FIXME: Move the color initialization to the DisplayHook, which # should be split into a prompt manager and displayhook. We probably # even need a centralize colors management object. - self.magic('colors %s' % self.colors) + self.run_line_magic('colors', self.colors) # Defined here so that it's included in the documentation @functools.wraps(magic.MagicsManager.register_function) @@ -2044,7 +2044,7 @@ class InteractiveShell(SingletonConfigurable): self.magics_manager.register_function(func, magic_kind=magic_kind, magic_name=magic_name) - def run_line_magic(self, magic_name, line, back_compat=False): + def run_line_magic(self, magic_name, line, _stack_depth=1): """Execute the given line magic. Parameters @@ -2055,8 +2055,8 @@ class InteractiveShell(SingletonConfigurable): line : str The rest of the input line as a single string. - back_compat : bool - If run_line_magic() is called from magic() then it should be 'True'. + _stack_depth : int + If run_line_magic() is called from magic() then _stack_depth=2. This is added to ensure backward compatibility for use of 'get_ipython().magic()' """ fn = self.find_line_magic(magic_name) @@ -2070,13 +2070,9 @@ class InteractiveShell(SingletonConfigurable): # Note: this is the distance in the stack to the user's frame. # This will need to be updated if the internal calling logic gets # refactored, or else we'll be expanding the wrong variables. + # Determine stack_depth depending on where run_line_magic() has been called - if back_compat: - # run_line_magic() called directly using 'get_ipython().magic()' - stack_depth = 2 - else: - # run_line_magic() called directly using 'get_ipython().run_line_magic()' - stack_depth = 1 + stack_depth = _stack_depth magic_arg_s = self.var_expand(line, stack_depth) # Put magic args in a list so we can call with f(*a) syntax args = [magic_arg_s] @@ -2164,7 +2160,7 @@ class InteractiveShell(SingletonConfigurable): # TODO: should we issue a loud deprecation warning here? magic_name, _, magic_arg_s = arg_s.partition(' ') magic_name = magic_name.lstrip(prefilter.ESC_MAGIC) - return self.run_line_magic(magic_name, magic_arg_s, back_compat=True) + return self.run_line_magic(magic_name, magic_arg_s, _stack_depth=2) #------------------------------------------------------------------------- # Things related to macros diff --git a/IPython/core/tests/test_handlers.py b/IPython/core/tests/test_handlers.py index 75f38e2..45409d9 100644 --- a/IPython/core/tests/test_handlers.py +++ b/IPython/core/tests/test_handlers.py @@ -53,7 +53,7 @@ def test_handlers(): # line. run([(i,py3compat.u_format(o)) for i,o in \ [('"no change"', '"no change"'), # normal - (u"lsmagic", "get_ipython().run_line_magic({u}'lsmagic', {u}'')"), # magic + (u"lsmagic", "get_ipython().run_line_magic('lsmagic', '')"), # magic #("a = b # PYTHON-MODE", '_i'), # emacs -- avoids _in cache ]]) diff --git a/IPython/core/tests/test_inputsplitter.py b/IPython/core/tests/test_inputsplitter.py index afb1a3c..d528078 100644 --- a/IPython/core/tests/test_inputsplitter.py +++ b/IPython/core/tests/test_inputsplitter.py @@ -568,7 +568,7 @@ class CellMagicsCommon(object): def test_whole_cell(self): src = "%%cellm line\nbody\n" out = self.sp.transform_cell(src) - ref = u"get_ipython().run_cell_magic({u}'cellm', {u}'line', {u}'body')\n" + ref = u"get_ipython().run_cell_magic('cellm', 'line', 'body')\n" nt.assert_equal(out, py3compat.u_format(ref)) def test_cellmagic_help(self): diff --git a/IPython/core/tests/test_inputtransformer.py b/IPython/core/tests/test_inputtransformer.py index 5459a5a..90a1d5a 100644 --- a/IPython/core/tests/test_inputtransformer.py +++ b/IPython/core/tests/test_inputtransformer.py @@ -39,16 +39,16 @@ def transform_checker(tests, transformer, **kwargs): syntax = \ dict(assign_system = [(i,py3compat.u_format(o)) for i,o in \ - [(u'a =! ls', "a = get_ipython().getoutput({u}'ls')"), - (u'b = !ls', "b = get_ipython().getoutput({u}'ls')"), - (u'c= !ls', "c = get_ipython().getoutput({u}'ls')"), + [(u'a =! ls', "a = get_ipython().getoutput('ls')"), + (u'b = !ls', "b = get_ipython().getoutput('ls')"), + (u'c= !ls', "c = get_ipython().getoutput('ls')"), (u'd == !ls', u'd == !ls'), # Invalid syntax, but we leave == alone. ('x=1', 'x=1'), # normal input is unmodified (' ',' '), # blank lines are kept intact # Tuple unpacking - (u"a, b = !echo 'a\\nb'", u"a, b = get_ipython().getoutput({u}\"echo 'a\\\\nb'\")"), - (u"a,= !echo 'a'", u"a, = get_ipython().getoutput({u}\"echo 'a'\")"), - (u"a, *bc = !echo 'a\\nb\\nc'", u"a, *bc = get_ipython().getoutput({u}\"echo 'a\\\\nb\\\\nc'\")"), + (u"a, b = !echo 'a\\nb'", u"a, b = get_ipython().getoutput(\"echo 'a\\\\nb'\")"), + (u"a,= !echo 'a'", u"a, = get_ipython().getoutput(\"echo 'a'\")"), + (u"a, *bc = !echo 'a\\nb\\nc'", u"a, *bc = get_ipython().getoutput(\"echo 'a\\\\nb\\\\nc'\")"), # Tuple unpacking with regular Python expressions, not our syntax. (u"a, b = range(2)", u"a, b = range(2)"), (u"a, = range(1)", u"a, = range(1)"), @@ -57,13 +57,13 @@ syntax = \ assign_magic = [(i,py3compat.u_format(o)) for i,o in \ - [(u'a =% who', "a = get_ipython().run_line_magic({u}'who', {u}'')"), - (u'b = %who', "b = get_ipython().run_line_magic({u}'who', {u}'')"), - (u'c= %ls', "c = get_ipython().run_line_magic({u}'ls', {u}'')"), + [(u'a =% who', "a = get_ipython().run_line_magic('who', '')"), + (u'b = %who', "b = get_ipython().run_line_magic('who', '')"), + (u'c= %ls', "c = get_ipython().run_line_magic('ls', '')"), (u'd == %ls', u'd == %ls'), # Invalid syntax, but we leave == alone. ('x=1', 'x=1'), # normal input is unmodified (' ',' '), # blank lines are kept intact - (u"a, b = %foo", u"a, b = get_ipython().run_line_magic({u}'foo', {u}'')"), + (u"a, b = %foo", u"a, b = get_ipython().run_line_magic('foo', '')"), ]], classic_prompt = @@ -87,53 +87,53 @@ syntax = \ # System calls escaped_shell = [(i,py3compat.u_format(o)) for i,o in \ - [ (u'!ls', "get_ipython().system({u}'ls')"), + [ (u'!ls', "get_ipython().system('ls')"), # Double-escape shell, this means to capture the output of the # subprocess and return it - (u'!!ls', "get_ipython().getoutput({u}'ls')"), + (u'!!ls', "get_ipython().getoutput('ls')"), ]], # Help/object info escaped_help = [(i,py3compat.u_format(o)) for i,o in \ [ (u'?', 'get_ipython().show_usage()'), - (u'?x1', "get_ipython().run_line_magic({u}'pinfo', {u}'x1')"), - (u'??x2', "get_ipython().run_line_magic({u}'pinfo2', {u}'x2')"), - (u'?a.*s', "get_ipython().run_line_magic({u}'psearch', {u}'a.*s')"), - (u'?%hist1', "get_ipython().run_line_magic({u}'pinfo', {u}'%hist1')"), - (u'?%%hist2', "get_ipython().run_line_magic({u}'pinfo', {u}'%%hist2')"), - (u'?abc = qwe', "get_ipython().run_line_magic({u}'pinfo', {u}'abc')"), + (u'?x1', "get_ipython().run_line_magic('pinfo', 'x1')"), + (u'??x2', "get_ipython().run_line_magic('pinfo2', 'x2')"), + (u'?a.*s', "get_ipython().run_line_magic('psearch', 'a.*s')"), + (u'?%hist1', "get_ipython().run_line_magic('pinfo', '%hist1')"), + (u'?%%hist2', "get_ipython().run_line_magic('pinfo', '%%hist2')"), + (u'?abc = qwe', "get_ipython().run_line_magic('pinfo', 'abc')"), ]], end_help = [(i,py3compat.u_format(o)) for i,o in \ - [ (u'x3?', "get_ipython().run_line_magic({u}'pinfo', {u}'x3')"), - (u'x4??', "get_ipython().run_line_magic({u}'pinfo2', {u}'x4')"), - (u'%hist1?', "get_ipython().run_line_magic({u}'pinfo', {u}'%hist1')"), - (u'%hist2??', "get_ipython().run_line_magic({u}'pinfo2', {u}'%hist2')"), - (u'%%hist3?', "get_ipython().run_line_magic({u}'pinfo', {u}'%%hist3')"), - (u'%%hist4??', "get_ipython().run_line_magic({u}'pinfo2', {u}'%%hist4')"), - (u'f*?', "get_ipython().run_line_magic({u}'psearch', {u}'f*')"), - (u'ax.*aspe*?', "get_ipython().run_line_magic({u}'psearch', {u}'ax.*aspe*')"), - (u'a = abc?', "get_ipython().set_next_input({u}'a = abc');" - "get_ipython().run_line_magic({u}'pinfo', {u}'abc')"), - (u'a = abc.qe??', "get_ipython().set_next_input({u}'a = abc.qe');" - "get_ipython().run_line_magic({u}'pinfo2', {u}'abc.qe')"), - (u'a = *.items?', "get_ipython().set_next_input({u}'a = *.items');" - "get_ipython().run_line_magic({u}'psearch', {u}'*.items')"), - (u'plot(a?', "get_ipython().set_next_input({u}'plot(a');" - "get_ipython().run_line_magic({u}'pinfo', {u}'a')"), + [ (u'x3?', "get_ipython().run_line_magic('pinfo', 'x3')"), + (u'x4??', "get_ipython().run_line_magic('pinfo2', 'x4')"), + (u'%hist1?', "get_ipython().run_line_magic('pinfo', '%hist1')"), + (u'%hist2??', "get_ipython().run_line_magic('pinfo2', '%hist2')"), + (u'%%hist3?', "get_ipython().run_line_magic('pinfo', '%%hist3')"), + (u'%%hist4??', "get_ipython().run_line_magic('pinfo2', '%%hist4')"), + (u'f*?', "get_ipython().run_line_magic('psearch', 'f*')"), + (u'ax.*aspe*?', "get_ipython().run_line_magic('psearch', 'ax.*aspe*')"), + (u'a = abc?', "get_ipython().set_next_input('a = abc');" + "get_ipython().run_line_magic('pinfo', 'abc')"), + (u'a = abc.qe??', "get_ipython().set_next_input('a = abc.qe');" + "get_ipython().run_line_magic('pinfo2', 'abc.qe')"), + (u'a = *.items?', "get_ipython().set_next_input('a = *.items');" + "get_ipython().run_line_magic('psearch', '*.items')"), + (u'plot(a?', "get_ipython().set_next_input('plot(a');" + "get_ipython().run_line_magic('pinfo', 'a')"), (u'a*2 #comment?', 'a*2 #comment?'), ]], # Explicit magic calls escaped_magic = [(i,py3compat.u_format(o)) for i,o in \ - [ (u'%cd', "get_ipython().run_line_magic({u}'cd', {u}'')"), - (u'%cd /home', "get_ipython().run_line_magic({u}'cd', {u}'/home')"), + [ (u'%cd', "get_ipython().run_line_magic('cd', '')"), + (u'%cd /home', "get_ipython().run_line_magic('cd', '/home')"), # Backslashes need to be escaped. - (u'%cd C:\\User', "get_ipython().run_line_magic({u}'cd', {u}'C:\\\\User')"), - (u' %magic', " get_ipython().run_line_magic({u}'magic', {u}'')"), + (u'%cd C:\\User', "get_ipython().run_line_magic('cd', 'C:\\\\User')"), + (u' %magic', " get_ipython().run_line_magic('magic', '')"), ]], # Quoting with separate arguments @@ -163,11 +163,11 @@ syntax = \ # Check that we transform prompts before other transforms mixed = [(i,py3compat.u_format(o)) for i,o in \ - [ (u'In [1]: %lsmagic', "get_ipython().run_line_magic({u}'lsmagic', {u}'')"), - (u'>>> %lsmagic', "get_ipython().run_line_magic({u}'lsmagic', {u}'')"), - (u'In [2]: !ls', "get_ipython().system({u}'ls')"), - (u'In [3]: abs?', "get_ipython().run_line_magic({u}'pinfo', {u}'abs')"), - (u'In [4]: b = %who', "b = get_ipython().run_line_magic({u}'who', {u}'')"), + [ (u'In [1]: %lsmagic', "get_ipython().run_line_magic('lsmagic', '')"), + (u'>>> %lsmagic', "get_ipython().run_line_magic('lsmagic', '')"), + (u'In [2]: !ls', "get_ipython().system('ls')"), + (u'In [3]: abs?', "get_ipython().run_line_magic('pinfo', 'abs')"), + (u'In [4]: b = %who', "b = get_ipython().run_line_magic('who', '')"), ]], ) @@ -283,11 +283,11 @@ syntax_ml = \ cellmagic = [ [(u'%%foo a', None), - (None, u_fmt("get_ipython().run_cell_magic({u}'foo', {u}'a', {u}'')")), + (None, u_fmt("get_ipython().run_cell_magic('foo', 'a', '')")), ], [(u'%%bar 123', None), (u'hello', None), - (None , u_fmt("get_ipython().run_cell_magic({u}'bar', {u}'123', {u}'hello')")), + (None , u_fmt("get_ipython().run_cell_magic('bar', '123', 'hello')")), ], [(u'a=5', 'a=5'), (u'%%cellmagic', '%%cellmagic'), @@ -296,31 +296,31 @@ syntax_ml = \ escaped = [ [('%abc def \\', None), - ('ghi', u_fmt("get_ipython().run_line_magic({u}'abc', {u}'def ghi')")), + ('ghi', u_fmt("get_ipython().run_line_magic('abc', 'def ghi')")), ], [('%abc def \\', None), ('ghi\\', None), - (None, u_fmt("get_ipython().run_line_magic({u}'abc', {u}'def ghi')")), + (None, u_fmt("get_ipython().run_line_magic('abc', 'def ghi')")), ], ], assign_magic = [ [(u'a = %bc de \\', None), - (u'fg', u_fmt("a = get_ipython().run_line_magic({u}'bc', {u}'de fg')")), + (u'fg', u_fmt("a = get_ipython().run_line_magic('bc', 'de fg')")), ], [(u'a = %bc de \\', None), (u'fg\\', None), - (None, u_fmt("a = get_ipython().run_line_magic({u}'bc', {u}'de fg')")), + (None, u_fmt("a = get_ipython().run_line_magic('bc', 'de fg')")), ], ], assign_system = [ [(u'a = !bc de \\', None), - (u'fg', u_fmt("a = get_ipython().getoutput({u}'bc de fg')")), + (u'fg', u_fmt("a = get_ipython().getoutput('bc de fg')")), ], [(u'a = !bc de \\', None), (u'fg\\', None), - (None, u_fmt("a = get_ipython().getoutput({u}'bc de fg')")), + (None, u_fmt("a = get_ipython().getoutput('bc de fg')")), ], ], ) @@ -436,7 +436,7 @@ def test_cellmagic(): line_example = [(u'%%bar 123', None), (u'hello', None), - (u'' , u_fmt("get_ipython().run_cell_magic({u}'bar', {u}'123', {u}'hello')")), + (u'' , u_fmt("get_ipython().run_cell_magic('bar', '123', 'hello')")), ] transform_checker(line_example, ipt.cellmagic, end_on_blank_line=True) @@ -474,16 +474,16 @@ def decistmt(tokens): def test_token_input_transformer(): - tests = [(u'1.2', u_fmt(u"Decimal ({u}'1.2')")), + tests = [(u'1.2', u_fmt(u"Decimal ('1.2')")), (u'"1.2"', u'"1.2"'), ] tt.check_pairs(transform_and_reset(decistmt), tests) ml_tests = \ [ [(u"a = 1.2; b = '''x", None), - (u"y'''", u_fmt(u"a =Decimal ({u}'1.2');b ='''x\ny'''")), + (u"y'''", u_fmt(u"a =Decimal ('1.2');b ='''x\ny'''")), ], [(u"a = [1.2,", None), - (u"3]", u_fmt(u"a =[Decimal ({u}'1.2'),\n3 ]")), + (u"3]", u_fmt(u"a =[Decimal ('1.2'),\n3 ]")), ], [(u"a = '''foo", None), # Test resetting when within a multi-line string (u"bar", None), diff --git a/IPython/core/tests/test_magic.py b/IPython/core/tests/test_magic.py index 25a57a6..88dfa46 100644 --- a/IPython/core/tests/test_magic.py +++ b/IPython/core/tests/test_magic.py @@ -510,16 +510,16 @@ def doctest_precision(): In [1]: f = get_ipython().display_formatter.formatters['text/plain'] In [2]: %precision 5 - Out[2]: {u}'%.5f' + Out[2]: '%.5f' In [3]: f.float_format - Out[3]: {u}'%.5f' + Out[3]: '%.5f' In [4]: %precision %e - Out[4]: {u}'%e' + Out[4]: '%e' In [5]: f(3.1415927) - Out[5]: {u}'3.141593e+00' + Out[5]: '3.141593e+00' """ def test_psearch():