##// END OF EJS Templates
move custom exception at module....
Matthias Bussonnier -
Show More
@@ -1,902 +1,903 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Tests for the key interactiveshell module.
2 """Tests for the key interactiveshell module.
3
3
4 Historically the main classes in interactiveshell have been under-tested. This
4 Historically the main classes in interactiveshell have been under-tested. This
5 module should grow as many single-method tests as possible to trap many of the
5 module should grow as many single-method tests as possible to trap many of the
6 recurring bugs we seem to encounter with high-level interaction.
6 recurring bugs we seem to encounter with high-level interaction.
7 """
7 """
8
8
9 # Copyright (c) IPython Development Team.
9 # Copyright (c) IPython Development Team.
10 # Distributed under the terms of the Modified BSD License.
10 # Distributed under the terms of the Modified BSD License.
11
11
12 import ast
12 import ast
13 import os
13 import os
14 import signal
14 import signal
15 import shutil
15 import shutil
16 import sys
16 import sys
17 import tempfile
17 import tempfile
18 import unittest
18 import unittest
19 try:
19 try:
20 from unittest import mock
20 from unittest import mock
21 except ImportError:
21 except ImportError:
22 import mock
22 import mock
23 from os.path import join
23 from os.path import join
24
24
25 import nose.tools as nt
25 import nose.tools as nt
26
26
27 from IPython.core.error import InputRejected
27 from IPython.core.error import InputRejected
28 from IPython.core.inputtransformer import InputTransformer
28 from IPython.core.inputtransformer import InputTransformer
29 from IPython.testing.decorators import (
29 from IPython.testing.decorators import (
30 skipif, skip_win32, onlyif_unicode_paths, onlyif_cmds_exist,
30 skipif, skip_win32, onlyif_unicode_paths, onlyif_cmds_exist,
31 )
31 )
32 from IPython.testing import tools as tt
32 from IPython.testing import tools as tt
33 from IPython.utils import io
33 from IPython.utils import io
34 from IPython.utils.process import find_cmd
34 from IPython.utils.process import find_cmd
35 from IPython.utils import py3compat
35 from IPython.utils import py3compat
36 from IPython.utils.py3compat import unicode_type, PY3
36 from IPython.utils.py3compat import unicode_type, PY3
37
37
38 if PY3:
38 if PY3:
39 from io import StringIO
39 from io import StringIO
40 else:
40 else:
41 from StringIO import StringIO
41 from StringIO import StringIO
42
42
43 #-----------------------------------------------------------------------------
43 #-----------------------------------------------------------------------------
44 # Globals
44 # Globals
45 #-----------------------------------------------------------------------------
45 #-----------------------------------------------------------------------------
46 # This is used by every single test, no point repeating it ad nauseam
46 # This is used by every single test, no point repeating it ad nauseam
47 ip = get_ipython()
47 ip = get_ipython()
48
48
49 #-----------------------------------------------------------------------------
49 #-----------------------------------------------------------------------------
50 # Tests
50 # Tests
51 #-----------------------------------------------------------------------------
51 #-----------------------------------------------------------------------------
52
52
53 class DerivedInterrupt(KeyboardInterrupt):
54 pass
55
53 class InteractiveShellTestCase(unittest.TestCase):
56 class InteractiveShellTestCase(unittest.TestCase):
54 def test_naked_string_cells(self):
57 def test_naked_string_cells(self):
55 """Test that cells with only naked strings are fully executed"""
58 """Test that cells with only naked strings are fully executed"""
56 # First, single-line inputs
59 # First, single-line inputs
57 ip.run_cell('"a"\n')
60 ip.run_cell('"a"\n')
58 self.assertEqual(ip.user_ns['_'], 'a')
61 self.assertEqual(ip.user_ns['_'], 'a')
59 # And also multi-line cells
62 # And also multi-line cells
60 ip.run_cell('"""a\nb"""\n')
63 ip.run_cell('"""a\nb"""\n')
61 self.assertEqual(ip.user_ns['_'], 'a\nb')
64 self.assertEqual(ip.user_ns['_'], 'a\nb')
62
65
63 def test_run_empty_cell(self):
66 def test_run_empty_cell(self):
64 """Just make sure we don't get a horrible error with a blank
67 """Just make sure we don't get a horrible error with a blank
65 cell of input. Yes, I did overlook that."""
68 cell of input. Yes, I did overlook that."""
66 old_xc = ip.execution_count
69 old_xc = ip.execution_count
67 res = ip.run_cell('')
70 res = ip.run_cell('')
68 self.assertEqual(ip.execution_count, old_xc)
71 self.assertEqual(ip.execution_count, old_xc)
69 self.assertEqual(res.execution_count, None)
72 self.assertEqual(res.execution_count, None)
70
73
71 def test_run_cell_multiline(self):
74 def test_run_cell_multiline(self):
72 """Multi-block, multi-line cells must execute correctly.
75 """Multi-block, multi-line cells must execute correctly.
73 """
76 """
74 src = '\n'.join(["x=1",
77 src = '\n'.join(["x=1",
75 "y=2",
78 "y=2",
76 "if 1:",
79 "if 1:",
77 " x += 1",
80 " x += 1",
78 " y += 1",])
81 " y += 1",])
79 res = ip.run_cell(src)
82 res = ip.run_cell(src)
80 self.assertEqual(ip.user_ns['x'], 2)
83 self.assertEqual(ip.user_ns['x'], 2)
81 self.assertEqual(ip.user_ns['y'], 3)
84 self.assertEqual(ip.user_ns['y'], 3)
82 self.assertEqual(res.success, True)
85 self.assertEqual(res.success, True)
83 self.assertEqual(res.result, None)
86 self.assertEqual(res.result, None)
84
87
85 def test_multiline_string_cells(self):
88 def test_multiline_string_cells(self):
86 "Code sprinkled with multiline strings should execute (GH-306)"
89 "Code sprinkled with multiline strings should execute (GH-306)"
87 ip.run_cell('tmp=0')
90 ip.run_cell('tmp=0')
88 self.assertEqual(ip.user_ns['tmp'], 0)
91 self.assertEqual(ip.user_ns['tmp'], 0)
89 res = ip.run_cell('tmp=1;"""a\nb"""\n')
92 res = ip.run_cell('tmp=1;"""a\nb"""\n')
90 self.assertEqual(ip.user_ns['tmp'], 1)
93 self.assertEqual(ip.user_ns['tmp'], 1)
91 self.assertEqual(res.success, True)
94 self.assertEqual(res.success, True)
92 self.assertEqual(res.result, "a\nb")
95 self.assertEqual(res.result, "a\nb")
93
96
94 def test_dont_cache_with_semicolon(self):
97 def test_dont_cache_with_semicolon(self):
95 "Ending a line with semicolon should not cache the returned object (GH-307)"
98 "Ending a line with semicolon should not cache the returned object (GH-307)"
96 oldlen = len(ip.user_ns['Out'])
99 oldlen = len(ip.user_ns['Out'])
97 for cell in ['1;', '1;1;']:
100 for cell in ['1;', '1;1;']:
98 res = ip.run_cell(cell, store_history=True)
101 res = ip.run_cell(cell, store_history=True)
99 newlen = len(ip.user_ns['Out'])
102 newlen = len(ip.user_ns['Out'])
100 self.assertEqual(oldlen, newlen)
103 self.assertEqual(oldlen, newlen)
101 self.assertIsNone(res.result)
104 self.assertIsNone(res.result)
102 i = 0
105 i = 0
103 #also test the default caching behavior
106 #also test the default caching behavior
104 for cell in ['1', '1;1']:
107 for cell in ['1', '1;1']:
105 ip.run_cell(cell, store_history=True)
108 ip.run_cell(cell, store_history=True)
106 newlen = len(ip.user_ns['Out'])
109 newlen = len(ip.user_ns['Out'])
107 i += 1
110 i += 1
108 self.assertEqual(oldlen+i, newlen)
111 self.assertEqual(oldlen+i, newlen)
109
112
110 def test_syntax_error(self):
113 def test_syntax_error(self):
111 res = ip.run_cell("raise = 3")
114 res = ip.run_cell("raise = 3")
112 self.assertIsInstance(res.error_before_exec, SyntaxError)
115 self.assertIsInstance(res.error_before_exec, SyntaxError)
113
116
114 def test_In_variable(self):
117 def test_In_variable(self):
115 "Verify that In variable grows with user input (GH-284)"
118 "Verify that In variable grows with user input (GH-284)"
116 oldlen = len(ip.user_ns['In'])
119 oldlen = len(ip.user_ns['In'])
117 ip.run_cell('1;', store_history=True)
120 ip.run_cell('1;', store_history=True)
118 newlen = len(ip.user_ns['In'])
121 newlen = len(ip.user_ns['In'])
119 self.assertEqual(oldlen+1, newlen)
122 self.assertEqual(oldlen+1, newlen)
120 self.assertEqual(ip.user_ns['In'][-1],'1;')
123 self.assertEqual(ip.user_ns['In'][-1],'1;')
121
124
122 def test_magic_names_in_string(self):
125 def test_magic_names_in_string(self):
123 ip.run_cell('a = """\n%exit\n"""')
126 ip.run_cell('a = """\n%exit\n"""')
124 self.assertEqual(ip.user_ns['a'], '\n%exit\n')
127 self.assertEqual(ip.user_ns['a'], '\n%exit\n')
125
128
126 def test_trailing_newline(self):
129 def test_trailing_newline(self):
127 """test that running !(command) does not raise a SyntaxError"""
130 """test that running !(command) does not raise a SyntaxError"""
128 ip.run_cell('!(true)\n', False)
131 ip.run_cell('!(true)\n', False)
129 ip.run_cell('!(true)\n\n\n', False)
132 ip.run_cell('!(true)\n\n\n', False)
130
133
131 def test_gh_597(self):
134 def test_gh_597(self):
132 """Pretty-printing lists of objects with non-ascii reprs may cause
135 """Pretty-printing lists of objects with non-ascii reprs may cause
133 problems."""
136 problems."""
134 class Spam(object):
137 class Spam(object):
135 def __repr__(self):
138 def __repr__(self):
136 return "\xe9"*50
139 return "\xe9"*50
137 import IPython.core.formatters
140 import IPython.core.formatters
138 f = IPython.core.formatters.PlainTextFormatter()
141 f = IPython.core.formatters.PlainTextFormatter()
139 f([Spam(),Spam()])
142 f([Spam(),Spam()])
140
143
141
144
142 def test_future_flags(self):
145 def test_future_flags(self):
143 """Check that future flags are used for parsing code (gh-777)"""
146 """Check that future flags are used for parsing code (gh-777)"""
144 ip.run_cell('from __future__ import print_function')
147 ip.run_cell('from __future__ import print_function')
145 try:
148 try:
146 ip.run_cell('prfunc_return_val = print(1,2, sep=" ")')
149 ip.run_cell('prfunc_return_val = print(1,2, sep=" ")')
147 assert 'prfunc_return_val' in ip.user_ns
150 assert 'prfunc_return_val' in ip.user_ns
148 finally:
151 finally:
149 # Reset compiler flags so we don't mess up other tests.
152 # Reset compiler flags so we don't mess up other tests.
150 ip.compile.reset_compiler_flags()
153 ip.compile.reset_compiler_flags()
151
154
152 def test_future_unicode(self):
155 def test_future_unicode(self):
153 """Check that unicode_literals is imported from __future__ (gh #786)"""
156 """Check that unicode_literals is imported from __future__ (gh #786)"""
154 try:
157 try:
155 ip.run_cell(u'byte_str = "a"')
158 ip.run_cell(u'byte_str = "a"')
156 assert isinstance(ip.user_ns['byte_str'], str) # string literals are byte strings by default
159 assert isinstance(ip.user_ns['byte_str'], str) # string literals are byte strings by default
157 ip.run_cell('from __future__ import unicode_literals')
160 ip.run_cell('from __future__ import unicode_literals')
158 ip.run_cell(u'unicode_str = "a"')
161 ip.run_cell(u'unicode_str = "a"')
159 assert isinstance(ip.user_ns['unicode_str'], unicode_type) # strings literals are now unicode
162 assert isinstance(ip.user_ns['unicode_str'], unicode_type) # strings literals are now unicode
160 finally:
163 finally:
161 # Reset compiler flags so we don't mess up other tests.
164 # Reset compiler flags so we don't mess up other tests.
162 ip.compile.reset_compiler_flags()
165 ip.compile.reset_compiler_flags()
163
166
164 def test_can_pickle(self):
167 def test_can_pickle(self):
165 "Can we pickle objects defined interactively (GH-29)"
168 "Can we pickle objects defined interactively (GH-29)"
166 ip = get_ipython()
169 ip = get_ipython()
167 ip.reset()
170 ip.reset()
168 ip.run_cell(("class Mylist(list):\n"
171 ip.run_cell(("class Mylist(list):\n"
169 " def __init__(self,x=[]):\n"
172 " def __init__(self,x=[]):\n"
170 " list.__init__(self,x)"))
173 " list.__init__(self,x)"))
171 ip.run_cell("w=Mylist([1,2,3])")
174 ip.run_cell("w=Mylist([1,2,3])")
172
175
173 from pickle import dumps
176 from pickle import dumps
174
177
175 # We need to swap in our main module - this is only necessary
178 # We need to swap in our main module - this is only necessary
176 # inside the test framework, because IPython puts the interactive module
179 # inside the test framework, because IPython puts the interactive module
177 # in place (but the test framework undoes this).
180 # in place (but the test framework undoes this).
178 _main = sys.modules['__main__']
181 _main = sys.modules['__main__']
179 sys.modules['__main__'] = ip.user_module
182 sys.modules['__main__'] = ip.user_module
180 try:
183 try:
181 res = dumps(ip.user_ns["w"])
184 res = dumps(ip.user_ns["w"])
182 finally:
185 finally:
183 sys.modules['__main__'] = _main
186 sys.modules['__main__'] = _main
184 self.assertTrue(isinstance(res, bytes))
187 self.assertTrue(isinstance(res, bytes))
185
188
186 def test_global_ns(self):
189 def test_global_ns(self):
187 "Code in functions must be able to access variables outside them."
190 "Code in functions must be able to access variables outside them."
188 ip = get_ipython()
191 ip = get_ipython()
189 ip.run_cell("a = 10")
192 ip.run_cell("a = 10")
190 ip.run_cell(("def f(x):\n"
193 ip.run_cell(("def f(x):\n"
191 " return x + a"))
194 " return x + a"))
192 ip.run_cell("b = f(12)")
195 ip.run_cell("b = f(12)")
193 self.assertEqual(ip.user_ns["b"], 22)
196 self.assertEqual(ip.user_ns["b"], 22)
194
197
195 def test_bad_custom_tb(self):
198 def test_bad_custom_tb(self):
196 """Check that InteractiveShell is protected from bad custom exception handlers"""
199 """Check that InteractiveShell is protected from bad custom exception handlers"""
197 from IPython.utils import io
200 from IPython.utils import io
198 save_stderr = io.stderr
201 save_stderr = io.stderr
199 try:
202 try:
200 # capture stderr
203 # capture stderr
201 io.stderr = StringIO()
204 io.stderr = StringIO()
202 ip.set_custom_exc((IOError,), lambda etype,value,tb: 1/0)
205 ip.set_custom_exc((IOError,), lambda etype,value,tb: 1/0)
203 self.assertEqual(ip.custom_exceptions, (IOError,))
206 self.assertEqual(ip.custom_exceptions, (IOError,))
204 ip.run_cell(u'raise IOError("foo")')
207 ip.run_cell(u'raise IOError("foo")')
205 self.assertEqual(ip.custom_exceptions, ())
208 self.assertEqual(ip.custom_exceptions, ())
206 self.assertTrue("Custom TB Handler failed" in io.stderr.getvalue())
209 self.assertTrue("Custom TB Handler failed" in io.stderr.getvalue())
207 finally:
210 finally:
208 io.stderr = save_stderr
211 io.stderr = save_stderr
209
212
210 def test_bad_custom_tb_return(self):
213 def test_bad_custom_tb_return(self):
211 """Check that InteractiveShell is protected from bad return types in custom exception handlers"""
214 """Check that InteractiveShell is protected from bad return types in custom exception handlers"""
212 from IPython.utils import io
215 from IPython.utils import io
213 save_stderr = io.stderr
216 save_stderr = io.stderr
214 try:
217 try:
215 # capture stderr
218 # capture stderr
216 io.stderr = StringIO()
219 io.stderr = StringIO()
217 ip.set_custom_exc((NameError,),lambda etype,value,tb, tb_offset=None: 1)
220 ip.set_custom_exc((NameError,),lambda etype,value,tb, tb_offset=None: 1)
218 self.assertEqual(ip.custom_exceptions, (NameError,))
221 self.assertEqual(ip.custom_exceptions, (NameError,))
219 ip.run_cell(u'a=abracadabra')
222 ip.run_cell(u'a=abracadabra')
220 self.assertEqual(ip.custom_exceptions, ())
223 self.assertEqual(ip.custom_exceptions, ())
221 self.assertTrue("Custom TB Handler failed" in io.stderr.getvalue())
224 self.assertTrue("Custom TB Handler failed" in io.stderr.getvalue())
222 finally:
225 finally:
223 io.stderr = save_stderr
226 io.stderr = save_stderr
224
227
225 def test_drop_by_id(self):
228 def test_drop_by_id(self):
226 myvars = {"a":object(), "b":object(), "c": object()}
229 myvars = {"a":object(), "b":object(), "c": object()}
227 ip.push(myvars, interactive=False)
230 ip.push(myvars, interactive=False)
228 for name in myvars:
231 for name in myvars:
229 assert name in ip.user_ns, name
232 assert name in ip.user_ns, name
230 assert name in ip.user_ns_hidden, name
233 assert name in ip.user_ns_hidden, name
231 ip.user_ns['b'] = 12
234 ip.user_ns['b'] = 12
232 ip.drop_by_id(myvars)
235 ip.drop_by_id(myvars)
233 for name in ["a", "c"]:
236 for name in ["a", "c"]:
234 assert name not in ip.user_ns, name
237 assert name not in ip.user_ns, name
235 assert name not in ip.user_ns_hidden, name
238 assert name not in ip.user_ns_hidden, name
236 assert ip.user_ns['b'] == 12
239 assert ip.user_ns['b'] == 12
237 ip.reset()
240 ip.reset()
238
241
239 def test_var_expand(self):
242 def test_var_expand(self):
240 ip.user_ns['f'] = u'Ca\xf1o'
243 ip.user_ns['f'] = u'Ca\xf1o'
241 self.assertEqual(ip.var_expand(u'echo $f'), u'echo Ca\xf1o')
244 self.assertEqual(ip.var_expand(u'echo $f'), u'echo Ca\xf1o')
242 self.assertEqual(ip.var_expand(u'echo {f}'), u'echo Ca\xf1o')
245 self.assertEqual(ip.var_expand(u'echo {f}'), u'echo Ca\xf1o')
243 self.assertEqual(ip.var_expand(u'echo {f[:-1]}'), u'echo Ca\xf1')
246 self.assertEqual(ip.var_expand(u'echo {f[:-1]}'), u'echo Ca\xf1')
244 self.assertEqual(ip.var_expand(u'echo {1*2}'), u'echo 2')
247 self.assertEqual(ip.var_expand(u'echo {1*2}'), u'echo 2')
245
248
246 ip.user_ns['f'] = b'Ca\xc3\xb1o'
249 ip.user_ns['f'] = b'Ca\xc3\xb1o'
247 # This should not raise any exception:
250 # This should not raise any exception:
248 ip.var_expand(u'echo $f')
251 ip.var_expand(u'echo $f')
249
252
250 def test_var_expand_local(self):
253 def test_var_expand_local(self):
251 """Test local variable expansion in !system and %magic calls"""
254 """Test local variable expansion in !system and %magic calls"""
252 # !system
255 # !system
253 ip.run_cell('def test():\n'
256 ip.run_cell('def test():\n'
254 ' lvar = "ttt"\n'
257 ' lvar = "ttt"\n'
255 ' ret = !echo {lvar}\n'
258 ' ret = !echo {lvar}\n'
256 ' return ret[0]\n')
259 ' return ret[0]\n')
257 res = ip.user_ns['test']()
260 res = ip.user_ns['test']()
258 nt.assert_in('ttt', res)
261 nt.assert_in('ttt', res)
259
262
260 # %magic
263 # %magic
261 ip.run_cell('def makemacro():\n'
264 ip.run_cell('def makemacro():\n'
262 ' macroname = "macro_var_expand_locals"\n'
265 ' macroname = "macro_var_expand_locals"\n'
263 ' %macro {macroname} codestr\n')
266 ' %macro {macroname} codestr\n')
264 ip.user_ns['codestr'] = "str(12)"
267 ip.user_ns['codestr'] = "str(12)"
265 ip.run_cell('makemacro()')
268 ip.run_cell('makemacro()')
266 nt.assert_in('macro_var_expand_locals', ip.user_ns)
269 nt.assert_in('macro_var_expand_locals', ip.user_ns)
267
270
268 def test_var_expand_self(self):
271 def test_var_expand_self(self):
269 """Test variable expansion with the name 'self', which was failing.
272 """Test variable expansion with the name 'self', which was failing.
270
273
271 See https://github.com/ipython/ipython/issues/1878#issuecomment-7698218
274 See https://github.com/ipython/ipython/issues/1878#issuecomment-7698218
272 """
275 """
273 ip.run_cell('class cTest:\n'
276 ip.run_cell('class cTest:\n'
274 ' classvar="see me"\n'
277 ' classvar="see me"\n'
275 ' def test(self):\n'
278 ' def test(self):\n'
276 ' res = !echo Variable: {self.classvar}\n'
279 ' res = !echo Variable: {self.classvar}\n'
277 ' return res[0]\n')
280 ' return res[0]\n')
278 nt.assert_in('see me', ip.user_ns['cTest']().test())
281 nt.assert_in('see me', ip.user_ns['cTest']().test())
279
282
280 def test_bad_var_expand(self):
283 def test_bad_var_expand(self):
281 """var_expand on invalid formats shouldn't raise"""
284 """var_expand on invalid formats shouldn't raise"""
282 # SyntaxError
285 # SyntaxError
283 self.assertEqual(ip.var_expand(u"{'a':5}"), u"{'a':5}")
286 self.assertEqual(ip.var_expand(u"{'a':5}"), u"{'a':5}")
284 # NameError
287 # NameError
285 self.assertEqual(ip.var_expand(u"{asdf}"), u"{asdf}")
288 self.assertEqual(ip.var_expand(u"{asdf}"), u"{asdf}")
286 # ZeroDivisionError
289 # ZeroDivisionError
287 self.assertEqual(ip.var_expand(u"{1/0}"), u"{1/0}")
290 self.assertEqual(ip.var_expand(u"{1/0}"), u"{1/0}")
288
291
289 def test_silent_postexec(self):
292 def test_silent_postexec(self):
290 """run_cell(silent=True) doesn't invoke pre/post_run_cell callbacks"""
293 """run_cell(silent=True) doesn't invoke pre/post_run_cell callbacks"""
291 pre_explicit = mock.Mock()
294 pre_explicit = mock.Mock()
292 pre_always = mock.Mock()
295 pre_always = mock.Mock()
293 post_explicit = mock.Mock()
296 post_explicit = mock.Mock()
294 post_always = mock.Mock()
297 post_always = mock.Mock()
295
298
296 ip.events.register('pre_run_cell', pre_explicit)
299 ip.events.register('pre_run_cell', pre_explicit)
297 ip.events.register('pre_execute', pre_always)
300 ip.events.register('pre_execute', pre_always)
298 ip.events.register('post_run_cell', post_explicit)
301 ip.events.register('post_run_cell', post_explicit)
299 ip.events.register('post_execute', post_always)
302 ip.events.register('post_execute', post_always)
300
303
301 try:
304 try:
302 ip.run_cell("1", silent=True)
305 ip.run_cell("1", silent=True)
303 assert pre_always.called
306 assert pre_always.called
304 assert not pre_explicit.called
307 assert not pre_explicit.called
305 assert post_always.called
308 assert post_always.called
306 assert not post_explicit.called
309 assert not post_explicit.called
307 # double-check that non-silent exec did what we expected
310 # double-check that non-silent exec did what we expected
308 # silent to avoid
311 # silent to avoid
309 ip.run_cell("1")
312 ip.run_cell("1")
310 assert pre_explicit.called
313 assert pre_explicit.called
311 assert post_explicit.called
314 assert post_explicit.called
312 finally:
315 finally:
313 # remove post-exec
316 # remove post-exec
314 ip.events.unregister('pre_run_cell', pre_explicit)
317 ip.events.unregister('pre_run_cell', pre_explicit)
315 ip.events.unregister('pre_execute', pre_always)
318 ip.events.unregister('pre_execute', pre_always)
316 ip.events.unregister('post_run_cell', post_explicit)
319 ip.events.unregister('post_run_cell', post_explicit)
317 ip.events.unregister('post_execute', post_always)
320 ip.events.unregister('post_execute', post_always)
318
321
319 def test_silent_noadvance(self):
322 def test_silent_noadvance(self):
320 """run_cell(silent=True) doesn't advance execution_count"""
323 """run_cell(silent=True) doesn't advance execution_count"""
321 ec = ip.execution_count
324 ec = ip.execution_count
322 # silent should force store_history=False
325 # silent should force store_history=False
323 ip.run_cell("1", store_history=True, silent=True)
326 ip.run_cell("1", store_history=True, silent=True)
324
327
325 self.assertEqual(ec, ip.execution_count)
328 self.assertEqual(ec, ip.execution_count)
326 # double-check that non-silent exec did what we expected
329 # double-check that non-silent exec did what we expected
327 # silent to avoid
330 # silent to avoid
328 ip.run_cell("1", store_history=True)
331 ip.run_cell("1", store_history=True)
329 self.assertEqual(ec+1, ip.execution_count)
332 self.assertEqual(ec+1, ip.execution_count)
330
333
331 def test_silent_nodisplayhook(self):
334 def test_silent_nodisplayhook(self):
332 """run_cell(silent=True) doesn't trigger displayhook"""
335 """run_cell(silent=True) doesn't trigger displayhook"""
333 d = dict(called=False)
336 d = dict(called=False)
334
337
335 trap = ip.display_trap
338 trap = ip.display_trap
336 save_hook = trap.hook
339 save_hook = trap.hook
337
340
338 def failing_hook(*args, **kwargs):
341 def failing_hook(*args, **kwargs):
339 d['called'] = True
342 d['called'] = True
340
343
341 try:
344 try:
342 trap.hook = failing_hook
345 trap.hook = failing_hook
343 res = ip.run_cell("1", silent=True)
346 res = ip.run_cell("1", silent=True)
344 self.assertFalse(d['called'])
347 self.assertFalse(d['called'])
345 self.assertIsNone(res.result)
348 self.assertIsNone(res.result)
346 # double-check that non-silent exec did what we expected
349 # double-check that non-silent exec did what we expected
347 # silent to avoid
350 # silent to avoid
348 ip.run_cell("1")
351 ip.run_cell("1")
349 self.assertTrue(d['called'])
352 self.assertTrue(d['called'])
350 finally:
353 finally:
351 trap.hook = save_hook
354 trap.hook = save_hook
352
355
353 @skipif(sys.version_info[0] >= 3, "softspace removed in py3")
356 @skipif(sys.version_info[0] >= 3, "softspace removed in py3")
354 def test_print_softspace(self):
357 def test_print_softspace(self):
355 """Verify that softspace is handled correctly when executing multiple
358 """Verify that softspace is handled correctly when executing multiple
356 statements.
359 statements.
357
360
358 In [1]: print 1; print 2
361 In [1]: print 1; print 2
359 1
362 1
360 2
363 2
361
364
362 In [2]: print 1,; print 2
365 In [2]: print 1,; print 2
363 1 2
366 1 2
364 """
367 """
365
368
366 def test_ofind_line_magic(self):
369 def test_ofind_line_magic(self):
367 from IPython.core.magic import register_line_magic
370 from IPython.core.magic import register_line_magic
368
371
369 @register_line_magic
372 @register_line_magic
370 def lmagic(line):
373 def lmagic(line):
371 "A line magic"
374 "A line magic"
372
375
373 # Get info on line magic
376 # Get info on line magic
374 lfind = ip._ofind('lmagic')
377 lfind = ip._ofind('lmagic')
375 info = dict(found=True, isalias=False, ismagic=True,
378 info = dict(found=True, isalias=False, ismagic=True,
376 namespace = 'IPython internal', obj= lmagic.__wrapped__,
379 namespace = 'IPython internal', obj= lmagic.__wrapped__,
377 parent = None)
380 parent = None)
378 nt.assert_equal(lfind, info)
381 nt.assert_equal(lfind, info)
379
382
380 def test_ofind_cell_magic(self):
383 def test_ofind_cell_magic(self):
381 from IPython.core.magic import register_cell_magic
384 from IPython.core.magic import register_cell_magic
382
385
383 @register_cell_magic
386 @register_cell_magic
384 def cmagic(line, cell):
387 def cmagic(line, cell):
385 "A cell magic"
388 "A cell magic"
386
389
387 # Get info on cell magic
390 # Get info on cell magic
388 find = ip._ofind('cmagic')
391 find = ip._ofind('cmagic')
389 info = dict(found=True, isalias=False, ismagic=True,
392 info = dict(found=True, isalias=False, ismagic=True,
390 namespace = 'IPython internal', obj= cmagic.__wrapped__,
393 namespace = 'IPython internal', obj= cmagic.__wrapped__,
391 parent = None)
394 parent = None)
392 nt.assert_equal(find, info)
395 nt.assert_equal(find, info)
393
396
394 def test_ofind_property_with_error(self):
397 def test_ofind_property_with_error(self):
395 class A(object):
398 class A(object):
396 @property
399 @property
397 def foo(self):
400 def foo(self):
398 raise NotImplementedError()
401 raise NotImplementedError()
399 a = A()
402 a = A()
400
403
401 found = ip._ofind('a.foo', [('locals', locals())])
404 found = ip._ofind('a.foo', [('locals', locals())])
402 info = dict(found=True, isalias=False, ismagic=False,
405 info = dict(found=True, isalias=False, ismagic=False,
403 namespace='locals', obj=A.foo, parent=a)
406 namespace='locals', obj=A.foo, parent=a)
404 nt.assert_equal(found, info)
407 nt.assert_equal(found, info)
405
408
406 def test_ofind_multiple_attribute_lookups(self):
409 def test_ofind_multiple_attribute_lookups(self):
407 class A(object):
410 class A(object):
408 @property
411 @property
409 def foo(self):
412 def foo(self):
410 raise NotImplementedError()
413 raise NotImplementedError()
411
414
412 a = A()
415 a = A()
413 a.a = A()
416 a.a = A()
414 a.a.a = A()
417 a.a.a = A()
415
418
416 found = ip._ofind('a.a.a.foo', [('locals', locals())])
419 found = ip._ofind('a.a.a.foo', [('locals', locals())])
417 info = dict(found=True, isalias=False, ismagic=False,
420 info = dict(found=True, isalias=False, ismagic=False,
418 namespace='locals', obj=A.foo, parent=a.a.a)
421 namespace='locals', obj=A.foo, parent=a.a.a)
419 nt.assert_equal(found, info)
422 nt.assert_equal(found, info)
420
423
421 def test_ofind_slotted_attributes(self):
424 def test_ofind_slotted_attributes(self):
422 class A(object):
425 class A(object):
423 __slots__ = ['foo']
426 __slots__ = ['foo']
424 def __init__(self):
427 def __init__(self):
425 self.foo = 'bar'
428 self.foo = 'bar'
426
429
427 a = A()
430 a = A()
428 found = ip._ofind('a.foo', [('locals', locals())])
431 found = ip._ofind('a.foo', [('locals', locals())])
429 info = dict(found=True, isalias=False, ismagic=False,
432 info = dict(found=True, isalias=False, ismagic=False,
430 namespace='locals', obj=a.foo, parent=a)
433 namespace='locals', obj=a.foo, parent=a)
431 nt.assert_equal(found, info)
434 nt.assert_equal(found, info)
432
435
433 found = ip._ofind('a.bar', [('locals', locals())])
436 found = ip._ofind('a.bar', [('locals', locals())])
434 info = dict(found=False, isalias=False, ismagic=False,
437 info = dict(found=False, isalias=False, ismagic=False,
435 namespace=None, obj=None, parent=a)
438 namespace=None, obj=None, parent=a)
436 nt.assert_equal(found, info)
439 nt.assert_equal(found, info)
437
440
438 def test_ofind_prefers_property_to_instance_level_attribute(self):
441 def test_ofind_prefers_property_to_instance_level_attribute(self):
439 class A(object):
442 class A(object):
440 @property
443 @property
441 def foo(self):
444 def foo(self):
442 return 'bar'
445 return 'bar'
443 a = A()
446 a = A()
444 a.__dict__['foo'] = 'baz'
447 a.__dict__['foo'] = 'baz'
445 nt.assert_equal(a.foo, 'bar')
448 nt.assert_equal(a.foo, 'bar')
446 found = ip._ofind('a.foo', [('locals', locals())])
449 found = ip._ofind('a.foo', [('locals', locals())])
447 nt.assert_is(found['obj'], A.foo)
450 nt.assert_is(found['obj'], A.foo)
448
451
449 def test_custom_exception(self):
452 def test_custom_exception(self):
450 called = []
453 called = []
451 def my_handler(shell, etype, value, tb, tb_offset=None):
454 def my_handler(shell, etype, value, tb, tb_offset=None):
452 called.append(etype)
455 called.append(etype)
453 shell.showtraceback((etype, value, tb), tb_offset=tb_offset)
456 shell.showtraceback((etype, value, tb), tb_offset=tb_offset)
454
457
455 ip.set_custom_exc((ValueError,), my_handler)
458 ip.set_custom_exc((ValueError,), my_handler)
456 try:
459 try:
457 res = ip.run_cell("raise ValueError('test')")
460 res = ip.run_cell("raise ValueError('test')")
458 # Check that this was called, and only once.
461 # Check that this was called, and only once.
459 self.assertEqual(called, [ValueError])
462 self.assertEqual(called, [ValueError])
460 # Check that the error is on the result object
463 # Check that the error is on the result object
461 self.assertIsInstance(res.error_in_exec, ValueError)
464 self.assertIsInstance(res.error_in_exec, ValueError)
462 finally:
465 finally:
463 # Reset the custom exception hook
466 # Reset the custom exception hook
464 ip.set_custom_exc((), None)
467 ip.set_custom_exc((), None)
465
468
466 @skipif(sys.version_info[0] >= 3, "no differences with __future__ in py3")
469 @skipif(sys.version_info[0] >= 3, "no differences with __future__ in py3")
467 def test_future_environment(self):
470 def test_future_environment(self):
468 "Can we run code with & without the shell's __future__ imports?"
471 "Can we run code with & without the shell's __future__ imports?"
469 ip.run_cell("from __future__ import division")
472 ip.run_cell("from __future__ import division")
470 ip.run_cell("a = 1/2", shell_futures=True)
473 ip.run_cell("a = 1/2", shell_futures=True)
471 self.assertEqual(ip.user_ns['a'], 0.5)
474 self.assertEqual(ip.user_ns['a'], 0.5)
472 ip.run_cell("b = 1/2", shell_futures=False)
475 ip.run_cell("b = 1/2", shell_futures=False)
473 self.assertEqual(ip.user_ns['b'], 0)
476 self.assertEqual(ip.user_ns['b'], 0)
474
477
475 ip.compile.reset_compiler_flags()
478 ip.compile.reset_compiler_flags()
476 # This shouldn't leak to the shell's compiler
479 # This shouldn't leak to the shell's compiler
477 ip.run_cell("from __future__ import division \nc=1/2", shell_futures=False)
480 ip.run_cell("from __future__ import division \nc=1/2", shell_futures=False)
478 self.assertEqual(ip.user_ns['c'], 0.5)
481 self.assertEqual(ip.user_ns['c'], 0.5)
479 ip.run_cell("d = 1/2", shell_futures=True)
482 ip.run_cell("d = 1/2", shell_futures=True)
480 self.assertEqual(ip.user_ns['d'], 0)
483 self.assertEqual(ip.user_ns['d'], 0)
481
484
482 def test_mktempfile(self):
485 def test_mktempfile(self):
483 filename = ip.mktempfile()
486 filename = ip.mktempfile()
484 # Check that we can open the file again on Windows
487 # Check that we can open the file again on Windows
485 with open(filename, 'w') as f:
488 with open(filename, 'w') as f:
486 f.write('abc')
489 f.write('abc')
487
490
488 filename = ip.mktempfile(data='blah')
491 filename = ip.mktempfile(data='blah')
489 with open(filename, 'r') as f:
492 with open(filename, 'r') as f:
490 self.assertEqual(f.read(), 'blah')
493 self.assertEqual(f.read(), 'blah')
491
494
492 def test_new_main_mod(self):
495 def test_new_main_mod(self):
493 # Smoketest to check that this accepts a unicode module name
496 # Smoketest to check that this accepts a unicode module name
494 name = u'jiefmw'
497 name = u'jiefmw'
495 mod = ip.new_main_mod(u'%s.py' % name, name)
498 mod = ip.new_main_mod(u'%s.py' % name, name)
496 self.assertEqual(mod.__name__, name)
499 self.assertEqual(mod.__name__, name)
497
500
498 def test_get_exception_only(self):
501 def test_get_exception_only(self):
499 try:
502 try:
500 raise KeyboardInterrupt
503 raise KeyboardInterrupt
501 except KeyboardInterrupt:
504 except KeyboardInterrupt:
502 msg = ip.get_exception_only()
505 msg = ip.get_exception_only()
503 self.assertEqual(msg, 'KeyboardInterrupt\n')
506 self.assertEqual(msg, 'KeyboardInterrupt\n')
504
507
505 class DerivedInterrupt(KeyboardInterrupt):
506 pass
507 try:
508 try:
508 raise DerivedInterrupt("foo")
509 raise DerivedInterrupt("foo")
509 except KeyboardInterrupt:
510 except KeyboardInterrupt:
510 msg = ip.get_exception_only()
511 msg = ip.get_exception_only()
511 if sys.version_info[0] <= 2:
512 if sys.version_info[0] <= 2:
512 self.assertEqual(msg, 'DerivedInterrupt: foo\n')
513 self.assertEqual(msg, 'DerivedInterrupt: foo\n')
513 else:
514 else:
514 self.assertEqual(msg, 'IPython.core.tests.test_interactiveshell.DerivedInterrupt: foo\n')
515 self.assertEqual(msg, 'IPython.core.tests.test_interactiveshell.DerivedInterrupt: foo\n')
515
516
516 class TestSafeExecfileNonAsciiPath(unittest.TestCase):
517 class TestSafeExecfileNonAsciiPath(unittest.TestCase):
517
518
518 @onlyif_unicode_paths
519 @onlyif_unicode_paths
519 def setUp(self):
520 def setUp(self):
520 self.BASETESTDIR = tempfile.mkdtemp()
521 self.BASETESTDIR = tempfile.mkdtemp()
521 self.TESTDIR = join(self.BASETESTDIR, u"Γ₯Àâ")
522 self.TESTDIR = join(self.BASETESTDIR, u"Γ₯Àâ")
522 os.mkdir(self.TESTDIR)
523 os.mkdir(self.TESTDIR)
523 with open(join(self.TESTDIR, u"Γ₯Àâtestscript.py"), "w") as sfile:
524 with open(join(self.TESTDIR, u"Γ₯Àâtestscript.py"), "w") as sfile:
524 sfile.write("pass\n")
525 sfile.write("pass\n")
525 self.oldpath = py3compat.getcwd()
526 self.oldpath = py3compat.getcwd()
526 os.chdir(self.TESTDIR)
527 os.chdir(self.TESTDIR)
527 self.fname = u"Γ₯Àâtestscript.py"
528 self.fname = u"Γ₯Àâtestscript.py"
528
529
529 def tearDown(self):
530 def tearDown(self):
530 os.chdir(self.oldpath)
531 os.chdir(self.oldpath)
531 shutil.rmtree(self.BASETESTDIR)
532 shutil.rmtree(self.BASETESTDIR)
532
533
533 @onlyif_unicode_paths
534 @onlyif_unicode_paths
534 def test_1(self):
535 def test_1(self):
535 """Test safe_execfile with non-ascii path
536 """Test safe_execfile with non-ascii path
536 """
537 """
537 ip.safe_execfile(self.fname, {}, raise_exceptions=True)
538 ip.safe_execfile(self.fname, {}, raise_exceptions=True)
538
539
539 class ExitCodeChecks(tt.TempFileMixin):
540 class ExitCodeChecks(tt.TempFileMixin):
540 def test_exit_code_ok(self):
541 def test_exit_code_ok(self):
541 self.system('exit 0')
542 self.system('exit 0')
542 self.assertEqual(ip.user_ns['_exit_code'], 0)
543 self.assertEqual(ip.user_ns['_exit_code'], 0)
543
544
544 def test_exit_code_error(self):
545 def test_exit_code_error(self):
545 self.system('exit 1')
546 self.system('exit 1')
546 self.assertEqual(ip.user_ns['_exit_code'], 1)
547 self.assertEqual(ip.user_ns['_exit_code'], 1)
547
548
548 @skipif(not hasattr(signal, 'SIGALRM'))
549 @skipif(not hasattr(signal, 'SIGALRM'))
549 def test_exit_code_signal(self):
550 def test_exit_code_signal(self):
550 self.mktmp("import signal, time\n"
551 self.mktmp("import signal, time\n"
551 "signal.setitimer(signal.ITIMER_REAL, 0.1)\n"
552 "signal.setitimer(signal.ITIMER_REAL, 0.1)\n"
552 "time.sleep(1)\n")
553 "time.sleep(1)\n")
553 self.system("%s %s" % (sys.executable, self.fname))
554 self.system("%s %s" % (sys.executable, self.fname))
554 self.assertEqual(ip.user_ns['_exit_code'], -signal.SIGALRM)
555 self.assertEqual(ip.user_ns['_exit_code'], -signal.SIGALRM)
555
556
556 @onlyif_cmds_exist("csh")
557 @onlyif_cmds_exist("csh")
557 def test_exit_code_signal_csh(self):
558 def test_exit_code_signal_csh(self):
558 SHELL = os.environ.get('SHELL', None)
559 SHELL = os.environ.get('SHELL', None)
559 os.environ['SHELL'] = find_cmd("csh")
560 os.environ['SHELL'] = find_cmd("csh")
560 try:
561 try:
561 self.test_exit_code_signal()
562 self.test_exit_code_signal()
562 finally:
563 finally:
563 if SHELL is not None:
564 if SHELL is not None:
564 os.environ['SHELL'] = SHELL
565 os.environ['SHELL'] = SHELL
565 else:
566 else:
566 del os.environ['SHELL']
567 del os.environ['SHELL']
567
568
568 class TestSystemRaw(unittest.TestCase, ExitCodeChecks):
569 class TestSystemRaw(unittest.TestCase, ExitCodeChecks):
569 system = ip.system_raw
570 system = ip.system_raw
570
571
571 @onlyif_unicode_paths
572 @onlyif_unicode_paths
572 def test_1(self):
573 def test_1(self):
573 """Test system_raw with non-ascii cmd
574 """Test system_raw with non-ascii cmd
574 """
575 """
575 cmd = u'''python -c "'Γ₯Àâ'" '''
576 cmd = u'''python -c "'Γ₯Àâ'" '''
576 ip.system_raw(cmd)
577 ip.system_raw(cmd)
577
578
578 @mock.patch('subprocess.call', side_effect=KeyboardInterrupt)
579 @mock.patch('subprocess.call', side_effect=KeyboardInterrupt)
579 @mock.patch('os.system', side_effect=KeyboardInterrupt)
580 @mock.patch('os.system', side_effect=KeyboardInterrupt)
580 def test_control_c(self, *mocks):
581 def test_control_c(self, *mocks):
581 try:
582 try:
582 self.system("sleep 1 # wont happen")
583 self.system("sleep 1 # wont happen")
583 except KeyboardInterrupt:
584 except KeyboardInterrupt:
584 self.fail("system call should intercept "
585 self.fail("system call should intercept "
585 "keyboard interrupt from subprocess.call")
586 "keyboard interrupt from subprocess.call")
586 self.assertEqual(ip.user_ns['_exit_code'], -signal.SIGINT)
587 self.assertEqual(ip.user_ns['_exit_code'], -signal.SIGINT)
587
588
588 # TODO: Exit codes are currently ignored on Windows.
589 # TODO: Exit codes are currently ignored on Windows.
589 class TestSystemPipedExitCode(unittest.TestCase, ExitCodeChecks):
590 class TestSystemPipedExitCode(unittest.TestCase, ExitCodeChecks):
590 system = ip.system_piped
591 system = ip.system_piped
591
592
592 @skip_win32
593 @skip_win32
593 def test_exit_code_ok(self):
594 def test_exit_code_ok(self):
594 ExitCodeChecks.test_exit_code_ok(self)
595 ExitCodeChecks.test_exit_code_ok(self)
595
596
596 @skip_win32
597 @skip_win32
597 def test_exit_code_error(self):
598 def test_exit_code_error(self):
598 ExitCodeChecks.test_exit_code_error(self)
599 ExitCodeChecks.test_exit_code_error(self)
599
600
600 @skip_win32
601 @skip_win32
601 def test_exit_code_signal(self):
602 def test_exit_code_signal(self):
602 ExitCodeChecks.test_exit_code_signal(self)
603 ExitCodeChecks.test_exit_code_signal(self)
603
604
604 class TestModules(unittest.TestCase, tt.TempFileMixin):
605 class TestModules(unittest.TestCase, tt.TempFileMixin):
605 def test_extraneous_loads(self):
606 def test_extraneous_loads(self):
606 """Test we're not loading modules on startup that we shouldn't.
607 """Test we're not loading modules on startup that we shouldn't.
607 """
608 """
608 self.mktmp("import sys\n"
609 self.mktmp("import sys\n"
609 "print('numpy' in sys.modules)\n"
610 "print('numpy' in sys.modules)\n"
610 "print('ipython_parallel' in sys.modules)\n"
611 "print('ipython_parallel' in sys.modules)\n"
611 "print('ipython_kernel' in sys.modules)\n"
612 "print('ipython_kernel' in sys.modules)\n"
612 )
613 )
613 out = "False\nFalse\nFalse\n"
614 out = "False\nFalse\nFalse\n"
614 tt.ipexec_validate(self.fname, out)
615 tt.ipexec_validate(self.fname, out)
615
616
616 class Negator(ast.NodeTransformer):
617 class Negator(ast.NodeTransformer):
617 """Negates all number literals in an AST."""
618 """Negates all number literals in an AST."""
618 def visit_Num(self, node):
619 def visit_Num(self, node):
619 node.n = -node.n
620 node.n = -node.n
620 return node
621 return node
621
622
622 class TestAstTransform(unittest.TestCase):
623 class TestAstTransform(unittest.TestCase):
623 def setUp(self):
624 def setUp(self):
624 self.negator = Negator()
625 self.negator = Negator()
625 ip.ast_transformers.append(self.negator)
626 ip.ast_transformers.append(self.negator)
626
627
627 def tearDown(self):
628 def tearDown(self):
628 ip.ast_transformers.remove(self.negator)
629 ip.ast_transformers.remove(self.negator)
629
630
630 def test_run_cell(self):
631 def test_run_cell(self):
631 with tt.AssertPrints('-34'):
632 with tt.AssertPrints('-34'):
632 ip.run_cell('print (12 + 22)')
633 ip.run_cell('print (12 + 22)')
633
634
634 # A named reference to a number shouldn't be transformed.
635 # A named reference to a number shouldn't be transformed.
635 ip.user_ns['n'] = 55
636 ip.user_ns['n'] = 55
636 with tt.AssertNotPrints('-55'):
637 with tt.AssertNotPrints('-55'):
637 ip.run_cell('print (n)')
638 ip.run_cell('print (n)')
638
639
639 def test_timeit(self):
640 def test_timeit(self):
640 called = set()
641 called = set()
641 def f(x):
642 def f(x):
642 called.add(x)
643 called.add(x)
643 ip.push({'f':f})
644 ip.push({'f':f})
644
645
645 with tt.AssertPrints("best of "):
646 with tt.AssertPrints("best of "):
646 ip.run_line_magic("timeit", "-n1 f(1)")
647 ip.run_line_magic("timeit", "-n1 f(1)")
647 self.assertEqual(called, set([-1]))
648 self.assertEqual(called, set([-1]))
648 called.clear()
649 called.clear()
649
650
650 with tt.AssertPrints("best of "):
651 with tt.AssertPrints("best of "):
651 ip.run_cell_magic("timeit", "-n1 f(2)", "f(3)")
652 ip.run_cell_magic("timeit", "-n1 f(2)", "f(3)")
652 self.assertEqual(called, set([-2, -3]))
653 self.assertEqual(called, set([-2, -3]))
653
654
654 def test_time(self):
655 def test_time(self):
655 called = []
656 called = []
656 def f(x):
657 def f(x):
657 called.append(x)
658 called.append(x)
658 ip.push({'f':f})
659 ip.push({'f':f})
659
660
660 # Test with an expression
661 # Test with an expression
661 with tt.AssertPrints("Wall time: "):
662 with tt.AssertPrints("Wall time: "):
662 ip.run_line_magic("time", "f(5+9)")
663 ip.run_line_magic("time", "f(5+9)")
663 self.assertEqual(called, [-14])
664 self.assertEqual(called, [-14])
664 called[:] = []
665 called[:] = []
665
666
666 # Test with a statement (different code path)
667 # Test with a statement (different code path)
667 with tt.AssertPrints("Wall time: "):
668 with tt.AssertPrints("Wall time: "):
668 ip.run_line_magic("time", "a = f(-3 + -2)")
669 ip.run_line_magic("time", "a = f(-3 + -2)")
669 self.assertEqual(called, [5])
670 self.assertEqual(called, [5])
670
671
671 def test_macro(self):
672 def test_macro(self):
672 ip.push({'a':10})
673 ip.push({'a':10})
673 # The AST transformation makes this do a+=-1
674 # The AST transformation makes this do a+=-1
674 ip.define_macro("amacro", "a+=1\nprint(a)")
675 ip.define_macro("amacro", "a+=1\nprint(a)")
675
676
676 with tt.AssertPrints("9"):
677 with tt.AssertPrints("9"):
677 ip.run_cell("amacro")
678 ip.run_cell("amacro")
678 with tt.AssertPrints("8"):
679 with tt.AssertPrints("8"):
679 ip.run_cell("amacro")
680 ip.run_cell("amacro")
680
681
681 class IntegerWrapper(ast.NodeTransformer):
682 class IntegerWrapper(ast.NodeTransformer):
682 """Wraps all integers in a call to Integer()"""
683 """Wraps all integers in a call to Integer()"""
683 def visit_Num(self, node):
684 def visit_Num(self, node):
684 if isinstance(node.n, int):
685 if isinstance(node.n, int):
685 return ast.Call(func=ast.Name(id='Integer', ctx=ast.Load()),
686 return ast.Call(func=ast.Name(id='Integer', ctx=ast.Load()),
686 args=[node], keywords=[])
687 args=[node], keywords=[])
687 return node
688 return node
688
689
689 class TestAstTransform2(unittest.TestCase):
690 class TestAstTransform2(unittest.TestCase):
690 def setUp(self):
691 def setUp(self):
691 self.intwrapper = IntegerWrapper()
692 self.intwrapper = IntegerWrapper()
692 ip.ast_transformers.append(self.intwrapper)
693 ip.ast_transformers.append(self.intwrapper)
693
694
694 self.calls = []
695 self.calls = []
695 def Integer(*args):
696 def Integer(*args):
696 self.calls.append(args)
697 self.calls.append(args)
697 return args
698 return args
698 ip.push({"Integer": Integer})
699 ip.push({"Integer": Integer})
699
700
700 def tearDown(self):
701 def tearDown(self):
701 ip.ast_transformers.remove(self.intwrapper)
702 ip.ast_transformers.remove(self.intwrapper)
702 del ip.user_ns['Integer']
703 del ip.user_ns['Integer']
703
704
704 def test_run_cell(self):
705 def test_run_cell(self):
705 ip.run_cell("n = 2")
706 ip.run_cell("n = 2")
706 self.assertEqual(self.calls, [(2,)])
707 self.assertEqual(self.calls, [(2,)])
707
708
708 # This shouldn't throw an error
709 # This shouldn't throw an error
709 ip.run_cell("o = 2.0")
710 ip.run_cell("o = 2.0")
710 self.assertEqual(ip.user_ns['o'], 2.0)
711 self.assertEqual(ip.user_ns['o'], 2.0)
711
712
712 def test_timeit(self):
713 def test_timeit(self):
713 called = set()
714 called = set()
714 def f(x):
715 def f(x):
715 called.add(x)
716 called.add(x)
716 ip.push({'f':f})
717 ip.push({'f':f})
717
718
718 with tt.AssertPrints("best of "):
719 with tt.AssertPrints("best of "):
719 ip.run_line_magic("timeit", "-n1 f(1)")
720 ip.run_line_magic("timeit", "-n1 f(1)")
720 self.assertEqual(called, set([(1,)]))
721 self.assertEqual(called, set([(1,)]))
721 called.clear()
722 called.clear()
722
723
723 with tt.AssertPrints("best of "):
724 with tt.AssertPrints("best of "):
724 ip.run_cell_magic("timeit", "-n1 f(2)", "f(3)")
725 ip.run_cell_magic("timeit", "-n1 f(2)", "f(3)")
725 self.assertEqual(called, set([(2,), (3,)]))
726 self.assertEqual(called, set([(2,), (3,)]))
726
727
727 class ErrorTransformer(ast.NodeTransformer):
728 class ErrorTransformer(ast.NodeTransformer):
728 """Throws an error when it sees a number."""
729 """Throws an error when it sees a number."""
729 def visit_Num(self, node):
730 def visit_Num(self, node):
730 raise ValueError("test")
731 raise ValueError("test")
731
732
732 class TestAstTransformError(unittest.TestCase):
733 class TestAstTransformError(unittest.TestCase):
733 def test_unregistering(self):
734 def test_unregistering(self):
734 err_transformer = ErrorTransformer()
735 err_transformer = ErrorTransformer()
735 ip.ast_transformers.append(err_transformer)
736 ip.ast_transformers.append(err_transformer)
736
737
737 with tt.AssertPrints("unregister", channel='stderr'):
738 with tt.AssertPrints("unregister", channel='stderr'):
738 ip.run_cell("1 + 2")
739 ip.run_cell("1 + 2")
739
740
740 # This should have been removed.
741 # This should have been removed.
741 nt.assert_not_in(err_transformer, ip.ast_transformers)
742 nt.assert_not_in(err_transformer, ip.ast_transformers)
742
743
743
744
744 class StringRejector(ast.NodeTransformer):
745 class StringRejector(ast.NodeTransformer):
745 """Throws an InputRejected when it sees a string literal.
746 """Throws an InputRejected when it sees a string literal.
746
747
747 Used to verify that NodeTransformers can signal that a piece of code should
748 Used to verify that NodeTransformers can signal that a piece of code should
748 not be executed by throwing an InputRejected.
749 not be executed by throwing an InputRejected.
749 """
750 """
750
751
751 def visit_Str(self, node):
752 def visit_Str(self, node):
752 raise InputRejected("test")
753 raise InputRejected("test")
753
754
754
755
755 class TestAstTransformInputRejection(unittest.TestCase):
756 class TestAstTransformInputRejection(unittest.TestCase):
756
757
757 def setUp(self):
758 def setUp(self):
758 self.transformer = StringRejector()
759 self.transformer = StringRejector()
759 ip.ast_transformers.append(self.transformer)
760 ip.ast_transformers.append(self.transformer)
760
761
761 def tearDown(self):
762 def tearDown(self):
762 ip.ast_transformers.remove(self.transformer)
763 ip.ast_transformers.remove(self.transformer)
763
764
764 def test_input_rejection(self):
765 def test_input_rejection(self):
765 """Check that NodeTransformers can reject input."""
766 """Check that NodeTransformers can reject input."""
766
767
767 expect_exception_tb = tt.AssertPrints("InputRejected: test")
768 expect_exception_tb = tt.AssertPrints("InputRejected: test")
768 expect_no_cell_output = tt.AssertNotPrints("'unsafe'", suppress=False)
769 expect_no_cell_output = tt.AssertNotPrints("'unsafe'", suppress=False)
769
770
770 # Run the same check twice to verify that the transformer is not
771 # Run the same check twice to verify that the transformer is not
771 # disabled after raising.
772 # disabled after raising.
772 with expect_exception_tb, expect_no_cell_output:
773 with expect_exception_tb, expect_no_cell_output:
773 ip.run_cell("'unsafe'")
774 ip.run_cell("'unsafe'")
774
775
775 with expect_exception_tb, expect_no_cell_output:
776 with expect_exception_tb, expect_no_cell_output:
776 res = ip.run_cell("'unsafe'")
777 res = ip.run_cell("'unsafe'")
777
778
778 self.assertIsInstance(res.error_before_exec, InputRejected)
779 self.assertIsInstance(res.error_before_exec, InputRejected)
779
780
780 def test__IPYTHON__():
781 def test__IPYTHON__():
781 # This shouldn't raise a NameError, that's all
782 # This shouldn't raise a NameError, that's all
782 __IPYTHON__
783 __IPYTHON__
783
784
784
785
785 class DummyRepr(object):
786 class DummyRepr(object):
786 def __repr__(self):
787 def __repr__(self):
787 return "DummyRepr"
788 return "DummyRepr"
788
789
789 def _repr_html_(self):
790 def _repr_html_(self):
790 return "<b>dummy</b>"
791 return "<b>dummy</b>"
791
792
792 def _repr_javascript_(self):
793 def _repr_javascript_(self):
793 return "console.log('hi');", {'key': 'value'}
794 return "console.log('hi');", {'key': 'value'}
794
795
795
796
796 def test_user_variables():
797 def test_user_variables():
797 # enable all formatters
798 # enable all formatters
798 ip.display_formatter.active_types = ip.display_formatter.format_types
799 ip.display_formatter.active_types = ip.display_formatter.format_types
799
800
800 ip.user_ns['dummy'] = d = DummyRepr()
801 ip.user_ns['dummy'] = d = DummyRepr()
801 keys = set(['dummy', 'doesnotexist'])
802 keys = set(['dummy', 'doesnotexist'])
802 r = ip.user_expressions({ key:key for key in keys})
803 r = ip.user_expressions({ key:key for key in keys})
803
804
804 nt.assert_equal(keys, set(r.keys()))
805 nt.assert_equal(keys, set(r.keys()))
805 dummy = r['dummy']
806 dummy = r['dummy']
806 nt.assert_equal(set(['status', 'data', 'metadata']), set(dummy.keys()))
807 nt.assert_equal(set(['status', 'data', 'metadata']), set(dummy.keys()))
807 nt.assert_equal(dummy['status'], 'ok')
808 nt.assert_equal(dummy['status'], 'ok')
808 data = dummy['data']
809 data = dummy['data']
809 metadata = dummy['metadata']
810 metadata = dummy['metadata']
810 nt.assert_equal(data.get('text/html'), d._repr_html_())
811 nt.assert_equal(data.get('text/html'), d._repr_html_())
811 js, jsmd = d._repr_javascript_()
812 js, jsmd = d._repr_javascript_()
812 nt.assert_equal(data.get('application/javascript'), js)
813 nt.assert_equal(data.get('application/javascript'), js)
813 nt.assert_equal(metadata.get('application/javascript'), jsmd)
814 nt.assert_equal(metadata.get('application/javascript'), jsmd)
814
815
815 dne = r['doesnotexist']
816 dne = r['doesnotexist']
816 nt.assert_equal(dne['status'], 'error')
817 nt.assert_equal(dne['status'], 'error')
817 nt.assert_equal(dne['ename'], 'NameError')
818 nt.assert_equal(dne['ename'], 'NameError')
818
819
819 # back to text only
820 # back to text only
820 ip.display_formatter.active_types = ['text/plain']
821 ip.display_formatter.active_types = ['text/plain']
821
822
822 def test_user_expression():
823 def test_user_expression():
823 # enable all formatters
824 # enable all formatters
824 ip.display_formatter.active_types = ip.display_formatter.format_types
825 ip.display_formatter.active_types = ip.display_formatter.format_types
825 query = {
826 query = {
826 'a' : '1 + 2',
827 'a' : '1 + 2',
827 'b' : '1/0',
828 'b' : '1/0',
828 }
829 }
829 r = ip.user_expressions(query)
830 r = ip.user_expressions(query)
830 import pprint
831 import pprint
831 pprint.pprint(r)
832 pprint.pprint(r)
832 nt.assert_equal(set(r.keys()), set(query.keys()))
833 nt.assert_equal(set(r.keys()), set(query.keys()))
833 a = r['a']
834 a = r['a']
834 nt.assert_equal(set(['status', 'data', 'metadata']), set(a.keys()))
835 nt.assert_equal(set(['status', 'data', 'metadata']), set(a.keys()))
835 nt.assert_equal(a['status'], 'ok')
836 nt.assert_equal(a['status'], 'ok')
836 data = a['data']
837 data = a['data']
837 metadata = a['metadata']
838 metadata = a['metadata']
838 nt.assert_equal(data.get('text/plain'), '3')
839 nt.assert_equal(data.get('text/plain'), '3')
839
840
840 b = r['b']
841 b = r['b']
841 nt.assert_equal(b['status'], 'error')
842 nt.assert_equal(b['status'], 'error')
842 nt.assert_equal(b['ename'], 'ZeroDivisionError')
843 nt.assert_equal(b['ename'], 'ZeroDivisionError')
843
844
844 # back to text only
845 # back to text only
845 ip.display_formatter.active_types = ['text/plain']
846 ip.display_formatter.active_types = ['text/plain']
846
847
847
848
848
849
849
850
850
851
851 class TestSyntaxErrorTransformer(unittest.TestCase):
852 class TestSyntaxErrorTransformer(unittest.TestCase):
852 """Check that SyntaxError raised by an input transformer is handled by run_cell()"""
853 """Check that SyntaxError raised by an input transformer is handled by run_cell()"""
853
854
854 class SyntaxErrorTransformer(InputTransformer):
855 class SyntaxErrorTransformer(InputTransformer):
855
856
856 def push(self, line):
857 def push(self, line):
857 pos = line.find('syntaxerror')
858 pos = line.find('syntaxerror')
858 if pos >= 0:
859 if pos >= 0:
859 e = SyntaxError('input contains "syntaxerror"')
860 e = SyntaxError('input contains "syntaxerror"')
860 e.text = line
861 e.text = line
861 e.offset = pos + 1
862 e.offset = pos + 1
862 raise e
863 raise e
863 return line
864 return line
864
865
865 def reset(self):
866 def reset(self):
866 pass
867 pass
867
868
868 def setUp(self):
869 def setUp(self):
869 self.transformer = TestSyntaxErrorTransformer.SyntaxErrorTransformer()
870 self.transformer = TestSyntaxErrorTransformer.SyntaxErrorTransformer()
870 ip.input_splitter.python_line_transforms.append(self.transformer)
871 ip.input_splitter.python_line_transforms.append(self.transformer)
871 ip.input_transformer_manager.python_line_transforms.append(self.transformer)
872 ip.input_transformer_manager.python_line_transforms.append(self.transformer)
872
873
873 def tearDown(self):
874 def tearDown(self):
874 ip.input_splitter.python_line_transforms.remove(self.transformer)
875 ip.input_splitter.python_line_transforms.remove(self.transformer)
875 ip.input_transformer_manager.python_line_transforms.remove(self.transformer)
876 ip.input_transformer_manager.python_line_transforms.remove(self.transformer)
876
877
877 def test_syntaxerror_input_transformer(self):
878 def test_syntaxerror_input_transformer(self):
878 with tt.AssertPrints('1234'):
879 with tt.AssertPrints('1234'):
879 ip.run_cell('1234')
880 ip.run_cell('1234')
880 with tt.AssertPrints('SyntaxError: invalid syntax'):
881 with tt.AssertPrints('SyntaxError: invalid syntax'):
881 ip.run_cell('1 2 3') # plain python syntax error
882 ip.run_cell('1 2 3') # plain python syntax error
882 with tt.AssertPrints('SyntaxError: input contains "syntaxerror"'):
883 with tt.AssertPrints('SyntaxError: input contains "syntaxerror"'):
883 ip.run_cell('2345 # syntaxerror') # input transformer syntax error
884 ip.run_cell('2345 # syntaxerror') # input transformer syntax error
884 with tt.AssertPrints('3456'):
885 with tt.AssertPrints('3456'):
885 ip.run_cell('3456')
886 ip.run_cell('3456')
886
887
887
888
888
889
889 def test_warning_suppression():
890 def test_warning_suppression():
890 ip.run_cell("import warnings")
891 ip.run_cell("import warnings")
891 try:
892 try:
892 with tt.AssertPrints("UserWarning: asdf", channel="stderr"):
893 with tt.AssertPrints("UserWarning: asdf", channel="stderr"):
893 ip.run_cell("warnings.warn('asdf')")
894 ip.run_cell("warnings.warn('asdf')")
894 # Here's the real test -- if we run that again, we should get the
895 # Here's the real test -- if we run that again, we should get the
895 # warning again. Traditionally, each warning was only issued once per
896 # warning again. Traditionally, each warning was only issued once per
896 # IPython session (approximately), even if the user typed in new and
897 # IPython session (approximately), even if the user typed in new and
897 # different code that should have also triggered the warning, leading
898 # different code that should have also triggered the warning, leading
898 # to much confusion.
899 # to much confusion.
899 with tt.AssertPrints("UserWarning: asdf", channel="stderr"):
900 with tt.AssertPrints("UserWarning: asdf", channel="stderr"):
900 ip.run_cell("warnings.warn('asdf')")
901 ip.run_cell("warnings.warn('asdf')")
901 finally:
902 finally:
902 ip.run_cell("del warnings")
903 ip.run_cell("del warnings")
General Comments 0
You need to be logged in to leave comments. Login now