summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-01-26 10:09:59 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-01-26 10:09:59 (GMT)
commitc959b0cd3061bdad445f839c13ecb69e86ec0b9c (patch)
tree63a4f4ca444f492aa2a28aef8c6d77c52ad1ed03
parent230586739cc090ededbe45f101d8df75e5772455 (diff)
parent2bef58577f1caa293a4843f4bdf245407825a61a (diff)
downloadcpython-c959b0cd3061bdad445f839c13ecb69e86ec0b9c.zip
cpython-c959b0cd3061bdad445f839c13ecb69e86ec0b9c.tar.gz
cpython-c959b0cd3061bdad445f839c13ecb69e86ec0b9c.tar.bz2
Issue #18518: timeit now rejects statements which can't be compiled outside
a function or a loop (e.g. "return" or "break").
-rw-r--r--Doc/library/timeit.rst6
-rw-r--r--Lib/test/test_timeit.py12
-rwxr-xr-xLib/timeit.py6
-rw-r--r--Misc/NEWS3
4 files changed, 21 insertions, 6 deletions
diff --git a/Doc/library/timeit.rst b/Doc/library/timeit.rst
index dea1ba7..17588bd 100644
--- a/Doc/library/timeit.rst
+++ b/Doc/library/timeit.rst
@@ -69,12 +69,6 @@ The module defines three convenience functions and a public class:
.. 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
index b3a96bb..267d2c8 100644
--- a/Lib/test/test_timeit.py
+++ b/Lib/test/test_timeit.py
@@ -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()"
diff --git a/Lib/timeit.py b/Lib/timeit.py
index 5971d37..3807794 100755
--- a/Lib/timeit.py
+++ b/Lib/timeit.py
@@ -115,6 +115,12 @@ class Timer:
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
index 15f763c..45d27be 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -218,6 +218,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.