dispatch: move initialization of sys.std* files
authorYuya Nishihara <yuya@tcha.org>
Mon, 02 Oct 2017 07:18:24 +0100
changeset 40078 163fa0aea71ea8524036b248a3db2837866e1124
parent 40077 b09b3eaf9c969f900339943988c5930dc6768e40
child 40079 b3073e175c17d27005b8133569b165ea89ae7e0a
push id573
push usergszorc@mozilla.com
push dateWed, 18 Oct 2017 21:03:35 +0000
dispatch: move initialization of sys.std* files I'll add another Python 3 hack.
contrib/hgperf
hg
mercurial/dispatch.py
--- a/contrib/hgperf
+++ b/contrib/hgperf
@@ -89,12 +89,9 @@ def runcommand(lui, repo, cmd, fullargs,
     lui.pushbuffer()
     timer(lambda : orgruncommand(lui, repo, cmd, fullargs, ui,
                                  options, d, cmdpats, cmdoptions))
     ui.popbuffer()
     lui.popbuffer()
 
 dispatch.runcommand = runcommand
 
-for fp in (sys.stdin, sys.stdout, sys.stderr):
-    util.setbinary(fp)
-
 dispatch.run()
--- a/hg
+++ b/hg
@@ -32,16 +32,10 @@ try:
     if sys.version_info[0] < 3 or sys.version_info >= (3, 6):
         import hgdemandimport; hgdemandimport.enable()
 except ImportError:
     sys.stderr.write("abort: couldn't find mercurial libraries in [%s]\n" %
                      ' '.join(sys.path))
     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()
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -70,16 +70,17 @@ class request(object):
                     self.ui.warn(('error in exit handlers:\n'))
                     self.ui.traceback(force=True)
         finally:
             if exc is not None:
                 raise exc
 
 def run():
     "run the command in sys.argv"
+    _initstdio()
     req = request(pycompat.sysargv[1:])
     err = None
     try:
         status = (dispatch(req) or 0) & 255
     except error.StdioError as e:
         err = e
         status = -1
     if util.safehasattr(req.ui, 'fout'):
@@ -90,16 +91,20 @@ def run():
             status = -1
     if util.safehasattr(req.ui, 'ferr'):
         if err is not None and err.errno != errno.EPIPE:
             req.ui.ferr.write('abort: %s\n' %
                               encoding.strtolocal(err.strerror))
         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
     # probably be investigated and tweaked.
     return [s for s in symbols if sim(s) > 0.6]
 
 def _reportsimilar(write, similar):
     if len(similar) == 1: