]> granicus.if.org Git - python/commitdiff
Issue #5633: Fixed timeit when the statement is a string and the setup is not.
authorSerhiy Storchaka <storchaka@gmail.com>
Sat, 30 May 2015 16:37:19 +0000 (19:37 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Sat, 30 May 2015 16:37:19 +0000 (19:37 +0300)
Lib/test/test_timeit.py
Lib/timeit.py
Misc/NEWS

index a084b68b3f3ec2559dee44c7143bc4aec630c922..3a3359c9699b4855ac0fba89ec9de35c96b72654 100644 (file)
@@ -120,6 +120,9 @@ class TestTimeit(unittest.TestCase):
     def test_timeit_callable_stmt(self):
         self.timeit(self.fake_callable_stmt, self.fake_setup, number=3)
 
+    def test_timeit_callable_setup(self):
+        self.timeit(self.fake_stmt, self.fake_callable_setup, number=3)
+
     def test_timeit_callable_stmt_and_setup(self):
         self.timeit(self.fake_callable_stmt,
                 self.fake_callable_setup, number=3)
@@ -169,6 +172,10 @@ class TestTimeit(unittest.TestCase):
         self.repeat(self.fake_callable_stmt, self.fake_setup,
                 repeat=3, number=5)
 
+    def test_repeat_callable_setup(self):
+        self.repeat(self.fake_stmt, self.fake_callable_setup,
+                repeat=3, number=5)
+
     def test_repeat_callable_stmt_and_setup(self):
         self.repeat(self.fake_callable_stmt, self.fake_callable_setup,
                 repeat=3, number=5)
index a8992f8d771f8a1de520521337e269e44ef3004f..bf0301e66317dcdf2459239a4f64cad2d0f56221 100755 (executable)
@@ -78,7 +78,7 @@ else:
 # 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%(init)s):
     %(setup)s
     _t0 = _timer()
     for _i in _it:
@@ -132,9 +132,10 @@ class Timer:
             stmt = reindent(stmt, 8)
             if isinstance(setup, basestring):
                 setup = reindent(setup, 4)
-                src = template % {'stmt': stmt, 'setup': setup}
+                src = template % {'stmt': stmt, 'setup': setup, 'init': ''}
             elif hasattr(setup, '__call__'):
-                src = template % {'stmt': stmt, 'setup': '_setup()'}
+                src = template % {'stmt': stmt, 'setup': '_setup()',
+                                  'init': ', _setup=_setup'}
                 ns['_setup'] = setup
             else:
                 raise ValueError("setup is neither a string nor callable")
index a2a14d6c6dff639bdbbdb0fada422ff788e3a4f9..1fd5f842051e25b4c87cc37e066dae426d88825b 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -26,6 +26,8 @@ Core and Builtins
 Library
 -------
 
+- Issue #5633: Fixed timeit when the statement is a string and the setup is not.
+
 - Issue #24326: Fixed audioop.ratecv() with non-default weightB argument.
   Original patch by David Moore.