##// END OF EJS Templates
Simplify logic of passing local scope to magics that need it....
Fernando Perez -
Show More
@@ -2023,21 +2023,21 b' class InteractiveShell(SingletonConfigurable):'
2023 if next_input:
2023 if next_input:
2024 self.set_next_input(next_input)
2024 self.set_next_input(next_input)
2025
2025
2026 magic_name, _, magic_args = arg_s.partition(' ')
2026 magic_name, _, magic_arg_s = arg_s.partition(' ')
2027 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
2027 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
2028
2028
2029 fn = self.find_magic(magic_name)
2029 fn = self.find_magic(magic_name)
2030 if fn is None:
2030 if fn is None:
2031 error("Magic function `%s` not found." % magic_name)
2031 error("Magic function `%s` not found." % magic_name)
2032 else:
2032 else:
2033 magic_args = self.var_expand(magic_args, 1)
2033 magic_arg_s = self.var_expand(magic_arg_s, 1)
2034 # Put magic args in a list so we can call with f(*a) syntax
2035 args = [magic_arg_s]
2034 # Grab local namespace if we need it:
2036 # Grab local namespace if we need it:
2035 if getattr(fn, "needs_local_scope", False):
2037 if getattr(fn, "needs_local_scope", False):
2036 self._magic_locals = sys._getframe(1).f_locals
2038 args.append(sys._getframe(1).f_locals)
2037 with self.builtin_trap:
2039 with self.builtin_trap:
2038 result = fn(magic_args)
2040 result = fn(*args)
2039 # Ensure we're not keeping object references around:
2040 self._magic_locals = {}
2041 return result
2041 return result
2042
2042
2043 def define_magic(self, magic_name, func):
2043 def define_magic(self, magic_name, func):
@@ -2017,7 +2017,7 b' Currently the magic system has the following functions:\\n"""'
2017
2017
2018 @skip_doctest
2018 @skip_doctest
2019 @needs_local_scope
2019 @needs_local_scope
2020 def magic_time(self,parameter_s = ''):
2020 def magic_time(self,parameter_s, user_locals):
2021 """Time execution of a Python statement or expression.
2021 """Time execution of a Python statement or expression.
2022
2022
2023 The CPU and wall clock times are printed, and the value of the
2023 The CPU and wall clock times are printed, and the value of the
@@ -2084,19 +2084,17 b' Currently the magic system has the following functions:\\n"""'
2084 tc = clock()-t0
2084 tc = clock()-t0
2085 # skew measurement as little as possible
2085 # skew measurement as little as possible
2086 glob = self.shell.user_ns
2086 glob = self.shell.user_ns
2087 locs = self._magic_locals
2088 clk = clock2
2089 wtime = time.time
2087 wtime = time.time
2090 # time execution
2088 # time execution
2091 wall_st = wtime()
2089 wall_st = wtime()
2092 if mode=='eval':
2090 if mode=='eval':
2093 st = clk()
2091 st = clock2()
2094 out = eval(code, glob, locs)
2092 out = eval(code, glob, user_locals)
2095 end = clk()
2093 end = clock2()
2096 else:
2094 else:
2097 st = clk()
2095 st = clock2()
2098 exec code in glob, locs
2096 exec code in glob, user_locals
2099 end = clk()
2097 end = clock2()
2100 out = None
2098 out = None
2101 wall_end = wtime()
2099 wall_end = wtime()
2102 # Compute actual times and report
2100 # Compute actual times and report
General Comments 0
You need to be logged in to leave comments. Login now