8000 bpo-40670: More reliable validation of statements in timeit.Timer. (G… · python/cpython@557b9a5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 557b9a5

Browse files
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.
1 parent c5cb077 commit 557b9a5

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

Lib/test/test_timeit.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ def test_timer_invalid_stmt(self):
7777
self.assertRaises(SyntaxError, timeit.Timer, stmt='break')
7878
self.assertRaises(SyntaxError, timeit.Timer, stmt='continue')
7979
self.assertRaises(SyntaxError, timeit.Timer, stmt='from timeit import *')
80+
self.assertRaises(SyntaxError, timeit.Timer, stmt=' pass')
81+
self.assertRaises(SyntaxError, timeit.Timer,
82+
setup='while False:\n pass', stmt=' break')
8083

8184
def test_timer_invalid_setup(self):
8285
self.assertRaises(ValueError, timeit.Timer, setup=None)
@@ -86,6 +89,12 @@ def test_timer_invalid_setup(self):
8689
self.assertRaises(SyntaxError, timeit.Timer, setup='break')
8790
self.assertRaises(SyntaxError, timeit.Timer, setup='continue')
8891
self.assertRaises(SyntaxError, timeit.Timer, setup='from timeit import *')
92+
self.assertRaises(SyntaxError, timeit.Timer, setup=' pass')
93+
94+
def test_timer_empty_stmt(self):
95+
timeit.Timer(stmt='')
96+
timeit.Timer(stmt=' \n\t\f')
97+
timeit.Timer(stmt='# comment')
8998

9099
fake_setup = "import timeit\ntimeit._fake_timer.setup()"
91100
fake_stmt = "import timeit\ntimeit._fake_timer.inc()"

Lib/timeit.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def inner(_it, _timer{init}):
7272
_t0 = _timer()
7373
for _i in _it:
7474
{stmt}
75+
pass
7576
_t1 = _timer()
7677
return _t1 - _t0
7778
"""
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
More reliable validation of statements in :class:`timeit.Timer`. It now
2+
accepts "empty" statements (only whitespaces and comments) and rejects
3+
misindentent statements.

0 commit comments

Comments
 (0)
0