From: Serhiy Storchaka Date: Mon, 26 Jan 2015 10:09:17 +0000 (+0200) Subject: Issue #18518: timeit now rejects statements which can't be compiled outside X-Git-Tag: v3.4.3rc1~80 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=2bef58577f1caa293a4843f4bdf245407825a61a;p=thirdparty%2FPython%2Fcpython.git 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/Doc/library/timeit.rst b/Doc/library/timeit.rst index 19b5e4e7db97..503a705fbc38 100644 --- a/Doc/library/timeit.rst +++ b/Doc/library/timeit.rst @@ -64,12 +64,6 @@ The module defines three convenience functions and a public class: Create a :class:`Timer` instance with the given statement, *setup* code and *timer* function and run its :meth:`.timeit` method with *number* executions. - .. note:: - - Because :meth:`.timeit` is executing *stmt*, placing a return statement - in *stmt* will prevent :meth:`.timeit` from returning execution time. - It will instead return the data specified by your return statement. - .. function:: repeat(stmt='pass', setup='pass', timer=, repeat=3, number=1000000) diff --git a/Lib/test/test_timeit.py b/Lib/test/test_timeit.py index 625fb8da90b5..09e76e023d0a 100644 --- a/Lib/test/test_timeit.py +++ b/Lib/test/test_timeit.py @@ -73,9 +73,21 @@ 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='yield from ()') + self.assertRaises(SyntaxError, timeit.Timer, stmt='break') + self.assertRaises(SyntaxError, timeit.Timer, stmt='continue') + self.assertRaises(SyntaxError, timeit.Timer, stmt='from timeit import *') 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='yield from ()') + self.assertRaises(SyntaxError, timeit.Timer, setup='break') + self.assertRaises(SyntaxError, timeit.Timer, setup='continue') + self.assertRaises(SyntaxError, timeit.Timer, setup='from timeit import *') fake_setup = "import timeit; timeit._fake_timer.setup()" fake_stmt = "import timeit; timeit._fake_timer.inc()" diff --git a/Lib/timeit.py b/Lib/timeit.py index ead203051524..9cec000f7397 100755 --- a/Lib/timeit.py +++ b/Lib/timeit.py @@ -109,6 +109,12 @@ class Timer: self.timer = timer ns = {} 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) diff --git a/Misc/NEWS b/Misc/NEWS index c30c9d589f26..198092c548ba 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -50,6 +50,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 #23094: Fixed readline with frames in Python implementation of pickle. - Issue #23268: Fixed bugs in the comparison of ipaddress classes.