Debian Codegraph @

tests/test_prefilter.py

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