##// END OF EJS Templates
Test for local variable expansion in %magic commands.
Thomas Kluyver -
Show More
@@ -1,397 +1,407 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 Authors
8 Authors
9 -------
9 -------
10 * Fernando Perez
10 * Fernando Perez
11 """
11 """
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13 # Copyright (C) 2011 The IPython Development Team
13 # Copyright (C) 2011 The IPython Development Team
14 #
14 #
15 # Distributed under the terms of the BSD License. The full license is in
15 # Distributed under the terms of the BSD License. The full license is in
16 # the file COPYING, distributed as part of this software.
16 # the file COPYING, distributed as part of this software.
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18
18
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20 # Imports
20 # Imports
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22 # stdlib
22 # stdlib
23 import os
23 import os
24 import shutil
24 import shutil
25 import sys
25 import sys
26 import tempfile
26 import tempfile
27 import unittest
27 import unittest
28 from os.path import join
28 from os.path import join
29 from StringIO import StringIO
29 from StringIO import StringIO
30
30
31 # third-party
31 # third-party
32 import nose.tools as nt
32 import nose.tools as nt
33
33
34 # Our own
34 # Our own
35 from IPython.testing.decorators import skipif
35 from IPython.testing.decorators import skipif
36 from IPython.utils import io
36 from IPython.utils import io
37
37
38 #-----------------------------------------------------------------------------
38 #-----------------------------------------------------------------------------
39 # Globals
39 # Globals
40 #-----------------------------------------------------------------------------
40 #-----------------------------------------------------------------------------
41 # This is used by every single test, no point repeating it ad nauseam
41 # This is used by every single test, no point repeating it ad nauseam
42 ip = get_ipython()
42 ip = get_ipython()
43
43
44 #-----------------------------------------------------------------------------
44 #-----------------------------------------------------------------------------
45 # Tests
45 # Tests
46 #-----------------------------------------------------------------------------
46 #-----------------------------------------------------------------------------
47
47
48 class InteractiveShellTestCase(unittest.TestCase):
48 class InteractiveShellTestCase(unittest.TestCase):
49 def test_naked_string_cells(self):
49 def test_naked_string_cells(self):
50 """Test that cells with only naked strings are fully executed"""
50 """Test that cells with only naked strings are fully executed"""
51 # First, single-line inputs
51 # First, single-line inputs
52 ip.run_cell('"a"\n')
52 ip.run_cell('"a"\n')
53 self.assertEquals(ip.user_ns['_'], 'a')
53 self.assertEquals(ip.user_ns['_'], 'a')
54 # And also multi-line cells
54 # And also multi-line cells
55 ip.run_cell('"""a\nb"""\n')
55 ip.run_cell('"""a\nb"""\n')
56 self.assertEquals(ip.user_ns['_'], 'a\nb')
56 self.assertEquals(ip.user_ns['_'], 'a\nb')
57
57
58 def test_run_empty_cell(self):
58 def test_run_empty_cell(self):
59 """Just make sure we don't get a horrible error with a blank
59 """Just make sure we don't get a horrible error with a blank
60 cell of input. Yes, I did overlook that."""
60 cell of input. Yes, I did overlook that."""
61 old_xc = ip.execution_count
61 old_xc = ip.execution_count
62 ip.run_cell('')
62 ip.run_cell('')
63 self.assertEquals(ip.execution_count, old_xc)
63 self.assertEquals(ip.execution_count, old_xc)
64
64
65 def test_run_cell_multiline(self):
65 def test_run_cell_multiline(self):
66 """Multi-block, multi-line cells must execute correctly.
66 """Multi-block, multi-line cells must execute correctly.
67 """
67 """
68 src = '\n'.join(["x=1",
68 src = '\n'.join(["x=1",
69 "y=2",
69 "y=2",
70 "if 1:",
70 "if 1:",
71 " x += 1",
71 " x += 1",
72 " y += 1",])
72 " y += 1",])
73 ip.run_cell(src)
73 ip.run_cell(src)
74 self.assertEquals(ip.user_ns['x'], 2)
74 self.assertEquals(ip.user_ns['x'], 2)
75 self.assertEquals(ip.user_ns['y'], 3)
75 self.assertEquals(ip.user_ns['y'], 3)
76
76
77 def test_multiline_string_cells(self):
77 def test_multiline_string_cells(self):
78 "Code sprinkled with multiline strings should execute (GH-306)"
78 "Code sprinkled with multiline strings should execute (GH-306)"
79 ip.run_cell('tmp=0')
79 ip.run_cell('tmp=0')
80 self.assertEquals(ip.user_ns['tmp'], 0)
80 self.assertEquals(ip.user_ns['tmp'], 0)
81 ip.run_cell('tmp=1;"""a\nb"""\n')
81 ip.run_cell('tmp=1;"""a\nb"""\n')
82 self.assertEquals(ip.user_ns['tmp'], 1)
82 self.assertEquals(ip.user_ns['tmp'], 1)
83
83
84 def test_dont_cache_with_semicolon(self):
84 def test_dont_cache_with_semicolon(self):
85 "Ending a line with semicolon should not cache the returned object (GH-307)"
85 "Ending a line with semicolon should not cache the returned object (GH-307)"
86 oldlen = len(ip.user_ns['Out'])
86 oldlen = len(ip.user_ns['Out'])
87 a = ip.run_cell('1;', store_history=True)
87 a = ip.run_cell('1;', store_history=True)
88 newlen = len(ip.user_ns['Out'])
88 newlen = len(ip.user_ns['Out'])
89 self.assertEquals(oldlen, newlen)
89 self.assertEquals(oldlen, newlen)
90 #also test the default caching behavior
90 #also test the default caching behavior
91 ip.run_cell('1', store_history=True)
91 ip.run_cell('1', store_history=True)
92 newlen = len(ip.user_ns['Out'])
92 newlen = len(ip.user_ns['Out'])
93 self.assertEquals(oldlen+1, newlen)
93 self.assertEquals(oldlen+1, newlen)
94
94
95 def test_In_variable(self):
95 def test_In_variable(self):
96 "Verify that In variable grows with user input (GH-284)"
96 "Verify that In variable grows with user input (GH-284)"
97 oldlen = len(ip.user_ns['In'])
97 oldlen = len(ip.user_ns['In'])
98 ip.run_cell('1;', store_history=True)
98 ip.run_cell('1;', store_history=True)
99 newlen = len(ip.user_ns['In'])
99 newlen = len(ip.user_ns['In'])
100 self.assertEquals(oldlen+1, newlen)
100 self.assertEquals(oldlen+1, newlen)
101 self.assertEquals(ip.user_ns['In'][-1],'1;')
101 self.assertEquals(ip.user_ns['In'][-1],'1;')
102
102
103 def test_magic_names_in_string(self):
103 def test_magic_names_in_string(self):
104 ip.run_cell('a = """\n%exit\n"""')
104 ip.run_cell('a = """\n%exit\n"""')
105 self.assertEquals(ip.user_ns['a'], '\n%exit\n')
105 self.assertEquals(ip.user_ns['a'], '\n%exit\n')
106
106
107 def test_alias_crash(self):
107 def test_alias_crash(self):
108 """Errors in prefilter can't crash IPython"""
108 """Errors in prefilter can't crash IPython"""
109 ip.run_cell('%alias parts echo first %s second %s')
109 ip.run_cell('%alias parts echo first %s second %s')
110 # capture stderr:
110 # capture stderr:
111 save_err = io.stderr
111 save_err = io.stderr
112 io.stderr = StringIO()
112 io.stderr = StringIO()
113 ip.run_cell('parts 1')
113 ip.run_cell('parts 1')
114 err = io.stderr.getvalue()
114 err = io.stderr.getvalue()
115 io.stderr = save_err
115 io.stderr = save_err
116 self.assertEquals(err.split(':')[0], 'ERROR')
116 self.assertEquals(err.split(':')[0], 'ERROR')
117
117
118 def test_trailing_newline(self):
118 def test_trailing_newline(self):
119 """test that running !(command) does not raise a SyntaxError"""
119 """test that running !(command) does not raise a SyntaxError"""
120 ip.run_cell('!(true)\n', False)
120 ip.run_cell('!(true)\n', False)
121 ip.run_cell('!(true)\n\n\n', False)
121 ip.run_cell('!(true)\n\n\n', False)
122
122
123 def test_gh_597(self):
123 def test_gh_597(self):
124 """Pretty-printing lists of objects with non-ascii reprs may cause
124 """Pretty-printing lists of objects with non-ascii reprs may cause
125 problems."""
125 problems."""
126 class Spam(object):
126 class Spam(object):
127 def __repr__(self):
127 def __repr__(self):
128 return "\xe9"*50
128 return "\xe9"*50
129 import IPython.core.formatters
129 import IPython.core.formatters
130 f = IPython.core.formatters.PlainTextFormatter()
130 f = IPython.core.formatters.PlainTextFormatter()
131 f([Spam(),Spam()])
131 f([Spam(),Spam()])
132
132
133
133
134 def test_future_flags(self):
134 def test_future_flags(self):
135 """Check that future flags are used for parsing code (gh-777)"""
135 """Check that future flags are used for parsing code (gh-777)"""
136 ip.run_cell('from __future__ import print_function')
136 ip.run_cell('from __future__ import print_function')
137 try:
137 try:
138 ip.run_cell('prfunc_return_val = print(1,2, sep=" ")')
138 ip.run_cell('prfunc_return_val = print(1,2, sep=" ")')
139 assert 'prfunc_return_val' in ip.user_ns
139 assert 'prfunc_return_val' in ip.user_ns
140 finally:
140 finally:
141 # Reset compiler flags so we don't mess up other tests.
141 # Reset compiler flags so we don't mess up other tests.
142 ip.compile.reset_compiler_flags()
142 ip.compile.reset_compiler_flags()
143
143
144 def test_future_unicode(self):
144 def test_future_unicode(self):
145 """Check that unicode_literals is imported from __future__ (gh #786)"""
145 """Check that unicode_literals is imported from __future__ (gh #786)"""
146 try:
146 try:
147 ip.run_cell(u'byte_str = "a"')
147 ip.run_cell(u'byte_str = "a"')
148 assert isinstance(ip.user_ns['byte_str'], str) # string literals are byte strings by default
148 assert isinstance(ip.user_ns['byte_str'], str) # string literals are byte strings by default
149 ip.run_cell('from __future__ import unicode_literals')
149 ip.run_cell('from __future__ import unicode_literals')
150 ip.run_cell(u'unicode_str = "a"')
150 ip.run_cell(u'unicode_str = "a"')
151 assert isinstance(ip.user_ns['unicode_str'], unicode) # strings literals are now unicode
151 assert isinstance(ip.user_ns['unicode_str'], unicode) # strings literals are now unicode
152 finally:
152 finally:
153 # Reset compiler flags so we don't mess up other tests.
153 # Reset compiler flags so we don't mess up other tests.
154 ip.compile.reset_compiler_flags()
154 ip.compile.reset_compiler_flags()
155
155
156 def test_can_pickle(self):
156 def test_can_pickle(self):
157 "Can we pickle objects defined interactively (GH-29)"
157 "Can we pickle objects defined interactively (GH-29)"
158 ip = get_ipython()
158 ip = get_ipython()
159 ip.reset()
159 ip.reset()
160 ip.run_cell(("class Mylist(list):\n"
160 ip.run_cell(("class Mylist(list):\n"
161 " def __init__(self,x=[]):\n"
161 " def __init__(self,x=[]):\n"
162 " list.__init__(self,x)"))
162 " list.__init__(self,x)"))
163 ip.run_cell("w=Mylist([1,2,3])")
163 ip.run_cell("w=Mylist([1,2,3])")
164
164
165 from cPickle import dumps
165 from cPickle import dumps
166
166
167 # We need to swap in our main module - this is only necessary
167 # We need to swap in our main module - this is only necessary
168 # inside the test framework, because IPython puts the interactive module
168 # inside the test framework, because IPython puts the interactive module
169 # in place (but the test framework undoes this).
169 # in place (but the test framework undoes this).
170 _main = sys.modules['__main__']
170 _main = sys.modules['__main__']
171 sys.modules['__main__'] = ip.user_module
171 sys.modules['__main__'] = ip.user_module
172 try:
172 try:
173 res = dumps(ip.user_ns["w"])
173 res = dumps(ip.user_ns["w"])
174 finally:
174 finally:
175 sys.modules['__main__'] = _main
175 sys.modules['__main__'] = _main
176 self.assertTrue(isinstance(res, bytes))
176 self.assertTrue(isinstance(res, bytes))
177
177
178 def test_global_ns(self):
178 def test_global_ns(self):
179 "Code in functions must be able to access variables outside them."
179 "Code in functions must be able to access variables outside them."
180 ip = get_ipython()
180 ip = get_ipython()
181 ip.run_cell("a = 10")
181 ip.run_cell("a = 10")
182 ip.run_cell(("def f(x):\n"
182 ip.run_cell(("def f(x):\n"
183 " return x + a"))
183 " return x + a"))
184 ip.run_cell("b = f(12)")
184 ip.run_cell("b = f(12)")
185 self.assertEqual(ip.user_ns["b"], 22)
185 self.assertEqual(ip.user_ns["b"], 22)
186
186
187 def test_bad_custom_tb(self):
187 def test_bad_custom_tb(self):
188 """Check that InteractiveShell is protected from bad custom exception handlers"""
188 """Check that InteractiveShell is protected from bad custom exception handlers"""
189 from IPython.utils import io
189 from IPython.utils import io
190 save_stderr = io.stderr
190 save_stderr = io.stderr
191 try:
191 try:
192 # capture stderr
192 # capture stderr
193 io.stderr = StringIO()
193 io.stderr = StringIO()
194 ip.set_custom_exc((IOError,), lambda etype,value,tb: 1/0)
194 ip.set_custom_exc((IOError,), lambda etype,value,tb: 1/0)
195 self.assertEquals(ip.custom_exceptions, (IOError,))
195 self.assertEquals(ip.custom_exceptions, (IOError,))
196 ip.run_cell(u'raise IOError("foo")')
196 ip.run_cell(u'raise IOError("foo")')
197 self.assertEquals(ip.custom_exceptions, ())
197 self.assertEquals(ip.custom_exceptions, ())
198 self.assertTrue("Custom TB Handler failed" in io.stderr.getvalue())
198 self.assertTrue("Custom TB Handler failed" in io.stderr.getvalue())
199 finally:
199 finally:
200 io.stderr = save_stderr
200 io.stderr = save_stderr
201
201
202 def test_bad_custom_tb_return(self):
202 def test_bad_custom_tb_return(self):
203 """Check that InteractiveShell is protected from bad return types in custom exception handlers"""
203 """Check that InteractiveShell is protected from bad return types in custom exception handlers"""
204 from IPython.utils import io
204 from IPython.utils import io
205 save_stderr = io.stderr
205 save_stderr = io.stderr
206 try:
206 try:
207 # capture stderr
207 # capture stderr
208 io.stderr = StringIO()
208 io.stderr = StringIO()
209 ip.set_custom_exc((NameError,),lambda etype,value,tb, tb_offset=None: 1)
209 ip.set_custom_exc((NameError,),lambda etype,value,tb, tb_offset=None: 1)
210 self.assertEquals(ip.custom_exceptions, (NameError,))
210 self.assertEquals(ip.custom_exceptions, (NameError,))
211 ip.run_cell(u'a=abracadabra')
211 ip.run_cell(u'a=abracadabra')
212 self.assertEquals(ip.custom_exceptions, ())
212 self.assertEquals(ip.custom_exceptions, ())
213 self.assertTrue("Custom TB Handler failed" in io.stderr.getvalue())
213 self.assertTrue("Custom TB Handler failed" in io.stderr.getvalue())
214 finally:
214 finally:
215 io.stderr = save_stderr
215 io.stderr = save_stderr
216
216
217 def test_drop_by_id(self):
217 def test_drop_by_id(self):
218 myvars = {"a":object(), "b":object(), "c": object()}
218 myvars = {"a":object(), "b":object(), "c": object()}
219 ip.push(myvars, interactive=False)
219 ip.push(myvars, interactive=False)
220 for name in myvars:
220 for name in myvars:
221 assert name in ip.user_ns, name
221 assert name in ip.user_ns, name
222 assert name in ip.user_ns_hidden, name
222 assert name in ip.user_ns_hidden, name
223 ip.user_ns['b'] = 12
223 ip.user_ns['b'] = 12
224 ip.drop_by_id(myvars)
224 ip.drop_by_id(myvars)
225 for name in ["a", "c"]:
225 for name in ["a", "c"]:
226 assert name not in ip.user_ns, name
226 assert name not in ip.user_ns, name
227 assert name not in ip.user_ns_hidden, name
227 assert name not in ip.user_ns_hidden, name
228 assert ip.user_ns['b'] == 12
228 assert ip.user_ns['b'] == 12
229 ip.reset()
229 ip.reset()
230
230
231 def test_var_expand(self):
231 def test_var_expand(self):
232 ip.user_ns['f'] = u'Ca\xf1o'
232 ip.user_ns['f'] = u'Ca\xf1o'
233 self.assertEqual(ip.var_expand(u'echo $f'), u'echo Ca\xf1o')
233 self.assertEqual(ip.var_expand(u'echo $f'), u'echo Ca\xf1o')
234 self.assertEqual(ip.var_expand(u'echo {f}'), u'echo Ca\xf1o')
234 self.assertEqual(ip.var_expand(u'echo {f}'), u'echo Ca\xf1o')
235 self.assertEqual(ip.var_expand(u'echo {f[:-1]}'), u'echo Ca\xf1')
235 self.assertEqual(ip.var_expand(u'echo {f[:-1]}'), u'echo Ca\xf1')
236 self.assertEqual(ip.var_expand(u'echo {1*2}'), u'echo 2')
236 self.assertEqual(ip.var_expand(u'echo {1*2}'), u'echo 2')
237
237
238 ip.user_ns['f'] = b'Ca\xc3\xb1o'
238 ip.user_ns['f'] = b'Ca\xc3\xb1o'
239 # This should not raise any exception:
239 # This should not raise any exception:
240 ip.var_expand(u'echo $f')
240 ip.var_expand(u'echo $f')
241
241
242 def test_var_expand_local(self):
242 def test_var_expand_local(self):
243 """Test local variable expansion in !system and %magic calls"""
244 # !system
243 ip.run_cell('def test():\n'
245 ip.run_cell('def test():\n'
244 ' lvar = "ttt"\n'
246 ' lvar = "ttt"\n'
245 ' ret = !echo {lvar}\n'
247 ' ret = !echo {lvar}\n'
246 ' return ret[0]\n')
248 ' return ret[0]\n')
247 res = ip.user_ns['test']()
249 res = ip.user_ns['test']()
248 nt.assert_in('ttt', res)
250 nt.assert_in('ttt', res)
249
251
252 # %magic
253 ip.run_cell('def makemacro():\n'
254 ' macroname = "macro_var_expand_locals"\n'
255 ' %macro {macroname} codestr\n')
256 ip.user_ns['codestr'] = "str(12)"
257 ip.run_cell('makemacro()')
258 nt.assert_in('macro_var_expand_locals', ip.user_ns)
259
250 def test_bad_var_expand(self):
260 def test_bad_var_expand(self):
251 """var_expand on invalid formats shouldn't raise"""
261 """var_expand on invalid formats shouldn't raise"""
252 # SyntaxError
262 # SyntaxError
253 self.assertEqual(ip.var_expand(u"{'a':5}"), u"{'a':5}")
263 self.assertEqual(ip.var_expand(u"{'a':5}"), u"{'a':5}")
254 # NameError
264 # NameError
255 self.assertEqual(ip.var_expand(u"{asdf}"), u"{asdf}")
265 self.assertEqual(ip.var_expand(u"{asdf}"), u"{asdf}")
256 # ZeroDivisionError
266 # ZeroDivisionError
257 self.assertEqual(ip.var_expand(u"{1/0}"), u"{1/0}")
267 self.assertEqual(ip.var_expand(u"{1/0}"), u"{1/0}")
258
268
259 def test_silent_nopostexec(self):
269 def test_silent_nopostexec(self):
260 """run_cell(silent=True) doesn't invoke post-exec funcs"""
270 """run_cell(silent=True) doesn't invoke post-exec funcs"""
261 d = dict(called=False)
271 d = dict(called=False)
262 def set_called():
272 def set_called():
263 d['called'] = True
273 d['called'] = True
264
274
265 ip.register_post_execute(set_called)
275 ip.register_post_execute(set_called)
266 ip.run_cell("1", silent=True)
276 ip.run_cell("1", silent=True)
267 self.assertFalse(d['called'])
277 self.assertFalse(d['called'])
268 # double-check that non-silent exec did what we expected
278 # double-check that non-silent exec did what we expected
269 # silent to avoid
279 # silent to avoid
270 ip.run_cell("1")
280 ip.run_cell("1")
271 self.assertTrue(d['called'])
281 self.assertTrue(d['called'])
272 # remove post-exec
282 # remove post-exec
273 ip._post_execute.pop(set_called)
283 ip._post_execute.pop(set_called)
274
284
275 def test_silent_noadvance(self):
285 def test_silent_noadvance(self):
276 """run_cell(silent=True) doesn't advance execution_count"""
286 """run_cell(silent=True) doesn't advance execution_count"""
277 ec = ip.execution_count
287 ec = ip.execution_count
278 # silent should force store_history=False
288 # silent should force store_history=False
279 ip.run_cell("1", store_history=True, silent=True)
289 ip.run_cell("1", store_history=True, silent=True)
280
290
281 self.assertEquals(ec, ip.execution_count)
291 self.assertEquals(ec, ip.execution_count)
282 # double-check that non-silent exec did what we expected
292 # double-check that non-silent exec did what we expected
283 # silent to avoid
293 # silent to avoid
284 ip.run_cell("1", store_history=True)
294 ip.run_cell("1", store_history=True)
285 self.assertEquals(ec+1, ip.execution_count)
295 self.assertEquals(ec+1, ip.execution_count)
286
296
287 def test_silent_nodisplayhook(self):
297 def test_silent_nodisplayhook(self):
288 """run_cell(silent=True) doesn't trigger displayhook"""
298 """run_cell(silent=True) doesn't trigger displayhook"""
289 d = dict(called=False)
299 d = dict(called=False)
290
300
291 trap = ip.display_trap
301 trap = ip.display_trap
292 save_hook = trap.hook
302 save_hook = trap.hook
293
303
294 def failing_hook(*args, **kwargs):
304 def failing_hook(*args, **kwargs):
295 d['called'] = True
305 d['called'] = True
296
306
297 try:
307 try:
298 trap.hook = failing_hook
308 trap.hook = failing_hook
299 ip.run_cell("1", silent=True)
309 ip.run_cell("1", silent=True)
300 self.assertFalse(d['called'])
310 self.assertFalse(d['called'])
301 # double-check that non-silent exec did what we expected
311 # double-check that non-silent exec did what we expected
302 # silent to avoid
312 # silent to avoid
303 ip.run_cell("1")
313 ip.run_cell("1")
304 self.assertTrue(d['called'])
314 self.assertTrue(d['called'])
305 finally:
315 finally:
306 trap.hook = save_hook
316 trap.hook = save_hook
307
317
308 @skipif(sys.version_info[0] >= 3, "softspace removed in py3")
318 @skipif(sys.version_info[0] >= 3, "softspace removed in py3")
309 def test_print_softspace(self):
319 def test_print_softspace(self):
310 """Verify that softspace is handled correctly when executing multiple
320 """Verify that softspace is handled correctly when executing multiple
311 statements.
321 statements.
312
322
313 In [1]: print 1; print 2
323 In [1]: print 1; print 2
314 1
324 1
315 2
325 2
316
326
317 In [2]: print 1,; print 2
327 In [2]: print 1,; print 2
318 1 2
328 1 2
319 """
329 """
320
330
321 def test_ofind_line_magic(self):
331 def test_ofind_line_magic(self):
322 from IPython.core.magic import register_line_magic
332 from IPython.core.magic import register_line_magic
323
333
324 @register_line_magic
334 @register_line_magic
325 def lmagic(line):
335 def lmagic(line):
326 "A line magic"
336 "A line magic"
327
337
328 # Get info on line magic
338 # Get info on line magic
329 lfind = ip._ofind('lmagic')
339 lfind = ip._ofind('lmagic')
330 info = dict(found=True, isalias=False, ismagic=True,
340 info = dict(found=True, isalias=False, ismagic=True,
331 namespace = 'IPython internal', obj= lmagic.__wrapped__,
341 namespace = 'IPython internal', obj= lmagic.__wrapped__,
332 parent = None)
342 parent = None)
333 nt.assert_equal(lfind, info)
343 nt.assert_equal(lfind, info)
334
344
335 def test_ofind_cell_magic(self):
345 def test_ofind_cell_magic(self):
336 from IPython.core.magic import register_cell_magic
346 from IPython.core.magic import register_cell_magic
337
347
338 @register_cell_magic
348 @register_cell_magic
339 def cmagic(line, cell):
349 def cmagic(line, cell):
340 "A cell magic"
350 "A cell magic"
341
351
342 # Get info on cell magic
352 # Get info on cell magic
343 find = ip._ofind('cmagic')
353 find = ip._ofind('cmagic')
344 info = dict(found=True, isalias=False, ismagic=True,
354 info = dict(found=True, isalias=False, ismagic=True,
345 namespace = 'IPython internal', obj= cmagic.__wrapped__,
355 namespace = 'IPython internal', obj= cmagic.__wrapped__,
346 parent = None)
356 parent = None)
347 nt.assert_equal(find, info)
357 nt.assert_equal(find, info)
348
358
349 def test_custom_exception(self):
359 def test_custom_exception(self):
350 called = []
360 called = []
351 def my_handler(shell, etype, value, tb, tb_offset=None):
361 def my_handler(shell, etype, value, tb, tb_offset=None):
352 called.append(etype)
362 called.append(etype)
353 shell.showtraceback((etype, value, tb), tb_offset=tb_offset)
363 shell.showtraceback((etype, value, tb), tb_offset=tb_offset)
354
364
355 ip.set_custom_exc((ValueError,), my_handler)
365 ip.set_custom_exc((ValueError,), my_handler)
356 try:
366 try:
357 ip.run_cell("raise ValueError('test')")
367 ip.run_cell("raise ValueError('test')")
358 # Check that this was called, and only once.
368 # Check that this was called, and only once.
359 self.assertEqual(called, [ValueError])
369 self.assertEqual(called, [ValueError])
360 finally:
370 finally:
361 # Reset the custom exception hook
371 # Reset the custom exception hook
362 ip.set_custom_exc((), None)
372 ip.set_custom_exc((), None)
363
373
364
374
365 class TestSafeExecfileNonAsciiPath(unittest.TestCase):
375 class TestSafeExecfileNonAsciiPath(unittest.TestCase):
366
376
367 def setUp(self):
377 def setUp(self):
368 self.BASETESTDIR = tempfile.mkdtemp()
378 self.BASETESTDIR = tempfile.mkdtemp()
369 self.TESTDIR = join(self.BASETESTDIR, u"åäö")
379 self.TESTDIR = join(self.BASETESTDIR, u"åäö")
370 os.mkdir(self.TESTDIR)
380 os.mkdir(self.TESTDIR)
371 with open(join(self.TESTDIR, u"åäötestscript.py"), "w") as sfile:
381 with open(join(self.TESTDIR, u"åäötestscript.py"), "w") as sfile:
372 sfile.write("pass\n")
382 sfile.write("pass\n")
373 self.oldpath = os.getcwdu()
383 self.oldpath = os.getcwdu()
374 os.chdir(self.TESTDIR)
384 os.chdir(self.TESTDIR)
375 self.fname = u"åäötestscript.py"
385 self.fname = u"åäötestscript.py"
376
386
377 def tearDown(self):
387 def tearDown(self):
378 os.chdir(self.oldpath)
388 os.chdir(self.oldpath)
379 shutil.rmtree(self.BASETESTDIR)
389 shutil.rmtree(self.BASETESTDIR)
380
390
381 def test_1(self):
391 def test_1(self):
382 """Test safe_execfile with non-ascii path
392 """Test safe_execfile with non-ascii path
383 """
393 """
384 ip.safe_execfile(self.fname, {}, raise_exceptions=True)
394 ip.safe_execfile(self.fname, {}, raise_exceptions=True)
385
395
386
396
387 class TestSystemRaw(unittest.TestCase):
397 class TestSystemRaw(unittest.TestCase):
388 def test_1(self):
398 def test_1(self):
389 """Test system_raw with non-ascii cmd
399 """Test system_raw with non-ascii cmd
390 """
400 """
391 cmd = ur'''python -c "'åäö'" '''
401 cmd = ur'''python -c "'åäö'" '''
392 ip.system_raw(cmd)
402 ip.system_raw(cmd)
393
403
394
404
395 def test__IPYTHON__():
405 def test__IPYTHON__():
396 # This shouldn't raise a NameError, that's all
406 # This shouldn't raise a NameError, that's all
397 __IPYTHON__
407 __IPYTHON__
General Comments 0
You need to be logged in to leave comments. Login now