Permalink
Browse files

Simplify logic of passing local scope to magics that need it.

Just use an argument rather than setting more global state.
  • Loading branch information...
1 parent 7e14db6 commit f26fdb6b68c1b27aaffd3523d075bba50129f88b @fperez committed May 8, 2012
Showing with 13 additions and 15 deletions.
  1. +6 −6 IPython/core/interactiveshell.py
  2. +7 −9 IPython/core/magic.py
@@ -2023,21 +2023,21 @@ def magic(self, arg_s, next_input=None):
if next_input:
self.set_next_input(next_input)
- magic_name, _, magic_args = arg_s.partition(' ')
+ magic_name, _, magic_arg_s = arg_s.partition(' ')
magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
fn = self.find_magic(magic_name)
if fn is None:
error("Magic function `%s` not found." % magic_name)
else:
- magic_args = self.var_expand(magic_args, 1)
+ magic_arg_s = self.var_expand(magic_arg_s, 1)
+ # Put magic args in a list so we can call with f(*a) syntax
+ args = [magic_arg_s]
# Grab local namespace if we need it:
if getattr(fn, "needs_local_scope", False):
- self._magic_locals = sys._getframe(1).f_locals
+ args.append(sys._getframe(1).f_locals)
with self.builtin_trap:
- result = fn(magic_args)
- # Ensure we're not keeping object references around:
- self._magic_locals = {}
+ result = fn(*args)
return result
def define_magic(self, magic_name, func):
View
@@ -2017,7 +2017,7 @@ def magic_timeit(self, parameter_s =''):
@skip_doctest
@needs_local_scope
- def magic_time(self,parameter_s = ''):
+ def magic_time(self,parameter_s, user_locals):
"""Time execution of a Python statement or expression.
The CPU and wall clock times are printed, and the value of the
@@ -2084,19 +2084,17 @@ def magic_time(self,parameter_s = ''):
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, locs)
- end = clk()
+ st = clock2()
+ out = eval(code, glob, user_locals)
+ end = clock2()
else:
- st = clk()
- exec code in glob, locs
- end = clk()
+ st = clock2()
+ exec code in glob, user_locals
+ end = clock2()
out = None
wall_end = wtime()
# Compute actual times and report

0 comments on commit f26fdb6

Please sign in to comment.