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