Show More
@@ -22,6 +22,7 b' import __future__' | |||||
22 | import abc |
|
22 | import abc | |
23 | import atexit |
|
23 | import atexit | |
24 | import codeop |
|
24 | import codeop | |
|
25 | import inspect | |||
25 | import os |
|
26 | import os | |
26 | import re |
|
27 | import re | |
27 | import sys |
|
28 | import sys | |
@@ -1740,9 +1741,14 b' class InteractiveShell(Configurable, Magic):' | |||||
1740 | error("Magic function `%s` not found." % magic_name) |
|
1741 | error("Magic function `%s` not found." % magic_name) | |
1741 | else: |
|
1742 | else: | |
1742 | magic_args = self.var_expand(magic_args,1) |
|
1743 | magic_args = self.var_expand(magic_args,1) | |
|
1744 | # Grab local namespace if we need it: | |||
|
1745 | if getattr(fn, "needs_local_scope", False): | |||
|
1746 | self._magic_locals = sys._getframe(1).f_locals | |||
1743 | with nested(self.builtin_trap,): |
|
1747 | with nested(self.builtin_trap,): | |
1744 | result = fn(magic_args) |
|
1748 | result = fn(magic_args) | |
1745 | return result |
|
1749 | # Ensure we're not keeping object references around: | |
|
1750 | self._magic_locals = {} | |||
|
1751 | return result | |||
1746 |
|
1752 | |||
1747 | def define_magic(self, magicname, func): |
|
1753 | def define_magic(self, magicname, func): | |
1748 | """Expose own function as magic function for ipython |
|
1754 | """Expose own function as magic function for ipython |
@@ -85,6 +85,10 b' def compress_dhist(dh):' | |||||
85 |
|
85 | |||
86 | return newhead + tail |
|
86 | return newhead + tail | |
87 |
|
87 | |||
|
88 | def needs_local_scope(func): | |||
|
89 | """Decorator to mark magic functions which need to local scope to run.""" | |||
|
90 | func.needs_local_scope = True | |||
|
91 | return func | |||
88 |
|
92 | |||
89 | #*************************************************************************** |
|
93 | #*************************************************************************** | |
90 | # Main class implementing Magic functionality |
|
94 | # Main class implementing Magic functionality | |
@@ -1863,6 +1867,7 b' Currently the magic system has the following functions:\\n"""' | |||||
1863 | print "Compiler time: %.2f s" % tc |
|
1867 | print "Compiler time: %.2f s" % tc | |
1864 |
|
1868 | |||
1865 | @testdec.skip_doctest |
|
1869 | @testdec.skip_doctest | |
|
1870 | @needs_local_scope | |||
1866 | def magic_time(self,parameter_s = ''): |
|
1871 | def magic_time(self,parameter_s = ''): | |
1867 | """Time execution of a Python statement or expression. |
|
1872 | """Time execution of a Python statement or expression. | |
1868 |
|
1873 | |||
@@ -1928,17 +1933,18 b' Currently the magic system has the following functions:\\n"""' | |||||
1928 | tc = clock()-t0 |
|
1933 | tc = clock()-t0 | |
1929 | # skew measurement as little as possible |
|
1934 | # skew measurement as little as possible | |
1930 | glob = self.shell.user_ns |
|
1935 | glob = self.shell.user_ns | |
|
1936 | locs = self._magic_locals | |||
1931 | clk = clock2 |
|
1937 | clk = clock2 | |
1932 | wtime = time.time |
|
1938 | wtime = time.time | |
1933 | # time execution |
|
1939 | # time execution | |
1934 | wall_st = wtime() |
|
1940 | wall_st = wtime() | |
1935 | if mode=='eval': |
|
1941 | if mode=='eval': | |
1936 | st = clk() |
|
1942 | st = clk() | |
1937 | out = eval(code,glob) |
|
1943 | out = eval(code, glob, locs) | |
1938 | end = clk() |
|
1944 | end = clk() | |
1939 | else: |
|
1945 | else: | |
1940 | st = clk() |
|
1946 | st = clk() | |
1941 | exec code in glob |
|
1947 | exec code in glob, locs | |
1942 | end = clk() |
|
1948 | end = clk() | |
1943 | out = None |
|
1949 | out = None | |
1944 | wall_end = wtime() |
|
1950 | wall_end = wtime() |
@@ -274,6 +274,14 b' def doctest_time():' | |||||
274 | In [10]: %time None |
|
274 | In [10]: %time None | |
275 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
275 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s | |
276 | Wall time: 0.00 s |
|
276 | Wall time: 0.00 s | |
|
277 | ||||
|
278 | In [11]: def f(kmjy): | |||
|
279 | ....: %time print 2*kmjy | |||
|
280 | ||||
|
281 | In [12]: f(3) | |||
|
282 | 6 | |||
|
283 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s | |||
|
284 | Wall time: 0.00 s | |||
277 | """ |
|
285 | """ | |
278 |
|
286 | |||
279 |
|
287 |
General Comments 0
You need to be logged in to leave comments.
Login now