From d978e37a42684a9454bc5dc561edd81dd04e8dc2 2017-08-08 16:46:43 From: Thomas Kluyver Date: 2017-08-08 16:46:43 Subject: [PATCH] Backport PR #10737: Raise UsageError on magic not found avoids reporting missing magics as succesful execution. closes 10736 --- diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index 51040bc..41b2704 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -2064,7 +2064,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 @@ -2101,7 +2101,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 fde9e71..9845cee 100644 --- a/IPython/core/tests/test_magic.py +++ b/IPython/core/tests/test_magic.py @@ -80,6 +80,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