diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index 7e91912..b5433ae 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -22,6 +22,7 @@ import __future__ import abc import atexit import codeop +import inspect import os import re import sys @@ -1727,6 +1728,10 @@ class InteractiveShell(Configurable, Magic): valid Python code you can type at the interpreter, including loops and compound statements. """ + # Save the scope of the call so magic functions like %time can + # evaluate expressions in it. + self._magic_locals = inspect.stack()[1][0].f_locals + args = arg_s.split(' ',1) magic_name = args[0] magic_name = magic_name.lstrip(prefilter.ESC_MAGIC) diff --git a/IPython/core/magic.py b/IPython/core/magic.py index 3e8d117..593fb4c 100644 --- a/IPython/core/magic.py +++ b/IPython/core/magic.py @@ -1928,17 +1928,18 @@ Currently the magic system has the following functions:\n""" tc = clock()-t0 # skew measurement as little as possible glob = self.shell.user_ns + locs = self._magic_locals clk = clock2 wtime = time.time # time execution wall_st = wtime() if mode=='eval': st = clk() - out = eval(code,glob) + out = eval(code, glob, locs) end = clk() else: st = clk() - exec code in glob + exec code in glob, locs end = clk() out = None wall_end = wtime()