Commit e82e14e2 authored by Serhiy Storchaka's avatar Serhiy Storchaka

Issue #18518: timeit now rejects statements which can't be compiled outside

a function or a loop (e.g. "return" or "break").
parent 6ff3e37a
......@@ -73,9 +73,17 @@ 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='break')
self.assertRaises(SyntaxError, timeit.Timer, stmt='continue')
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='break')
self.assertRaises(SyntaxError, timeit.Timer, setup='continue')
fake_setup = "import timeit; timeit._fake_timer.setup()"
fake_stmt = "import timeit; timeit._fake_timer.inc()"
......
......@@ -123,6 +123,12 @@ class Timer:
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)
......
......@@ -15,6 +15,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 #19996: Make :mod:`httplib` ignore headers with no name rather than
assuming the body has started.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment