Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ Lib/site-packages/*
Lib/test/data/*
!Lib/test/data/README
cpython/

8 changes: 3 additions & 5 deletions Lib/test/test_timeit.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ def test_repeat_function_zero_iters(self):
def assert_exc_string(self, exc_string, expected_exc_name):
exc_lines = exc_string.splitlines()
self.assertGreater(len(exc_lines), 2)
self.assertTrue(exc_lines[0].startswith('Traceback'))
self.assertTrue(exc_lines[-1].startswith(expected_exc_name))
self.assertStartsWith(exc_lines[0], 'Traceback')
self.assertStartsWith(exc_lines[-1], expected_exc_name)

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

def test_main_verbose(self):
s = self.run_main(switches=['-v'])
Expand Down
28 changes: 15 additions & 13 deletions Lib/timeit.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#! /usr/bin/env python3

"""Tool for measuring execution time of small code snippets.

This module avoids a number of common traps for measuring execution
Expand Down Expand Up @@ -46,7 +44,6 @@
timeit(string, string) -> float
repeat(string, string) -> list
default_timer() -> float

"""

import gc
Expand Down Expand Up @@ -175,15 +172,20 @@ def timeit(self, number=default_number):
"""
it = itertools.repeat(None, number)
# XXX RUSTPYTHON TODO: gc module implementation
# gcold = gc.isenabled()
# gc.disable()
# try:
# timing = self.inner(it, self.timer)
# finally:
# if gcold:
# gc.enable()
# return timing
return self.inner(it, self.timer)
try:
gcold = gc.isenabled()
gc.disable()
except NotImplementedError:
gcold = False
try:
timing = self.inner(it, self.timer)
finally:
if gcold:
try:
gc.enable()
except NotImplementedError:
pass
return timing

def repeat(self, repeat=default_repeat, number=default_number):
"""Call timeit() a few times.
Expand Down Expand Up @@ -306,7 +308,7 @@ def main(args=None, *, _wrap_timer=None):
precision += 1
verbose += 1
if o in ("-h", "--help"):
print(__doc__, end=' ')
print(__doc__, end="")
return 0
setup = "\n".join(setup) or "pass"

Expand Down
Loading