[Python-checkins] cpython (2.7): Issue #5633: Fixed timeit when the statement is a string and the setup is not.

serhiy.storchaka python-checkins at python.org
Sat May 30 18:46:03 CEST 2015


https://hg.python.org/cpython/rev/14d1018940cb
changeset:   96385:14d1018940cb
branch:      2.7
parent:      96375:82490d05f3b0
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sat May 30 19:37:19 2015 +0300
summary:
  Issue #5633: Fixed timeit when the statement is a string and the setup is not.

files:
  Lib/test/test_timeit.py |  7 +++++++
  Lib/timeit.py           |  7 ++++---
  Misc/NEWS               |  2 ++
  3 files changed, 13 insertions(+), 3 deletions(-)


diff --git a/Lib/test/test_timeit.py b/Lib/test/test_timeit.py
--- a/Lib/test/test_timeit.py
+++ b/Lib/test/test_timeit.py
@@ -120,6 +120,9 @@
     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 @@
         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)
diff --git a/Lib/timeit.py b/Lib/timeit.py
--- a/Lib/timeit.py
+++ b/Lib/timeit.py
@@ -78,7 +78,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%(init)s):
     %(setup)s
     _t0 = _timer()
     for _i in _it:
@@ -132,9 +132,10 @@
             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")
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -26,6 +26,8 @@
 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.
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list