changeset 46173:a9765e0a461d

dispatch: gate against missing stdout/stderr We do need procutil.stdin/stdout/stderr, but we don't care much for sys.std*. Let's leave them be None as it is the Python 3 way.
author Yuya Nishihara <yuya@tcha.org>
date Fri, 18 Dec 2020 20:14:54 +0900
parents d04c0e494cfe
children a1601ff3877c
files mercurial/dispatch.py
diffstat 1 files changed, 29 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/dispatch.py	Fri Dec 18 20:35:11 2020 +0900
+++ b/mercurial/dispatch.py	Fri Dec 18 20:14:54 2020 +0900
@@ -166,26 +166,34 @@
         # "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 @@
 
     def _silencestdio():
         for fp in (sys.stdout, sys.stderr):
+            if fp is None:
+                continue
             # Check if the file is okay
             try:
                 fp.flush()