Issue #18518: timeit now rejects statements which can't be compiled outside
a function or a loop (e.g. "return" or "break").
diff --git a/Lib/timeit.py b/Lib/timeit.py
index ef246ce..a8992f8 100755
--- a/Lib/timeit.py
+++ b/Lib/timeit.py
@@ -123,6 +123,12 @@
self.timer = timer
ns = {}
if isinstance(stmt, basestring):
+ # Check that the code can be compiled outside a function
+ if isinstance(setup, basestring):
+ 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, basestring):
setup = reindent(setup, 4)