blob: 2691a2c30ce80a8533c0e4c10a510368ef5eed12 [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:
20 parts.append('%s sec' % seconds)
21 if ms:
22 parts.append('%s ms' % ms)
23 if not parts:
24 return '0 ms'
25
26 parts = parts[:2]
27 return ' '.join(parts)
Victor Stinner7ad16eb2018-06-01 11:04:45 +020028
29
30def removepy(names):
31 if not names:
32 return
33 for idx, name in enumerate(names):
34 basename, ext = os.path.splitext(name)
35 if ext == '.py':
36 names[idx] = basename
37
38
39def count(n, word):
40 if n == 1:
41 return "%d %s" % (n, word)
42 else:
43 return "%d %ss" % (n, word)
44
45
46def printlist(x, width=70, indent=4, file=None):
47 """Print the elements of iterable x to stdout.
48
49 Optional arg width (default 70) is the maximum line length.
50 Optional arg indent (default 4) is the number of blanks with which to
51 begin each line.
52 """
53
54 blanks = ' ' * indent
55 # Print the sorted list: 'x' may be a '--random' list or a set()
56 print(textwrap.fill(' '.join(str(elt) for elt in sorted(x)), width,
57 initial_indent=blanks, subsequent_indent=blanks),
58 file=file)
Victor Stinner4d299832019-04-26 04:08:53 +020059
60
61def print_warning(msg):
62 print(f"Warning -- {msg}", file=sys.stderr, flush=True)
Victor Stinner95f61c82019-06-13 01:09:04 +020063
64
65orig_unraisablehook = None
66
67
68def regrtest_unraisable_hook(unraisable):
69 global orig_unraisablehook
70 support.environment_altered = True
71 print_warning("Unraisable exception")
72 orig_unraisablehook(unraisable)
73
74
75def setup_unraisable_hook():
76 global orig_unraisablehook
77 orig_unraisablehook = sys.unraisablehook
78 sys.unraisablehook = regrtest_unraisable_hook