]> granicus.if.org Git - python/commitdiff
Issue #18518: timeit now rejects statements which can't be compiled outside
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 26 Jan 2015 10:09:17 +0000 (12:09 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Mon, 26 Jan 2015 10:09:17 +0000 (12:09 +0200)
a function or a loop (e.g. "return" or "break").

Doc/library/timeit.rst
Lib/test/test_timeit.py
Lib/timeit.py
Misc/NEWS

index 19b5e4e7db97a83312da4c88172503c97b3b91e9..503a705fbc3898b06fec2eb8d10469624f8067e7 100644 (file)
@@ -64,12 +64,6 @@ The module defines three convenience functions and a public class:
    Create a :class:`Timer` instance with the given statement, *setup* code and
    *timer* function and run its :meth:`.timeit` method with *number* executions.
 
-   .. 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)
 
index 625fb8da90b589ec495713e487273423eaebaf22..09e76e023d0a31733bdfc632dfe8449e24ed6abe 100644 (file)
@@ -73,9 +73,21 @@ class TestTimeit(unittest.TestCase):
 
     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()"
index ead2030515243dea620ccdfba4f66846b702e0ee..9cec000f7397697404d769f0347656178226c1bf 100755 (executable)
@@ -109,6 +109,12 @@ class Timer:
         self.timer = timer
         ns = {}
         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)
index c30c9d589f26fe84786d1b2a60b04b8a604d2096..198092c548bac3ea44431d834cad132988b59208 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -50,6 +50,9 @@ Core and Builtins
 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.