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): ...@@ -73,9 +73,17 @@ class TestTimeit(unittest.TestCase):
def test_timer_invalid_stmt(self): def test_timer_invalid_stmt(self):
self.assertRaises(ValueError, timeit.Timer, stmt=None) 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): def test_timer_invalid_setup(self):
self.assertRaises(ValueError, timeit.Timer, setup=None) 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_setup = "import timeit; timeit._fake_timer.setup()"
fake_stmt = "import timeit; timeit._fake_timer.inc()" fake_stmt = "import timeit; timeit._fake_timer.inc()"
......
...@@ -123,6 +123,12 @@ class Timer: ...@@ -123,6 +123,12 @@ class Timer:
self.timer = timer self.timer = timer
ns = {} ns = {}
if isinstance(stmt, basestring): 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) stmt = reindent(stmt, 8)
if isinstance(setup, basestring): if isinstance(setup, basestring):
setup = reindent(setup, 4) setup = reindent(setup, 4)
......
...@@ -15,6 +15,9 @@ Core and Builtins ...@@ -15,6 +15,9 @@ Core and Builtins
Library 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 - Issue #19996: Make :mod:`httplib` ignore headers with no name rather than
assuming the body has started. 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