[PATCH] util: flush stderr explicitly after using warnings.warn()

Manuel Jacob me at manueljacob.de
Fri Jun 19 19:16:41 UTC 2020


On 2020-06-19 17:12, Pulkit Goyal wrote:
> # HG changeset patch
> # User Pulkit Goyal <7895pulkit at gmail.com>
> # Date 1592579534 -19800
> #      Fri Jun 19 20:42:14 2020 +0530
> # Node ID dc3c03ad1dc77fba867a7381249103081609a95e
> # Parent  1bd88e1bd312b02fa3cba45fccb147f316b372eb
> # EXP-Topic chg-test
> util: flush stderr explicitly after using warnings.warn()
> 
> Due to some unknown reasons, when using chg with python3, the 
> warnings.warn()
> output is not flushed.

The warnings module won’t flush output: 
https://github.com/python/cpython/blob/v3.8.3/Lib/warnings.py#L30 This 
is probably worth fixing on its own, but it’s outside of Mercurial.

Python 2 uses the libc streams. Python 3 has its own streams and 
initialization.

On many systems, libc sets stderr to unbuffered. (I couldn’t verify, but 
I think it is not necessarily the case on Windows. Maybe the warning is 
not even flushed without chg on Python 2 on Windows.) Python 3.8 sets 
sys.stderr fully buffered if not interactive and not explicitly 
configured unbuffered (source: 
https://docs.python.org/3.8/library/sys.html#sys.stderr). Python 3.9 
sets sys.stderr always line-buffered if not explicitly configured 
unbuffered (source: 
https://docs.python.org/3.9/library/sys.html#sys.stderr).

I think there’s a problem for both stdout and stderr. Glibc decides 
buffering on the first write to the stream. Python 3 decides buffering 
at interpreter startup. To solve the problem generally, sys.stdout and 
sys.stderr need to be replaced after each fork of chgserver.

> Fixes test-devel-warnings.t on py3 with chg.
> 
> diff --git a/mercurial/util.py b/mercurial/util.py
> --- a/mercurial/util.py
> +++ b/mercurial/util.py
> @@ -204,6 +204,8 @@ def nouideprecwarn(msg, version, stackle
>              b" update your code.)"
>          ) % version
>          warnings.warn(pycompat.sysstr(msg), DeprecationWarning, 
> stacklevel + 1)
> +        # on python 3 with chg, we will need to explicitly flush the 
> output
> +        sys.stderr.flush()
> 
> 
>  DIGESTS = {
> 
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel at mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel



More information about the Mercurial-devel mailing list