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