diff options
author | Victor Stinner <vstinner@python.org> | 2021-10-11 21:07:21 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-11 21:07:21 (GMT) |
commit | 1ebd798fddef51e1f6fd40a4860278a1851ac268 (patch) | |
tree | ac15076d8bd096d265072c9888361640400a8eee | |
parent | 47717d1186563695e798b40350d15b00d04a5237 (diff) | |
download | cpython-1ebd798fddef51e1f6fd40a4860278a1851ac268.zip cpython-1ebd798fddef51e1f6fd40a4860278a1851ac268.tar.gz cpython-1ebd798fddef51e1f6fd40a4860278a1851ac268.tar.bz2 |
bpo-45410: Add test.support.flush_std_streams() (GH-28885)
support.print_warning() now flushs sys.stdout.
-rw-r--r-- | Doc/library/test.rst | 9 | ||||
-rw-r--r-- | Lib/test/libregrtest/utils.py | 11 | ||||
-rw-r--r-- | Lib/test/support/__init__.py | 9 |
3 files changed, 20 insertions, 9 deletions
diff --git a/Doc/library/test.rst b/Doc/library/test.rst index a8dc354..699db14 100644 --- a/Doc/library/test.rst +++ b/Doc/library/test.rst @@ -607,6 +607,15 @@ The :mod:`test.support` module defines the following functions: 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: diff --git a/Lib/test/libregrtest/utils.py b/Lib/test/libregrtest/utils.py index ad18b50..c71467a 100644 --- a/Lib/test/libregrtest/utils.py +++ b/Lib/test/libregrtest/utils.py @@ -68,20 +68,13 @@ def print_warning(msg): 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() @@ -104,7 +97,7 @@ def regrtest_threading_excepthook(args): 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() diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index b29f438..0bbe813 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -1167,7 +1167,16 @@ def run_doctest(module, verbosity=None, optionflags=0): #======================================================================= # 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(): |