##// END OF EJS Templates
Merge pull request #9144 from takluyver/i8791...
Matthias Bussonnier -
r21957:5e43fab2 merge
parent child Browse files
Show More
@@ -400,7 +400,7 b' def cellmagic(end_on_blank_line=False):'
400 line = tpl % (magic_name, first, u'\n'.join(body))
400 line = tpl % (magic_name, first, u'\n'.join(body))
401
401
402
402
403 def _strip_prompts(prompt_re, initial_re=None):
403 def _strip_prompts(prompt_re, initial_re=None, turnoff_re=None):
404 """Remove matching input prompts from a block of input.
404 """Remove matching input prompts from a block of input.
405
405
406 Parameters
406 Parameters
@@ -428,6 +428,14 b' def _strip_prompts(prompt_re, initial_re=None):'
428 if line is None:
428 if line is None:
429 continue
429 continue
430 out, n1 = initial_re.subn('', line, count=1)
430 out, n1 = initial_re.subn('', line, count=1)
431 if turnoff_re and not n1:
432 if turnoff_re.match(line):
433 # We're in e.g. a cell magic; disable this transformer for
434 # the rest of the cell.
435 while line is not None:
436 line = (yield line)
437 continue
438
431 line = (yield out)
439 line = (yield out)
432
440
433 if line is None:
441 if line is None:
@@ -455,14 +463,18 b' def classic_prompt():'
455 # FIXME: non-capturing version (?:...) usable?
463 # FIXME: non-capturing version (?:...) usable?
456 prompt_re = re.compile(r'^(>>>|\.\.\.)( |$)')
464 prompt_re = re.compile(r'^(>>>|\.\.\.)( |$)')
457 initial_re = re.compile(r'^>>>( |$)')
465 initial_re = re.compile(r'^>>>( |$)')
458 return _strip_prompts(prompt_re, initial_re)
466 # Any %magic/!system is IPython syntax, so we needn't look for >>> prompts
467 turnoff_re = re.compile(r'^[%!]')
468 return _strip_prompts(prompt_re, initial_re, turnoff_re)
459
469
460 @CoroutineInputTransformer.wrap
470 @CoroutineInputTransformer.wrap
461 def ipy_prompt():
471 def ipy_prompt():
462 """Strip IPython's In [1]:/...: prompts."""
472 """Strip IPython's In [1]:/...: prompts."""
463 # FIXME: non-capturing version (?:...) usable?
473 # FIXME: non-capturing version (?:...) usable?
464 prompt_re = re.compile(r'^(In \[\d+\]: |\s*\.{3,}: ?)')
474 prompt_re = re.compile(r'^(In \[\d+\]: |\s*\.{3,}: ?)')
465 return _strip_prompts(prompt_re)
475 # Disable prompt stripping inside cell magics
476 turnoff_re = re.compile(r'^%%')
477 return _strip_prompts(prompt_re, turnoff_re=turnoff_re)
466
478
467
479
468 @CoroutineInputTransformer.wrap
480 @CoroutineInputTransformer.wrap
@@ -452,7 +452,7 b' class IPythonInputTestCase(InputSplitterTestCase):'
452 ("%%cellm a\nIn[1]:", u'cellm', u'a', u'In[1]:'),
452 ("%%cellm a\nIn[1]:", u'cellm', u'a', u'In[1]:'),
453 ("%%cellm \nline\n>>> hi", u'cellm', u'', u'line\n>>> hi'),
453 ("%%cellm \nline\n>>> hi", u'cellm', u'', u'line\n>>> hi'),
454 (">>> %%cellm \nline\n>>> hi", u'cellm', u'', u'line\nhi'),
454 (">>> %%cellm \nline\n>>> hi", u'cellm', u'', u'line\nhi'),
455 ("%%cellm \n>>> hi", u'cellm', u'', u'hi'),
455 ("%%cellm \n>>> hi", u'cellm', u'', u'>>> hi'),
456 ("%%cellm \nline1\nline2", u'cellm', u'', u'line1\nline2'),
456 ("%%cellm \nline1\nline2", u'cellm', u'', u'line1\nline2'),
457 ("%%cellm \nline1\\\\\nline2", u'cellm', u'', u'line1\\\\\nline2'),
457 ("%%cellm \nline1\\\\\nline2", u'cellm', u'', u'line1\\\\\nline2'),
458 ]:
458 ]:
@@ -360,12 +360,25 b' def test_classic_prompt():'
360 for example in syntax_ml['multiline_datastructure_prompt']:
360 for example in syntax_ml['multiline_datastructure_prompt']:
361 transform_checker(example, ipt.classic_prompt)
361 transform_checker(example, ipt.classic_prompt)
362
362
363 # Check that we don't transform the second line if the first is obviously
364 # IPython syntax
365 transform_checker([
366 (u'%foo', '%foo'),
367 (u'>>> bar', '>>> bar'),
368 ], ipt.classic_prompt)
369
363
370
364 def test_ipy_prompt():
371 def test_ipy_prompt():
365 tt.check_pairs(transform_and_reset(ipt.ipy_prompt), syntax['ipy_prompt'])
372 tt.check_pairs(transform_and_reset(ipt.ipy_prompt), syntax['ipy_prompt'])
366 for example in syntax_ml['ipy_prompt']:
373 for example in syntax_ml['ipy_prompt']:
367 transform_checker(example, ipt.ipy_prompt)
374 transform_checker(example, ipt.ipy_prompt)
368
375
376 # Check that we don't transform the second line if we're inside a cell magic
377 transform_checker([
378 (u'%%foo', '%%foo'),
379 (u'In [1]: bar', 'In [1]: bar'),
380 ], ipt.ipy_prompt)
381
369 def test_coding_cookie():
382 def test_coding_cookie():
370 tt.check_pairs(transform_and_reset(ipt.strip_encoding_cookie), syntax['strip_encoding_cookie'])
383 tt.check_pairs(transform_and_reset(ipt.strip_encoding_cookie), syntax['strip_encoding_cookie'])
371 for example in syntax_ml['strip_encoding_cookie']:
384 for example in syntax_ml['strip_encoding_cookie']:
General Comments 0
You need to be logged in to leave comments. Login now