##// END OF EJS Templates
fix for #10327: _stack_depth added and {u} string literals removed from core/tests
adityausathe -
Show More
@@ -2036,7 +2036,7 b' class InteractiveShell(SingletonConfigurable):'
2036 # FIXME: Move the color initialization to the DisplayHook, which
2036 # FIXME: Move the color initialization to the DisplayHook, which
2037 # should be split into a prompt manager and displayhook. We probably
2037 # should be split into a prompt manager and displayhook. We probably
2038 # even need a centralize colors management object.
2038 # even need a centralize colors management object.
2039 self.magic('colors %s' % self.colors)
2039 self.run_line_magic('colors', self.colors)
2040
2040
2041 # Defined here so that it's included in the documentation
2041 # Defined here so that it's included in the documentation
2042 @functools.wraps(magic.MagicsManager.register_function)
2042 @functools.wraps(magic.MagicsManager.register_function)
@@ -2044,7 +2044,7 b' class InteractiveShell(SingletonConfigurable):'
2044 self.magics_manager.register_function(func,
2044 self.magics_manager.register_function(func,
2045 magic_kind=magic_kind, magic_name=magic_name)
2045 magic_kind=magic_kind, magic_name=magic_name)
2046
2046
2047 def run_line_magic(self, magic_name, line, back_compat=False):
2047 def run_line_magic(self, magic_name, line, _stack_depth=1):
2048 """Execute the given line magic.
2048 """Execute the given line magic.
2049
2049
2050 Parameters
2050 Parameters
@@ -2055,8 +2055,8 b' class InteractiveShell(SingletonConfigurable):'
2055 line : str
2055 line : str
2056 The rest of the input line as a single string.
2056 The rest of the input line as a single string.
2057
2057
2058 back_compat : bool
2058 _stack_depth : int
2059 If run_line_magic() is called from magic() then it should be 'True'.
2059 If run_line_magic() is called from magic() then _stack_depth=2.
2060 This is added to ensure backward compatibility for use of 'get_ipython().magic()'
2060 This is added to ensure backward compatibility for use of 'get_ipython().magic()'
2061 """
2061 """
2062 fn = self.find_line_magic(magic_name)
2062 fn = self.find_line_magic(magic_name)
@@ -2070,13 +2070,9 b' class InteractiveShell(SingletonConfigurable):'
2070 # Note: this is the distance in the stack to the user's frame.
2070 # Note: this is the distance in the stack to the user's frame.
2071 # This will need to be updated if the internal calling logic gets
2071 # This will need to be updated if the internal calling logic gets
2072 # refactored, or else we'll be expanding the wrong variables.
2072 # refactored, or else we'll be expanding the wrong variables.
2073
2073 # Determine stack_depth depending on where run_line_magic() has been called
2074 # Determine stack_depth depending on where run_line_magic() has been called
2074 if back_compat:
2075 stack_depth = _stack_depth
2075 # run_line_magic() called directly using 'get_ipython().magic()'
2076 stack_depth = 2
2077 else:
2078 # run_line_magic() called directly using 'get_ipython().run_line_magic()'
2079 stack_depth = 1
2080 magic_arg_s = self.var_expand(line, stack_depth)
2076 magic_arg_s = self.var_expand(line, stack_depth)
2081 # Put magic args in a list so we can call with f(*a) syntax
2077 # Put magic args in a list so we can call with f(*a) syntax
2082 args = [magic_arg_s]
2078 args = [magic_arg_s]
@@ -2164,7 +2160,7 b' class InteractiveShell(SingletonConfigurable):'
2164 # TODO: should we issue a loud deprecation warning here?
2160 # TODO: should we issue a loud deprecation warning here?
2165 magic_name, _, magic_arg_s = arg_s.partition(' ')
2161 magic_name, _, magic_arg_s = arg_s.partition(' ')
2166 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
2162 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
2167 return self.run_line_magic(magic_name, magic_arg_s, back_compat=True)
2163 return self.run_line_magic(magic_name, magic_arg_s, _stack_depth=2)
2168
2164
2169 #-------------------------------------------------------------------------
2165 #-------------------------------------------------------------------------
2170 # Things related to macros
2166 # Things related to macros
@@ -53,7 +53,7 b' def test_handlers():'
53 # line.
53 # line.
54 run([(i,py3compat.u_format(o)) for i,o in \
54 run([(i,py3compat.u_format(o)) for i,o in \
55 [('"no change"', '"no change"'), # normal
55 [('"no change"', '"no change"'), # normal
56 (u"lsmagic", "get_ipython().run_line_magic({u}'lsmagic', {u}'')"), # magic
56 (u"lsmagic", "get_ipython().run_line_magic('lsmagic', '')"), # magic
57 #("a = b # PYTHON-MODE", '_i'), # emacs -- avoids _in cache
57 #("a = b # PYTHON-MODE", '_i'), # emacs -- avoids _in cache
58 ]])
58 ]])
59
59
@@ -568,7 +568,7 b' class CellMagicsCommon(object):'
568 def test_whole_cell(self):
568 def test_whole_cell(self):
569 src = "%%cellm line\nbody\n"
569 src = "%%cellm line\nbody\n"
570 out = self.sp.transform_cell(src)
570 out = self.sp.transform_cell(src)
571 ref = u"get_ipython().run_cell_magic({u}'cellm', {u}'line', {u}'body')\n"
571 ref = u"get_ipython().run_cell_magic('cellm', 'line', 'body')\n"
572 nt.assert_equal(out, py3compat.u_format(ref))
572 nt.assert_equal(out, py3compat.u_format(ref))
573
573
574 def test_cellmagic_help(self):
574 def test_cellmagic_help(self):
@@ -39,16 +39,16 b' def transform_checker(tests, transformer, **kwargs):'
39 syntax = \
39 syntax = \
40 dict(assign_system =
40 dict(assign_system =
41 [(i,py3compat.u_format(o)) for i,o in \
41 [(i,py3compat.u_format(o)) for i,o in \
42 [(u'a =! ls', "a = get_ipython().getoutput({u}'ls')"),
42 [(u'a =! ls', "a = get_ipython().getoutput('ls')"),
43 (u'b = !ls', "b = get_ipython().getoutput({u}'ls')"),
43 (u'b = !ls', "b = get_ipython().getoutput('ls')"),
44 (u'c= !ls', "c = get_ipython().getoutput({u}'ls')"),
44 (u'c= !ls', "c = get_ipython().getoutput('ls')"),
45 (u'd == !ls', u'd == !ls'), # Invalid syntax, but we leave == alone.
45 (u'd == !ls', u'd == !ls'), # Invalid syntax, but we leave == alone.
46 ('x=1', 'x=1'), # normal input is unmodified
46 ('x=1', 'x=1'), # normal input is unmodified
47 (' ',' '), # blank lines are kept intact
47 (' ',' '), # blank lines are kept intact
48 # Tuple unpacking
48 # Tuple unpacking
49 (u"a, b = !echo 'a\\nb'", u"a, b = get_ipython().getoutput({u}\"echo 'a\\\\nb'\")"),
49 (u"a, b = !echo 'a\\nb'", u"a, b = get_ipython().getoutput(\"echo 'a\\\\nb'\")"),
50 (u"a,= !echo 'a'", u"a, = get_ipython().getoutput({u}\"echo 'a'\")"),
50 (u"a,= !echo 'a'", u"a, = get_ipython().getoutput(\"echo 'a'\")"),
51 (u"a, *bc = !echo 'a\\nb\\nc'", u"a, *bc = get_ipython().getoutput({u}\"echo 'a\\\\nb\\\\nc'\")"),
51 (u"a, *bc = !echo 'a\\nb\\nc'", u"a, *bc = get_ipython().getoutput(\"echo 'a\\\\nb\\\\nc'\")"),
52 # Tuple unpacking with regular Python expressions, not our syntax.
52 # Tuple unpacking with regular Python expressions, not our syntax.
53 (u"a, b = range(2)", u"a, b = range(2)"),
53 (u"a, b = range(2)", u"a, b = range(2)"),
54 (u"a, = range(1)", u"a, = range(1)"),
54 (u"a, = range(1)", u"a, = range(1)"),
@@ -57,13 +57,13 b' syntax = \\'
57
57
58 assign_magic =
58 assign_magic =
59 [(i,py3compat.u_format(o)) for i,o in \
59 [(i,py3compat.u_format(o)) for i,o in \
60 [(u'a =% who', "a = get_ipython().run_line_magic({u}'who', {u}'')"),
60 [(u'a =% who', "a = get_ipython().run_line_magic('who', '')"),
61 (u'b = %who', "b = get_ipython().run_line_magic({u}'who', {u}'')"),
61 (u'b = %who', "b = get_ipython().run_line_magic('who', '')"),
62 (u'c= %ls', "c = get_ipython().run_line_magic({u}'ls', {u}'')"),
62 (u'c= %ls', "c = get_ipython().run_line_magic('ls', '')"),
63 (u'd == %ls', u'd == %ls'), # Invalid syntax, but we leave == alone.
63 (u'd == %ls', u'd == %ls'), # Invalid syntax, but we leave == alone.
64 ('x=1', 'x=1'), # normal input is unmodified
64 ('x=1', 'x=1'), # normal input is unmodified
65 (' ',' '), # blank lines are kept intact
65 (' ',' '), # blank lines are kept intact
66 (u"a, b = %foo", u"a, b = get_ipython().run_line_magic({u}'foo', {u}'')"),
66 (u"a, b = %foo", u"a, b = get_ipython().run_line_magic('foo', '')"),
67 ]],
67 ]],
68
68
69 classic_prompt =
69 classic_prompt =
@@ -87,53 +87,53 b' syntax = \\'
87 # System calls
87 # System calls
88 escaped_shell =
88 escaped_shell =
89 [(i,py3compat.u_format(o)) for i,o in \
89 [(i,py3compat.u_format(o)) for i,o in \
90 [ (u'!ls', "get_ipython().system({u}'ls')"),
90 [ (u'!ls', "get_ipython().system('ls')"),
91 # Double-escape shell, this means to capture the output of the
91 # Double-escape shell, this means to capture the output of the
92 # subprocess and return it
92 # subprocess and return it
93 (u'!!ls', "get_ipython().getoutput({u}'ls')"),
93 (u'!!ls', "get_ipython().getoutput('ls')"),
94 ]],
94 ]],
95
95
96 # Help/object info
96 # Help/object info
97 escaped_help =
97 escaped_help =
98 [(i,py3compat.u_format(o)) for i,o in \
98 [(i,py3compat.u_format(o)) for i,o in \
99 [ (u'?', 'get_ipython().show_usage()'),
99 [ (u'?', 'get_ipython().show_usage()'),
100 (u'?x1', "get_ipython().run_line_magic({u}'pinfo', {u}'x1')"),
100 (u'?x1', "get_ipython().run_line_magic('pinfo', 'x1')"),
101 (u'??x2', "get_ipython().run_line_magic({u}'pinfo2', {u}'x2')"),
101 (u'??x2', "get_ipython().run_line_magic('pinfo2', 'x2')"),
102 (u'?a.*s', "get_ipython().run_line_magic({u}'psearch', {u}'a.*s')"),
102 (u'?a.*s', "get_ipython().run_line_magic('psearch', 'a.*s')"),
103 (u'?%hist1', "get_ipython().run_line_magic({u}'pinfo', {u}'%hist1')"),
103 (u'?%hist1', "get_ipython().run_line_magic('pinfo', '%hist1')"),
104 (u'?%%hist2', "get_ipython().run_line_magic({u}'pinfo', {u}'%%hist2')"),
104 (u'?%%hist2', "get_ipython().run_line_magic('pinfo', '%%hist2')"),
105 (u'?abc = qwe', "get_ipython().run_line_magic({u}'pinfo', {u}'abc')"),
105 (u'?abc = qwe', "get_ipython().run_line_magic('pinfo', 'abc')"),
106 ]],
106 ]],
107
107
108 end_help =
108 end_help =
109 [(i,py3compat.u_format(o)) for i,o in \
109 [(i,py3compat.u_format(o)) for i,o in \
110 [ (u'x3?', "get_ipython().run_line_magic({u}'pinfo', {u}'x3')"),
110 [ (u'x3?', "get_ipython().run_line_magic('pinfo', 'x3')"),
111 (u'x4??', "get_ipython().run_line_magic({u}'pinfo2', {u}'x4')"),
111 (u'x4??', "get_ipython().run_line_magic('pinfo2', 'x4')"),
112 (u'%hist1?', "get_ipython().run_line_magic({u}'pinfo', {u}'%hist1')"),
112 (u'%hist1?', "get_ipython().run_line_magic('pinfo', '%hist1')"),
113 (u'%hist2??', "get_ipython().run_line_magic({u}'pinfo2', {u}'%hist2')"),
113 (u'%hist2??', "get_ipython().run_line_magic('pinfo2', '%hist2')"),
114 (u'%%hist3?', "get_ipython().run_line_magic({u}'pinfo', {u}'%%hist3')"),
114 (u'%%hist3?', "get_ipython().run_line_magic('pinfo', '%%hist3')"),
115 (u'%%hist4??', "get_ipython().run_line_magic({u}'pinfo2', {u}'%%hist4')"),
115 (u'%%hist4??', "get_ipython().run_line_magic('pinfo2', '%%hist4')"),
116 (u'f*?', "get_ipython().run_line_magic({u}'psearch', {u}'f*')"),
116 (u'f*?', "get_ipython().run_line_magic('psearch', 'f*')"),
117 (u'ax.*aspe*?', "get_ipython().run_line_magic({u}'psearch', {u}'ax.*aspe*')"),
117 (u'ax.*aspe*?', "get_ipython().run_line_magic('psearch', 'ax.*aspe*')"),
118 (u'a = abc?', "get_ipython().set_next_input({u}'a = abc');"
118 (u'a = abc?', "get_ipython().set_next_input('a = abc');"
119 "get_ipython().run_line_magic({u}'pinfo', {u}'abc')"),
119 "get_ipython().run_line_magic('pinfo', 'abc')"),
120 (u'a = abc.qe??', "get_ipython().set_next_input({u}'a = abc.qe');"
120 (u'a = abc.qe??', "get_ipython().set_next_input('a = abc.qe');"
121 "get_ipython().run_line_magic({u}'pinfo2', {u}'abc.qe')"),
121 "get_ipython().run_line_magic('pinfo2', 'abc.qe')"),
122 (u'a = *.items?', "get_ipython().set_next_input({u}'a = *.items');"
122 (u'a = *.items?', "get_ipython().set_next_input('a = *.items');"
123 "get_ipython().run_line_magic({u}'psearch', {u}'*.items')"),
123 "get_ipython().run_line_magic('psearch', '*.items')"),
124 (u'plot(a?', "get_ipython().set_next_input({u}'plot(a');"
124 (u'plot(a?', "get_ipython().set_next_input('plot(a');"
125 "get_ipython().run_line_magic({u}'pinfo', {u}'a')"),
125 "get_ipython().run_line_magic('pinfo', 'a')"),
126 (u'a*2 #comment?', 'a*2 #comment?'),
126 (u'a*2 #comment?', 'a*2 #comment?'),
127 ]],
127 ]],
128
128
129 # Explicit magic calls
129 # Explicit magic calls
130 escaped_magic =
130 escaped_magic =
131 [(i,py3compat.u_format(o)) for i,o in \
131 [(i,py3compat.u_format(o)) for i,o in \
132 [ (u'%cd', "get_ipython().run_line_magic({u}'cd', {u}'')"),
132 [ (u'%cd', "get_ipython().run_line_magic('cd', '')"),
133 (u'%cd /home', "get_ipython().run_line_magic({u}'cd', {u}'/home')"),
133 (u'%cd /home', "get_ipython().run_line_magic('cd', '/home')"),
134 # Backslashes need to be escaped.
134 # Backslashes need to be escaped.
135 (u'%cd C:\\User', "get_ipython().run_line_magic({u}'cd', {u}'C:\\\\User')"),
135 (u'%cd C:\\User', "get_ipython().run_line_magic('cd', 'C:\\\\User')"),
136 (u' %magic', " get_ipython().run_line_magic({u}'magic', {u}'')"),
136 (u' %magic', " get_ipython().run_line_magic('magic', '')"),
137 ]],
137 ]],
138
138
139 # Quoting with separate arguments
139 # Quoting with separate arguments
@@ -163,11 +163,11 b' syntax = \\'
163 # Check that we transform prompts before other transforms
163 # Check that we transform prompts before other transforms
164 mixed =
164 mixed =
165 [(i,py3compat.u_format(o)) for i,o in \
165 [(i,py3compat.u_format(o)) for i,o in \
166 [ (u'In [1]: %lsmagic', "get_ipython().run_line_magic({u}'lsmagic', {u}'')"),
166 [ (u'In [1]: %lsmagic', "get_ipython().run_line_magic('lsmagic', '')"),
167 (u'>>> %lsmagic', "get_ipython().run_line_magic({u}'lsmagic', {u}'')"),
167 (u'>>> %lsmagic', "get_ipython().run_line_magic('lsmagic', '')"),
168 (u'In [2]: !ls', "get_ipython().system({u}'ls')"),
168 (u'In [2]: !ls', "get_ipython().system('ls')"),
169 (u'In [3]: abs?', "get_ipython().run_line_magic({u}'pinfo', {u}'abs')"),
169 (u'In [3]: abs?', "get_ipython().run_line_magic('pinfo', 'abs')"),
170 (u'In [4]: b = %who', "b = get_ipython().run_line_magic({u}'who', {u}'')"),
170 (u'In [4]: b = %who', "b = get_ipython().run_line_magic('who', '')"),
171 ]],
171 ]],
172 )
172 )
173
173
@@ -283,11 +283,11 b' syntax_ml = \\'
283
283
284 cellmagic =
284 cellmagic =
285 [ [(u'%%foo a', None),
285 [ [(u'%%foo a', None),
286 (None, u_fmt("get_ipython().run_cell_magic({u}'foo', {u}'a', {u}'')")),
286 (None, u_fmt("get_ipython().run_cell_magic('foo', 'a', '')")),
287 ],
287 ],
288 [(u'%%bar 123', None),
288 [(u'%%bar 123', None),
289 (u'hello', None),
289 (u'hello', None),
290 (None , u_fmt("get_ipython().run_cell_magic({u}'bar', {u}'123', {u}'hello')")),
290 (None , u_fmt("get_ipython().run_cell_magic('bar', '123', 'hello')")),
291 ],
291 ],
292 [(u'a=5', 'a=5'),
292 [(u'a=5', 'a=5'),
293 (u'%%cellmagic', '%%cellmagic'),
293 (u'%%cellmagic', '%%cellmagic'),
@@ -296,31 +296,31 b' syntax_ml = \\'
296
296
297 escaped =
297 escaped =
298 [ [('%abc def \\', None),
298 [ [('%abc def \\', None),
299 ('ghi', u_fmt("get_ipython().run_line_magic({u}'abc', {u}'def ghi')")),
299 ('ghi', u_fmt("get_ipython().run_line_magic('abc', 'def ghi')")),
300 ],
300 ],
301 [('%abc def \\', None),
301 [('%abc def \\', None),
302 ('ghi\\', None),
302 ('ghi\\', None),
303 (None, u_fmt("get_ipython().run_line_magic({u}'abc', {u}'def ghi')")),
303 (None, u_fmt("get_ipython().run_line_magic('abc', 'def ghi')")),
304 ],
304 ],
305 ],
305 ],
306
306
307 assign_magic =
307 assign_magic =
308 [ [(u'a = %bc de \\', None),
308 [ [(u'a = %bc de \\', None),
309 (u'fg', u_fmt("a = get_ipython().run_line_magic({u}'bc', {u}'de fg')")),
309 (u'fg', u_fmt("a = get_ipython().run_line_magic('bc', 'de fg')")),
310 ],
310 ],
311 [(u'a = %bc de \\', None),
311 [(u'a = %bc de \\', None),
312 (u'fg\\', None),
312 (u'fg\\', None),
313 (None, u_fmt("a = get_ipython().run_line_magic({u}'bc', {u}'de fg')")),
313 (None, u_fmt("a = get_ipython().run_line_magic('bc', 'de fg')")),
314 ],
314 ],
315 ],
315 ],
316
316
317 assign_system =
317 assign_system =
318 [ [(u'a = !bc de \\', None),
318 [ [(u'a = !bc de \\', None),
319 (u'fg', u_fmt("a = get_ipython().getoutput({u}'bc de fg')")),
319 (u'fg', u_fmt("a = get_ipython().getoutput('bc de fg')")),
320 ],
320 ],
321 [(u'a = !bc de \\', None),
321 [(u'a = !bc de \\', None),
322 (u'fg\\', None),
322 (u'fg\\', None),
323 (None, u_fmt("a = get_ipython().getoutput({u}'bc de fg')")),
323 (None, u_fmt("a = get_ipython().getoutput('bc de fg')")),
324 ],
324 ],
325 ],
325 ],
326 )
326 )
@@ -436,7 +436,7 b' def test_cellmagic():'
436
436
437 line_example = [(u'%%bar 123', None),
437 line_example = [(u'%%bar 123', None),
438 (u'hello', None),
438 (u'hello', None),
439 (u'' , u_fmt("get_ipython().run_cell_magic({u}'bar', {u}'123', {u}'hello')")),
439 (u'' , u_fmt("get_ipython().run_cell_magic('bar', '123', 'hello')")),
440 ]
440 ]
441 transform_checker(line_example, ipt.cellmagic, end_on_blank_line=True)
441 transform_checker(line_example, ipt.cellmagic, end_on_blank_line=True)
442
442
@@ -474,16 +474,16 b' def decistmt(tokens):'
474
474
475
475
476 def test_token_input_transformer():
476 def test_token_input_transformer():
477 tests = [(u'1.2', u_fmt(u"Decimal ({u}'1.2')")),
477 tests = [(u'1.2', u_fmt(u"Decimal ('1.2')")),
478 (u'"1.2"', u'"1.2"'),
478 (u'"1.2"', u'"1.2"'),
479 ]
479 ]
480 tt.check_pairs(transform_and_reset(decistmt), tests)
480 tt.check_pairs(transform_and_reset(decistmt), tests)
481 ml_tests = \
481 ml_tests = \
482 [ [(u"a = 1.2; b = '''x", None),
482 [ [(u"a = 1.2; b = '''x", None),
483 (u"y'''", u_fmt(u"a =Decimal ({u}'1.2');b ='''x\ny'''")),
483 (u"y'''", u_fmt(u"a =Decimal ('1.2');b ='''x\ny'''")),
484 ],
484 ],
485 [(u"a = [1.2,", None),
485 [(u"a = [1.2,", None),
486 (u"3]", u_fmt(u"a =[Decimal ({u}'1.2'),\n3 ]")),
486 (u"3]", u_fmt(u"a =[Decimal ('1.2'),\n3 ]")),
487 ],
487 ],
488 [(u"a = '''foo", None), # Test resetting when within a multi-line string
488 [(u"a = '''foo", None), # Test resetting when within a multi-line string
489 (u"bar", None),
489 (u"bar", None),
@@ -510,16 +510,16 b' def doctest_precision():'
510 In [1]: f = get_ipython().display_formatter.formatters['text/plain']
510 In [1]: f = get_ipython().display_formatter.formatters['text/plain']
511
511
512 In [2]: %precision 5
512 In [2]: %precision 5
513 Out[2]: {u}'%.5f'
513 Out[2]: '%.5f'
514
514
515 In [3]: f.float_format
515 In [3]: f.float_format
516 Out[3]: {u}'%.5f'
516 Out[3]: '%.5f'
517
517
518 In [4]: %precision %e
518 In [4]: %precision %e
519 Out[4]: {u}'%e'
519 Out[4]: '%e'
520
520
521 In [5]: f(3.1415927)
521 In [5]: f(3.1415927)
522 Out[5]: {u}'3.141593e+00'
522 Out[5]: '3.141593e+00'
523 """
523 """
524
524
525 def test_psearch():
525 def test_psearch():
General Comments 0
You need to be logged in to leave comments. Login now