diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2020-09-22 13:16:46 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-22 13:16:46 (GMT) |
commit | 557b9a52edc4445cec293f2cc2c268c4f564adcf (patch) | |
tree | dc197fd8a194d1eb5fb31eb6f12736efa09ff7e7 | |
parent | c5cb077ab3c88394b7ac8ed4e746bd31b53e39f1 (diff) | |
download | cpython-557b9a52edc4445cec293f2cc2c268c4f564adcf.zip cpython-557b9a52edc4445cec293f2cc2c268c4f564adcf.tar.gz cpython-557b9a52edc4445cec293f2cc2c268c4f564adcf.tar.bz2 |
bpo-40670: More reliable validation of statements in timeit.Timer. (GH-22358)
It now accepts "empty" statements (only whitespaces and comments)
and rejects misindentent statements.
-rw-r--r-- | Lib/test/test_timeit.py | 9 | ||||
-rwxr-xr-x | Lib/timeit.py | 1 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2020-09-22-14-55-34.bpo-40670.R5sm68.rst | 3 |
3 files changed, 13 insertions, 0 deletions
diff --git a/Lib/test/test_timeit.py b/Lib/test/test_timeit.py index e02d4a7..72a104f 100644 --- a/Lib/test/test_timeit.py +++ b/Lib/test/test_timeit.py @@ -77,6 +77,9 @@ class TestTimeit(unittest.TestCase): self.assertRaises(SyntaxError, timeit.Timer, stmt='break') self.assertRaises(SyntaxError, timeit.Timer, stmt='continue') self.assertRaises(SyntaxError, timeit.Timer, stmt='from timeit import *') + self.assertRaises(SyntaxError, timeit.Timer, stmt=' pass') + self.assertRaises(SyntaxError, timeit.Timer, + setup='while False:\n pass', stmt=' break') def test_timer_invalid_setup(self): self.assertRaises(ValueError, timeit.Timer, setup=None) @@ -86,6 +89,12 @@ class TestTimeit(unittest.TestCase): self.assertRaises(SyntaxError, timeit.Timer, setup='break') self.assertRaises(SyntaxError, timeit.Timer, setup='continue') self.assertRaises(SyntaxError, timeit.Timer, setup='from timeit import *') + self.assertRaises(SyntaxError, timeit.Timer, setup=' pass') + + def test_timer_empty_stmt(self): + timeit.Timer(stmt='') + timeit.Timer(stmt=' \n\t\f') + timeit.Timer(stmt='# comment') fake_setup = "import timeit\ntimeit._fake_timer.setup()" fake_stmt = "import timeit\ntimeit._fake_timer.inc()" diff --git a/Lib/timeit.py b/Lib/timeit.py index 6c3ec01..9dfd454 100755 --- a/Lib/timeit.py +++ b/Lib/timeit.py @@ -72,6 +72,7 @@ def inner(_it, _timer{init}): _t0 = _timer() for _i in _it: {stmt} + pass _t1 = _timer() return _t1 - _t0 """ diff --git a/Misc/NEWS.d/next/Library/2020-09-22-14-55-34.bpo-40670.R5sm68.rst b/Misc/NEWS.d/next/Library/2020-09-22-14-55-34.bpo-40670.R5sm68.rst new file mode 100644 index 0000000..0436194 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-09-22-14-55-34.bpo-40670.R5sm68.rst @@ -0,0 +1,3 @@ +More reliable validation of statements in :class:`timeit.Timer`. It now +accepts "empty" statements (only whitespaces and comments) and rejects +misindentent statements. |