Patchwork [2,of,4] dispatch: move initialization of sys.std* files

login
register
mail settings
Submitter Yuya Nishihara
Date Oct. 4, 2017, 2:53 p.m.
Message ID <2b071e9303f748de41c0.1507128835@mimosa>
Download mbox | patch
Permalink /patch/24492/
State Accepted
Headers show

Comments

Yuya Nishihara - Oct. 4, 2017, 2:53 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1506925104 -3600
#      Mon Oct 02 07:18:24 2017 +0100
# Node ID 2b071e9303f748de41c02efdea158883d336224a
# Parent  bc4545271d0456e83e49667e1613057918f38093
dispatch: move initialization of sys.std* files

I'll add another Python 3 hack.

Patch

diff --git a/contrib/hgperf b/contrib/hgperf
--- a/contrib/hgperf
+++ b/contrib/hgperf
@@ -94,7 +94,4 @@  def runcommand(lui, repo, cmd, fullargs,
 
 dispatch.runcommand = runcommand
 
-for fp in (sys.stdin, sys.stdout, sys.stderr):
-    util.setbinary(fp)
-
 dispatch.run()
diff --git a/hg b/hg
--- a/hg
+++ b/hg
@@ -37,11 +37,5 @@  except ImportError:
     sys.stderr.write("(check your install and PYTHONPATH)\n")
     sys.exit(-1)
 
-from mercurial import (
-    dispatch,
-    util,
-)
-for fp in (sys.stdin, sys.stdout, sys.stderr):
-    util.setbinary(fp)
-
+from mercurial import dispatch
 dispatch.run()
diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -75,6 +75,7 @@  class request(object):
 
 def run():
     "run the command in sys.argv"
+    _initstdio()
     req = request(pycompat.sysargv[1:])
     err = None
     try:
@@ -95,6 +96,10 @@  def run():
         req.ui.ferr.flush()
     sys.exit(status & 255)
 
+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()
     # The cutoff for similarity here is pretty arbitrary. It should