##// END OF EJS Templates
Merge pull request #11716 from Pragnya02/fix_magic_time...
Matthias Bussonnier -
r25046:de0abd8b merge
parent child Browse files
Show More
@@ -298,7 +298,7 b' class DisplayHook(Configurable):'
298 self._, self.__, self.___ = '', '', ''
298 self._, self.__, self.___ = '', '', ''
299
299
300 if '_' not in builtin_mod.__dict__:
300 if '_' not in builtin_mod.__dict__:
301 self.shell.user_ns.update({'_':None,'__':None, '___':None})
301 self.shell.user_ns.update({'_':self._,'__':self.__,'___':self.___})
302 import gc
302 import gc
303 # TODO: Is this really needed?
303 # TODO: Is this really needed?
304 # IronPython blocks here forever
304 # IronPython blocks here forever
@@ -49,6 +49,14 b' from warnings import warn'
49 from logging import error
49 from logging import error
50 from io import StringIO
50 from io import StringIO
51
51
52 if sys.version_info > (3,8):
53 from ast import Module
54 else :
55 # mock the new API, ignore second argument
56 # see https://github.com/ipython/ipython/issues/11590
57 from ast import Module as OriginalModule
58 Module = lambda nodelist, type_ignores: OriginalModule(nodelist)
59
52
60
53 #-----------------------------------------------------------------------------
61 #-----------------------------------------------------------------------------
54 # Magic implementation classes
62 # Magic implementation classes
@@ -1261,6 +1269,7 b' python-profiler package from non-free.""")'
1261 # Minimum time above which compilation time will be reported
1269 # Minimum time above which compilation time will be reported
1262 tc_min = 0.1
1270 tc_min = 0.1
1263
1271
1272 expr_val=None
1264 if len(expr_ast.body)==1 and isinstance(expr_ast.body[0], ast.Expr):
1273 if len(expr_ast.body)==1 and isinstance(expr_ast.body[0], ast.Expr):
1265 mode = 'eval'
1274 mode = 'eval'
1266 source = '<timed eval>'
1275 source = '<timed eval>'
@@ -1268,6 +1277,15 b' python-profiler package from non-free.""")'
1268 else:
1277 else:
1269 mode = 'exec'
1278 mode = 'exec'
1270 source = '<timed exec>'
1279 source = '<timed exec>'
1280 # multi-line %%time case
1281 if len(expr_ast.body) > 1 :
1282 expr_val=expr_ast.body[-1]
1283 code_val = self.shell.compile(ast.Expression(expr_val.value)
1284 , '<timed eval>'
1285 , 'eval')
1286 expr_ast=expr_ast.body[:-1]
1287 expr_ast = Module(expr_ast, [])
1288
1271 t0 = clock()
1289 t0 = clock()
1272 code = self.shell.compile(expr_ast, source, mode)
1290 code = self.shell.compile(expr_ast, source, mode)
1273 tc = clock()-t0
1291 tc = clock()-t0
@@ -1289,11 +1307,15 b' python-profiler package from non-free.""")'
1289 st = clock2()
1307 st = clock2()
1290 try:
1308 try:
1291 exec(code, glob, local_ns)
1309 exec(code, glob, local_ns)
1310 out=None
1311 # multi-line %%time case
1312 if expr_val and isinstance(expr_val, ast.Expr):
1313 out = eval(code_val, glob, local_ns)
1292 except:
1314 except:
1293 self.shell.showtraceback()
1315 self.shell.showtraceback()
1294 return
1316 return
1295 end = clock2()
1317 end = clock2()
1296 out = None
1318
1297 wall_end = wtime()
1319 wall_end = wtime()
1298 # Compute actual times and report
1320 # Compute actual times and report
1299 wall_time = wall_end-wall_st
1321 wall_time = wall_end-wall_st
@@ -9,6 +9,7 b' import os'
9 import re
9 import re
10 import sys
10 import sys
11 import warnings
11 import warnings
12 from textwrap import dedent
12 from unittest import TestCase
13 from unittest import TestCase
13 from importlib import invalidate_caches
14 from importlib import invalidate_caches
14 from io import StringIO
15 from io import StringIO
@@ -419,6 +420,19 b' def test_time3():'
419 "run = 0\n"
420 "run = 0\n"
420 "run += 1")
421 "run += 1")
421
422
423 def test_multiline_time():
424 """Make sure last statement from time return a value."""
425 ip = get_ipython()
426 ip.user_ns.pop('run', None)
427
428 ip.run_cell(dedent("""\
429 %%time
430 a = "ho"
431 b = "hey"
432 a+b
433 """))
434 nt.assert_equal(ip.user_ns_hidden['_'], 'hohey')
435
422 def test_time_local_ns():
436 def test_time_local_ns():
423 """
437 """
424 Test that local_ns is actually global_ns when running a cell magic
438 Test that local_ns is actually global_ns when running a cell magic
General Comments 0
You need to be logged in to leave comments. Login now