def threading_cleanup(*original_values): _MAX_COUNT = 100 for count in range(_MAX_COUNT): values = _thread._count(), threading._dangling if values == original_values: break if not count: # Display a warning at the first iteration support.environment_altered = True dangling_threads = values[1] support.print_warning(f"threading_cleanup() failed to cleanup " f"{values[0] - original_values[0]} threads " f"(count: {values[0]}, " f"dangling: {len(dangling_threads)})") for thread in dangling_threads: support.print_warning(f"Dangling thread: {thread!r}") # Don't hold references to threads dangling_threads = None values = None time.sleep(0.01) support.gc_collect()
def check_print_warning(self, msg, expected): stderr = io.StringIO() old_stderr = sys.__stderr__ try: sys.__stderr__ = stderr support.print_warning(msg) finally: sys.__stderr__ = old_stderr self.assertEqual(stderr.getvalue(), expected)
def regrtest_unraisable_hook(unraisable): global orig_unraisablehook support.environment_altered = True support.print_warning("Unraisable exception") old_stderr = sys.stderr try: support.flush_std_streams() sys.stderr = support.print_warning.orig_stderr orig_unraisablehook(unraisable) sys.stderr.flush() finally: sys.stderr = old_stderr
def regrtest_threading_excepthook(args): global orig_threading_excepthook support.environment_altered = True support.print_warning( f"Uncaught thread exception: {args.exc_type.__name__}") old_stderr = sys.stderr try: support.flush_std_streams() sys.stderr = support.print_warning.orig_stderr orig_threading_excepthook(args) sys.stderr.flush() finally: sys.stderr = old_stderr
def print_warning(msg): support.print_warning(msg)
def check_print_warning(self, msg, expected): stderr = io.StringIO() with support.swap_attr(support.print_warning, 'orig_stderr', stderr): support.print_warning(msg) self.assertEqual(stderr.getvalue(), expected)