Patchwork [2,of,3,chg] procutil: use os.devnull if sys.stdout/err is None

login
register
mail settings
Submitter Pulkit Goyal
Date Dec. 18, 2020, 8:46 a.m.
Message ID <9f0555329584b3e91e74.1608281180@DESKTOP-STQPTJK>
Download mbox | patch
Permalink /patch/47930/
State Accepted
Headers show

Comments

Pulkit Goyal - Dec. 18, 2020, 8:46 a.m.
# HG changeset patch
# User Pulkit Goyal <7895pulkit@gmail.com>
# Date 1607427603 -19800
#      Tue Dec 08 17:10:03 2020 +0530
# Node ID 9f0555329584b3e91e74e0606d66d16dd7a4c82f
# Parent  e40f52691dac94d70a251b65806a4d1f4d2b5f19
# EXP-Topic chg-test
procutil: use os.devnull if sys.stdout/err is None

According to Yuya, it's better to make `procutil.stdin/err/out` always
available.

This is a bandaid until someone suggests something better.
Yuya Nishihara - Dec. 18, 2020, 10:41 a.m.
On Fri, 18 Dec 2020 14:16:20 +0530, Pulkit Goyal wrote:
> # HG changeset patch
> # User Pulkit Goyal <7895pulkit@gmail.com>
> # Date 1607427603 -19800
> #      Tue Dec 08 17:10:03 2020 +0530
> # Node ID 9f0555329584b3e91e74e0606d66d16dd7a4c82f
> # Parent  e40f52691dac94d70a251b65806a4d1f4d2b5f19
> # EXP-Topic chg-test
> procutil: use os.devnull if sys.stdout/err is None
> 
> According to Yuya, it's better to make `procutil.stdin/err/out` always
> available.
> 
> This is a bandaid until someone suggests something better.
> 
> diff -r e40f52691dac -r 9f0555329584 mercurial/utils/procutil.py
> --- a/mercurial/utils/procutil.py	Fri Apr 03 20:30:36 2020 +0530
> +++ b/mercurial/utils/procutil.py	Tue Dec 08 17:10:03 2020 +0530
> @@ -131,8 +131,16 @@
>      else:
>          stdin = open(os.devnull, 'rb')
>          os.close(stdin.fileno())
> -    stdout = _make_write_all(sys.stdout.buffer)
> -    stderr = _make_write_all(sys.stderr.buffer)
> +    if sys.stdout:
> +        stdout = _make_write_all(sys.stdout.buffer)
> +    else:
> +        stdout = open(os.devnull, 'rb')
> +        os.close(stdout.fileno())
> +    if sys.stderr:
> +        stderr = _make_write_all(sys.stderr.buffer)
> +    else:
> +        stderr = open(os.devnull, 'rb')

stdout/stderr should theoretically be 'wb'. I'll send a reworked version.

Patch

diff -r e40f52691dac -r 9f0555329584 mercurial/utils/procutil.py
--- a/mercurial/utils/procutil.py	Fri Apr 03 20:30:36 2020 +0530
+++ b/mercurial/utils/procutil.py	Tue Dec 08 17:10:03 2020 +0530
@@ -131,8 +131,16 @@ 
     else:
         stdin = open(os.devnull, 'rb')
         os.close(stdin.fileno())
-    stdout = _make_write_all(sys.stdout.buffer)
-    stderr = _make_write_all(sys.stderr.buffer)
+    if sys.stdout:
+        stdout = _make_write_all(sys.stdout.buffer)
+    else:
+        stdout = open(os.devnull, 'rb')
+        os.close(stdout.fileno())
+    if sys.stderr:
+        stderr = _make_write_all(sys.stderr.buffer)
+    else:
+        stderr = open(os.devnull, 'rb')
+        os.close(stderr.fileno())
     if pycompat.iswindows:
         # Work around Windows bugs.
         stdout = platform.winstdout(stdout)