Skip to content

Commit

Permalink
Merge pull request #11812 from Carreau/fix-time
Browse files Browse the repository at this point in the history
Fix time magic,
  • Loading branch information
Carreau committed Jul 3, 2019
2 parents 223d17f + 98e3997 commit 147ab7f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
15 changes: 7 additions & 8 deletions IPython/core/magics/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -1281,13 +1281,11 @@ def time(self,line='', cell=None, local_ns=None):
mode = 'exec'
source = '<timed exec>'
# multi-line %%time case
if len(expr_ast.body) > 1 :
expr_val=expr_ast.body[-1]
code_val = self.shell.compile(ast.Expression(expr_val.value)
, '<timed eval>'
, 'eval')
expr_ast=expr_ast.body[:-1]
if len(expr_ast.body) > 1 and isinstance(expr_ast.body[-1], ast.Expr):
expr_val= expr_ast.body[-1]
expr_ast = expr_ast.body[:-1]
expr_ast = Module(expr_ast, [])
expr_val = ast.Expression(expr_val.value)

t0 = clock()
code = self.shell.compile(expr_ast, source, mode)
Expand All @@ -1312,8 +1310,9 @@ def time(self,line='', cell=None, local_ns=None):
exec(code, glob, local_ns)
out=None
# multi-line %%time case
if expr_val and isinstance(expr_val, ast.Expr):
out = eval(code_val, glob, local_ns)
if expr_val is not None:
code_2 = self.shell.compile(expr_val, source, 'eval')
out = eval(code_2, glob, local_ns)
except:
self.shell.showtraceback()
return
Expand Down
9 changes: 9 additions & 0 deletions IPython/core/tests/test_magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,15 @@ def test_time():
with tt.AssertPrints("hihi", suppress=False):
ip.run_cell("f('hi')")

def test_time_last_not_expression():
ip.run_cell("%%time\n"
"var_1 = 1\n"
"var_2 = 2\n")
assert ip.user_ns['var_1'] == 1
del ip.user_ns['var_1']
assert ip.user_ns['var_2'] == 2
del ip.user_ns['var_2']


@dec.skip_win32
def test_time2():
Expand Down
11 changes: 10 additions & 1 deletion docs/source/whatsnew/version7.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
7.x Series
============

.. _version761:

IPython 7.6.1
=============

IPython 7.6.1 contain a critical bugfix in the ``%timeit`` magic, which would
crash on some inputs as a side effect of :ghpull:`11716`. See :ghpull:`11812`


.. _whatsnew760:

IPython 7.6.0
Expand All @@ -24,7 +33,7 @@ Python 3.8.
should decrease startup time. :ghpull:`11693`
- Autoreload now update the types of reloaded objects; this for example allow
pickling of reloaded objects. :ghpull:`11644`
- Fix a big where ``%%time`` magic would suppress cell output. :ghpull:`11716`
- Fix a bug where ``%%time`` magic would suppress cell output. :ghpull:`11716`


Prepare migration to pytest (instead of nose) for testing
Expand Down

0 comments on commit 147ab7f

Please sign in to comment.