[Python-checkins] cpython (merge 3.4 -> default): Issue #18518: timeit now rejects statements which can't be compiled outside

serhiy.storchaka python-checkins at python.org
Mon Jan 26 11:15:32 CET 2015


https://hg.python.org/cpython/rev/b0a686260b5d
changeset:   94305:b0a686260b5d
parent:      94297:c347c21e5afa
parent:      94304:a5769fa55791
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Mon Jan 26 12:09:59 2015 +0200
summary:
  Issue #18518: timeit now rejects statements which can't be compiled outside
a function or a loop (e.g. "return" or "break").

files:
  Doc/library/timeit.rst  |   6 ------
  Lib/test/test_timeit.py |  12 ++++++++++++
  Lib/timeit.py           |   6 ++++++
  Misc/NEWS               |   3 +++
  4 files changed, 21 insertions(+), 6 deletions(-)


diff --git a/Doc/library/timeit.rst b/Doc/library/timeit.rst
--- a/Doc/library/timeit.rst
+++ b/Doc/library/timeit.rst
@@ -69,12 +69,6 @@
    .. versionchanged:: 3.5
       The optional *globals* parameter was added.
 
-   .. note::
-
-        Because :meth:`.timeit` is executing *stmt*, placing a return statement
-        in *stmt* will prevent :meth:`.timeit` from returning execution time.
-        It will instead return the data specified by your return statement.
-
 
 .. function:: repeat(stmt='pass', setup='pass', timer=<default timer>, repeat=3, number=1000000, globals=None)
 
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
@@ -73,9 +73,21 @@
 
     def test_timer_invalid_stmt(self):
         self.assertRaises(ValueError, timeit.Timer, stmt=None)
+        self.assertRaises(SyntaxError, timeit.Timer, stmt='return')
+        self.assertRaises(SyntaxError, timeit.Timer, stmt='yield')
+        self.assertRaises(SyntaxError, timeit.Timer, stmt='yield from ()')
+        self.assertRaises(SyntaxError, timeit.Timer, stmt='break')
+        self.assertRaises(SyntaxError, timeit.Timer, stmt='continue')
+        self.assertRaises(SyntaxError, timeit.Timer, stmt='from timeit import *')
 
     def test_timer_invalid_setup(self):
         self.assertRaises(ValueError, timeit.Timer, setup=None)
+        self.assertRaises(SyntaxError, timeit.Timer, setup='return')
+        self.assertRaises(SyntaxError, timeit.Timer, setup='yield')
+        self.assertRaises(SyntaxError, timeit.Timer, setup='yield from ()')
+        self.assertRaises(SyntaxError, timeit.Timer, setup='break')
+        self.assertRaises(SyntaxError, timeit.Timer, setup='continue')
+        self.assertRaises(SyntaxError, timeit.Timer, setup='from timeit import *')
 
     fake_setup = "import timeit; timeit._fake_timer.setup()"
     fake_stmt = "import timeit; timeit._fake_timer.inc()"
diff --git a/Lib/timeit.py b/Lib/timeit.py
--- a/Lib/timeit.py
+++ b/Lib/timeit.py
@@ -115,6 +115,12 @@
         local_ns = {}
         global_ns = _globals() if globals is None else globals
         if isinstance(stmt, str):
+            # Check that the code can be compiled outside a function
+            if isinstance(setup, str):
+                compile(setup, dummy_src_name, "exec")
+                compile(setup + '\n' + stmt, dummy_src_name, "exec")
+            else:
+                compile(stmt, dummy_src_name, "exec")
             stmt = reindent(stmt, 8)
             if isinstance(setup, str):
                 setup = reindent(setup, 4)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -218,6 +218,9 @@
 Library
 -------
 
+- Issue #18518: timeit now rejects statements which can't be compiled outside
+  a function or a loop (e.g. "return" or "break").
+
 - Issue #23094: Fixed readline with frames in Python implementation of pickle.
 
 - Issue #23268: Fixed bugs in the comparison of ipaddress classes.

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


More information about the Python-checkins mailing list