support.print_warning() now flushs sys.stdout.
target of the "as" clause, if there is one.
+.. function:: flush_std_streams()
+
+ Call the ``flush()`` method on :data:`sys.stdout` and then on
+ :data:`sys.stderr`. It can be used to make sure that the logs order is
+ consistent before writing into stderr.
+
+ .. versionadded:: 3.11
+
+
.. function:: print_warning(msg)
Print a warning into :data:`sys.__stderr__`. Format the message as:
orig_unraisablehook = None
-def flush_std_streams():
- if sys.stdout is not None:
- sys.stdout.flush()
- if sys.stderr is not None:
- sys.stderr.flush()
-
-
def regrtest_unraisable_hook(unraisable):
global orig_unraisablehook
support.environment_altered = True
print_warning("Unraisable exception")
old_stderr = sys.stderr
try:
- flush_std_streams()
+ support.flush_std_streams()
sys.stderr = sys.__stderr__
orig_unraisablehook(unraisable)
sys.stderr.flush()
print_warning(f"Uncaught thread exception: {args.exc_type.__name__}")
old_stderr = sys.stderr
try:
- flush_std_streams()
+ support.flush_std_streams()
sys.stderr = sys.__stderr__
orig_threading_excepthook(args)
sys.stderr.flush()
#=======================================================================
# Support for saving and restoring the imported modules.
+def flush_std_streams():
+ if sys.stdout is not None:
+ sys.stdout.flush()
+ if sys.stderr is not None:
+ sys.stderr.flush()
+
+
def print_warning(msg):
+ # bpo-45410: Explicitly flush stdout to keep logs in order
+ flush_std_streams()
# bpo-39983: Print into sys.__stderr__ to display the warning even
# when sys.stderr is captured temporarily by a test
for line in msg.splitlines():