Patchwork [2,of,3] procutil: create dummy file object if sys.stdout/stderr is missing

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

Comments

Yuya Nishihara - Dec. 18, 2020, 12:56 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1608289751 -32400
#      Fri Dec 18 20:09:11 2020 +0900
# Node ID bd4e513e64c974c38963b60fc14d4bbce6d574fe
# Parent  47ce0f83ffad4fd66e4f76ea54733756398a98ba
procutil: create dummy file object if sys.stdout/stderr is missing

This basically simulates the Python 2 behavior. If libc stdio were used,
these file objects would be available and raise EBADF. There is subtle
difference between py2 and py3, but I think py3 behavior (i.e. exit 255)
is more correct.

"if" conditions are adjust so that they look similar to dispatch.initstdio().

Patch

diff --git a/mercurial/utils/procutil.py b/mercurial/utils/procutil.py
--- a/mercurial/utils/procutil.py
+++ b/mercurial/utils/procutil.py
@@ -120,19 +120,36 @@  def _make_write_all(stream):
     return WriteAllWrapper(stream)
 
 
+def _make_bad_file(mode):
+    """Creates a file which will raise EBADF for any I/O operation
+
+    This simulates the Python 2 behavior of closed stdio.
+    """
+    f = open(os.devnull, mode)
+    os.close(f.fileno())
+    return f
+
+
 if pycompat.ispy3:
-    # Python 3 implements its own I/O streams.
+    # Python 3 implements its own I/O streams. Unlike stdio of C library,
+    # sys.stdin/stdout/stderr may be None if underlying fd is closed.
+
     # TODO: .buffer might not exist if std streams were replaced; we'll need
     # a silly wrapper to make a bytes stream backed by a unicode one.
 
-    # sys.stdin can be None
-    if sys.stdin:
+    if sys.stdin is None:
+        stdin = _make_bad_file('rb')
+    else:
         stdin = sys.stdin.buffer
+    if sys.stdout is None:
+        stdout = _make_bad_file('wb')
     else:
-        stdin = open(os.devnull, 'rb')
-        os.close(stdin.fileno())
-    stdout = _make_write_all(sys.stdout.buffer)
-    stderr = _make_write_all(sys.stderr.buffer)
+        stdout = _make_write_all(sys.stdout.buffer)
+    if sys.stderr is None:
+        stderr = _make_bad_file('wb')
+    else:
+        stderr = _make_write_all(sys.stderr.buffer)
+
     if pycompat.iswindows:
         # Work around Windows bugs.
         stdout = platform.winstdout(stdout)
diff --git a/tests/test-basic.t b/tests/test-basic.t
--- a/tests/test-basic.t
+++ b/tests/test-basic.t
@@ -49,6 +49,26 @@  Writes to stdio succeed and fail appropr
   [255]
 #endif
 
+On Python 3, stdio may be None:
+
+  $ hg version -q 0<&-
+  Mercurial Distributed SCM * (glob)
+#if py3
+  $ hg version -q 1>&-
+  abort: Bad file descriptor
+  [255]
+#else
+  $ hg version -q 1>&-
+#endif
+  $ hg version -q 2>&-
+  Mercurial Distributed SCM * (glob)
+  $ hg unknown -q 1>&-
+  hg: unknown command 'unknown'
+  (did you mean debugknown?)
+  [255]
+  $ hg unknown -q 2>&-
+  [255]
+
   $ hg commit -m test
 
 This command is ancient: