[PATCH 3 of 4] py3: fully detach TextIOWrapper

Yuya Nishihara yuya at tcha.org
Wed Oct 4 10:53:56 EDT 2017


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1506923984 -3600
#      Mon Oct 02 06:59:44 2017 +0100
# Node ID fecbf1e8aa17474f448e126697fe287e44c92664
# Parent  2b071e9303f748de41c02efdea158883d336224a
py3: fully detach TextIOWrapper

This silences the following error message in test-basic.t, which would
probably be caused by the last-minute flush() run in __del__.

  $ hg status >/dev/full
  abort: No space left on devic
  Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='ANSI_X3.4-1968'>
  OSError: [Errno 28] No space left on device

Since sys.std* files are completely reimplemented in Python 3, we wouldn't
need the setbinary() hack, so replaced it.

diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -96,9 +96,15 @@ def run():
         req.ui.ferr.flush()
     sys.exit(status & 255)
 
-def _initstdio():
-    for fp in (sys.stdin, sys.stdout, sys.stderr):
-        util.setbinary(fp)
+if pycompat.ispy3:
+    def _initstdio():
+        # detach wrapper so it will never flush() underlying IO implicitly
+        for fp in (sys.stdin, sys.stdout, sys.stderr):
+            fp.detach()
+else:
+    def _initstdio():
+        for fp in (sys.stdin, sys.stdout, sys.stderr):
+            util.setbinary(fp)
 
 def _getsimilar(symbols, value):
     sim = lambda x: difflib.SequenceMatcher(None, value, x).ratio()


More information about the Mercurial-devel mailing list