mirror of
https://github.com/python/cpython.git
synced 2025-08-28 12:45:07 +00:00
Issue #18518: timeit now rejects statements which can't be compiled outside
a function or a loop (e.g. "return" or "break").
This commit is contained in:
commit
c959b0cd30
4 changed files with 21 additions and 6 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue