summaryrefslogtreecommitdiffstats
path: root/Lib/timeit.py
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 /Lib/timeit.py
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").
Diffstat (limited to 'Lib/timeit.py')
-rwxr-xr-xLib/timeit.py6
1 files changed, 6 insertions, 0 deletions
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)