##// END OF EJS Templates
Disable autocall for prefilter test
Thomas Kluyver -
Show More
@@ -1,118 +1,119 b''
1 """Tests for input manipulation machinery."""
1 """Tests for input manipulation machinery."""
2
2
3 #-----------------------------------------------------------------------------
3 #-----------------------------------------------------------------------------
4 # Imports
4 # Imports
5 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
6 import nose.tools as nt
6 import nose.tools as nt
7
7
8 from IPython.core.prefilter import AutocallChecker
8 from IPython.core.prefilter import AutocallChecker
9 from IPython.testing.globalipapp import get_ipython
9 from IPython.testing.globalipapp import get_ipython
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Tests
12 # Tests
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 ip = get_ipython()
14 ip = get_ipython()
15
15
16 def test_prefilter():
16 def test_prefilter():
17 """Test user input conversions"""
17 """Test user input conversions"""
18
18
19 # pairs of (raw, expected correct) input
19 # pairs of (raw, expected correct) input
20 pairs = [ ('2+2','2+2'),
20 pairs = [ ('2+2','2+2'),
21 ]
21 ]
22
22
23 for raw, correct in pairs:
23 for raw, correct in pairs:
24 nt.assert_equal(ip.prefilter(raw), correct)
24 nt.assert_equal(ip.prefilter(raw), correct)
25
25
26 def test_prefilter_shadowed():
26 def test_prefilter_shadowed():
27 def dummy_magic(line): pass
27 def dummy_magic(line): pass
28
28
29 prev_automagic_state = ip.automagic
29 prev_automagic_state = ip.automagic
30 ip.automagic = True
30 ip.automagic = True
31 ip.autocall = 0
31
32
32 try:
33 try:
33 # These should not be transformed - they are shadowed by other names
34 # These should not be transformed - they are shadowed by other names
34 for name in ['if', 'zip', 'get_ipython']: # keyword, builtin, global
35 for name in ['if', 'zip', 'get_ipython']: # keyword, builtin, global
35 ip.register_magic_function(dummy_magic, magic_name=name)
36 ip.register_magic_function(dummy_magic, magic_name=name)
36 res = ip.prefilter(name+' foo')
37 res = ip.prefilter(name+' foo')
37 nt.assert_equal(res, name+' foo')
38 nt.assert_equal(res, name+' foo')
38 del ip.magics_manager.magics['line'][name]
39 del ip.magics_manager.magics['line'][name]
39
40
40 # These should be transformed
41 # These should be transformed
41 for name in ['fi', 'piz', 'nohtypi_teg']:
42 for name in ['fi', 'piz', 'nohtypi_teg']:
42 ip.register_magic_function(dummy_magic, magic_name=name)
43 ip.register_magic_function(dummy_magic, magic_name=name)
43 res = ip.prefilter(name+' foo')
44 res = ip.prefilter(name+' foo')
44 nt.assert_not_equal(res, name+' foo')
45 nt.assert_not_equal(res, name+' foo')
45 del ip.magics_manager.magics['line'][name]
46 del ip.magics_manager.magics['line'][name]
46
47
47 finally:
48 finally:
48 ip.automagic = prev_automagic_state
49 ip.automagic = prev_automagic_state
49
50
50 def test_autocall_binops():
51 def test_autocall_binops():
51 """See https://github.com/ipython/ipython/issues/81"""
52 """See https://github.com/ipython/ipython/issues/81"""
52 ip.magic('autocall 2')
53 ip.magic('autocall 2')
53 f = lambda x: x
54 f = lambda x: x
54 ip.user_ns['f'] = f
55 ip.user_ns['f'] = f
55 try:
56 try:
56 nt.assert_equal(ip.prefilter('f 1'),'f(1)')
57 nt.assert_equal(ip.prefilter('f 1'),'f(1)')
57 for t in ['f +1', 'f -1']:
58 for t in ['f +1', 'f -1']:
58 nt.assert_equal(ip.prefilter(t), t)
59 nt.assert_equal(ip.prefilter(t), t)
59
60
60 # Run tests again with a more permissive exclude_regexp, which will
61 # Run tests again with a more permissive exclude_regexp, which will
61 # allow transformation of binary operations ('f -1' -> 'f(-1)').
62 # allow transformation of binary operations ('f -1' -> 'f(-1)').
62 pm = ip.prefilter_manager
63 pm = ip.prefilter_manager
63 ac = AutocallChecker(shell=pm.shell, prefilter_manager=pm,
64 ac = AutocallChecker(shell=pm.shell, prefilter_manager=pm,
64 config=pm.config)
65 config=pm.config)
65 try:
66 try:
66 ac.priority = 1
67 ac.priority = 1
67 ac.exclude_regexp = r'^[,&^\|\*/]|^is |^not |^in |^and |^or '
68 ac.exclude_regexp = r'^[,&^\|\*/]|^is |^not |^in |^and |^or '
68 pm.sort_checkers()
69 pm.sort_checkers()
69
70
70 nt.assert_equal(ip.prefilter('f -1'), 'f(-1)')
71 nt.assert_equal(ip.prefilter('f -1'), 'f(-1)')
71 nt.assert_equal(ip.prefilter('f +1'), 'f(+1)')
72 nt.assert_equal(ip.prefilter('f +1'), 'f(+1)')
72 finally:
73 finally:
73 pm.unregister_checker(ac)
74 pm.unregister_checker(ac)
74 finally:
75 finally:
75 ip.magic('autocall 0')
76 ip.magic('autocall 0')
76 del ip.user_ns['f']
77 del ip.user_ns['f']
77
78
78
79
79 def test_issue_114():
80 def test_issue_114():
80 """Check that multiline string literals don't expand as magic
81 """Check that multiline string literals don't expand as magic
81 see http://github.com/ipython/ipython/issues/114"""
82 see http://github.com/ipython/ipython/issues/114"""
82
83
83 template = '"""\n%s\n"""'
84 template = '"""\n%s\n"""'
84 # Store the current value of multi_line_specials and turn it off before
85 # Store the current value of multi_line_specials and turn it off before
85 # running test, since it could be true (case in which the test doesn't make
86 # running test, since it could be true (case in which the test doesn't make
86 # sense, as multiline string literals *will* expand as magic in that case).
87 # sense, as multiline string literals *will* expand as magic in that case).
87 msp = ip.prefilter_manager.multi_line_specials
88 msp = ip.prefilter_manager.multi_line_specials
88 ip.prefilter_manager.multi_line_specials = False
89 ip.prefilter_manager.multi_line_specials = False
89 try:
90 try:
90 for mgk in ip.magics_manager.lsmagic()['line']:
91 for mgk in ip.magics_manager.lsmagic()['line']:
91 raw = template % mgk
92 raw = template % mgk
92 nt.assert_equal(ip.prefilter(raw), raw)
93 nt.assert_equal(ip.prefilter(raw), raw)
93 finally:
94 finally:
94 ip.prefilter_manager.multi_line_specials = msp
95 ip.prefilter_manager.multi_line_specials = msp
95
96
96
97
97 def test_prefilter_attribute_errors():
98 def test_prefilter_attribute_errors():
98 """Capture exceptions thrown by user objects on attribute access.
99 """Capture exceptions thrown by user objects on attribute access.
99
100
100 See http://github.com/ipython/ipython/issues/988."""
101 See http://github.com/ipython/ipython/issues/988."""
101
102
102 class X(object):
103 class X(object):
103 def __getattr__(self, k):
104 def __getattr__(self, k):
104 raise ValueError('broken object')
105 raise ValueError('broken object')
105 def __call__(self, x):
106 def __call__(self, x):
106 return x
107 return x
107
108
108 # Create a callable broken object
109 # Create a callable broken object
109 ip.user_ns['x'] = X()
110 ip.user_ns['x'] = X()
110 ip.magic('autocall 2')
111 ip.magic('autocall 2')
111 try:
112 try:
112 # Even if x throws an attribute error when looking at its rewrite
113 # Even if x throws an attribute error when looking at its rewrite
113 # attribute, we should not crash. So the test here is simply making
114 # attribute, we should not crash. So the test here is simply making
114 # the prefilter call and not having an exception.
115 # the prefilter call and not having an exception.
115 ip.prefilter('x 1')
116 ip.prefilter('x 1')
116 finally:
117 finally:
117 del ip.user_ns['x']
118 del ip.user_ns['x']
118 ip.magic('autocall 0')
119 ip.magic('autocall 0')
General Comments 0
You need to be logged in to leave comments. Login now