Patchwork [1,of,3] encoding: move tonativestr() from procutil

login
register
mail settings
Submitter Matt Harbison
Date Oct. 14, 2018, 4:41 p.m.
Message ID <edf769c43bbb10048dce.1539535313@Envy>
Download mbox | patch
Permalink /patch/36012/
State Accepted
Headers show

Comments

Matt Harbison - Oct. 14, 2018, 4:41 p.m.
# HG changeset patch
# User Matt Harbison <matt_harbison@yahoo.com>
# Date 1539492413 14400
#      Sun Oct 14 00:46:53 2018 -0400
# Node ID edf769c43bbb10048dceed4cc2ddd46197943ff3
# Parent  8783710b1d58d2ccc0f611904af244a1dde214d4
encoding: move tonativestr() from procutil

There are uses for converting to Unicode on Windows but leaving as bytes on Unix
beyond process related stuff.

Patch

diff --git a/hgext/convert/common.py b/hgext/convert/common.py
--- a/hgext/convert/common.py
+++ b/hgext/convert/common.py
@@ -402,7 +402,7 @@  class commandline(object):
 
     def _run(self, cmd, *args, **kwargs):
         def popen(cmdline):
-            p = subprocess.Popen(procutil.tonativestr(cmdline),
+            p = subprocess.Popen(encoding.tonativestr(cmdline),
                                  shell=True, bufsize=-1,
                                  close_fds=procutil.closefds,
                                  stdout=subprocess.PIPE)
diff --git a/hgext/convert/gnuarch.py b/hgext/convert/gnuarch.py
--- a/hgext/convert/gnuarch.py
+++ b/hgext/convert/gnuarch.py
@@ -203,7 +203,7 @@  class gnuarch_source(common.converter_so
         cmdline += ['>', os.devnull, '2>', os.devnull]
         cmdline = procutil.quotecommand(' '.join(cmdline))
         self.ui.debug(cmdline, '\n')
-        return os.system(pycompat.rapply(procutil.tonativestr, cmdline))
+        return os.system(pycompat.rapply(encoding.tonativestr, cmdline))
 
     def _update(self, rev):
         self.ui.debug('applying revision %s...\n' % rev)
diff --git a/hgext/factotum.py b/hgext/factotum.py
--- a/hgext/factotum.py
+++ b/hgext/factotum.py
@@ -49,10 +49,8 @@  from __future__ import absolute_import
 
 import os
 from mercurial.i18n import _
-from mercurial.utils import (
-    procutil,
-)
 from mercurial import (
+    encoding,
     error,
     httpconnection,
     registrar,
@@ -86,7 +84,7 @@  def auth_getkey(self, params):
     if 'user=' not in params:
         params = '%s user?' % params
     params = '%s !password?' % params
-    os.system(procutil.tonativestr("%s -g '%s'" % (_executable, params)))
+    os.system(encoding.tonativestr("%s -g '%s'" % (_executable, params)))
 
 def auth_getuserpasswd(self, getkey, params):
     params = 'proto=pass %s' % params
diff --git a/hgext/fix.py b/hgext/fix.py
--- a/hgext/fix.py
+++ b/hgext/fix.py
@@ -58,14 +58,11 @@  from mercurial.i18n import _
 from mercurial.node import nullrev
 from mercurial.node import wdirrev
 
-from mercurial.utils import (
-    procutil,
-)
-
 from mercurial import (
     cmdutil,
     context,
     copies,
+    encoding,
     error,
     mdiff,
     merge,
@@ -452,9 +449,9 @@  def fixfile(ui, opts, fixers, fixctx, pa
                 continue
             ui.debug('subprocess: %s\n' % (command,))
             proc = subprocess.Popen(
-                procutil.tonativestr(command),
+                encoding.tonativestr(command),
                 shell=True,
-                cwd=procutil.tonativestr(b'/'),
+                cwd=encoding.tonativestr(b'/'),
                 stdin=subprocess.PIPE,
                 stdout=subprocess.PIPE,
                 stderr=subprocess.PIPE)
diff --git a/hgext/fsmonitor/pywatchman/__init__.py b/hgext/fsmonitor/pywatchman/__init__.py
--- a/hgext/fsmonitor/pywatchman/__init__.py
+++ b/hgext/fsmonitor/pywatchman/__init__.py
@@ -48,11 +48,8 @@  try:
 except ImportError:
     from . import pybser as bser
 
-from mercurial.utils import (
-    procutil,
-)
-
 from mercurial import (
+    encoding as hgencoding,
     pycompat,
 )
 
@@ -588,7 +585,7 @@  class CLIProcessTransport(Transport):
             '--no-pretty',
             '-j',
         ]
-        self.proc = subprocess.Popen(pycompat.rapply(procutil.tonativestr,
+        self.proc = subprocess.Popen(pycompat.rapply(hgencoding.tonativestr,
                                                      args),
                                      stdin=subprocess.PIPE,
                                      stdout=subprocess.PIPE)
@@ -831,7 +828,7 @@  class client(object):
                 startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
                 args['startupinfo'] = startupinfo
 
-            p = subprocess.Popen(pycompat.rapply(procutil.tonativestr, cmd),
+            p = subprocess.Popen(pycompat.rapply(hgencoding.tonativestr, cmd),
                                  **args)
 
         except OSError as e:
diff --git a/hgext/infinitepush/__init__.py b/hgext/infinitepush/__init__.py
--- a/hgext/infinitepush/__init__.py
+++ b/hgext/infinitepush/__init__.py
@@ -1181,6 +1181,6 @@  def _asyncsavemetadata(root, nodes):
         cmdline = [util.hgexecutable(), 'debugfillinfinitepushmetadata',
                    '-R', root] + nodesargs
         # Process will run in background. We don't care about the return code
-        subprocess.Popen(pycompat.rapply(procutil.tonativestr, cmdline),
+        subprocess.Popen(pycompat.rapply(encoding.tonativestr, cmdline),
                          close_fds=True, shell=False,
                          stdin=devnull, stdout=devnull, stderr=devnull)
diff --git a/hgext/infinitepush/store.py b/hgext/infinitepush/store.py
--- a/hgext/infinitepush/store.py
+++ b/hgext/infinitepush/store.py
@@ -15,9 +15,6 @@  from mercurial import (
     node,
     pycompat,
 )
-from mercurial.utils import (
-    procutil,
-)
 
 NamedTemporaryFile = tempfile.NamedTemporaryFile
 
@@ -119,7 +116,7 @@  class externalbundlestore(abstractbundle
 
     def _call_binary(self, args):
         p = subprocess.Popen(
-            pycompat.rapply(procutil.tonativestr, args),
+            pycompat.rapply(encoding.tonativestr, args),
             stdout=subprocess.PIPE, stderr=subprocess.PIPE,
             close_fds=True)
         stdout, stderr = p.communicate()
diff --git a/hgext/logtoprocess.py b/hgext/logtoprocess.py
--- a/hgext/logtoprocess.py
+++ b/hgext/logtoprocess.py
@@ -40,6 +40,7 @@  import subprocess
 import sys
 
 from mercurial import (
+    encoding,
     pycompat,
 )
 
@@ -65,7 +66,7 @@  def uisetup(ui):
             # we can't use close_fds *and* redirect stdin. I'm not sure that we
             # need to because the detached process has no console connection.
             subprocess.Popen(
-                procutil.tonativestr(script),
+                encoding.tonativestr(script),
                 shell=True, env=procutil.tonativeenv(env), close_fds=True,
                 creationflags=_creationflags)
     else:
@@ -90,7 +91,7 @@  def uisetup(ui):
                 nullrfd = open(os.devnull, 'r')
                 nullwfd = open(os.devnull, 'w')
                 subprocess.Popen(
-                    procutil.tonativestr(script),
+                    encoding.tonativestr(script),
                     shell=True, stdin=nullrfd,
                     stdout=nullwfd, stderr=nullwfd,
                     env=procutil.tonativeenv(env),
diff --git a/mercurial/debugcommands.py b/mercurial/debugcommands.py
--- a/mercurial/debugcommands.py
+++ b/mercurial/debugcommands.py
@@ -3090,7 +3090,7 @@  def debugwireproto(ui, repo, path=None, 
             '-R', repo.root,
             'debugserve', '--sshstdio',
         ]
-        proc = subprocess.Popen(pycompat.rapply(procutil.tonativestr, args),
+        proc = subprocess.Popen(pycompat.rapply(encoding.tonativestr, args),
                                 stdin=subprocess.PIPE,
                                 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                                 bufsize=0)
diff --git a/mercurial/encoding.py b/mercurial/encoding.py
--- a/mercurial/encoding.py
+++ b/mercurial/encoding.py
@@ -227,6 +227,11 @@  else:
     strfromlocal = pycompat.identity
     strmethod = pycompat.identity
 
+if pycompat.iswindows:
+    tonativestr = strfromlocal
+else:
+    tonativestr = pycompat.identity
+
 if not _nativeenviron:
     # now encoding and helper functions are available, recreate the environ
     # dict to be exported to other modules
diff --git a/mercurial/scmutil.py b/mercurial/scmutil.py
--- a/mercurial/scmutil.py
+++ b/mercurial/scmutil.py
@@ -1356,11 +1356,11 @@  def extdatasource(repo, source):
         if spec.startswith("shell:"):
             # external commands should be run relative to the repo root
             cmd = spec[6:]
-            proc = subprocess.Popen(procutil.tonativestr(cmd),
+            proc = subprocess.Popen(encoding.tonativestr(cmd),
                                     shell=True, bufsize=-1,
                                     close_fds=procutil.closefds,
                                     stdout=subprocess.PIPE,
-                                    cwd=procutil.tonativestr(repo.root))
+                                    cwd=encoding.tonativestr(repo.root))
             src = proc.stdout
         else:
             # treat as a URL or file
diff --git a/mercurial/subrepo.py b/mercurial/subrepo.py
--- a/mercurial/subrepo.py
+++ b/mercurial/subrepo.py
@@ -951,7 +951,7 @@  class svnsubrepo(abstractsubrepo):
             env['LANG'] = lc_all
             del env['LC_ALL']
         env['LC_MESSAGES'] = 'C'
-        p = subprocess.Popen(pycompat.rapply(procutil.tonativestr, cmd),
+        p = subprocess.Popen(pycompat.rapply(encoding.tonativestr, cmd),
                              bufsize=-1, close_fds=procutil.closefds,
                              stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                              universal_newlines=True,
@@ -1270,10 +1270,10 @@  class gitsubrepo(abstractsubrepo):
             # insert the argument in the front,
             # the end of git diff arguments is used for paths
             commands.insert(1, '--color')
-        p = subprocess.Popen(pycompat.rapply(procutil.tonativestr,
+        p = subprocess.Popen(pycompat.rapply(encoding.tonativestr,
                                              [self._gitexecutable] + commands),
                              bufsize=-1,
-                             cwd=pycompat.rapply(procutil.tonativestr, cwd),
+                             cwd=pycompat.rapply(encoding.tonativestr, cwd),
                              env=procutil.tonativeenv(env),
                              close_fds=procutil.closefds,
                              stdout=subprocess.PIPE, stderr=errpipe)
diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -1133,7 +1133,7 @@  class ui(object):
 
         try:
             pager = subprocess.Popen(
-                procutil.tonativestr(command), shell=shell, bufsize=-1,
+                encoding.tonativestr(command), shell=shell, bufsize=-1,
                 close_fds=procutil.closefds, stdin=subprocess.PIPE,
                 stdout=procutil.stdout, stderr=procutil.stderr,
                 env=procutil.tonativeenv(procutil.shellenviron(env)))
diff --git a/mercurial/utils/procutil.py b/mercurial/utils/procutil.py
--- a/mercurial/utils/procutil.py
+++ b/mercurial/utils/procutil.py
@@ -120,14 +120,14 @@  def popen(cmd, mode='rb', bufsize=-1):
     raise error.ProgrammingError('unsupported mode: %r' % mode)
 
 def _popenreader(cmd, bufsize):
-    p = subprocess.Popen(tonativestr(quotecommand(cmd)),
+    p = subprocess.Popen(encoding.tonativestr(quotecommand(cmd)),
                          shell=True, bufsize=bufsize,
                          close_fds=closefds,
                          stdout=subprocess.PIPE)
     return _pfile(p, p.stdout)
 
 def _popenwriter(cmd, bufsize):
-    p = subprocess.Popen(tonativestr(quotecommand(cmd)),
+    p = subprocess.Popen(encoding.tonativestr(quotecommand(cmd)),
                          shell=True, bufsize=bufsize,
                          close_fds=closefds,
                          stdin=subprocess.PIPE)
@@ -137,7 +137,7 @@  def popen2(cmd, env=None):
     # Setting bufsize to -1 lets the system decide the buffer size.
     # The default for bufsize is 0, meaning unbuffered. This leads to
     # poor performance on Mac OS X: http://bugs.python.org/issue4194
-    p = subprocess.Popen(tonativestr(cmd),
+    p = subprocess.Popen(encoding.tonativestr(cmd),
                          shell=True, bufsize=-1,
                          close_fds=closefds,
                          stdin=subprocess.PIPE, stdout=subprocess.PIPE,
@@ -149,7 +149,7 @@  def popen3(cmd, env=None):
     return stdin, stdout, stderr
 
 def popen4(cmd, env=None, bufsize=-1):
-    p = subprocess.Popen(tonativestr(cmd),
+    p = subprocess.Popen(encoding.tonativestr(cmd),
                          shell=True, bufsize=bufsize,
                          close_fds=closefds,
                          stdin=subprocess.PIPE, stdout=subprocess.PIPE,
@@ -159,7 +159,7 @@  def popen4(cmd, env=None, bufsize=-1):
 
 def pipefilter(s, cmd):
     '''filter string S through command CMD, returning its output'''
-    p = subprocess.Popen(tonativestr(cmd),
+    p = subprocess.Popen(encoding.tonativestr(cmd),
                          shell=True, close_fds=closefds,
                          stdin=subprocess.PIPE, stdout=subprocess.PIPE)
     pout, perr = p.communicate(s)
@@ -325,18 +325,14 @@  def shellenviron(environ=None):
 if pycompat.iswindows:
     def shelltonative(cmd, env):
         return platform.shelltocmdexe(cmd, shellenviron(env))
-
-    tonativestr = encoding.strfromlocal
 else:
     def shelltonative(cmd, env):
         return cmd
 
-    tonativestr = pycompat.identity
-
 def tonativeenv(env):
     '''convert the environment from bytes to strings suitable for Popen(), etc.
     '''
-    return pycompat.rapply(tonativestr, env)
+    return pycompat.rapply(encoding.tonativestr, env)
 
 def system(cmd, environ=None, cwd=None, out=None):
     '''enhanced shell command execution.
@@ -351,15 +347,15 @@  def system(cmd, environ=None, cwd=None, 
     cmd = quotecommand(cmd)
     env = shellenviron(environ)
     if out is None or isstdout(out):
-        rc = subprocess.call(tonativestr(cmd),
+        rc = subprocess.call(encoding.tonativestr(cmd),
                              shell=True, close_fds=closefds,
                              env=tonativeenv(env),
-                             cwd=pycompat.rapply(tonativestr, cwd))
+                             cwd=pycompat.rapply(encoding.tonativestr, cwd))
     else:
-        proc = subprocess.Popen(tonativestr(cmd),
+        proc = subprocess.Popen(encoding.tonativestr(cmd),
                                 shell=True, close_fds=closefds,
                                 env=tonativeenv(env),
-                                cwd=pycompat.rapply(tonativestr, cwd),
+                                cwd=pycompat.rapply(encoding.tonativestr, cwd),
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.STDOUT)
         for line in iter(proc.stdout.readline, ''):