##// END OF EJS Templates
Add transformers to understand code pasted with >>> or IPython prompts....
Fernando Perez -
Show More
@@ -0,0 +1,34 b''
1 """Tests for input manipulation machinery."""
2
3 #-----------------------------------------------------------------------------
4 # Imports
5 #-----------------------------------------------------------------------------
6 import nose.tools as nt
7
8 from IPython.testing import tools as tt, decorators as dec
9
10 #-----------------------------------------------------------------------------
11 # Tests
12 #-----------------------------------------------------------------------------
13 @dec.parametric
14 def test_prefilter():
15 """Test user input conversions"""
16
17 # pairs of (raw, expected correct) input
18 pairs = [ ('2+2','2+2'),
19 ('>>> 2+2','2+2'),
20 ('>>> # This is a comment\n'
21 '... 2+2',
22 '# This is a comment\n'
23 '2+2'),
24 # Some IPython input
25 ('In [1]: 1', '1'),
26 ('In [2]: for i in range(5):\n'
27 ' ...: print i,',
28 'for i in range(5):\n'
29 ' print i,'),
30 ]
31
32 ip = get_ipython()
33 for raw, correct in pairs:
34 yield nt.assert_equals(ip.prefilter(raw), correct)
@@ -3395,8 +3395,6 b' Defaulting color scheme to \'NoColor\'"""'
3395 your existing IPython session.
3395 your existing IPython session.
3396 """
3396 """
3397
3397
3398 # XXX - Fix this to have cleaner activate/deactivate calls.
3399 from IPython.extensions import InterpreterPasteInput as ipaste
3400 from IPython.utils.ipstruct import Struct
3398 from IPython.utils.ipstruct import Struct
3401
3399
3402 # Shorthands
3400 # Shorthands
@@ -3419,8 +3417,6 b' Defaulting color scheme to \'NoColor\'"""'
3419
3417
3420 if mode == False:
3418 if mode == False:
3421 # turn on
3419 # turn on
3422 ipaste.activate_prefilter()
3423
3424 oc.prompt1.p_template = '>>> '
3420 oc.prompt1.p_template = '>>> '
3425 oc.prompt2.p_template = '... '
3421 oc.prompt2.p_template = '... '
3426 oc.prompt_out.p_template = ''
3422 oc.prompt_out.p_template = ''
@@ -3439,8 +3435,6 b' Defaulting color scheme to \'NoColor\'"""'
3439
3435
3440 else:
3436 else:
3441 # turn off
3437 # turn off
3442 ipaste.deactivate_prefilter()
3443
3444 oc.prompt1.p_template = shell.prompt_in1
3438 oc.prompt1.p_template = shell.prompt_in1
3445 oc.prompt2.p_template = shell.prompt_in2
3439 oc.prompt2.p_template = shell.prompt_in2
3446 oc.prompt_out.p_template = shell.prompt_out
3440 oc.prompt_out.p_template = shell.prompt_out
@@ -3453,7 +3447,7 b' Defaulting color scheme to \'NoColor\'"""'
3453 oc.prompt1.pad_left = oc.prompt2.pad_left = \
3447 oc.prompt1.pad_left = oc.prompt2.pad_left = \
3454 oc.prompt_out.pad_left = dstore.rc_prompts_pad_left
3448 oc.prompt_out.pad_left = dstore.rc_prompts_pad_left
3455
3449
3456 rc.pprint = dstore.rc_pprint
3450 shell.pprint = dstore.rc_pprint
3457
3451
3458 shell.magic_xmode(dstore.xmode)
3452 shell.magic_xmode(dstore.xmode)
3459
3453
@@ -362,7 +362,7 b' class PrefilterManager(Component):'
362 line = transformer.transform(line, continue_prompt)
362 line = transformer.transform(line, continue_prompt)
363 return line
363 return line
364
364
365 def prefilter_line(self, line, continue_prompt):
365 def prefilter_line(self, line, continue_prompt=False):
366 """Prefilter a single input line as text.
366 """Prefilter a single input line as text.
367
367
368 This method prefilters a single line of text by calling the
368 This method prefilters a single line of text by calling the
@@ -508,6 +508,47 b' class AssignMagicTransformer(PrefilterTransformer):'
508 return line
508 return line
509
509
510
510
511 _classic_prompt_re = re.compile(r'(^[ \t]*>>> |^[ \t]*\.\.\. )')
512
513 class PyPromptTransformer(PrefilterTransformer):
514 """Handle inputs that start with '>>> ' syntax."""
515
516 priority = Int(50, config=True)
517
518 def transform(self, line, continue_prompt):
519
520 if not line or line.isspace() or line.strip() == '...':
521 # This allows us to recognize multiple input prompts separated by
522 # blank lines and pasted in a single chunk, very common when
523 # pasting doctests or long tutorial passages.
524 return ''
525 m = _classic_prompt_re.match(line)
526 if m:
527 return line[len(m.group(0)):]
528 else:
529 return line
530
531
532 _ipy_prompt_re = re.compile(r'(^[ \t]*In \[\d+\]: |^[ \t]*\ \ \ \.\.\.+: )')
533
534 class IPyPromptTransformer(PrefilterTransformer):
535 """Handle inputs that start classic IPython prompt syntax."""
536
537 priority = Int(50, config=True)
538
539 def transform(self, line, continue_prompt):
540
541 if not line or line.isspace() or line.strip() == '...':
542 # This allows us to recognize multiple input prompts separated by
543 # blank lines and pasted in a single chunk, very common when
544 # pasting doctests or long tutorial passages.
545 return ''
546 m = _ipy_prompt_re.match(line)
547 if m:
548 return line[len(m.group(0)):]
549 else:
550 return line
551
511 #-----------------------------------------------------------------------------
552 #-----------------------------------------------------------------------------
512 # Prefilter checkers
553 # Prefilter checkers
513 #-----------------------------------------------------------------------------
554 #-----------------------------------------------------------------------------
@@ -974,7 +1015,9 b' class EmacsHandler(PrefilterHandler):'
974
1015
975 _default_transformers = [
1016 _default_transformers = [
976 AssignSystemTransformer,
1017 AssignSystemTransformer,
977 AssignMagicTransformer
1018 AssignMagicTransformer,
1019 PyPromptTransformer,
1020 IPyPromptTransformer,
978 ]
1021 ]
979
1022
980 _default_checkers = [
1023 _default_checkers = [
@@ -999,4 +1042,3 b' _default_handlers = ['
999 HelpHandler,
1042 HelpHandler,
1000 EmacsHandler
1043 EmacsHandler
1001 ]
1044 ]
1002
@@ -27,7 +27,6 b' from IPython.testing import tools as tt'
27 #-----------------------------------------------------------------------------
27 #-----------------------------------------------------------------------------
28 # Test functions begin
28 # Test functions begin
29 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
30
31 def test_rehashx():
30 def test_rehashx():
32 # clear up everything
31 # clear up everything
33 _ip = get_ipython()
32 _ip = get_ipython()
@@ -189,3 +188,9 b' def doctest_time():'
189 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
188 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
190 Wall time: 0.00 s
189 Wall time: 0.00 s
191 """
190 """
191
192 def test_doctest_mode():
193 "Toggle doctest_mode twice, it should be a no-op and run without error"
194 _ip.magic('doctest_mode')
195 _ip.magic('doctest_mode')
196
1 NO CONTENT: file was removed
NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now