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.
This commit is contained in:
Serhiy Storchaka 2020-09-22 16:16:46 +03:00 committed by GitHub
parent c5cb077ab3
commit 557b9a52ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 0 deletions

View File

@ -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()"

View File

@ -72,6 +72,7 @@ def inner(_it, _timer{init}):
_t0 = _timer()
for _i in _it:
{stmt}
pass
_t1 = _timer()
return _t1 - _t0
"""

View File

@ -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.