Skip to content

Commit 568f24c

Browse files
Copilotyouknowone
andauthored
Upgrade timeit module from Python 3.14.2 (#6854)
--------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: youknowone <69878+youknowone@users.noreply.github.com>
1 parent 51701ab commit 568f24c

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ Lib/site-packages/*
2727
Lib/test/data/*
2828
!Lib/test/data/README
2929
cpython/
30+

Lib/test/test_timeit.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,8 @@ def test_repeat_function_zero_iters(self):
222222
def assert_exc_string(self, exc_string, expected_exc_name):
223223
exc_lines = exc_string.splitlines()
224224
self.assertGreater(len(exc_lines), 2)
225-
self.assertTrue(exc_lines[0].startswith('Traceback'))
226-
self.assertTrue(exc_lines[-1].startswith(expected_exc_name))
225+
self.assertStartsWith(exc_lines[0], 'Traceback')
226+
self.assertStartsWith(exc_lines[-1], expected_exc_name)
227227

228228
def test_print_exc(self):
229229
s = io.StringIO()
@@ -297,9 +297,7 @@ def test_main_negative_reps(self):
297297
@unittest.skipIf(sys.flags.optimize >= 2, "need __doc__")
298298
def test_main_help(self):
299299
s = self.run_main(switches=['-h'])
300-
# Note: It's not clear that the trailing space was intended as part of
301-
# the help text, but since it's there, check for it.
302-
self.assertEqual(s, timeit.__doc__ + ' ')
300+
self.assertEqual(s, timeit.__doc__)
303301

304302
def test_main_verbose(self):
305303
s = self.run_main(switches=['-v'])

Lib/timeit.py

100755100644
Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#! /usr/bin/env python3
2-
31
"""Tool for measuring execution time of small code snippets.
42
53
This module avoids a number of common traps for measuring execution
@@ -46,7 +44,6 @@
4644
timeit(string, string) -> float
4745
repeat(string, string) -> list
4846
default_timer() -> float
49-
5047
"""
5148

5249
import gc
@@ -175,15 +172,20 @@ def timeit(self, number=default_number):
175172
"""
176173
it = itertools.repeat(None, number)
177174
# XXX RUSTPYTHON TODO: gc module implementation
178-
# gcold = gc.isenabled()
179-
# gc.disable()
180-
# try:
181-
# timing = self.inner(it, self.timer)
182-
# finally:
183-
# if gcold:
184-
# gc.enable()
185-
# return timing
186-
return self.inner(it, self.timer)
175+
try:
176+
gcold = gc.isenabled()
177+
gc.disable()
178+
except NotImplementedError:
179+
gcold = False
180+
try:
181+
timing = self.inner(it, self.timer)
182+
finally:
183+
if gcold:
184+
try:
185+
gc.enable()
186+
except NotImplementedError:
187+
pass
188+
return timing
187189

188190
def repeat(self, repeat=default_repeat, number=default_number):
189191
"""Call timeit() a few times.
@@ -306,7 +308,7 @@ def main(args=None, *, _wrap_timer=None):
306308
precision += 1
307309
verbose += 1
308310
if o in ("-h", "--help"):
309-
print(__doc__, end=' ')
311+
print(__doc__, end="")
310312
return 0
311313
setup = "\n".join(setup) or "pass"
312314

0 commit comments

Comments
 (0)