Patchwork [1,of,3] dispatch: gate against missing stdout/stderr

login
register
mail settings
Submitter Yuya Nishihara
Date Dec. 18, 2020, 12:56 p.m.
Message ID <47ce0f83ffad4fd66e4f.1608296183@lemosa>
Download mbox | patch
Permalink /patch/47932/
State New
Headers show

Comments

Yuya Nishihara - Dec. 18, 2020, 12:56 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1608290094 -32400
#      Fri Dec 18 20:14:54 2020 +0900
# Node ID 47ce0f83ffad4fd66e4f76ea54733756398a98ba
# Parent  d109dda4a3e7bb1c6fa7de98b6872ddb80ec3827
dispatch: gate against missing stdout/stderr
Pulkit Goyal - Dec. 18, 2020, 7:59 p.m.
On Fri, Dec 18, 2020 at 6:26 PM Yuya Nishihara <yuya@tcha.org> wrote:
>
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1608290094 -32400
> #      Fri Dec 18 20:14:54 2020 +0900
> # Node ID 47ce0f83ffad4fd66e4f76ea54733756398a98ba
> # Parent  d109dda4a3e7bb1c6fa7de98b6872ddb80ec3827
> dispatch: gate against missing stdout/stderr

I was reviewing patches on phabricator and stumbled upon this:
https://phab.mercurial-scm.org/D9059

Patch

diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -166,26 +166,34 @@  if pycompat.ispy3:
         # "just work," here we change the sys.* streams to disable line ending
         # normalization, ensuring compatibility with our ui type.
 
-        # write_through is new in Python 3.7.
-        kwargs = {
-            "newline": "\n",
-            "line_buffering": sys.stdout.line_buffering,
-        }
-        if util.safehasattr(sys.stdout, "write_through"):
-            kwargs["write_through"] = sys.stdout.write_through
-        sys.stdout = io.TextIOWrapper(
-            sys.stdout.buffer, sys.stdout.encoding, sys.stdout.errors, **kwargs
-        )
+        if sys.stdout is not None:
+            # write_through is new in Python 3.7.
+            kwargs = {
+                "newline": "\n",
+                "line_buffering": sys.stdout.line_buffering,
+            }
+            if util.safehasattr(sys.stdout, "write_through"):
+                kwargs["write_through"] = sys.stdout.write_through
+            sys.stdout = io.TextIOWrapper(
+                sys.stdout.buffer,
+                sys.stdout.encoding,
+                sys.stdout.errors,
+                **kwargs
+            )
 
-        kwargs = {
-            "newline": "\n",
-            "line_buffering": sys.stderr.line_buffering,
-        }
-        if util.safehasattr(sys.stderr, "write_through"):
-            kwargs["write_through"] = sys.stderr.write_through
-        sys.stderr = io.TextIOWrapper(
-            sys.stderr.buffer, sys.stderr.encoding, sys.stderr.errors, **kwargs
-        )
+        if sys.stderr is not None:
+            kwargs = {
+                "newline": "\n",
+                "line_buffering": sys.stderr.line_buffering,
+            }
+            if util.safehasattr(sys.stderr, "write_through"):
+                kwargs["write_through"] = sys.stderr.write_through
+            sys.stderr = io.TextIOWrapper(
+                sys.stderr.buffer,
+                sys.stderr.encoding,
+                sys.stderr.errors,
+                **kwargs
+            )
 
         if sys.stdin is not None:
             # No write_through on read-only stream.
@@ -200,6 +208,8 @@  if pycompat.ispy3:
 
     def _silencestdio():
         for fp in (sys.stdout, sys.stderr):
+            if fp is None:
+                continue
             # Check if the file is okay
             try:
                 fp.flush()