2018-06-14 09:58:13 -03:00
|
|
|
import math
|
2019-04-25 23:08:53 -03:00
|
|
|
import os.path
|
|
|
|
import sys
|
2018-06-01 06:04:45 -03:00
|
|
|
import textwrap
|
2019-06-12 20:09:04 -03:00
|
|
|
from test import support
|
2018-06-01 06:04:45 -03:00
|
|
|
|
|
|
|
|
|
|
|
def format_duration(seconds):
|
2018-06-14 09:58:13 -03:00
|
|
|
ms = math.ceil(seconds * 1e3)
|
|
|
|
seconds, ms = divmod(ms, 1000)
|
|
|
|
minutes, seconds = divmod(seconds, 60)
|
|
|
|
hours, minutes = divmod(minutes, 60)
|
2018-06-01 06:04:45 -03:00
|
|
|
|
2018-06-14 09:58:13 -03:00
|
|
|
parts = []
|
2018-06-01 06:04:45 -03:00
|
|
|
if hours:
|
2018-06-14 09:58:13 -03:00
|
|
|
parts.append('%s hour' % hours)
|
|
|
|
if minutes:
|
|
|
|
parts.append('%s min' % minutes)
|
|
|
|
if seconds:
|
2019-10-03 11:15:16 -03:00
|
|
|
if parts:
|
|
|
|
# 2 min 1 sec
|
|
|
|
parts.append('%s sec' % seconds)
|
|
|
|
else:
|
|
|
|
# 1.0 sec
|
|
|
|
parts.append('%.1f sec' % (seconds + ms / 1000))
|
2018-06-14 09:58:13 -03:00
|
|
|
if not parts:
|
2019-10-03 11:15:16 -03:00
|
|
|
return '%s ms' % ms
|
2018-06-14 09:58:13 -03:00
|
|
|
|
|
|
|
parts = parts[:2]
|
|
|
|
return ' '.join(parts)
|
2018-06-01 06:04:45 -03:00
|
|
|
|
|
|
|
|
|
|
|
def removepy(names):
|
|
|
|
if not names:
|
|
|
|
return
|
|
|
|
for idx, name in enumerate(names):
|
|
|
|
basename, ext = os.path.splitext(name)
|
|
|
|
if ext == '.py':
|
|
|
|
names[idx] = basename
|
|
|
|
|
|
|
|
|
|
|
|
def count(n, word):
|
|
|
|
if n == 1:
|
|
|
|
return "%d %s" % (n, word)
|
|
|
|
else:
|
|
|
|
return "%d %ss" % (n, word)
|
|
|
|
|
|
|
|
|
|
|
|
def printlist(x, width=70, indent=4, file=None):
|
|
|
|
"""Print the elements of iterable x to stdout.
|
|
|
|
|
|
|
|
Optional arg width (default 70) is the maximum line length.
|
|
|
|
Optional arg indent (default 4) is the number of blanks with which to
|
|
|
|
begin each line.
|
|
|
|
"""
|
|
|
|
|
|
|
|
blanks = ' ' * indent
|
|
|
|
# Print the sorted list: 'x' may be a '--random' list or a set()
|
|
|
|
print(textwrap.fill(' '.join(str(elt) for elt in sorted(x)), width,
|
|
|
|
initial_indent=blanks, subsequent_indent=blanks),
|
|
|
|
file=file)
|
2019-04-25 23:08:53 -03:00
|
|
|
|
|
|
|
|
|
|
|
def print_warning(msg):
|
2020-04-23 14:03:52 -03:00
|
|
|
support.print_warning(msg)
|
2019-06-12 20:09:04 -03:00
|
|
|
|
|
|
|
|
|
|
|
orig_unraisablehook = None
|
|
|
|
|
|
|
|
|
|
|
|
def regrtest_unraisable_hook(unraisable):
|
|
|
|
global orig_unraisablehook
|
|
|
|
support.environment_altered = True
|
|
|
|
print_warning("Unraisable exception")
|
2020-08-03 17:51:23 -03:00
|
|
|
old_stderr = sys.stderr
|
|
|
|
try:
|
|
|
|
sys.stderr = sys.__stderr__
|
|
|
|
orig_unraisablehook(unraisable)
|
|
|
|
finally:
|
|
|
|
sys.stderr = old_stderr
|
2019-06-12 20:09:04 -03:00
|
|
|
|
|
|
|
|
|
|
|
def setup_unraisable_hook():
|
|
|
|
global orig_unraisablehook
|
|
|
|
orig_unraisablehook = sys.unraisablehook
|
|
|
|
sys.unraisablehook = regrtest_unraisable_hook
|