diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index 0055887..a58e0a8 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -1401,8 +1401,10 @@ class InteractiveShell(SingletonConfigurable): # Try to see if it's magic if not found: if oname.startswith(ESC_MAGIC): - oname = oname[1:] - obj = self.find_magic(oname) + oname = oname.lstrip(ESC_MAGIC) + obj = self.find_line_magic(oname) + if obj is None: + obj = self.find_cell_magic(oname) if obj is not None: found = True ospace = 'IPython internal' diff --git a/IPython/core/magic.py b/IPython/core/magic.py index fecd98b..a7d4d37 100644 --- a/IPython/core/magic.py +++ b/IPython/core/magic.py @@ -172,7 +172,6 @@ def _method_magic_marker(magic_kind): def _function_magic_marker(magic_kind): """Decorator factory for standalone functions. """ - validate_type(magic_kind) # This is a closure to capture the magic_kind. We could also use a class, diff --git a/IPython/core/magics/namespace.py b/IPython/core/magics/namespace.py index 62df7e7..b9fa9b1 100644 --- a/IPython/core/magics/namespace.py +++ b/IPython/core/magics/namespace.py @@ -43,8 +43,6 @@ class NamespaceMagics(Magics): '%pinfo object' is just a synonym for object? or ?object.""" #print 'pinfo par: <%s>' % parameter_s # dbg - - # detail_level: 0 -> obj? , 1 -> obj?? detail_level = 0 # We need to detect if we got called as 'pinfo pinfo foo', which can diff --git a/IPython/core/tests/test_interactiveshell.py b/IPython/core/tests/test_interactiveshell.py index 2ae07cf..fa5bf1b 100644 --- a/IPython/core/tests/test_interactiveshell.py +++ b/IPython/core/tests/test_interactiveshell.py @@ -22,23 +22,32 @@ Authors # stdlib import os import shutil +import sys import tempfile import unittest from os.path import join -import sys from StringIO import StringIO +# third-party +import nose.tools as nt + +# Our own from IPython.testing.decorators import skipif from IPython.utils import io #----------------------------------------------------------------------------- +# Globals +#----------------------------------------------------------------------------- +# This is used by every single test, no point repeating it ad nauseam +ip = get_ipython() + +#----------------------------------------------------------------------------- # Tests #----------------------------------------------------------------------------- class InteractiveShellTestCase(unittest.TestCase): def test_naked_string_cells(self): """Test that cells with only naked strings are fully executed""" - ip = get_ipython() # First, single-line inputs ip.run_cell('"a"\n') self.assertEquals(ip.user_ns['_'], 'a') @@ -49,7 +58,6 @@ class InteractiveShellTestCase(unittest.TestCase): def test_run_empty_cell(self): """Just make sure we don't get a horrible error with a blank cell of input. Yes, I did overlook that.""" - ip = get_ipython() old_xc = ip.execution_count ip.run_cell('') self.assertEquals(ip.execution_count, old_xc) @@ -57,7 +65,6 @@ class InteractiveShellTestCase(unittest.TestCase): def test_run_cell_multiline(self): """Multi-block, multi-line cells must execute correctly. """ - ip = get_ipython() src = '\n'.join(["x=1", "y=2", "if 1:", @@ -69,7 +76,6 @@ class InteractiveShellTestCase(unittest.TestCase): def test_multiline_string_cells(self): "Code sprinkled with multiline strings should execute (GH-306)" - ip = get_ipython() ip.run_cell('tmp=0') self.assertEquals(ip.user_ns['tmp'], 0) ip.run_cell('tmp=1;"""a\nb"""\n') @@ -77,7 +83,6 @@ class InteractiveShellTestCase(unittest.TestCase): def test_dont_cache_with_semicolon(self): "Ending a line with semicolon should not cache the returned object (GH-307)" - ip = get_ipython() oldlen = len(ip.user_ns['Out']) a = ip.run_cell('1;', store_history=True) newlen = len(ip.user_ns['Out']) @@ -89,7 +94,6 @@ class InteractiveShellTestCase(unittest.TestCase): def test_In_variable(self): "Verify that In variable grows with user input (GH-284)" - ip = get_ipython() oldlen = len(ip.user_ns['In']) ip.run_cell('1;', store_history=True) newlen = len(ip.user_ns['In']) @@ -97,13 +101,11 @@ class InteractiveShellTestCase(unittest.TestCase): self.assertEquals(ip.user_ns['In'][-1],'1;') def test_magic_names_in_string(self): - ip = get_ipython() ip.run_cell('a = """\n%exit\n"""') self.assertEquals(ip.user_ns['a'], '\n%exit\n') def test_alias_crash(self): """Errors in prefilter can't crash IPython""" - ip = get_ipython() ip.run_cell('%alias parts echo first %s second %s') # capture stderr: save_err = io.stderr @@ -115,7 +117,6 @@ class InteractiveShellTestCase(unittest.TestCase): def test_trailing_newline(self): """test that running !(command) does not raise a SyntaxError""" - ip = get_ipython() ip.run_cell('!(true)\n', False) ip.run_cell('!(true)\n\n\n', False) @@ -132,7 +133,6 @@ class InteractiveShellTestCase(unittest.TestCase): def test_future_flags(self): """Check that future flags are used for parsing code (gh-777)""" - ip = get_ipython() ip.run_cell('from __future__ import print_function') try: ip.run_cell('prfunc_return_val = print(1,2, sep=" ")') @@ -143,7 +143,6 @@ class InteractiveShellTestCase(unittest.TestCase): def test_future_unicode(self): """Check that unicode_literals is imported from __future__ (gh #786)""" - ip = get_ipython() try: ip.run_cell(u'byte_str = "a"') assert isinstance(ip.user_ns['byte_str'], str) # string literals are byte strings by default @@ -187,7 +186,6 @@ class InteractiveShellTestCase(unittest.TestCase): def test_bad_custom_tb(self): """Check that InteractiveShell is protected from bad custom exception handlers""" - ip = get_ipython() from IPython.utils import io save_stderr = io.stderr try: @@ -203,7 +201,6 @@ class InteractiveShellTestCase(unittest.TestCase): def test_bad_custom_tb_return(self): """Check that InteractiveShell is protected from bad return types in custom exception handlers""" - ip = get_ipython() from IPython.utils import io save_stderr = io.stderr try: @@ -218,7 +215,6 @@ class InteractiveShellTestCase(unittest.TestCase): io.stderr = save_stderr def test_drop_by_id(self): - ip = get_ipython() myvars = {"a":object(), "b":object(), "c": object()} ip.push(myvars, interactive=False) for name in myvars: @@ -233,7 +229,6 @@ class InteractiveShellTestCase(unittest.TestCase): ip.reset() def test_var_expand(self): - ip = get_ipython() ip.user_ns['f'] = u'Ca\xf1o' self.assertEqual(ip.var_expand(u'echo $f'), u'echo Ca\xf1o') self.assertEqual(ip.var_expand(u'echo {f}'), u'echo Ca\xf1o') @@ -246,8 +241,6 @@ class InteractiveShellTestCase(unittest.TestCase): def test_bad_var_expand(self): """var_expand on invalid formats shouldn't raise""" - ip = get_ipython() - # SyntaxError self.assertEqual(ip.var_expand(u"{'a':5}"), u"{'a':5}") # NameError @@ -257,8 +250,6 @@ class InteractiveShellTestCase(unittest.TestCase): def test_silent_nopostexec(self): """run_cell(silent=True) doesn't invoke post-exec funcs""" - ip = get_ipython() - d = dict(called=False) def set_called(): d['called'] = True @@ -275,8 +266,6 @@ class InteractiveShellTestCase(unittest.TestCase): def test_silent_noadvance(self): """run_cell(silent=True) doesn't advance execution_count""" - ip = get_ipython() - ec = ip.execution_count # silent should force store_history=False ip.run_cell("1", store_history=True, silent=True) @@ -289,8 +278,6 @@ class InteractiveShellTestCase(unittest.TestCase): def test_silent_nodisplayhook(self): """run_cell(silent=True) doesn't trigger displayhook""" - ip = get_ipython() - d = dict(called=False) trap = ip.display_trap @@ -322,6 +309,35 @@ class InteractiveShellTestCase(unittest.TestCase): In [2]: print 1,; print 2 1 2 """ + + def test_ofind_line_magic(self): + from IPython.core.magic import register_line_magic + + @register_line_magic + def lmagic(line): + "A line magic" + + # Get info on line magic + lfind = ip._ofind('lmagic') + info = dict(found=True, isalias=False, ismagic=True, + namespace = 'IPython internal', obj= lmagic.__wrapped__, + parent = None) + nt.assert_equal(lfind, info) + + def test_ofind_cell_magic(self): + from IPython.core.magic import register_cell_magic + + @register_cell_magic + def cmagic(line, cell): + "A cell magic" + + # Get info on cell magic + find = ip._ofind('cmagic') + info = dict(found=True, isalias=False, ismagic=True, + namespace = 'IPython internal', obj= cmagic.__wrapped__, + parent = None) + nt.assert_equal(find, info) + class TestSafeExecfileNonAsciiPath(unittest.TestCase): @@ -335,7 +351,6 @@ class TestSafeExecfileNonAsciiPath(unittest.TestCase): os.chdir(self.TESTDIR) self.fname = u"åäötestscript.py" - def tearDown(self): os.chdir(self.oldpath) shutil.rmtree(self.BASETESTDIR) @@ -343,7 +358,7 @@ class TestSafeExecfileNonAsciiPath(unittest.TestCase): def test_1(self): """Test safe_execfile with non-ascii path """ - _ip.safe_execfile(self.fname, {}, raise_exceptions=True) + ip.safe_execfile(self.fname, {}, raise_exceptions=True) class TestSystemRaw(unittest.TestCase): @@ -351,7 +366,7 @@ class TestSystemRaw(unittest.TestCase): """Test system_raw with non-ascii cmd """ cmd = ur'''python -c "'åäö'" ''' - _ip.system_raw(cmd) + ip.system_raw(cmd) def test__IPYTHON__(): diff --git a/IPython/core/tests/test_magic.py b/IPython/core/tests/test_magic.py index 362eb89..0b53133 100644 --- a/IPython/core/tests/test_magic.py +++ b/IPython/core/tests/test_magic.py @@ -544,3 +544,4 @@ class CellMagicTestCase(TestCase): # Check that nothing is registered as 'cellm33' c33 = _ip.find_cell_magic('cellm33') nt.assert_equals(c33, None) +