diff --git a/IPython/core/magics/execution.py b/IPython/core/magics/execution.py index 877e7cb..3734b0c 100644 --- a/IPython/core/magics/execution.py +++ b/IPython/core/magics/execution.py @@ -1013,6 +1013,13 @@ python-profiler package from non-free.""") ast_setup = self.shell.transform_ast(ast_setup) ast_stmt = self.shell.transform_ast(ast_stmt) + # Check that these compile to valid Python code *outside* the timer func + # Invalid code may become valid when put inside the function & loop, + # which messes up error messages. + # https://github.com/ipython/ipython/issues/10636 + self.shell.compile(ast_setup, "", "exec") + self.shell.compile(ast_stmt, "", "exec") + # This codestring is taken from timeit.template - we fill it in as an # AST, so that we can apply our AST transformations to the user code # without affecting the timing code. diff --git a/IPython/core/tests/test_magic.py b/IPython/core/tests/test_magic.py index eb40331..fde9e71 100644 --- a/IPython/core/tests/test_magic.py +++ b/IPython/core/tests/test_magic.py @@ -583,6 +583,9 @@ def test_timeit_futures(): ip.compile.reset_compiler_flags() with tt.AssertNotPrints('0.25'): ip.run_line_magic('timeit', '-n1 -r1 print(1/4)') +def test_timeit_invalid_return(): + with nt.assert_raises_regexp(SyntaxError, "outside function"): + _ip.run_line_magic('timeit', 'return') @dec.skipif(execution.profile is None) def test_prun_special_syntax():