]> git.ipfire.org Git - thirdparty/Python/cpython.git/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:38:26 +0000 (19:38 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Sat, 30 May 2015 16:38:26 +0000 (19:38 +0300)
Lib/test/test_timeit.py
Lib/timeit.py
Misc/NEWS

index 09e76e023d0a31733bdfc632dfe8449e24ed6abe..918a294c10f6a1c9d89f75d0cd20da5a4e6cbdf6 100644 (file)
@@ -124,6 +124,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)
@@ -173,6 +176,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 cf7446d8c1f50ef93a63c6c59cb6ec7630b4cdfe..0b1c601c97420c539a5e65a6ffab8b057ae9a747 100755 (executable)
@@ -65,7 +65,7 @@ default_timer = time.perf_counter
 # 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}):
     {setup}
     _t0 = _timer()
     for _i in _it:
@@ -119,9 +119,10 @@ class Timer:
             stmt = reindent(stmt, 8)
             if isinstance(setup, str):
                 setup = reindent(setup, 4)
-                src = template.format(stmt=stmt, setup=setup)
+                src = template.format(stmt=stmt, setup=setup, init='')
             elif callable(setup):
-                src = template.format(stmt=stmt, setup='_setup()')
+                src = template.format(stmt=stmt, setup='_setup()',
+                                      init=', _setup=_setup')
                 ns['_setup'] = setup
             else:
                 raise ValueError("setup is neither a string nor callable")
index 0a4eadb7089f5c0be8183eec587b38f778126a50..dada469adfbd5cd40a9bd3f8028e751eff8bd3df 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -60,6 +60,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.