quark created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  On Python 3 stdio objects can be None. That causes crashes. Fix it by opening
  devnull automatically.
  
  `stdin` can be `None` by using `0<&-` in bash, or spawning processes less
  carefully, for example, watchman used to cause such `None` stdin [1] (note:
  None is only observable on Python 3).
  
  [1]: 
https://github.com/facebook/watchman/commit/d241978aaa6b6d7c5b7260bc9e6d699d3a1cea53

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D9059

AFFECTED FILES
  mercurial/utils/procutil.py
  tests/test-stdio-missing.t

CHANGE DETAILS

diff --git a/tests/test-stdio-missing.t b/tests/test-stdio-missing.t
new file mode 100644
--- /dev/null
+++ b/tests/test-stdio-missing.t
@@ -0,0 +1,13 @@
+  $ cat > prompt.py << 'EOF'
+  > from mercurial import exthelper
+  > eh = exthelper.exthelper()
+  > cmdtable = eh.cmdtable
+  > @eh.command(b'prompt', [], norepo=True)
+  > def prompt(ui):
+  >     chosen = ui.promptchoice(b"is stdin present? (y/N) $$ &Yes $$ &No", 
default=1)
+  >     ui.write(b"chosen: %d\n" % chosen)
+  > EOF
+
+  $ hg --config extensions.prompt=prompt.py prompt 0<&-
+  is stdin present? (y/N)  n
+  chosen: 1
diff --git a/mercurial/utils/procutil.py b/mercurial/utils/procutil.py
--- a/mercurial/utils/procutil.py
+++ b/mercurial/utils/procutil.py
@@ -114,6 +114,11 @@
 
 
 if pycompat.ispy3:
+    # Stdio objects can be 'None' on Python 3. Most code paths (for example,
+    # dispatch.initstdio) do not expect that. Fix them by opening devnull.
+    sys.stdin = sys.stdin or open(os.devnull, "r")
+    sys.stdout = sys.stdout or open(os.devnull, "w")
+    sys.stderr = sys.stderr or sys.stdout
     # Python 3 implements its own I/O streams.
     # 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.



To: quark, #hg-reviewers
Cc: mercurial-patches, mercurial-devel
_______________________________________________
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel

Reply via email to