##// END OF EJS Templates
- Close %timeit bug reported by Stefan.
fptest -
Show More
@@ -1,7 +1,7 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Magic functions for InteractiveShell.
3 3
4 $Id: Magic.py 1787 2006-09-27 06:56:29Z fperez $"""
4 $Id: Magic.py 1815 2006-10-10 04:46:24Z fptest $"""
5 5
6 6 #*****************************************************************************
7 7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
@@ -62,6 +62,17 b' def on_off(tag):'
62 62
63 63 class Bunch: pass
64 64
65 def arg_split(s,posix=True):
66 """Split a command line's arguments in a shell-like manner.
67
68 This is a modified version of the standard library's shlex.split()
69 function, but with a default of posix=False for splitting, so that quotes
70 in inputs are respected."""
71
72 lex = shlex.shlex(s, posix=posix)
73 lex.whitespace_split = True
74 return list(lex)
75
65 76 #***************************************************************************
66 77 # Main class implementing Magic functionality
67 78 class Magic:
@@ -301,8 +312,12 b' license. To use profiling, please install"python2.3-profiler" from non-free.""")'
301 312 returned as a list (split on whitespace) instead of a string.
302 313
303 314 -list_all: put all option values in lists. Normally only options
304 appearing more than once are put in a list."""
315 appearing more than once are put in a list.
305 316
317 -posix (True): whether to split the input line in POSIX mode or not,
318 as per the conventions outlined in the shlex module from the
319 standard library."""
320
306 321 # inject default options at the beginning of the input line
307 322 caller = sys._getframe(1).f_code.co_name.replace('magic_','')
308 323 arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)
@@ -312,6 +327,7 b' license. To use profiling, please install"python2.3-profiler" from non-free.""")'
312 327 raise ValueError,'incorrect mode given: %s' % mode
313 328 # Get options
314 329 list_all = kw.get('list_all',0)
330 posix = kw.get('posix',True)
315 331
316 332 # Check if we have more than one argument to warrant extra processing:
317 333 odict = {} # Dictionary with options
@@ -319,7 +335,7 b' license. To use profiling, please install"python2.3-profiler" from non-free.""")'
319 335 if len(args) >= 1:
320 336 # If the list of inputs only has 0 or 1 thing in it, there's no
321 337 # need to look for options
322 argv = shlex.split(arg_str)
338 argv = arg_split(arg_str,posix)
323 339 # Do regular option processing
324 340 try:
325 341 opts,args = getopt(argv,opt_str,*long_opts)
@@ -1636,19 +1652,22 b' Currently the magic system has the following functions:\\n"""'
1636 1652 1 loops, best of 3: 2 s per loop
1637 1653
1638 1654
1639 The times reported by %timeit will be slightly higher than those reported
1640 by the timeit.py script when variables are accessed. This is due to the
1641 fact that %timeit executes the statement in the namespace of the shell,
1642 compared with timeit.py, which uses a single setup statement to import
1643 function or create variables. Generally, the bias does not matter as long
1644 as results from timeit.py are not mixed with those from %timeit."""
1655 The times reported by %timeit will be slightly higher than those
1656 reported by the timeit.py script when variables are accessed. This is
1657 due to the fact that %timeit executes the statement in the namespace
1658 of the shell, compared with timeit.py, which uses a single setup
1659 statement to import function or create variables. Generally, the bias
1660 does not matter as long as results from timeit.py are not mixed with
1661 those from %timeit."""
1662
1645 1663 import timeit
1646 1664 import math
1647 1665
1648 1666 units = ["s", "ms", "\xc2\xb5s", "ns"]
1649 1667 scaling = [1, 1e3, 1e6, 1e9]
1650 1668
1651 opts, stmt = self.parse_options(parameter_s,'n:r:tcp:')
1669 opts, stmt = self.parse_options(parameter_s,'n:r:tcp:',
1670 posix=False)
1652 1671 if stmt == "":
1653 1672 return
1654 1673 timefunc = timeit.default_timer
@@ -1665,7 +1684,8 b' Currently the magic system has the following functions:\\n"""'
1665 1684 # but is there a better way to achieve that the code stmt has access
1666 1685 # to the shell namespace?
1667 1686
1668 src = timeit.template % {'stmt': timeit.reindent(stmt, 8), 'setup': "pass"}
1687 src = timeit.template % {'stmt': timeit.reindent(stmt, 8),
1688 'setup': "pass"}
1669 1689 code = compile(src, "<magic-timeit>", "exec")
1670 1690 ns = {}
1671 1691 exec code in self.shell.user_ns, ns
@@ -1,3 +1,10 b''
1 2006-10-09 Fernando Perez <Fernando.Perez@colorado.edu>
2
3 * IPython/Magic.py (Magic.parse_options): add a new posix option
4 to allow parsing of input args in magics that doesn't strip quotes
5 (if posix=False). This also closes %timeit bug reported by
6 Stefan.
7
1 8 2006-10-03 Ville Vainio <vivainio@gmail.com>
2 9
3 10 * iplib.py (raw_input, interact): Return ValueError catching for
General Comments 0
You need to be logged in to leave comments. Login now