diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index fa9b1a0..1d537e8 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -2076,7 +2076,7 @@ class InteractiveShell(SingletonConfigurable): etpl = "Line magic function `%%%s` not found%s." extra = '' if cm is None else (' (But cell magic `%%%%%s` exists, ' 'did you mean that instead?)' % magic_name ) - error(etpl % (magic_name, extra)) + raise UsageError(etpl % (magic_name, extra)) else: # Note: this is the distance in the stack to the user's frame. # This will need to be updated if the internal calling logic gets @@ -2115,7 +2115,7 @@ class InteractiveShell(SingletonConfigurable): etpl = "Cell magic `%%{0}` not found{1}." extra = '' if lm is None else (' (But line magic `%{0}` exists, ' 'did you mean that instead?)'.format(magic_name)) - error(etpl.format(magic_name, extra)) + raise UsageError(etpl.format(magic_name, extra)) elif cell == '': message = '%%{0} is a cell magic, but the cell body is empty.'.format(magic_name) if self.find_line_magic(magic_name) is not None: diff --git a/IPython/core/tests/test_magic.py b/IPython/core/tests/test_magic.py index f4ab53f..f64d2a6 100644 --- a/IPython/core/tests/test_magic.py +++ b/IPython/core/tests/test_magic.py @@ -74,6 +74,35 @@ def test_extract_symbols_raises_exception_with_non_python_code(): with nt.assert_raises(SyntaxError): code.extract_symbols(source, "hello") + +def test_magic_not_found(): + # magic not found raises UsageError + with nt.assert_raises(UsageError): + _ip.magic('doesntexist') + + # ensure result isn't success when a magic isn't found + result = _ip.run_cell('%doesntexist') + assert isinstance(result.error_in_exec, UsageError) + + +def test_cell_magic_not_found(): + # magic not found raises UsageError + with nt.assert_raises(UsageError): + _ip.run_cell_magic('doesntexist', 'line', 'cell') + + # ensure result isn't success when a magic isn't found + result = _ip.run_cell('%%doesntexist') + assert isinstance(result.error_in_exec, UsageError) + + +def test_magic_error_status(): + def fail(shell): + 1/0 + _ip.register_magic_function(fail) + result = _ip.run_cell('%fail') + assert isinstance(result.error_in_exec, ZeroDivisionError) + + def test_config(): """ test that config magic does not raise can happen if Configurable init is moved too early into