bpo-33873: Backport regrtest from master to 3.7 (GH-7935) · python/cpython@d1f9481 (original) (raw)

6 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -462,6 +462,13 @@ def run_tests(self):
462 462 or self.tests or self.ns.args)):
463 463 self.display_header()
464 464
465 +if self.ns.huntrleaks:
466 +warmup, repetitions, _ = self.ns.huntrleaks
467 +if warmup < 3:
468 +msg = ("WARNING: Running tests with --huntrleaks/-R and less than "
469 +"3 warmup repetitions can give false positives!")
470 +print(msg, file=sys.stdout, flush=True)
471 +
465 472 if self.ns.randomize:
466 473 print("Using random seed", self.ns.random_seed)
467 474
@@ -526,6 +533,15 @@ def main(self, tests=None, **kwargs):
526 533 def _main(self, tests, kwargs):
527 534 self.ns = self.parse_args(kwargs)
528 535
536 +if self.ns.huntrleaks:
537 +warmup, repetitions, _ = self.ns.huntrleaks
538 +if warmup < 1 or repetitions < 1:
539 +msg = ("Invalid values for the --huntrleaks/-R parameters. The "
540 +"number of warmups and repetitions must be at least 1 "
541 +"each (1:1).")
542 +print(msg, file=sys.stderr, flush=True)
543 +sys.exit(2)
544 +
529 545 if self.ns.slaveargs is not None:
530 546 from test.libregrtest.runtest_mp import run_tests_slave
531 547 run_tests_slave(self.ns.slaveargs)
Original file line number Diff line number Diff line change
@@ -173,9 +173,10 @@ def test_runner():
173 173 if loader.errors:
174 174 raise Exception("errors while loading tests")
175 175 support.run_unittest(tests)
176 -test_runner()
177 176 if ns.huntrleaks:
178 177 refleak = dash_R(the_module, test, test_runner, ns.huntrleaks)
178 +else:
179 +test_runner()
179 180 test_time = time.time() - start_time
180 181 post_test_cleanup()
181 182 except support.ResourceDenied as msg:
Original file line number Diff line number Diff line change
@@ -200,7 +200,7 @@ def get_running(workers):
200 200 if (ok not in (CHILD_ERROR, INTERRUPTED)
201 201 and test_time >= PROGRESS_MIN_TIME
202 202 and not regrtest.ns.pgo):
203 -text += ' (%.0f sec)' % test_time
203 +text += ' (%s)' % format_duration(test_time)
204 204 elif ok == CHILD_ERROR:
205 205 text = '%s (%s)' % (text, test_time)
206 206 running = get_running(workers)
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
1 1 import os.path
2 +import math
2 3 import textwrap
3 4
4 5
5 6 def format_duration(seconds):
6 -if seconds < 1.0:
7 - return '%.0f ms' % (seconds * 1e3)
8 -if seconds < 60.0:
9 - return '%.0f sec' % seconds
7 +ms = math.ceil(seconds * 1e3)
8 +seconds, ms = divmod(ms, 1000)
9 +minutes, seconds = divmod(seconds, 60)
10 +hours, minutes = divmod(minutes, 60)
10 11
11 -minutes, seconds = divmod(seconds, 60.0)
12 -hours, minutes = divmod(minutes, 60.0)
12 +parts = []
13 13 if hours:
14 -return '%.0f hour %.0f min' % (hours, minutes)
15 -else:
16 -return '%.0f min %.0f sec' % (minutes, seconds)
14 +parts.append('%s hour' % hours)
15 +if minutes:
16 +parts.append('%s min' % minutes)
17 +if seconds:
18 +parts.append('%s sec' % seconds)
19 +if ms:
20 +parts.append('%s ms' % ms)
21 +if not parts:
22 +return '0 ms'
23 +
24 +parts = parts[:2]
25 +return ' '.join(parts)
17 26
18 27
19 28 def removepy(names):
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@
19 19 import unittest
20 20 from test import libregrtest
21 21 from test import support
22 +from test.libregrtest import utils
22 23
23 24
24 25 Py_DEBUG = hasattr(sys, 'getobjects')
@@ -980,5 +981,29 @@ def test_bug(self):
980 981 failed=testname, rerun=testname)
981 982
982 983
984 +class TestUtils(unittest.TestCase):
985 +def test_format_duration(self):
986 +self.assertEqual(utils.format_duration(0),
987 +'0 ms')
988 +self.assertEqual(utils.format_duration(1e-9),
989 +'1 ms')
990 +self.assertEqual(utils.format_duration(10e-3),
991 +'10 ms')
992 +self.assertEqual(utils.format_duration(1.5),
993 +'1 sec 500 ms')
994 +self.assertEqual(utils.format_duration(1),
995 +'1 sec')
996 +self.assertEqual(utils.format_duration(2 * 60),
997 +'2 min')
998 +self.assertEqual(utils.format_duration(2 * 60 + 1),
999 +'2 min 1 sec')
1000 +self.assertEqual(utils.format_duration(3 * 3600),
1001 +'3 hour')
1002 +self.assertEqual(utils.format_duration(3 * 3600 + 2 * 60 + 1),
1003 +'3 hour 2 min')
1004 +self.assertEqual(utils.format_duration(3 * 3600 + 1),
1005 +'3 hour 1 sec')
1006 +
1007 +
983 1008 if __name__ == '__main__':
984 1009 unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1 +Fix a bug in ``regrtest`` that caused an extra test to run if
2 +--huntrleaks/-R was used. Exit with error in case that invalid
3 +parameters are specified to --huntrleaks/-R (at least one warmup
4 +run and one repetition must be used).