blob: 0368694b2adcb7d675fe18beea27230be98a56c1 [file] [log] [blame]
Victor Stinner4ffe9c22018-06-14 14:58:13 +02001import math
Victor Stinner4d299832019-04-26 04:08:53 +02002import os.path
3import sys
Victor Stinner7ad16eb2018-06-01 11:04:45 +02004import textwrap
Victor Stinner95f61c82019-06-13 01:09:04 +02005from test import support
Victor Stinner7ad16eb2018-06-01 11:04:45 +02006
7
8def format_duration(seconds):
Victor Stinner4ffe9c22018-06-14 14:58:13 +02009 ms = math.ceil(seconds * 1e3)
10 seconds, ms = divmod(ms, 1000)
11 minutes, seconds = divmod(seconds, 60)
12 hours, minutes = divmod(minutes, 60)
Victor Stinner7ad16eb2018-06-01 11:04:45 +020013
Victor Stinner4ffe9c22018-06-14 14:58:13 +020014 parts = []
Victor Stinner7ad16eb2018-06-01 11:04:45 +020015 if hours:
Victor Stinner4ffe9c22018-06-14 14:58:13 +020016 parts.append('%s hour' % hours)
17 if minutes:
18 parts.append('%s min' % minutes)
19 if seconds:
Victor Stinner098e2562019-10-03 16:15:16 +020020 if parts:
21 # 2 min 1 sec
22 parts.append('%s sec' % seconds)
23 else:
24 # 1.0 sec
25 parts.append('%.1f sec' % (seconds + ms / 1000))
Victor Stinner4ffe9c22018-06-14 14:58:13 +020026 if not parts:
Victor Stinner098e2562019-10-03 16:15:16 +020027 return '%s ms' % ms
Victor Stinner4ffe9c22018-06-14 14:58:13 +020028
29 parts = parts[:2]
30 return ' '.join(parts)
Victor Stinner7ad16eb2018-06-01 11:04:45 +020031
32
33def removepy(names):
34 if not names:
35 return
36 for idx, name in enumerate(names):
37 basename, ext = os.path.splitext(name)
38 if ext == '.py':
39 names[idx] = basename
40
41
42def count(n, word):
43 if n == 1:
44 return "%d %s" % (n, word)
45 else:
46 return "%d %ss" % (n, word)
47
48
49def printlist(x, width=70, indent=4, file=None):
50 """Print the elements of iterable x to stdout.
51
52 Optional arg width (default 70) is the maximum line length.
53 Optional arg indent (default 4) is the number of blanks with which to
54 begin each line.
55 """
56
57 blanks = ' ' * indent
58 # Print the sorted list: 'x' may be a '--random' list or a set()
59 print(textwrap.fill(' '.join(str(elt) for elt in sorted(x)), width,
60 initial_indent=blanks, subsequent_indent=blanks),
61 file=file)
Victor Stinner4d299832019-04-26 04:08:53 +020062
63
64def print_warning(msg):
Victor Stinnerd663d342020-04-23 19:03:52 +020065 support.print_warning(msg)
Victor Stinner95f61c82019-06-13 01:09:04 +020066
67
68orig_unraisablehook = None
69
70
71def regrtest_unraisable_hook(unraisable):
72 global orig_unraisablehook
73 support.environment_altered = True
74 print_warning("Unraisable exception")
75 orig_unraisablehook(unraisable)
76
77
78def setup_unraisable_hook():
79 global orig_unraisablehook
80 orig_unraisablehook = sys.unraisablehook
81 sys.unraisablehook = regrtest_unraisable_hook