##// END OF EJS Templates
Add support for finding cell magics with ?/??....
Fernando Perez -
Show More
@@ -1401,8 +1401,10 b' class InteractiveShell(SingletonConfigurable):'
1401 # Try to see if it's magic
1401 # Try to see if it's magic
1402 if not found:
1402 if not found:
1403 if oname.startswith(ESC_MAGIC):
1403 if oname.startswith(ESC_MAGIC):
1404 oname = oname[1:]
1404 oname = oname.lstrip(ESC_MAGIC)
1405 obj = self.find_magic(oname)
1405 obj = self.find_line_magic(oname)
1406 if obj is None:
1407 obj = self.find_cell_magic(oname)
1406 if obj is not None:
1408 if obj is not None:
1407 found = True
1409 found = True
1408 ospace = 'IPython internal'
1410 ospace = 'IPython internal'
@@ -172,7 +172,6 b' def _method_magic_marker(magic_kind):'
172 def _function_magic_marker(magic_kind):
172 def _function_magic_marker(magic_kind):
173 """Decorator factory for standalone functions.
173 """Decorator factory for standalone functions.
174 """
174 """
175
176 validate_type(magic_kind)
175 validate_type(magic_kind)
177
176
178 # This is a closure to capture the magic_kind. We could also use a class,
177 # This is a closure to capture the magic_kind. We could also use a class,
@@ -43,8 +43,6 b' class NamespaceMagics(Magics):'
43 '%pinfo object' is just a synonym for object? or ?object."""
43 '%pinfo object' is just a synonym for object? or ?object."""
44
44
45 #print 'pinfo par: <%s>' % parameter_s # dbg
45 #print 'pinfo par: <%s>' % parameter_s # dbg
46
47
48 # detail_level: 0 -> obj? , 1 -> obj??
46 # detail_level: 0 -> obj? , 1 -> obj??
49 detail_level = 0
47 detail_level = 0
50 # We need to detect if we got called as 'pinfo pinfo foo', which can
48 # We need to detect if we got called as 'pinfo pinfo foo', which can
@@ -22,23 +22,32 b' Authors'
22 # stdlib
22 # stdlib
23 import os
23 import os
24 import shutil
24 import shutil
25 import sys
25 import tempfile
26 import tempfile
26 import unittest
27 import unittest
27 from os.path import join
28 from os.path import join
28 import sys
29 from StringIO import StringIO
29 from StringIO import StringIO
30
30
31 # third-party
32 import nose.tools as nt
33
34 # Our own
31 from IPython.testing.decorators import skipif
35 from IPython.testing.decorators import skipif
32 from IPython.utils import io
36 from IPython.utils import io
33
37
34 #-----------------------------------------------------------------------------
38 #-----------------------------------------------------------------------------
39 # Globals
40 #-----------------------------------------------------------------------------
41 # This is used by every single test, no point repeating it ad nauseam
42 ip = get_ipython()
43
44 #-----------------------------------------------------------------------------
35 # Tests
45 # Tests
36 #-----------------------------------------------------------------------------
46 #-----------------------------------------------------------------------------
37
47
38 class InteractiveShellTestCase(unittest.TestCase):
48 class InteractiveShellTestCase(unittest.TestCase):
39 def test_naked_string_cells(self):
49 def test_naked_string_cells(self):
40 """Test that cells with only naked strings are fully executed"""
50 """Test that cells with only naked strings are fully executed"""
41 ip = get_ipython()
42 # First, single-line inputs
51 # First, single-line inputs
43 ip.run_cell('"a"\n')
52 ip.run_cell('"a"\n')
44 self.assertEquals(ip.user_ns['_'], 'a')
53 self.assertEquals(ip.user_ns['_'], 'a')
@@ -49,7 +58,6 b' class InteractiveShellTestCase(unittest.TestCase):'
49 def test_run_empty_cell(self):
58 def test_run_empty_cell(self):
50 """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
51 cell of input. Yes, I did overlook that."""
60 cell of input. Yes, I did overlook that."""
52 ip = get_ipython()
53 old_xc = ip.execution_count
61 old_xc = ip.execution_count
54 ip.run_cell('')
62 ip.run_cell('')
55 self.assertEquals(ip.execution_count, old_xc)
63 self.assertEquals(ip.execution_count, old_xc)
@@ -57,7 +65,6 b' class InteractiveShellTestCase(unittest.TestCase):'
57 def test_run_cell_multiline(self):
65 def test_run_cell_multiline(self):
58 """Multi-block, multi-line cells must execute correctly.
66 """Multi-block, multi-line cells must execute correctly.
59 """
67 """
60 ip = get_ipython()
61 src = '\n'.join(["x=1",
68 src = '\n'.join(["x=1",
62 "y=2",
69 "y=2",
63 "if 1:",
70 "if 1:",
@@ -69,7 +76,6 b' class InteractiveShellTestCase(unittest.TestCase):'
69
76
70 def test_multiline_string_cells(self):
77 def test_multiline_string_cells(self):
71 "Code sprinkled with multiline strings should execute (GH-306)"
78 "Code sprinkled with multiline strings should execute (GH-306)"
72 ip = get_ipython()
73 ip.run_cell('tmp=0')
79 ip.run_cell('tmp=0')
74 self.assertEquals(ip.user_ns['tmp'], 0)
80 self.assertEquals(ip.user_ns['tmp'], 0)
75 ip.run_cell('tmp=1;"""a\nb"""\n')
81 ip.run_cell('tmp=1;"""a\nb"""\n')
@@ -77,7 +83,6 b' class InteractiveShellTestCase(unittest.TestCase):'
77
83
78 def test_dont_cache_with_semicolon(self):
84 def test_dont_cache_with_semicolon(self):
79 "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)"
80 ip = get_ipython()
81 oldlen = len(ip.user_ns['Out'])
86 oldlen = len(ip.user_ns['Out'])
82 a = ip.run_cell('1;', store_history=True)
87 a = ip.run_cell('1;', store_history=True)
83 newlen = len(ip.user_ns['Out'])
88 newlen = len(ip.user_ns['Out'])
@@ -89,7 +94,6 b' class InteractiveShellTestCase(unittest.TestCase):'
89
94
90 def test_In_variable(self):
95 def test_In_variable(self):
91 "Verify that In variable grows with user input (GH-284)"
96 "Verify that In variable grows with user input (GH-284)"
92 ip = get_ipython()
93 oldlen = len(ip.user_ns['In'])
97 oldlen = len(ip.user_ns['In'])
94 ip.run_cell('1;', store_history=True)
98 ip.run_cell('1;', store_history=True)
95 newlen = len(ip.user_ns['In'])
99 newlen = len(ip.user_ns['In'])
@@ -97,13 +101,11 b' class InteractiveShellTestCase(unittest.TestCase):'
97 self.assertEquals(ip.user_ns['In'][-1],'1;')
101 self.assertEquals(ip.user_ns['In'][-1],'1;')
98
102
99 def test_magic_names_in_string(self):
103 def test_magic_names_in_string(self):
100 ip = get_ipython()
101 ip.run_cell('a = """\n%exit\n"""')
104 ip.run_cell('a = """\n%exit\n"""')
102 self.assertEquals(ip.user_ns['a'], '\n%exit\n')
105 self.assertEquals(ip.user_ns['a'], '\n%exit\n')
103
106
104 def test_alias_crash(self):
107 def test_alias_crash(self):
105 """Errors in prefilter can't crash IPython"""
108 """Errors in prefilter can't crash IPython"""
106 ip = get_ipython()
107 ip.run_cell('%alias parts echo first %s second %s')
109 ip.run_cell('%alias parts echo first %s second %s')
108 # capture stderr:
110 # capture stderr:
109 save_err = io.stderr
111 save_err = io.stderr
@@ -115,7 +117,6 b' class InteractiveShellTestCase(unittest.TestCase):'
115
117
116 def test_trailing_newline(self):
118 def test_trailing_newline(self):
117 """test that running !(command) does not raise a SyntaxError"""
119 """test that running !(command) does not raise a SyntaxError"""
118 ip = get_ipython()
119 ip.run_cell('!(true)\n', False)
120 ip.run_cell('!(true)\n', False)
120 ip.run_cell('!(true)\n\n\n', False)
121 ip.run_cell('!(true)\n\n\n', False)
121
122
@@ -132,7 +133,6 b' class InteractiveShellTestCase(unittest.TestCase):'
132
133
133 def test_future_flags(self):
134 def test_future_flags(self):
134 """Check that future flags are used for parsing code (gh-777)"""
135 """Check that future flags are used for parsing code (gh-777)"""
135 ip = get_ipython()
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=" ")')
@@ -143,7 +143,6 b' class InteractiveShellTestCase(unittest.TestCase):'
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 ip = get_ipython()
147 try:
146 try:
148 ip.run_cell(u'byte_str = "a"')
147 ip.run_cell(u'byte_str = "a"')
149 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
@@ -187,7 +186,6 b' class InteractiveShellTestCase(unittest.TestCase):'
187
186
188 def test_bad_custom_tb(self):
187 def test_bad_custom_tb(self):
189 """Check that InteractiveShell is protected from bad custom exception handlers"""
188 """Check that InteractiveShell is protected from bad custom exception handlers"""
190 ip = get_ipython()
191 from IPython.utils import io
189 from IPython.utils import io
192 save_stderr = io.stderr
190 save_stderr = io.stderr
193 try:
191 try:
@@ -203,7 +201,6 b' class InteractiveShellTestCase(unittest.TestCase):'
203
201
204 def test_bad_custom_tb_return(self):
202 def test_bad_custom_tb_return(self):
205 """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"""
206 ip = get_ipython()
207 from IPython.utils import io
204 from IPython.utils import io
208 save_stderr = io.stderr
205 save_stderr = io.stderr
209 try:
206 try:
@@ -218,7 +215,6 b' class InteractiveShellTestCase(unittest.TestCase):'
218 io.stderr = save_stderr
215 io.stderr = save_stderr
219
216
220 def test_drop_by_id(self):
217 def test_drop_by_id(self):
221 ip = get_ipython()
222 myvars = {"a":object(), "b":object(), "c": object()}
218 myvars = {"a":object(), "b":object(), "c": object()}
223 ip.push(myvars, interactive=False)
219 ip.push(myvars, interactive=False)
224 for name in myvars:
220 for name in myvars:
@@ -233,7 +229,6 b' class InteractiveShellTestCase(unittest.TestCase):'
233 ip.reset()
229 ip.reset()
234
230
235 def test_var_expand(self):
231 def test_var_expand(self):
236 ip = get_ipython()
237 ip.user_ns['f'] = u'Ca\xf1o'
232 ip.user_ns['f'] = u'Ca\xf1o'
238 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')
239 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')
@@ -246,8 +241,6 b' class InteractiveShellTestCase(unittest.TestCase):'
246
241
247 def test_bad_var_expand(self):
242 def test_bad_var_expand(self):
248 """var_expand on invalid formats shouldn't raise"""
243 """var_expand on invalid formats shouldn't raise"""
249 ip = get_ipython()
250
251 # SyntaxError
244 # SyntaxError
252 self.assertEqual(ip.var_expand(u"{'a':5}"), u"{'a':5}")
245 self.assertEqual(ip.var_expand(u"{'a':5}"), u"{'a':5}")
253 # NameError
246 # NameError
@@ -257,8 +250,6 b' class InteractiveShellTestCase(unittest.TestCase):'
257
250
258 def test_silent_nopostexec(self):
251 def test_silent_nopostexec(self):
259 """run_cell(silent=True) doesn't invoke post-exec funcs"""
252 """run_cell(silent=True) doesn't invoke post-exec funcs"""
260 ip = get_ipython()
261
262 d = dict(called=False)
253 d = dict(called=False)
263 def set_called():
254 def set_called():
264 d['called'] = True
255 d['called'] = True
@@ -275,8 +266,6 b' class InteractiveShellTestCase(unittest.TestCase):'
275
266
276 def test_silent_noadvance(self):
267 def test_silent_noadvance(self):
277 """run_cell(silent=True) doesn't advance execution_count"""
268 """run_cell(silent=True) doesn't advance execution_count"""
278 ip = get_ipython()
279
280 ec = ip.execution_count
269 ec = ip.execution_count
281 # silent should force store_history=False
270 # silent should force store_history=False
282 ip.run_cell("1", store_history=True, silent=True)
271 ip.run_cell("1", store_history=True, silent=True)
@@ -289,8 +278,6 b' class InteractiveShellTestCase(unittest.TestCase):'
289
278
290 def test_silent_nodisplayhook(self):
279 def test_silent_nodisplayhook(self):
291 """run_cell(silent=True) doesn't trigger displayhook"""
280 """run_cell(silent=True) doesn't trigger displayhook"""
292 ip = get_ipython()
293
294 d = dict(called=False)
281 d = dict(called=False)
295
282
296 trap = ip.display_trap
283 trap = ip.display_trap
@@ -323,6 +310,35 b' class InteractiveShellTestCase(unittest.TestCase):'
323 1 2
310 1 2
324 """
311 """
325
312
313 def test_ofind_line_magic(self):
314 from IPython.core.magic import register_line_magic
315
316 @register_line_magic
317 def lmagic(line):
318 "A line magic"
319
320 # Get info on line magic
321 lfind = ip._ofind('lmagic')
322 info = dict(found=True, isalias=False, ismagic=True,
323 namespace = 'IPython internal', obj= lmagic.__wrapped__,
324 parent = None)
325 nt.assert_equal(lfind, info)
326
327 def test_ofind_cell_magic(self):
328 from IPython.core.magic import register_cell_magic
329
330 @register_cell_magic
331 def cmagic(line, cell):
332 "A cell magic"
333
334 # Get info on cell magic
335 find = ip._ofind('cmagic')
336 info = dict(found=True, isalias=False, ismagic=True,
337 namespace = 'IPython internal', obj= cmagic.__wrapped__,
338 parent = None)
339 nt.assert_equal(find, info)
340
341
326 class TestSafeExecfileNonAsciiPath(unittest.TestCase):
342 class TestSafeExecfileNonAsciiPath(unittest.TestCase):
327
343
328 def setUp(self):
344 def setUp(self):
@@ -335,7 +351,6 b' class TestSafeExecfileNonAsciiPath(unittest.TestCase):'
335 os.chdir(self.TESTDIR)
351 os.chdir(self.TESTDIR)
336 self.fname = u"åäötestscript.py"
352 self.fname = u"åäötestscript.py"
337
353
338
339 def tearDown(self):
354 def tearDown(self):
340 os.chdir(self.oldpath)
355 os.chdir(self.oldpath)
341 shutil.rmtree(self.BASETESTDIR)
356 shutil.rmtree(self.BASETESTDIR)
@@ -343,7 +358,7 b' class TestSafeExecfileNonAsciiPath(unittest.TestCase):'
343 def test_1(self):
358 def test_1(self):
344 """Test safe_execfile with non-ascii path
359 """Test safe_execfile with non-ascii path
345 """
360 """
346 _ip.safe_execfile(self.fname, {}, raise_exceptions=True)
361 ip.safe_execfile(self.fname, {}, raise_exceptions=True)
347
362
348
363
349 class TestSystemRaw(unittest.TestCase):
364 class TestSystemRaw(unittest.TestCase):
@@ -351,7 +366,7 b' class TestSystemRaw(unittest.TestCase):'
351 """Test system_raw with non-ascii cmd
366 """Test system_raw with non-ascii cmd
352 """
367 """
353 cmd = ur'''python -c "'åäö'" '''
368 cmd = ur'''python -c "'åäö'" '''
354 _ip.system_raw(cmd)
369 ip.system_raw(cmd)
355
370
356
371
357 def test__IPYTHON__():
372 def test__IPYTHON__():
@@ -544,3 +544,4 b' class CellMagicTestCase(TestCase):'
544 # Check that nothing is registered as 'cellm33'
544 # Check that nothing is registered as 'cellm33'
545 c33 = _ip.find_cell_magic('cellm33')
545 c33 = _ip.find_cell_magic('cellm33')
546 nt.assert_equals(c33, None)
546 nt.assert_equals(c33, None)
547
General Comments 0
You need to be logged in to leave comments. Login now