]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-40670: More reliable validation of statements in timeit.Timer. (GH-22358)
authorSerhiy Storchaka <storchaka@gmail.com>
Tue, 22 Sep 2020 13:16:46 +0000 (16:16 +0300)
committerGitHub <noreply@github.com>
Tue, 22 Sep 2020 13:16:46 +0000 (16:16 +0300)
It now accepts "empty" statements (only whitespaces and comments)
and rejects misindentent statements.

Lib/test/test_timeit.py
Lib/timeit.py
Misc/NEWS.d/next/Library/2020-09-22-14-55-34.bpo-40670.R5sm68.rst [new file with mode: 0644]

index e02d4a71a9ba7c9b32b411f95d76de91d90b92bb..72a104fc1a6790d668e2fb78e1339837351a7b81 100644 (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()"
index 6c3ec01067f2d415a87eda572b8727be383d0141..9dfd454936e6b8c790297f2020d1295fc447dde6 100755 (executable)
@@ -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 (file)
index 0000000..0436194
--- /dev/null
@@ -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.