##// END OF EJS Templates
Merge pull request #10638 from adityausathe/master...
Thomas Kluyver -
r23765:c05f400f merge
parent child Browse files
Show More
@@ -198,11 +198,14 b' def _make_help_call(target, esc, lspace, next_input=None):'
198 198 else 'psearch' if '*' in target \
199 199 else 'pinfo'
200 200 arg = " ".join([method, target])
201 #Prepare arguments for get_ipython().run_line_magic(magic_name, magic_args)
202 t_magic_name, _, t_magic_arg_s = arg.partition(' ')
203 t_magic_name = t_magic_name.lstrip(ESC_MAGIC)
201 204 if next_input is None:
202 return '%sget_ipython().magic(%r)' % (lspace, arg)
205 return '%sget_ipython().run_line_magic(%r, %r)' % (lspace, t_magic_name, t_magic_arg_s)
203 206 else:
204 return '%sget_ipython().set_next_input(%r);get_ipython().magic(%r)' % \
205 (lspace, next_input, arg)
207 return '%sget_ipython().set_next_input(%r);get_ipython().run_line_magic(%r, %r)' % \
208 (lspace, next_input, t_magic_name, t_magic_arg_s)
206 209
207 210 # These define the transformations for the different escape characters.
208 211 def _tr_system(line_info):
@@ -225,11 +228,14 b' def _tr_help(line_info):'
225 228
226 229 def _tr_magic(line_info):
227 230 "Translate lines escaped with: %"
228 tpl = '%sget_ipython().magic(%r)'
231 tpl = '%sget_ipython().run_line_magic(%r, %r)'
229 232 if line_info.line.startswith(ESC_MAGIC2):
230 233 return line_info.line
231 234 cmd = ' '.join([line_info.ifun, line_info.the_rest]).strip()
232 return tpl % (line_info.pre, cmd)
235 #Prepare arguments for get_ipython().run_line_magic(magic_name, magic_args)
236 t_magic_name, _, t_magic_arg_s = cmd.partition(' ')
237 t_magic_name = t_magic_name.lstrip(ESC_MAGIC)
238 return tpl % (line_info.pre, t_magic_name, t_magic_arg_s)
233 239
234 240 def _tr_quote(line_info):
235 241 "Translate lines escaped with: ,"
@@ -514,12 +520,15 b' def assign_from_system(line):'
514 520 return assign_system_template % m.group('lhs', 'cmd')
515 521
516 522 assign_magic_re = re.compile(r'{}%\s*(?P<cmd>.*)'.format(_assign_pat), re.VERBOSE)
517 assign_magic_template = '%s = get_ipython().magic(%r)'
523 assign_magic_template = '%s = get_ipython().run_line_magic(%r, %r)'
518 524 @StatelessInputTransformer.wrap
519 525 def assign_from_magic(line):
520 526 """Transform assignment from magic commands (e.g. a = %who_ls)"""
521 527 m = assign_magic_re.match(line)
522 528 if m is None:
523 529 return line
524
525 return assign_magic_template % m.group('lhs', 'cmd')
530 #Prepare arguments for get_ipython().run_line_magic(magic_name, magic_args)
531 m_lhs, m_cmd = m.group('lhs', 'cmd')
532 t_magic_name, _, t_magic_arg_s = m_cmd.partition(' ')
533 t_magic_name = t_magic_name.lstrip(ESC_MAGIC)
534 return assign_magic_template % (m_lhs, t_magic_name, t_magic_arg_s)
@@ -2036,7 +2036,7 b' class InteractiveShell(SingletonConfigurable):'
2036 2036 # FIXME: Move the color initialization to the DisplayHook, which
2037 2037 # should be split into a prompt manager and displayhook. We probably
2038 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 2041 # Defined here so that it's included in the documentation
2042 2042 @functools.wraps(magic.MagicsManager.register_function)
@@ -2044,7 +2044,7 b' class InteractiveShell(SingletonConfigurable):'
2044 2044 self.magics_manager.register_function(func,
2045 2045 magic_kind=magic_kind, magic_name=magic_name)
2046 2046
2047 def run_line_magic(self, magic_name, line):
2047 def run_line_magic(self, magic_name, line, _stack_depth=1):
2048 2048 """Execute the given line magic.
2049 2049
2050 2050 Parameters
@@ -2054,6 +2054,10 b' class InteractiveShell(SingletonConfigurable):'
2054 2054
2055 2055 line : str
2056 2056 The rest of the input line as a single string.
2057
2058 _stack_depth : int
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()'
2057 2061 """
2058 2062 fn = self.find_line_magic(magic_name)
2059 2063 if fn is None:
@@ -2066,7 +2070,9 b' class InteractiveShell(SingletonConfigurable):'
2066 2070 # Note: this is the distance in the stack to the user's frame.
2067 2071 # This will need to be updated if the internal calling logic gets
2068 2072 # refactored, or else we'll be expanding the wrong variables.
2069 stack_depth = 2
2073
2074 # Determine stack_depth depending on where run_line_magic() has been called
2075 stack_depth = _stack_depth
2070 2076 magic_arg_s = self.var_expand(line, stack_depth)
2071 2077 # Put magic args in a list so we can call with f(*a) syntax
2072 2078 args = [magic_arg_s]
@@ -2154,7 +2160,7 b' class InteractiveShell(SingletonConfigurable):'
2154 2160 # TODO: should we issue a loud deprecation warning here?
2155 2161 magic_name, _, magic_arg_s = arg_s.partition(' ')
2156 2162 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
2157 return self.run_line_magic(magic_name, magic_arg_s)
2163 return self.run_line_magic(magic_name, magic_arg_s, _stack_depth=2)
2158 2164
2159 2165 #-------------------------------------------------------------------------
2160 2166 # Things related to macros
@@ -61,7 +61,7 b' class HistoryMagics(Magics):'
61 61 source before executing it (things like magics or aliases are turned
62 62 into function calls, for example). With this option, you'll see the
63 63 native history instead of the user-entered version: '%%cd /' will be
64 seen as 'get_ipython().magic("%%cd /")' instead of '%%cd /'.
64 seen as 'get_ipython().run_line_magic("cd", "/")' instead of '%%cd /'.
65 65 """)
66 66 @argument(
67 67 '-f', dest='filename',
@@ -589,8 +589,11 b' class MagicHandler(PrefilterHandler):'
589 589 """Execute magic functions."""
590 590 ifun = line_info.ifun
591 591 the_rest = line_info.the_rest
592 cmd = '%sget_ipython().magic(%r)' % (line_info.pre_whitespace,
593 (ifun + " " + the_rest))
592 #Prepare arguments for get_ipython().run_line_magic(magic_name, magic_args)
593 t_arg_s = ifun + " " + the_rest
594 t_magic_name, _, t_magic_arg_s = t_arg_s.partition(' ')
595 t_magic_name = t_magic_name.lstrip(ESC_MAGIC)
596 cmd = '%sget_ipython().run_line_magic(%r, %r)' % (line_info.pre_whitespace, t_magic_name, t_magic_arg_s)
594 597 return cmd
595 598
596 599
@@ -53,7 +53,7 b' def test_handlers():'
53 53 # line.
54 54 run([(i,py3compat.u_format(o)) for i,o in \
55 55 [('"no change"', '"no change"'), # normal
56 (u"lsmagic", "get_ipython().magic({u}'lsmagic ')"), # magic
56 (u"lsmagic", "get_ipython().run_line_magic('lsmagic', '')"), # magic
57 57 #("a = b # PYTHON-MODE", '_i'), # emacs -- avoids _in cache
58 58 ]])
59 59
@@ -479,11 +479,11 b' class IPythonInputTestCase(InputSplitterTestCase):'
479 479
480 480 for raw, expected in [
481 481 ("a=5", "a=5#"),
482 ("%ls foo", "get_ipython().magic(%r)" % u'ls foo#'),
483 ("!ls foo\n%ls bar", "get_ipython().system(%r)\nget_ipython().magic(%r)" % (
484 u'ls foo#', u'ls bar#'
482 ("%ls foo", "get_ipython().run_line_magic(%r, %r)" % (u'ls', u'foo#')),
483 ("!ls foo\n%ls bar", "get_ipython().system(%r)\nget_ipython().run_line_magic(%r, %r)" % (
484 u'ls foo#', u'ls', u'bar#'
485 485 )),
486 ("1\n2\n3\n%ls foo\n4\n5", "1#\n2#\n3#\nget_ipython().magic(%r)\n4#\n5#" % u'ls foo#'),
486 ("1\n2\n3\n%ls foo\n4\n5", "1#\n2#\n3#\nget_ipython().run_line_magic(%r, %r)\n4#\n5#" % (u'ls', u'foo#')),
487 487 ]:
488 488 out = isp.transform_cell(raw)
489 489 self.assertEqual(out.rstrip(), expected.rstrip())
@@ -568,7 +568,7 b' class CellMagicsCommon(object):'
568 568 def test_whole_cell(self):
569 569 src = "%%cellm line\nbody\n"
570 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 572 nt.assert_equal(out, py3compat.u_format(ref))
573 573
574 574 def test_cellmagic_help(self):
@@ -39,16 +39,16 b' def transform_checker(tests, transformer, **kwargs):'
39 39 syntax = \
40 40 dict(assign_system =
41 41 [(i,py3compat.u_format(o)) for i,o in \
42 [(u'a =! ls', "a = get_ipython().getoutput({u}'ls')"),
43 (u'b = !ls', "b = get_ipython().getoutput({u}'ls')"),
44 (u'c= !ls', "c = get_ipython().getoutput({u}'ls')"),
42 [(u'a =! ls', "a = get_ipython().getoutput('ls')"),
43 (u'b = !ls', "b = get_ipython().getoutput('ls')"),
44 (u'c= !ls', "c = get_ipython().getoutput('ls')"),
45 45 (u'd == !ls', u'd == !ls'), # Invalid syntax, but we leave == alone.
46 46 ('x=1', 'x=1'), # normal input is unmodified
47 47 (' ',' '), # blank lines are kept intact
48 48 # Tuple unpacking
49 (u"a, b = !echo 'a\\nb'", u"a, b = get_ipython().getoutput({u}\"echo 'a\\\\nb'\")"),
50 (u"a,= !echo 'a'", u"a, = get_ipython().getoutput({u}\"echo 'a'\")"),
51 (u"a, *bc = !echo 'a\\nb\\nc'", u"a, *bc = get_ipython().getoutput({u}\"echo 'a\\\\nb\\\\nc'\")"),
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(\"echo 'a'\")"),
51 (u"a, *bc = !echo 'a\\nb\\nc'", u"a, *bc = get_ipython().getoutput(\"echo 'a\\\\nb\\\\nc'\")"),
52 52 # Tuple unpacking with regular Python expressions, not our syntax.
53 53 (u"a, b = range(2)", u"a, b = range(2)"),
54 54 (u"a, = range(1)", u"a, = range(1)"),
@@ -57,13 +57,13 b' syntax = \\'
57 57
58 58 assign_magic =
59 59 [(i,py3compat.u_format(o)) for i,o in \
60 [(u'a =% who', "a = get_ipython().magic({u}'who')"),
61 (u'b = %who', "b = get_ipython().magic({u}'who')"),
62 (u'c= %ls', "c = get_ipython().magic({u}'ls')"),
60 [(u'a =% who', "a = get_ipython().run_line_magic('who', '')"),
61 (u'b = %who', "b = get_ipython().run_line_magic('who', '')"),
62 (u'c= %ls', "c = get_ipython().run_line_magic('ls', '')"),
63 63 (u'd == %ls', u'd == %ls'), # Invalid syntax, but we leave == alone.
64 64 ('x=1', 'x=1'), # normal input is unmodified
65 65 (' ',' '), # blank lines are kept intact
66 (u"a, b = %foo", u"a, b = get_ipython().magic({u}'foo')"),
66 (u"a, b = %foo", u"a, b = get_ipython().run_line_magic('foo', '')"),
67 67 ]],
68 68
69 69 classic_prompt =
@@ -87,53 +87,53 b' syntax = \\'
87 87 # System calls
88 88 escaped_shell =
89 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 91 # Double-escape shell, this means to capture the output of the
92 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 96 # Help/object info
97 97 escaped_help =
98 98 [(i,py3compat.u_format(o)) for i,o in \
99 99 [ (u'?', 'get_ipython().show_usage()'),
100 (u'?x1', "get_ipython().magic({u}'pinfo x1')"),
101 (u'??x2', "get_ipython().magic({u}'pinfo2 x2')"),
102 (u'?a.*s', "get_ipython().magic({u}'psearch a.*s')"),
103 (u'?%hist1', "get_ipython().magic({u}'pinfo %hist1')"),
104 (u'?%%hist2', "get_ipython().magic({u}'pinfo %%hist2')"),
105 (u'?abc = qwe', "get_ipython().magic({u}'pinfo abc')"),
100 (u'?x1', "get_ipython().run_line_magic('pinfo', 'x1')"),
101 (u'??x2', "get_ipython().run_line_magic('pinfo2', 'x2')"),
102 (u'?a.*s', "get_ipython().run_line_magic('psearch', 'a.*s')"),
103 (u'?%hist1', "get_ipython().run_line_magic('pinfo', '%hist1')"),
104 (u'?%%hist2', "get_ipython().run_line_magic('pinfo', '%%hist2')"),
105 (u'?abc = qwe', "get_ipython().run_line_magic('pinfo', 'abc')"),
106 106 ]],
107 107
108 108 end_help =
109 109 [(i,py3compat.u_format(o)) for i,o in \
110 [ (u'x3?', "get_ipython().magic({u}'pinfo x3')"),
111 (u'x4??', "get_ipython().magic({u}'pinfo2 x4')"),
112 (u'%hist1?', "get_ipython().magic({u}'pinfo %hist1')"),
113 (u'%hist2??', "get_ipython().magic({u}'pinfo2 %hist2')"),
114 (u'%%hist3?', "get_ipython().magic({u}'pinfo %%hist3')"),
115 (u'%%hist4??', "get_ipython().magic({u}'pinfo2 %%hist4')"),
116 (u'f*?', "get_ipython().magic({u}'psearch f*')"),
117 (u'ax.*aspe*?', "get_ipython().magic({u}'psearch ax.*aspe*')"),
118 (u'a = abc?', "get_ipython().set_next_input({u}'a = abc');"
119 "get_ipython().magic({u}'pinfo abc')"),
120 (u'a = abc.qe??', "get_ipython().set_next_input({u}'a = abc.qe');"
121 "get_ipython().magic({u}'pinfo2 abc.qe')"),
122 (u'a = *.items?', "get_ipython().set_next_input({u}'a = *.items');"
123 "get_ipython().magic({u}'psearch *.items')"),
124 (u'plot(a?', "get_ipython().set_next_input({u}'plot(a');"
125 "get_ipython().magic({u}'pinfo a')"),
110 [ (u'x3?', "get_ipython().run_line_magic('pinfo', 'x3')"),
111 (u'x4??', "get_ipython().run_line_magic('pinfo2', 'x4')"),
112 (u'%hist1?', "get_ipython().run_line_magic('pinfo', '%hist1')"),
113 (u'%hist2??', "get_ipython().run_line_magic('pinfo2', '%hist2')"),
114 (u'%%hist3?', "get_ipython().run_line_magic('pinfo', '%%hist3')"),
115 (u'%%hist4??', "get_ipython().run_line_magic('pinfo2', '%%hist4')"),
116 (u'f*?', "get_ipython().run_line_magic('psearch', 'f*')"),
117 (u'ax.*aspe*?', "get_ipython().run_line_magic('psearch', 'ax.*aspe*')"),
118 (u'a = abc?', "get_ipython().set_next_input('a = abc');"
119 "get_ipython().run_line_magic('pinfo', 'abc')"),
120 (u'a = abc.qe??', "get_ipython().set_next_input('a = abc.qe');"
121 "get_ipython().run_line_magic('pinfo2', 'abc.qe')"),
122 (u'a = *.items?', "get_ipython().set_next_input('a = *.items');"
123 "get_ipython().run_line_magic('psearch', '*.items')"),
124 (u'plot(a?', "get_ipython().set_next_input('plot(a');"
125 "get_ipython().run_line_magic('pinfo', 'a')"),
126 126 (u'a*2 #comment?', 'a*2 #comment?'),
127 127 ]],
128 128
129 129 # Explicit magic calls
130 130 escaped_magic =
131 131 [(i,py3compat.u_format(o)) for i,o in \
132 [ (u'%cd', "get_ipython().magic({u}'cd')"),
133 (u'%cd /home', "get_ipython().magic({u}'cd /home')"),
132 [ (u'%cd', "get_ipython().run_line_magic('cd', '')"),
133 (u'%cd /home', "get_ipython().run_line_magic('cd', '/home')"),
134 134 # Backslashes need to be escaped.
135 (u'%cd C:\\User', "get_ipython().magic({u}'cd C:\\\\User')"),
136 (u' %magic', " get_ipython().magic({u}'magic')"),
135 (u'%cd C:\\User', "get_ipython().run_line_magic('cd', 'C:\\\\User')"),
136 (u' %magic', " get_ipython().run_line_magic('magic', '')"),
137 137 ]],
138 138
139 139 # Quoting with separate arguments
@@ -163,11 +163,11 b' syntax = \\'
163 163 # Check that we transform prompts before other transforms
164 164 mixed =
165 165 [(i,py3compat.u_format(o)) for i,o in \
166 [ (u'In [1]: %lsmagic', "get_ipython().magic({u}'lsmagic')"),
167 (u'>>> %lsmagic', "get_ipython().magic({u}'lsmagic')"),
168 (u'In [2]: !ls', "get_ipython().system({u}'ls')"),
169 (u'In [3]: abs?', "get_ipython().magic({u}'pinfo abs')"),
170 (u'In [4]: b = %who', "b = get_ipython().magic({u}'who')"),
166 [ (u'In [1]: %lsmagic', "get_ipython().run_line_magic('lsmagic', '')"),
167 (u'>>> %lsmagic', "get_ipython().run_line_magic('lsmagic', '')"),
168 (u'In [2]: !ls', "get_ipython().system('ls')"),
169 (u'In [3]: abs?', "get_ipython().run_line_magic('pinfo', 'abs')"),
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 284 cellmagic =
285 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 288 [(u'%%bar 123', None),
289 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 292 [(u'a=5', 'a=5'),
293 293 (u'%%cellmagic', '%%cellmagic'),
@@ -296,31 +296,31 b' syntax_ml = \\'
296 296
297 297 escaped =
298 298 [ [('%abc def \\', None),
299 ('ghi', u_fmt("get_ipython().magic({u}'abc def ghi')")),
299 ('ghi', u_fmt("get_ipython().run_line_magic('abc', 'def ghi')")),
300 300 ],
301 301 [('%abc def \\', None),
302 302 ('ghi\\', None),
303 (None, u_fmt("get_ipython().magic({u}'abc def ghi')")),
303 (None, u_fmt("get_ipython().run_line_magic('abc', 'def ghi')")),
304 304 ],
305 305 ],
306 306
307 307 assign_magic =
308 308 [ [(u'a = %bc de \\', None),
309 (u'fg', u_fmt("a = get_ipython().magic({u}'bc de fg')")),
309 (u'fg', u_fmt("a = get_ipython().run_line_magic('bc', 'de fg')")),
310 310 ],
311 311 [(u'a = %bc de \\', None),
312 312 (u'fg\\', None),
313 (None, u_fmt("a = get_ipython().magic({u}'bc de fg')")),
313 (None, u_fmt("a = get_ipython().run_line_magic('bc', 'de fg')")),
314 314 ],
315 315 ],
316 316
317 317 assign_system =
318 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 321 [(u'a = !bc de \\', None),
322 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 437 line_example = [(u'%%bar 123', None),
438 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 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 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 478 (u'"1.2"', u'"1.2"'),
479 479 ]
480 480 tt.check_pairs(transform_and_reset(decistmt), tests)
481 481 ml_tests = \
482 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 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 488 [(u"a = '''foo", None), # Test resetting when within a multi-line string
489 489 (u"bar", None),
@@ -510,16 +510,16 b' def doctest_precision():'
510 510 In [1]: f = get_ipython().display_formatter.formatters['text/plain']
511 511
512 512 In [2]: %precision 5
513 Out[2]: {u}'%.5f'
513 Out[2]: '%.5f'
514 514
515 515 In [3]: f.float_format
516 Out[3]: {u}'%.5f'
516 Out[3]: '%.5f'
517 517
518 518 In [4]: %precision %e
519 Out[4]: {u}'%e'
519 Out[4]: '%e'
520 520
521 521 In [5]: f(3.1415927)
522 Out[5]: {u}'3.141593e+00'
522 Out[5]: '3.141593e+00'
523 523 """
524 524
525 525 def test_psearch():
General Comments 0
You need to be logged in to leave comments. Login now