Patchwork [5,of,7] procutil: bulk-replace util.std* to point to new module

login
register
mail settings
Submitter Yuya Nishihara
Date March 25, 2018, 3:36 a.m.
Message ID <45b434eaa0d479be8078.1521948979@mimosa>
Download mbox | patch
Permalink /patch/29834/
State Accepted
Headers show

Comments

Yuya Nishihara - March 25, 2018, 3:36 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1521871773 -32400
#      Sat Mar 24 15:09:33 2018 +0900
# Node ID 45b434eaa0d479be8078bbe16533d2b0d94c7220
# Parent  8adca2640e0b1eb61be50146d429d4276b6f52bd
procutil: bulk-replace util.std* to point to new module

Patch

diff --git a/mercurial/chgserver.py b/mercurial/chgserver.py
--- a/mercurial/chgserver.py
+++ b/mercurial/chgserver.py
@@ -61,6 +61,10 @@  from . import (
     util,
 )
 
+from .utils import (
+    procutil,
+)
+
 _log = commandserver.log
 
 def _hashlist(items):
@@ -200,7 +204,7 @@  def _newchgui(srcui, csystem, attachio):
             # these situations and will behave differently (write to stdout).
             if (out is not self.fout
                 or not util.safehasattr(self.fout, 'fileno')
-                or self.fout.fileno() != util.stdout.fileno()):
+                or self.fout.fileno() != procutil.stdout.fileno()):
                 return util.system(cmd, environ=environ, cwd=cwd, out=out)
             self.flush()
             return self._csystem(cmd, util.shellenviron(environ), cwd)
diff --git a/mercurial/commandserver.py b/mercurial/commandserver.py
--- a/mercurial/commandserver.py
+++ b/mercurial/commandserver.py
@@ -29,6 +29,9 @@  from . import (
     pycompat,
     util,
 )
+from .utils import (
+    procutil,
+)
 
 logfile = None
 
@@ -308,8 +311,8 @@  def _protectio(ui):
     ui.flush()
     newfiles = []
     nullfd = os.open(os.devnull, os.O_RDWR)
-    for f, sysf, mode in [(ui.fin, util.stdin, r'rb'),
-                          (ui.fout, util.stdout, r'wb')]:
+    for f, sysf, mode in [(ui.fin, procutil.stdin, r'rb'),
+                          (ui.fout, procutil.stdout, r'wb')]:
         if f is sysf:
             newfd = os.dup(f.fileno())
             os.dup2(nullfd, f.fileno())
diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -42,6 +42,7 @@  from . import (
 )
 
 from .utils import (
+    procutil,
     stringutil,
 )
 
@@ -127,8 +128,8 @@  if pycompat.ispy3:
                 pass
             # Otherwise mark it as closed to silence "Exception ignored in"
             # message emitted by the interpreter finalizer. Be careful to
-            # not close util.stdout, which may be a fdopen-ed file object and
-            # its close() actually closes the underlying file descriptor.
+            # not close procutil.stdout, which may be a fdopen-ed file object
+            # and its close() actually closes the underlying file descriptor.
             try:
                 fp.close()
             except IOError:
@@ -180,7 +181,7 @@  def dispatch(req):
     elif req.ui:
         ferr = req.ui.ferr
     else:
-        ferr = util.stderr
+        ferr = procutil.stderr
 
     try:
         if not req.ui:
diff --git a/mercurial/hgweb/wsgicgi.py b/mercurial/hgweb/wsgicgi.py
--- a/mercurial/hgweb/wsgicgi.py
+++ b/mercurial/hgweb/wsgicgi.py
@@ -15,13 +15,17 @@  from .. import (
     util,
 )
 
+from ..utils import (
+    procutil,
+)
+
 from . import (
     common,
 )
 
 def launch(application):
-    util.setbinary(util.stdin)
-    util.setbinary(util.stdout)
+    util.setbinary(procutil.stdin)
+    util.setbinary(procutil.stdout)
 
     environ = dict(encoding.environ.iteritems())
     environ.setdefault(r'PATH_INFO', '')
@@ -31,12 +35,12 @@  def launch(application):
         if environ[r'PATH_INFO'].startswith(scriptname):
             environ[r'PATH_INFO'] = environ[r'PATH_INFO'][len(scriptname):]
 
-    stdin = util.stdin
+    stdin = procutil.stdin
     if environ.get(r'HTTP_EXPECT', r'').lower() == r'100-continue':
-        stdin = common.continuereader(stdin, util.stdout.write)
+        stdin = common.continuereader(stdin, procutil.stdout.write)
 
     environ[r'wsgi.input'] = stdin
-    environ[r'wsgi.errors'] = util.stderr
+    environ[r'wsgi.errors'] = procutil.stderr
     environ[r'wsgi.version'] = (1, 0)
     environ[r'wsgi.multithread'] = False
     environ[r'wsgi.multiprocess'] = True
@@ -49,7 +53,7 @@  def launch(application):
 
     headers_set = []
     headers_sent = []
-    out = util.stdout
+    out = procutil.stdout
 
     def write(data):
         if not headers_set:
diff --git a/mercurial/hook.py b/mercurial/hook.py
--- a/mercurial/hook.py
+++ b/mercurial/hook.py
@@ -19,6 +19,9 @@  from . import (
     pycompat,
     util,
 )
+from .utils import (
+    procutil,
+)
 
 def _pythonhook(ui, repo, htype, hname, funcname, args, throw):
     '''call python hook. hook is callable object, looked up as
@@ -222,11 +225,11 @@  def runhooks(ui, repo, htype, hooks, thr
         for hname, cmd in hooks:
             if oldstdout == -1 and _redirect:
                 try:
-                    stdoutno = util.stdout.fileno()
-                    stderrno = util.stderr.fileno()
+                    stdoutno = procutil.stdout.fileno()
+                    stderrno = procutil.stderr.fileno()
                     # temporarily redirect stdout to stderr, if possible
                     if stdoutno >= 0 and stderrno >= 0:
-                        util.stdout.flush()
+                        procutil.stdout.flush()
                         oldstdout = os.dup(stdoutno)
                         os.dup2(stderrno, stdoutno)
                 except (OSError, AttributeError):
@@ -269,10 +272,10 @@  def runhooks(ui, repo, htype, hooks, thr
         # The stderr is fully buffered on Windows when connected to a pipe.
         # A forcible flush is required to make small stderr data in the
         # remote side available to the client immediately.
-        util.stderr.flush()
+        procutil.stderr.flush()
 
         if _redirect and oldstdout >= 0:
-            util.stdout.flush()  # write hook output to stderr fd
+            procutil.stdout.flush()  # write hook output to stderr fd
             os.dup2(oldstdout, stdoutno)
             os.close(oldstdout)
 
diff --git a/mercurial/keepalive.py b/mercurial/keepalive.py
--- a/mercurial/keepalive.py
+++ b/mercurial/keepalive.py
@@ -97,6 +97,9 @@  from . import (
     urllibcompat,
     util,
 )
+from .utils import (
+    procutil,
+)
 
 httplib = util.httplib
 urlerr = util.urlerr
@@ -635,14 +638,14 @@  def continuity(url):
 def comp(N, url):
     print('  making %i connections to:\n  %s' % (N, url))
 
-    util.stdout.write('  first using the normal urllib handlers')
+    procutil.stdout.write('  first using the normal urllib handlers')
     # first use normal opener
     opener = urlreq.buildopener()
     urlreq.installopener(opener)
     t1 = fetch(N, url)
     print('  TIME: %.3f s' % t1)
 
-    util.stdout.write('  now using the keepalive handler       ')
+    procutil.stdout.write('  now using the keepalive handler       ')
     # now install the keepalive handler and try again
     opener = urlreq.buildopener(HTTPHandler())
     urlreq.installopener(opener)
@@ -687,11 +690,11 @@  def test_timeout(url):
     i = 20
     print("  waiting %i seconds for the server to close the connection" % i)
     while i > 0:
-        util.stdout.write('\r  %2i' % i)
-        util.stdout.flush()
+        procutil.stdout.write('\r  %2i' % i)
+        procutil.stdout.flush()
         time.sleep(1)
         i -= 1
-    util.stderr.write('\r')
+    procutil.stderr.write('\r')
 
     print("  fetching the file a second time")
     fo = urlreq.urlopen(url)
diff --git a/mercurial/server.py b/mercurial/server.py
--- a/mercurial/server.py
+++ b/mercurial/server.py
@@ -22,6 +22,10 @@  from . import (
     util,
 )
 
+from .utils import (
+    procutil,
+)
+
 def runservice(opts, parentfn=None, initfn=None, runfn=None, logfile=None,
                runargs=None, appendpid=False):
     '''Run a command as a service.'''
@@ -87,8 +91,8 @@  def runservice(opts, parentfn=None, init
                 raise error.Abort(_('invalid value for --daemon-postexec: %s')
                                   % inst)
         util.hidewindow()
-        util.stdout.flush()
-        util.stderr.flush()
+        procutil.stdout.flush()
+        procutil.stderr.flush()
 
         nullfd = os.open(os.devnull, os.O_RDWR)
         logfilefd = nullfd
diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -39,6 +39,7 @@  from . import (
 )
 from .utils import (
     dateutil,
+    procutil,
     stringutil,
 )
 
@@ -250,9 +251,9 @@  class ui(object):
             self.httppasswordmgrdb = src.httppasswordmgrdb
             self._blockedtimes = src._blockedtimes
         else:
-            self.fout = util.stdout
-            self.ferr = util.stderr
-            self.fin = util.stdin
+            self.fout = procutil.stdout
+            self.ferr = procutil.stderr
+            self.fin = procutil.stdin
             self.pageractive = False
             self._disablepager = False
             self._tweaked = False
@@ -1099,7 +1100,7 @@  class ui(object):
             pager = subprocess.Popen(
                 command, shell=shell, bufsize=-1,
                 close_fds=util.closefds, stdin=subprocess.PIPE,
-                stdout=util.stdout, stderr=util.stderr,
+                stdout=procutil.stdout, stderr=procutil.stderr,
                 env=util.shellenviron(env))
         except OSError as e:
             if e.errno == errno.ENOENT and not shell:
@@ -1109,20 +1110,20 @@  class ui(object):
             raise
 
         # back up original file descriptors
-        stdoutfd = os.dup(util.stdout.fileno())
-        stderrfd = os.dup(util.stderr.fileno())
+        stdoutfd = os.dup(procutil.stdout.fileno())
+        stderrfd = os.dup(procutil.stderr.fileno())
 
-        os.dup2(pager.stdin.fileno(), util.stdout.fileno())
-        if self._isatty(util.stderr):
-            os.dup2(pager.stdin.fileno(), util.stderr.fileno())
+        os.dup2(pager.stdin.fileno(), procutil.stdout.fileno())
+        if self._isatty(procutil.stderr):
+            os.dup2(pager.stdin.fileno(), procutil.stderr.fileno())
 
         @self.atexit
         def killpager():
             if util.safehasattr(signal, "SIGINT"):
                 signal.signal(signal.SIGINT, signal.SIG_IGN)
             # restore original fds, closing pager.stdin copies in the process
-            os.dup2(stdoutfd, util.stdout.fileno())
-            os.dup2(stderrfd, util.stderr.fileno())
+            os.dup2(stdoutfd, procutil.stdout.fileno())
+            os.dup2(stderrfd, procutil.stderr.fileno())
             pager.stdin.close()
             pager.wait()
 
diff --git a/mercurial/wireproto.py b/mercurial/wireproto.py
--- a/mercurial/wireproto.py
+++ b/mercurial/wireproto.py
@@ -35,6 +35,7 @@  from . import (
 )
 
 from .utils import (
+    procutil,
     stringutil,
 )
 
@@ -519,8 +520,8 @@  def options(cmd, keys, others):
             opts[k] = others[k]
             del others[k]
     if others:
-        util.stderr.write("warning: %s ignored unexpected arguments %s\n"
-                          % (cmd, ",".join(others)))
+        procutil.stderr.write("warning: %s ignored unexpected arguments %s\n"
+                              % (cmd, ",".join(others)))
     return opts
 
 def bundle1allowed(repo, action):
@@ -1081,14 +1082,14 @@  def unbundle(repo, proto, heads):
                 try:
                     raise
                 except error.Abort:
-                    # The old code we moved used util.stderr directly.
+                    # The old code we moved used procutil.stderr directly.
                     # We did not change it to minimise code change.
                     # This need to be moved to something proper.
                     # Feel free to do it.
-                    util.stderr.write("abort: %s\n" % exc)
+                    procutil.stderr.write("abort: %s\n" % exc)
                     if exc.hint is not None:
-                        util.stderr.write("(%s)\n" % exc.hint)
-                    util.stderr.flush()
+                        procutil.stderr.write("(%s)\n" % exc.hint)
+                    procutil.stderr.flush()
                     return pushres(0, output.getvalue() if output else '')
                 except error.PushRaced:
                     return pusherr(pycompat.bytestr(exc),
diff --git a/tests/test-commit-interactive.t b/tests/test-commit-interactive.t
--- a/tests/test-commit-interactive.t
+++ b/tests/test-commit-interactive.t
@@ -900,7 +900,9 @@  This tests that translated help message 
   > from __future__ import absolute_import
   > from mercurial import (
   >     pycompat,
-  >     util,
+  > )
+  > from mercurial.utils import (
+  >     procutil,
   > )
   > def escape(c):
   >     o = ord(c)
@@ -908,8 +910,9 @@  This tests that translated help message 
   >         return c
   >     else:
   >         return br'\x%02x' % o # escape char setting MSB
-  > for l in util.stdin:
-  >     util.stdout.write(b''.join(escape(c) for c in pycompat.iterbytestr(l)))
+  > for l in procutil.stdin:
+  >     procutil.stdout.write(
+  >         b''.join(escape(c) for c in pycompat.iterbytestr(l)))
   > EOF
 
   $ hg commit -i --encoding cp932 2>&1 <<EOF | $PYTHON $TESTTMP/escape.py | grep '^y - '
diff --git a/tests/test-config-env.py b/tests/test-config-env.py
--- a/tests/test-config-env.py
+++ b/tests/test-config-env.py
@@ -11,6 +11,10 @@  from mercurial import (
     util,
 )
 
+from mercurial.utils import (
+    procutil,
+)
+
 testtmp = encoding.environ[b'TESTTMP']
 
 # prepare hgrc files
@@ -41,9 +45,9 @@  def printconfigs(env):
     ui = uimod.ui.load()
     for section, name, value in ui.walkconfig():
         source = ui.configsource(section, name)
-        util.stdout.write(b'%s.%s=%s # %s\n'
-                          % (section, name, value, util.pconvert(source)))
-    util.stdout.write(b'\n')
+        procutil.stdout.write(b'%s.%s=%s # %s\n'
+                              % (section, name, value, util.pconvert(source)))
+    procutil.stdout.write(b'\n')
 
 # environment variable overrides
 printconfigs({})