##// END OF EJS Templates
Remove assignment to ip in test files...
Matthias Bussonnier -
Show More
@@ -1,312 +1,311 b''
1 1 """
2 2 Test for async helpers.
3 3
4 4 Should only trigger on python 3.5+ or will have syntax errors.
5 5 """
6
7 6 import sys
8 7 from itertools import chain, repeat
9 8 import nose.tools as nt
10 9 from textwrap import dedent, indent
11 10 from unittest import TestCase
12 11 from IPython.testing.decorators import skip_without
13 12
14 ip = get_ipython()
13
15 14 iprc = lambda x: ip.run_cell(dedent(x)).raise_error()
16 15 iprc_nr = lambda x: ip.run_cell(dedent(x))
17 16
18 17 if sys.version_info > (3, 5):
19 18 from IPython.core.async_helpers import _should_be_async
20 19
21 20 class AsyncTest(TestCase):
22 21 def test_should_be_async(self):
23 22 nt.assert_false(_should_be_async("False"))
24 23 nt.assert_true(_should_be_async("await bar()"))
25 24 nt.assert_true(_should_be_async("x = await bar()"))
26 25 nt.assert_false(
27 26 _should_be_async(
28 27 dedent(
29 28 """
30 29 async def awaitable():
31 30 pass
32 31 """
33 32 )
34 33 )
35 34 )
36 35
37 36 def _get_top_level_cases(self):
38 37 # These are test cases that should be valid in a function
39 38 # but invalid outside of a function.
40 39 test_cases = []
41 40 test_cases.append(('basic', "{val}"))
42 41
43 42 # Note, in all conditional cases, I use True instead of
44 43 # False so that the peephole optimizer won't optimize away
45 44 # the return, so CPython will see this as a syntax error:
46 45 #
47 46 # while True:
48 47 # break
49 48 # return
50 49 #
51 50 # But not this:
52 51 #
53 52 # while False:
54 53 # return
55 54 #
56 55 # See https://bugs.python.org/issue1875
57 56
58 57 test_cases.append(('if', dedent("""
59 58 if True:
60 59 {val}
61 60 """)))
62 61
63 62 test_cases.append(('while', dedent("""
64 63 while True:
65 64 {val}
66 65 break
67 66 """)))
68 67
69 68 test_cases.append(('try', dedent("""
70 69 try:
71 70 {val}
72 71 except:
73 72 pass
74 73 """)))
75 74
76 75 test_cases.append(('except', dedent("""
77 76 try:
78 77 pass
79 78 except:
80 79 {val}
81 80 """)))
82 81
83 82 test_cases.append(('finally', dedent("""
84 83 try:
85 84 pass
86 85 except:
87 86 pass
88 87 finally:
89 88 {val}
90 89 """)))
91 90
92 91 test_cases.append(('for', dedent("""
93 92 for _ in range(4):
94 93 {val}
95 94 """)))
96 95
97 96
98 97 test_cases.append(('nested', dedent("""
99 98 if True:
100 99 while True:
101 100 {val}
102 101 break
103 102 """)))
104 103
105 104 test_cases.append(('deep-nested', dedent("""
106 105 if True:
107 106 while True:
108 107 break
109 108 for x in range(3):
110 109 if True:
111 110 while True:
112 111 for x in range(3):
113 112 {val}
114 113 """)))
115 114
116 115 return test_cases
117 116
118 117 def _get_ry_syntax_errors(self):
119 118 # This is a mix of tests that should be a syntax error if
120 119 # return or yield whether or not they are in a function
121 120
122 121 test_cases = []
123 122
124 123 test_cases.append(('class', dedent("""
125 124 class V:
126 125 {val}
127 126 """)))
128 127
129 128 test_cases.append(('nested-class', dedent("""
130 129 class V:
131 130 class C:
132 131 {val}
133 132 """)))
134 133
135 134 return test_cases
136 135
137 136
138 137 def test_top_level_return_error(self):
139 138 tl_err_test_cases = self._get_top_level_cases()
140 139 tl_err_test_cases.extend(self._get_ry_syntax_errors())
141 140
142 141 vals = ('return', 'yield', 'yield from (_ for _ in range(3))',
143 142 dedent('''
144 143 def f():
145 144 pass
146 145 return
147 146 '''),
148 147 )
149 148
150 149 for test_name, test_case in tl_err_test_cases:
151 150 # This example should work if 'pass' is used as the value
152 151 with self.subTest((test_name, 'pass')):
153 152 iprc(test_case.format(val='pass'))
154 153
155 154 # It should fail with all the values
156 155 for val in vals:
157 156 with self.subTest((test_name, val)):
158 157 msg = "Syntax error not raised for %s, %s" % (test_name, val)
159 158 with self.assertRaises(SyntaxError, msg=msg):
160 159 iprc(test_case.format(val=val))
161 160
162 161 def test_in_func_no_error(self):
163 162 # Test that the implementation of top-level return/yield
164 163 # detection isn't *too* aggressive, and works inside a function
165 164 func_contexts = []
166 165
167 166 func_contexts.append(('func', False, dedent("""
168 167 def f():""")))
169 168
170 169 func_contexts.append(('method', False, dedent("""
171 170 class MyClass:
172 171 def __init__(self):
173 172 """)))
174 173
175 174 func_contexts.append(('async-func', True, dedent("""
176 175 async def f():""")))
177 176
178 177 func_contexts.append(('async-method', True, dedent("""
179 178 class MyClass:
180 179 async def f(self):""")))
181 180
182 181 func_contexts.append(('closure', False, dedent("""
183 182 def f():
184 183 def g():
185 184 """)))
186 185
187 186 def nest_case(context, case):
188 187 # Detect indentation
189 188 lines = context.strip().splitlines()
190 189 prefix_len = 0
191 190 for c in lines[-1]:
192 191 if c != ' ':
193 192 break
194 193 prefix_len += 1
195 194
196 195 indented_case = indent(case, ' ' * (prefix_len + 4))
197 196 return context + '\n' + indented_case
198 197
199 198 # Gather and run the tests
200 199
201 200 # yield is allowed in async functions, starting in Python 3.6,
202 201 # and yield from is not allowed in any version
203 202 vals = ('return', 'yield', 'yield from (_ for _ in range(3))')
204 203 async_safe = (True,
205 204 sys.version_info >= (3, 6),
206 205 False)
207 206 vals = tuple(zip(vals, async_safe))
208 207
209 208 success_tests = zip(self._get_top_level_cases(), repeat(False))
210 209 failure_tests = zip(self._get_ry_syntax_errors(), repeat(True))
211 210
212 211 tests = chain(success_tests, failure_tests)
213 212
214 213 for context_name, async_func, context in func_contexts:
215 214 for (test_name, test_case), should_fail in tests:
216 215 nested_case = nest_case(context, test_case)
217 216
218 217 for val, async_safe in vals:
219 218 val_should_fail = (should_fail or
220 219 (async_func and not async_safe))
221 220
222 221 test_id = (context_name, test_name, val)
223 222 cell = nested_case.format(val=val)
224 223
225 224 with self.subTest(test_id):
226 225 if val_should_fail:
227 226 msg = ("SyntaxError not raised for %s" %
228 227 str(test_id))
229 228 with self.assertRaises(SyntaxError, msg=msg):
230 229 iprc(cell)
231 230
232 231 print(cell)
233 232 else:
234 233 iprc(cell)
235 234
236 235 def test_nonlocal(self):
237 236 # fails if outer scope is not a function scope or if var not defined
238 237 with self.assertRaises(SyntaxError):
239 238 iprc("nonlocal x")
240 239 iprc("""
241 240 x = 1
242 241 def f():
243 242 nonlocal x
244 243 x = 10000
245 244 yield x
246 245 """)
247 246 iprc("""
248 247 def f():
249 248 def g():
250 249 nonlocal x
251 250 x = 10000
252 251 yield x
253 252 """)
254 253
255 254 # works if outer scope is a function scope and var exists
256 255 iprc("""
257 256 def f():
258 257 x = 20
259 258 def g():
260 259 nonlocal x
261 260 x = 10000
262 261 yield x
263 262 """)
264 263
265 264
266 265 def test_execute(self):
267 266 iprc("""
268 267 import asyncio
269 268 await asyncio.sleep(0.001)
270 269 """
271 270 )
272 271
273 272 def test_autoawait(self):
274 273 iprc("%autoawait False")
275 274 iprc("%autoawait True")
276 275 iprc("""
277 276 from asyncio import sleep
278 277 await sleep(0.1)
279 278 """
280 279 )
281 280
282 281 @skip_without('curio')
283 282 def test_autoawait_curio(self):
284 283 iprc("%autoawait curio")
285 284
286 285 @skip_without('trio')
287 286 def test_autoawait_trio(self):
288 287 iprc("%autoawait trio")
289 288
290 289 @skip_without('trio')
291 290 def test_autoawait_trio_wrong_sleep(self):
292 291 iprc("%autoawait trio")
293 292 res = iprc_nr("""
294 293 import asyncio
295 294 await asyncio.sleep(0)
296 295 """)
297 296 with nt.assert_raises(TypeError):
298 297 res.raise_error()
299 298
300 299 @skip_without('trio')
301 300 def test_autoawait_asyncio_wrong_sleep(self):
302 301 iprc("%autoawait asyncio")
303 302 res = iprc_nr("""
304 303 import trio
305 304 await trio.sleep(0)
306 305 """)
307 306 with nt.assert_raises(RuntimeError):
308 307 res.raise_error()
309 308
310 309
311 310 def tearDown(self):
312 311 ip.loop_runner = "asyncio"
@@ -1,72 +1,67 b''
1 1 """These kinds of tests are less than ideal, but at least they run.
2 2
3 3 This was an old test that was being run interactively in the top-level tests/
4 4 directory, which we are removing. For now putting this here ensures at least
5 5 we do run the test, though ultimately this functionality should all be tested
6 6 with better-isolated tests that don't rely on the global instance in iptest.
7 7 """
8 8 from IPython.core.splitinput import LineInfo
9 9 from IPython.core.prefilter import AutocallChecker
10 10 from IPython.utils import py3compat
11 from IPython.testing.globalipapp import get_ipython
12
13
14 ip = get_ipython()
15
16 11
17 12 def doctest_autocall():
18 13 """
19 14 In [1]: def f1(a,b,c):
20 15 ...: return a+b+c
21 16 ...:
22 17
23 18 In [2]: def f2(a):
24 19 ...: return a + a
25 20 ...:
26 21
27 22 In [3]: def r(x):
28 23 ...: return True
29 24 ...:
30 25
31 26 In [4]: ;f2 a b c
32 27 Out[4]: 'a b ca b c'
33 28
34 29 In [5]: assert _ == "a b ca b c"
35 30
36 31 In [6]: ,f1 a b c
37 32 Out[6]: 'abc'
38 33
39 34 In [7]: assert _ == 'abc'
40 35
41 36 In [8]: print(_)
42 37 abc
43 38
44 39 In [9]: /f1 1,2,3
45 40 Out[9]: 6
46 41
47 42 In [10]: assert _ == 6
48 43
49 44 In [11]: /f2 4
50 45 Out[11]: 8
51 46
52 47 In [12]: assert _ == 8
53 48
54 49 In [12]: del f1, f2
55 50
56 51 In [13]: ,r a
57 52 Out[13]: True
58 53
59 54 In [14]: assert _ == True
60 55
61 56 In [15]: r'a'
62 57 Out[15]: 'a'
63 58
64 59 In [16]: assert _ == 'a'
65 60 """
66 61
67 62
68 63 def test_autocall_should_ignore_raw_strings():
69 64 line_info = LineInfo("r'a'")
70 65 pm = ip.prefilter_manager
71 66 ac = AutocallChecker(shell=pm.shell, prefilter_manager=pm, config=pm.config)
72 67 assert ac.check(line_info) is None
@@ -1,114 +1,112 b''
1 1 import sys
2 2 from IPython.testing.tools import AssertPrints, AssertNotPrints
3 3 from IPython.core.displayhook import CapturingDisplayHook
4 4 from IPython.utils.capture import CapturedIO
5 5
6 ip = get_ipython()
7
8 6 def test_output_displayed():
9 7 """Checking to make sure that output is displayed"""
10 8
11 9 with AssertPrints('2'):
12 10 ip.run_cell('1+1', store_history=True)
13 11
14 12 with AssertPrints('2'):
15 13 ip.run_cell('1+1 # comment with a semicolon;', store_history=True)
16 14
17 15 with AssertPrints('2'):
18 16 ip.run_cell('1+1\n#commented_out_function();', store_history=True)
19 17
20 18
21 19 def test_output_quiet():
22 20 """Checking to make sure that output is quiet"""
23 21
24 22 with AssertNotPrints('2'):
25 23 ip.run_cell('1+1;', store_history=True)
26 24
27 25 with AssertNotPrints('2'):
28 26 ip.run_cell('1+1; # comment with a semicolon', store_history=True)
29 27
30 28 with AssertNotPrints('2'):
31 29 ip.run_cell('1+1;\n#commented_out_function()', store_history=True)
32 30
33 31 def test_underscore_no_overrite_user():
34 32 ip.run_cell('_ = 42', store_history=True)
35 33 ip.run_cell('1+1', store_history=True)
36 34
37 35 with AssertPrints('42'):
38 36 ip.run_cell('print(_)', store_history=True)
39 37
40 38 ip.run_cell('del _', store_history=True)
41 39 ip.run_cell('6+6', store_history=True)
42 40 with AssertPrints('12'):
43 41 ip.run_cell('_', store_history=True)
44 42
45 43
46 44 def test_underscore_no_overrite_builtins():
47 45 ip.run_cell("import gettext ; gettext.install('foo')", store_history=True)
48 46 ip.run_cell('3+3', store_history=True)
49 47
50 48 with AssertPrints('gettext'):
51 49 ip.run_cell('print(_)', store_history=True)
52 50
53 51 ip.run_cell('_ = "userset"', store_history=True)
54 52
55 53 with AssertPrints('userset'):
56 54 ip.run_cell('print(_)', store_history=True)
57 55 ip.run_cell('import builtins; del builtins._')
58 56
59 57
60 58 def test_interactivehooks_ast_modes():
61 59 """
62 60 Test that ast nodes can be triggered with different modes
63 61 """
64 62 saved_mode = ip.ast_node_interactivity
65 63 ip.ast_node_interactivity = 'last_expr_or_assign'
66 64
67 65 try:
68 66 with AssertPrints('2'):
69 67 ip.run_cell('a = 1+1', store_history=True)
70 68
71 69 with AssertPrints('9'):
72 70 ip.run_cell('b = 1+8 # comment with a semicolon;', store_history=False)
73 71
74 72 with AssertPrints('7'):
75 73 ip.run_cell('c = 1+6\n#commented_out_function();', store_history=True)
76 74
77 75 ip.run_cell('d = 11', store_history=True)
78 76 with AssertPrints('12'):
79 77 ip.run_cell('d += 1', store_history=True)
80 78
81 79 with AssertNotPrints('42'):
82 80 ip.run_cell('(u,v) = (41+1, 43-1)')
83 81
84 82 finally:
85 83 ip.ast_node_interactivity = saved_mode
86 84
87 85 def test_interactivehooks_ast_modes_semi_supress():
88 86 """
89 87 Test that ast nodes can be triggered with different modes and suppressed
90 88 by semicolon
91 89 """
92 90 saved_mode = ip.ast_node_interactivity
93 91 ip.ast_node_interactivity = 'last_expr_or_assign'
94 92
95 93 try:
96 94 with AssertNotPrints('2'):
97 95 ip.run_cell('x = 1+1;', store_history=True)
98 96
99 97 with AssertNotPrints('7'):
100 98 ip.run_cell('y = 1+6; # comment with a semicolon', store_history=True)
101 99
102 100 with AssertNotPrints('9'):
103 101 ip.run_cell('z = 1+8;\n#commented_out_function()', store_history=True)
104 102
105 103 finally:
106 104 ip.ast_node_interactivity = saved_mode
107 105
108 106 def test_capture_display_hook_format():
109 107 """Tests that the capture display hook conforms to the CapturedIO output format"""
110 108 hook = CapturingDisplayHook(ip)
111 109 hook({"foo": "bar"})
112 110 captured = CapturedIO(sys.stdout, sys.stderr, hook.outputs)
113 111 # Should not raise with RichOutput transformation error
114 112 captured.outputs
@@ -1,97 +1,95 b''
1 1 """Tests for input handlers.
2 2 """
3 3 #-----------------------------------------------------------------------------
4 4 # Module imports
5 5 #-----------------------------------------------------------------------------
6 6
7 7 # third party
8 8 import nose.tools as nt
9 9
10 10 # our own packages
11 11 from IPython.core import autocall
12 12 from IPython.testing import tools as tt
13 from IPython.testing.globalipapp import get_ipython
14 13 from IPython.utils import py3compat
15 14
16 15 #-----------------------------------------------------------------------------
17 16 # Globals
18 17 #-----------------------------------------------------------------------------
19 18
20 19 # Get the public instance of IPython
21 ip = get_ipython()
22 20
23 21 failures = []
24 22 num_tests = 0
25 23
26 24 #-----------------------------------------------------------------------------
27 25 # Test functions
28 26 #-----------------------------------------------------------------------------
29 27
30 28 class CallableIndexable(object):
31 29 def __getitem__(self, idx): return True
32 30 def __call__(self, *args, **kws): return True
33 31
34 32
35 33 class Autocallable(autocall.IPyAutocall):
36 34 def __call__(self):
37 35 return "called"
38 36
39 37
40 38 def run(tests):
41 39 """Loop through a list of (pre, post) inputs, where pre is the string
42 40 handed to ipython, and post is how that string looks after it's been
43 41 transformed (i.e. ipython's notion of _i)"""
44 42 tt.check_pairs(ip.prefilter_manager.prefilter_lines, tests)
45 43
46 44
47 45 def test_handlers():
48 46 call_idx = CallableIndexable()
49 47 ip.user_ns['call_idx'] = call_idx
50 48
51 49 # For many of the below, we're also checking that leading whitespace
52 50 # turns off the esc char, which it should unless there is a continuation
53 51 # line.
54 52 run([(i,py3compat.u_format(o)) for i,o in \
55 53 [('"no change"', '"no change"'), # normal
56 54 (u"lsmagic", "get_ipython().run_line_magic('lsmagic', '')"), # magic
57 55 #("a = b # PYTHON-MODE", '_i'), # emacs -- avoids _in cache
58 56 ]])
59 57
60 58 # Objects which are instances of IPyAutocall are *always* autocalled
61 59 autocallable = Autocallable()
62 60 ip.user_ns['autocallable'] = autocallable
63 61
64 62 # auto
65 63 ip.magic('autocall 0')
66 64 # Only explicit escapes or instances of IPyAutocallable should get
67 65 # expanded
68 66 run([
69 67 ('len "abc"', 'len "abc"'),
70 68 ('autocallable', 'autocallable()'),
71 69 # Don't add extra brackets (gh-1117)
72 70 ('autocallable()', 'autocallable()'),
73 71 ])
74 72 ip.magic('autocall 1')
75 73 run([
76 74 ('len "abc"', 'len("abc")'),
77 75 ('len "abc";', 'len("abc");'), # ; is special -- moves out of parens
78 76 # Autocall is turned off if first arg is [] and the object
79 77 # is both callable and indexable. Like so:
80 78 ('len [1,2]', 'len([1,2])'), # len doesn't support __getitem__...
81 79 ('call_idx [1]', 'call_idx [1]'), # call_idx *does*..
82 80 ('call_idx 1', 'call_idx(1)'),
83 81 ('len', 'len'), # only at 2 does it auto-call on single args
84 82 ])
85 83 ip.magic('autocall 2')
86 84 run([
87 85 ('len "abc"', 'len("abc")'),
88 86 ('len "abc";', 'len("abc");'),
89 87 ('len [1,2]', 'len([1,2])'),
90 88 ('call_idx [1]', 'call_idx [1]'),
91 89 ('call_idx 1', 'call_idx(1)'),
92 90 # This is what's different:
93 91 ('len', 'len()'), # only at 2 does it auto-call on single args
94 92 ])
95 93 ip.magic('autocall 1')
96 94
97 95 nt.assert_equal(failures, [])
@@ -1,254 +1,246 b''
1 1 """Tests for the key interactiveshell module, where the main ipython class is defined.
2 2 """
3 3 #-----------------------------------------------------------------------------
4 4 # Module imports
5 5 #-----------------------------------------------------------------------------
6 6
7 7 # third party
8 8 import nose.tools as nt
9 9
10 10 # our own packages
11 from IPython.testing.globalipapp import get_ipython
12
13 #-----------------------------------------------------------------------------
14 # Globals
15 #-----------------------------------------------------------------------------
16
17 # Get the public instance of IPython
18 ip = get_ipython()
19 11
20 12 #-----------------------------------------------------------------------------
21 13 # Test functions
22 14 #-----------------------------------------------------------------------------
23 15
24 16 def test_reset():
25 17 """reset must clear most namespaces."""
26 18
27 19 # Check that reset runs without error
28 20 ip.reset()
29 21
30 22 # Once we've reset it (to clear of any junk that might have been there from
31 23 # other tests, we can count how many variables are in the user's namespace
32 24 nvars_user_ns = len(ip.user_ns)
33 25 nvars_hidden = len(ip.user_ns_hidden)
34 26
35 27 # Now add a few variables to user_ns, and check that reset clears them
36 28 ip.user_ns['x'] = 1
37 29 ip.user_ns['y'] = 1
38 30 ip.reset()
39 31
40 32 # Finally, check that all namespaces have only as many variables as we
41 33 # expect to find in them:
42 34 nt.assert_equal(len(ip.user_ns), nvars_user_ns)
43 35 nt.assert_equal(len(ip.user_ns_hidden), nvars_hidden)
44 36
45 37
46 38 # Tests for reporting of exceptions in various modes, handling of SystemExit,
47 39 # and %tb functionality. This is really a mix of testing ultraTB and interactiveshell.
48 40
49 41 def doctest_tb_plain():
50 42 """
51 43 In [18]: xmode plain
52 44 Exception reporting mode: Plain
53 45
54 46 In [19]: run simpleerr.py
55 47 Traceback (most recent call last):
56 48 ...line 32, in <module>
57 49 bar(mode)
58 50 ...line 16, in bar
59 51 div0()
60 52 ...line 8, in div0
61 53 x/y
62 54 ZeroDivisionError: ...
63 55 """
64 56
65 57
66 58 def doctest_tb_context():
67 59 """
68 60 In [3]: xmode context
69 61 Exception reporting mode: Context
70 62
71 63 In [4]: run simpleerr.py
72 64 ---------------------------------------------------------------------------
73 65 ZeroDivisionError Traceback (most recent call last)
74 66 <BLANKLINE>
75 67 ... in <module>
76 68 30 mode = 'div'
77 69 31
78 70 ---> 32 bar(mode)
79 71 <BLANKLINE>
80 72 ... in bar(mode)
81 73 14 "bar"
82 74 15 if mode=='div':
83 75 ---> 16 div0()
84 76 17 elif mode=='exit':
85 77 18 try:
86 78 <BLANKLINE>
87 79 ... in div0()
88 80 6 x = 1
89 81 7 y = 0
90 82 ----> 8 x/y
91 83 9
92 84 10 def sysexit(stat, mode):
93 85 <BLANKLINE>
94 86 ZeroDivisionError: ...
95 87 """
96 88
97 89
98 90 def doctest_tb_verbose():
99 91 """
100 92 In [5]: xmode verbose
101 93 Exception reporting mode: Verbose
102 94
103 95 In [6]: run simpleerr.py
104 96 ---------------------------------------------------------------------------
105 97 ZeroDivisionError Traceback (most recent call last)
106 98 <BLANKLINE>
107 99 ... in <module>
108 100 30 mode = 'div'
109 101 31
110 102 ---> 32 bar(mode)
111 103 global bar = <function bar at ...>
112 104 global mode = 'div'
113 105 <BLANKLINE>
114 106 ... in bar(mode='div')
115 107 14 "bar"
116 108 15 if mode=='div':
117 109 ---> 16 div0()
118 110 global div0 = <function div0 at ...>
119 111 17 elif mode=='exit':
120 112 18 try:
121 113 <BLANKLINE>
122 114 ... in div0()
123 115 6 x = 1
124 116 7 y = 0
125 117 ----> 8 x/y
126 118 x = 1
127 119 y = 0
128 120 9
129 121 10 def sysexit(stat, mode):
130 122 <BLANKLINE>
131 123 ZeroDivisionError: ...
132 124 """
133 125
134 126 def doctest_tb_sysexit():
135 127 """
136 128 In [17]: %xmode plain
137 129 Exception reporting mode: Plain
138 130
139 131 In [18]: %run simpleerr.py exit
140 132 An exception has occurred, use %tb to see the full traceback.
141 133 SystemExit: (1, 'Mode = exit')
142 134
143 135 In [19]: %run simpleerr.py exit 2
144 136 An exception has occurred, use %tb to see the full traceback.
145 137 SystemExit: (2, 'Mode = exit')
146 138
147 139 In [20]: %tb
148 140 Traceback (most recent call last):
149 141 File ... in <module>
150 142 bar(mode)
151 143 File ... line 22, in bar
152 144 sysexit(stat, mode)
153 145 File ... line 11, in sysexit
154 146 raise SystemExit(stat, 'Mode = %s' % mode)
155 147 SystemExit: (2, 'Mode = exit')
156 148
157 149 In [21]: %xmode context
158 150 Exception reporting mode: Context
159 151
160 152 In [22]: %tb
161 153 ---------------------------------------------------------------------------
162 154 SystemExit Traceback (most recent call last)
163 155 <BLANKLINE>
164 156 ...<module>
165 157 30 mode = 'div'
166 158 31
167 159 ---> 32 bar(mode)
168 160 <BLANKLINE>
169 161 ...bar(mode)
170 162 20 except:
171 163 21 stat = 1
172 164 ---> 22 sysexit(stat, mode)
173 165 23 else:
174 166 24 raise ValueError('Unknown mode')
175 167 <BLANKLINE>
176 168 ...sysexit(stat, mode)
177 169 9
178 170 10 def sysexit(stat, mode):
179 171 ---> 11 raise SystemExit(stat, 'Mode = %s' % mode)
180 172 12
181 173 13 def bar(mode):
182 174 <BLANKLINE>
183 175 SystemExit: (2, 'Mode = exit')
184 176
185 177 In [23]: %xmode verbose
186 178 Exception reporting mode: Verbose
187 179
188 180 In [24]: %tb
189 181 ---------------------------------------------------------------------------
190 182 SystemExit Traceback (most recent call last)
191 183 <BLANKLINE>
192 184 ... in <module>
193 185 30 mode = 'div'
194 186 31
195 187 ---> 32 bar(mode)
196 188 global bar = <function bar at ...>
197 189 global mode = 'exit'
198 190 <BLANKLINE>
199 191 ... in bar(mode='exit')
200 192 20 except:
201 193 21 stat = 1
202 194 ---> 22 sysexit(stat, mode)
203 195 global sysexit = <function sysexit at ...>
204 196 stat = 2
205 197 mode = 'exit'
206 198 23 else:
207 199 24 raise ValueError('Unknown mode')
208 200 <BLANKLINE>
209 201 ... in sysexit(stat=2, mode='exit')
210 202 9
211 203 10 def sysexit(stat, mode):
212 204 ---> 11 raise SystemExit(stat, 'Mode = %s' % mode)
213 205 global SystemExit = undefined
214 206 stat = 2
215 207 mode = 'exit'
216 208 12
217 209 13 def bar(mode):
218 210 <BLANKLINE>
219 211 SystemExit: (2, 'Mode = exit')
220 212 """
221 213
222 214
223 215 def test_run_cell():
224 216 import textwrap
225 217 ip.run_cell('a = 10\na+=1')
226 218 ip.run_cell('assert a == 11\nassert 1')
227 219
228 220 nt.assert_equal(ip.user_ns['a'], 11)
229 221 complex = textwrap.dedent("""
230 222 if 1:
231 223 print "hello"
232 224 if 1:
233 225 print "world"
234 226
235 227 if 2:
236 228 print "foo"
237 229
238 230 if 3:
239 231 print "bar"
240 232
241 233 if 4:
242 234 print "bar"
243 235
244 236 """)
245 237 # Simply verifies that this kind of input is run
246 238 ip.run_cell(complex)
247 239
248 240
249 241 def test_db():
250 242 """Test the internal database used for variable persistence."""
251 243 ip.db['__unittest_'] = 12
252 244 nt.assert_equal(ip.db['__unittest_'], 12)
253 245 del ip.db['__unittest_']
254 246 assert '__unittest_' not in ip.db
@@ -1,32 +1,30 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Test IPython.core.logger"""
3 3
4 4 import os.path
5 5
6 6 import nose.tools as nt
7 7 from IPython.utils.tempdir import TemporaryDirectory
8 8
9 _ip = get_ipython()
10
11 9 def test_logstart_inaccessible_file():
12 10 try:
13 11 _ip.logger.logstart(logfname="/") # Opening that filename will fail.
14 12 except IOError:
15 13 pass
16 14 else:
17 15 nt.assert_true(False) # The try block should never pass.
18 16
19 17 try:
20 18 _ip.run_cell("a=1") # Check it doesn't try to log this
21 19 finally:
22 20 _ip.logger.log_active = False # If this fails, don't let later tests fail
23 21
24 22 def test_logstart_unicode():
25 23 with TemporaryDirectory() as tdir:
26 24 logfname = os.path.join(tdir, "test_unicode.log")
27 25 _ip.run_cell("'abc€'")
28 26 try:
29 27 _ip.magic("logstart -to %s" % logfname)
30 28 _ip.run_cell("'abc€'")
31 29 finally:
32 30 _ip.logger.logstop()
@@ -1,119 +1,117 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 from IPython.testing.globalipapp import get_ipython
10 9
11 10 #-----------------------------------------------------------------------------
12 11 # Tests
13 12 #-----------------------------------------------------------------------------
14 ip = get_ipython()
15 13
16 14 def test_prefilter():
17 15 """Test user input conversions"""
18 16
19 17 # pairs of (raw, expected correct) input
20 18 pairs = [ ('2+2','2+2'),
21 19 ]
22 20
23 21 for raw, correct in pairs:
24 22 nt.assert_equal(ip.prefilter(raw), correct)
25 23
26 24 def test_prefilter_shadowed():
27 25 def dummy_magic(line): pass
28 26
29 27 prev_automagic_state = ip.automagic
30 28 ip.automagic = True
31 29 ip.autocall = 0
32 30
33 31 try:
34 32 # These should not be transformed - they are shadowed by other names
35 33 for name in ['if', 'zip', 'get_ipython']: # keyword, builtin, global
36 34 ip.register_magic_function(dummy_magic, magic_name=name)
37 35 res = ip.prefilter(name+' foo')
38 36 nt.assert_equal(res, name+' foo')
39 37 del ip.magics_manager.magics['line'][name]
40 38
41 39 # These should be transformed
42 40 for name in ['fi', 'piz', 'nohtypi_teg']:
43 41 ip.register_magic_function(dummy_magic, magic_name=name)
44 42 res = ip.prefilter(name+' foo')
45 43 nt.assert_not_equal(res, name+' foo')
46 44 del ip.magics_manager.magics['line'][name]
47 45
48 46 finally:
49 47 ip.automagic = prev_automagic_state
50 48
51 49 def test_autocall_binops():
52 50 """See https://github.com/ipython/ipython/issues/81"""
53 51 ip.magic('autocall 2')
54 52 f = lambda x: x
55 53 ip.user_ns['f'] = f
56 54 try:
57 55 nt.assert_equal(ip.prefilter('f 1'),'f(1)')
58 56 for t in ['f +1', 'f -1']:
59 57 nt.assert_equal(ip.prefilter(t), t)
60 58
61 59 # Run tests again with a more permissive exclude_regexp, which will
62 60 # allow transformation of binary operations ('f -1' -> 'f(-1)').
63 61 pm = ip.prefilter_manager
64 62 ac = AutocallChecker(shell=pm.shell, prefilter_manager=pm,
65 63 config=pm.config)
66 64 try:
67 65 ac.priority = 1
68 66 ac.exclude_regexp = r'^[,&^\|\*/]|^is |^not |^in |^and |^or '
69 67 pm.sort_checkers()
70 68
71 69 nt.assert_equal(ip.prefilter('f -1'), 'f(-1)')
72 70 nt.assert_equal(ip.prefilter('f +1'), 'f(+1)')
73 71 finally:
74 72 pm.unregister_checker(ac)
75 73 finally:
76 74 ip.magic('autocall 0')
77 75 del ip.user_ns['f']
78 76
79 77
80 78 def test_issue_114():
81 79 """Check that multiline string literals don't expand as magic
82 80 see http://github.com/ipython/ipython/issues/114"""
83 81
84 82 template = '"""\n%s\n"""'
85 83 # Store the current value of multi_line_specials and turn it off before
86 84 # running test, since it could be true (case in which the test doesn't make
87 85 # sense, as multiline string literals *will* expand as magic in that case).
88 86 msp = ip.prefilter_manager.multi_line_specials
89 87 ip.prefilter_manager.multi_line_specials = False
90 88 try:
91 89 for mgk in ip.magics_manager.lsmagic()['line']:
92 90 raw = template % mgk
93 91 nt.assert_equal(ip.prefilter(raw), raw)
94 92 finally:
95 93 ip.prefilter_manager.multi_line_specials = msp
96 94
97 95
98 96 def test_prefilter_attribute_errors():
99 97 """Capture exceptions thrown by user objects on attribute access.
100 98
101 99 See http://github.com/ipython/ipython/issues/988."""
102 100
103 101 class X(object):
104 102 def __getattr__(self, k):
105 103 raise ValueError('broken object')
106 104 def __call__(self, x):
107 105 return x
108 106
109 107 # Create a callable broken object
110 108 ip.user_ns['x'] = X()
111 109 ip.magic('autocall 2')
112 110 try:
113 111 # Even if x throws an attribute error when looking at its rewrite
114 112 # attribute, we should not crash. So the test here is simply making
115 113 # the prefilter call and not having an exception.
116 114 ip.prefilter('x 1')
117 115 finally:
118 116 del ip.user_ns['x']
119 117 ip.magic('autocall 0')
@@ -1,34 +1,30 b''
1 1 # -*- coding: utf-8
2 2 """Tests for prompt generation."""
3 3
4 4 import unittest
5 5
6 6 from IPython.core.prompts import LazyEvaluate
7 from IPython.testing.globalipapp import get_ipython
8
9 ip = get_ipython()
10
11 7
12 8 class PromptTests(unittest.TestCase):
13 9 def test_lazy_eval_unicode(self):
14 10 u = u'ünicødé'
15 11 lz = LazyEvaluate(lambda : u)
16 12 self.assertEqual(str(lz), u)
17 13 self.assertEqual(format(lz), u)
18 14
19 15 def test_lazy_eval_nonascii_bytes(self):
20 16 u = u'ünicødé'
21 17 b = u.encode('utf8')
22 18 lz = LazyEvaluate(lambda : b)
23 19 # unicode(lz) would fail
24 20 self.assertEqual(str(lz), str(b))
25 21 self.assertEqual(format(lz), str(b))
26 22
27 23 def test_lazy_eval_float(self):
28 24 f = 0.503
29 25 lz = LazyEvaluate(lambda : f)
30 26
31 27 self.assertEqual(str(lz), str(f))
32 28 self.assertEqual(format(lz), str(f))
33 29 self.assertEqual(format(lz, '.1'), '0.5')
34 30
General Comments 0
You need to be logged in to leave comments. Login now