--- /usr/lib/python2.5/timeit.py 2008-03-07 05:35:55.000000000 +0100 +++ mytimeit.py 2008-04-01 09:37:15.000000000 +0200 @@ -77,7 +77,7 @@ # in Timer.__init__() depend on setup being indented 4 spaces and stmt # being indented 8 spaces. template = """ -def inner(_it, _timer): +def inner(_it, _timer, %(inject)s): %(setup)s _t0 = _timer() for _i in _it: @@ -106,15 +106,19 @@ multi-line string literals. """ - def __init__(self, stmt="pass", setup="pass", timer=default_timer): + def __init__(self, stmt="pass", setup="pass", timer=default_timer, ns=None): """Constructor. See class doc string.""" + if ns is None: + ns = {} + if callable(stmt): + ns["_func"] = stmt + stmt = "_func()" self.timer = timer stmt = reindent(stmt, 8) setup = reindent(setup, 4) - src = template % {'stmt': stmt, 'setup': setup} + src = template % {'stmt': stmt, 'setup': setup, 'inject': ','.join("%s=%s" % (s, s) for s in ns)} self.src = src # Save for traceback display code = compile(src, dummy_src_name, "exec") - ns = {} exec code in globals(), ns self.inner = ns["inner"]