##// END OF EJS Templates
Merge pull request #3606 from minrk/cellmagictransform...
Thomas Kluyver -
r11484:801fda16 merge
parent child Browse files
Show More
@@ -486,17 +486,19 b' class IPythonInputSplitter(InputSplitter):'
486 if physical_line_transforms is not None:
486 if physical_line_transforms is not None:
487 self.physical_line_transforms = physical_line_transforms
487 self.physical_line_transforms = physical_line_transforms
488 else:
488 else:
489 self.physical_line_transforms = [leading_indent(),
489 self.physical_line_transforms = [
490 leading_indent(),
490 classic_prompt(),
491 classic_prompt(),
491 ipy_prompt(),
492 ipy_prompt(),
492 strip_encoding_cookie(),
493 strip_encoding_cookie(),
494 cellmagic(end_on_blank_line=line_input_checker),
493 ]
495 ]
494
496
495 self.assemble_logical_lines = assemble_logical_lines()
497 self.assemble_logical_lines = assemble_logical_lines()
496 if logical_line_transforms is not None:
498 if logical_line_transforms is not None:
497 self.logical_line_transforms = logical_line_transforms
499 self.logical_line_transforms = logical_line_transforms
498 else:
500 else:
499 self.logical_line_transforms = [cellmagic(end_on_blank_line=line_input_checker),
501 self.logical_line_transforms = [
500 help_end(),
502 help_end(),
501 escaped_commands(),
503 escaped_commands(),
502 assign_from_magic(),
504 assign_from_magic(),
@@ -227,6 +227,8 b' def _tr_help(line_info):'
227 def _tr_magic(line_info):
227 def _tr_magic(line_info):
228 "Translate lines escaped with: %"
228 "Translate lines escaped with: %"
229 tpl = '%sget_ipython().magic(%r)'
229 tpl = '%sget_ipython().magic(%r)'
230 if line_info.line.startswith(ESC_MAGIC2):
231 return line_info.line
230 cmd = ' '.join([line_info.ifun, line_info.the_rest]).strip()
232 cmd = ' '.join([line_info.ifun, line_info.the_rest]).strip()
231 return tpl % (line_info.pre, cmd)
233 return tpl % (line_info.pre, cmd)
232
234
@@ -272,7 +274,8 b' _help_end_re = re.compile(r"""(%{0,2}'
272 [a-zA-Z_*][\w*]* # Variable name
274 [a-zA-Z_*][\w*]* # Variable name
273 (\.[a-zA-Z_*][\w*]*)* # .etc.etc
275 (\.[a-zA-Z_*][\w*]*)* # .etc.etc
274 )
276 )
275 (\?\??)$ # ? or ??""",
277 (\?\??)$ # ? or ??
278 """,
276 re.VERBOSE)
279 re.VERBOSE)
277
280
278 def has_comment(src):
281 def has_comment(src):
@@ -328,7 +331,14 b' def cellmagic(end_on_blank_line=False):'
328 line = ''
331 line = ''
329 while True:
332 while True:
330 line = (yield line)
333 line = (yield line)
331 if (not line) or (not line.startswith(ESC_MAGIC2)):
334 # consume leading empty lines
335 while not line:
336 line = (yield line)
337
338 if not line.startswith(ESC_MAGIC2):
339 # This isn't a cell magic, idle waiting for reset then start over
340 while line is not None:
341 line = (yield line)
332 continue
342 continue
333
343
334 if cellmagic_help_re.match(line):
344 if cellmagic_help_re.match(line):
@@ -1672,7 +1672,13 b' class InteractiveShell(SingletonConfigurable):'
1672
1672
1673 return etype, value, tb
1673 return etype, value, tb
1674
1674
1675
1675 def show_usage_error(self, exc):
1676 """Show a short message for UsageErrors
1677
1678 These are special exceptions that shouldn't show a traceback.
1679 """
1680 self.write_err("UsageError: %s" % exc)
1681
1676 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None,
1682 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None,
1677 exception_only=False):
1683 exception_only=False):
1678 """Display the exception that just occurred.
1684 """Display the exception that just occurred.
@@ -1698,7 +1704,7 b' class InteractiveShell(SingletonConfigurable):'
1698 # line, there may be SyntaxError cases with imported code.
1704 # line, there may be SyntaxError cases with imported code.
1699 self.showsyntaxerror(filename)
1705 self.showsyntaxerror(filename)
1700 elif etype is UsageError:
1706 elif etype is UsageError:
1701 self.write_err("UsageError: %s" % value)
1707 self.show_usage_error(value)
1702 else:
1708 else:
1703 if exception_only:
1709 if exception_only:
1704 stb = ['An exception has occurred, use %tb to see '
1710 stb = ['An exception has occurred, use %tb to see '
@@ -446,6 +446,24 b' class IPythonInputTestCase(InputSplitterTestCase):'
446 out = isp.transform_cell(raw)
446 out = isp.transform_cell(raw)
447 # Match ignoring trailing whitespace
447 # Match ignoring trailing whitespace
448 self.assertEqual(out.rstrip(), out_t.rstrip())
448 self.assertEqual(out.rstrip(), out_t.rstrip())
449
450 def test_cellmagic_preempt(self):
451 isp = self.isp
452 for raw, name, line, cell in [
453 ("%%cellm a\nIn[1]:", u'cellm', u'a', u'In[1]:'),
454 ("%%cellm \nline\n>>>hi", u'cellm', u'', u'line\n>>>hi'),
455 (">>>%%cellm \nline\n>>>hi", u'cellm', u'', u'line\nhi'),
456 ("%%cellm \n>>>hi", u'cellm', u'', u'hi'),
457 ("%%cellm \nline1\nline2", u'cellm', u'', u'line1\nline2'),
458 ("%%cellm \nline1\\\\\nline2", u'cellm', u'', u'line1\\\\\nline2'),
459 ]:
460 expected = "get_ipython().run_cell_magic(%r, %r, %r)" % (
461 name, line, cell
462 )
463 out = isp.transform_cell(raw)
464 self.assertEqual(out.rstrip(), expected.rstrip())
465
466
449
467
450 #-----------------------------------------------------------------------------
468 #-----------------------------------------------------------------------------
451 # Main - use as a script, mostly for developer experiments
469 # Main - use as a script, mostly for developer experiments
@@ -259,6 +259,9 b' syntax_ml = \\'
259 (u'hello', None),
259 (u'hello', None),
260 (None , u_fmt("get_ipython().run_cell_magic({u}'bar', {u}'123', {u}'hello')")),
260 (None , u_fmt("get_ipython().run_cell_magic({u}'bar', {u}'123', {u}'hello')")),
261 ],
261 ],
262 [(u'a=5', 'a=5'),
263 (u'%%cellmagic', '%%cellmagic'),
264 ],
262 ],
265 ],
263
266
264 escaped =
267 escaped =
General Comments 0
You need to be logged in to leave comments. Login now