[Python-checkins] bpo-40670: More reliable validation of statements in timeit.Timer. (GH-22358)
Serhiy Storchaka
webhook-mailer at python.org
Tue Sep 22 09:16:55 EDT 2020
https://github.com/python/cpython/commit/557b9a52edc4445cec293f2cc2c268c4f564adcf
commit: 557b9a52edc4445cec293f2cc2c268c4f564adcf
branch: master
author: Serhiy Storchaka <storchaka at gmail.com>
committer: GitHub <noreply at github.com>
date: 2020-09-22T16:16:46+03:00
summary:
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.
files:
A Misc/NEWS.d/next/Library/2020-09-22-14-55-34.bpo-40670.R5sm68.rst
M Lib/test/test_timeit.py
M Lib/timeit.py
diff --git a/Lib/test/test_timeit.py b/Lib/test/test_timeit.py
index e02d4a71a9ba7..72a104fc1a679 100644
--- a/Lib/test/test_timeit.py
+++ b/Lib/test/test_timeit.py
@@ -77,6 +77,9 @@ def test_timer_invalid_stmt(self):
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 @@ def test_timer_invalid_setup(self):
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 6c3ec01067f2d..9dfd454936e6b 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 0000000000000..0436194d736ab
--- /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.
More information about the Python-checkins
mailing list