1import math 2import os.path 3import sys 4import textwrap 5from test import support 6 7 8def format_duration(seconds): 9 ms = math.ceil(seconds * 1e3) 10 seconds, ms = divmod(ms, 1000) 11 minutes, seconds = divmod(seconds, 60) 12 hours, minutes = divmod(minutes, 60) 13 14 parts = [] 15 if hours: 16 parts.append('%s hour' % hours) 17 if minutes: 18 parts.append('%s min' % minutes) 19 if seconds: 20 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)) 26 if not parts: 27 return '%s ms' % ms 28 29 parts = parts[:2] 30 return ' '.join(parts) 31 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) 62 63 64def print_warning(msg): 65 support.print_warning(msg) 66 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 old_stderr = sys.stderr 76 try: 77 sys.stderr = sys.__stderr__ 78 orig_unraisablehook(unraisable) 79 finally: 80 sys.stderr = old_stderr 81 82 83def setup_unraisable_hook(): 84 global orig_unraisablehook 85 orig_unraisablehook = sys.unraisablehook 86 sys.unraisablehook = regrtest_unraisable_hook 87