Patchwork [1,of,2] py3: replace pycompat.getenv with encoding.environ.get

login
register
mail settings
Submitter Pulkit Goyal
Date Jan. 15, 2017, 8:12 a.m.
Message ID <8980743ae0283ac02b20.1484467971@pulkit-goyal>
Download mbox | patch
Permalink /patch/18221/
State Accepted
Headers show

Comments

Pulkit Goyal - Jan. 15, 2017, 8:12 a.m.
# HG changeset patch
# User Pulkit Goyal <7895pulkit@gmail.com>
# Date 1484466425 -19800
#      Sun Jan 15 13:17:05 2017 +0530
# Node ID 8980743ae0283ac02b20be0732ac23e4dd50dc10
# Parent  79f98c1b8d718bfa94f12eb62872598ad7ec9c2e
py3: replace pycompat.getenv with encoding.environ.get

pycompat.getenv returns os.getenvb on py3 which is not available on Windows.
This patch replaces them with encoding.environ.get and checks to ensure no
new instances of os.getenv or os.setenv are introduced.
Yuya Nishihara - Jan. 15, 2017, 8:53 a.m.
On Sun, 15 Jan 2017 13:42:51 +0530, Pulkit Goyal wrote:
> # HG changeset patch
> # User Pulkit Goyal <7895pulkit@gmail.com>
> # Date 1484466425 -19800
> #      Sun Jan 15 13:17:05 2017 +0530
> # Node ID 8980743ae0283ac02b20be0732ac23e4dd50dc10
> # Parent  79f98c1b8d718bfa94f12eb62872598ad7ec9c2e
> py3: replace pycompat.getenv with encoding.environ.get

Queued this, thanks!

Patch

diff -r 79f98c1b8d71 -r 8980743ae028 contrib/check-code.py
--- a/contrib/check-code.py	Sat Jan 14 20:05:15 2017 +0530
+++ b/contrib/check-code.py	Sun Jan 15 13:17:05 2017 +0530
@@ -464,9 +464,10 @@ 
     (r'os\.sep', "use pycompat.ossep instead (py3)"),
     (r'os\.pathsep', "use pycompat.ospathsep instead (py3)"),
     (r'os\.altsep', "use pycompat.osaltsep instead (py3)"),
-    (r'os\.getenv', "use pycompat.osgetenv instead (py3)"),
     (r'sys\.platform', "use pycompat.sysplatform instead (py3)"),
     (r'getopt\.getopt', "use pycompat.getoptb instead (py3)"),
+    (r'os\.getenv', "use encoding.environ.get instead"),
+    (r'os\.setenv', "modifying the environ dict is not preferred"),
   ],
   # warnings
   [],
diff -r 79f98c1b8d71 -r 8980743ae028 hgext/largefiles/lfutil.py
--- a/hgext/largefiles/lfutil.py	Sat Jan 14 20:05:15 2017 +0530
+++ b/hgext/largefiles/lfutil.py	Sun Jan 15 13:17:05 2017 +0530
@@ -19,6 +19,7 @@ 
 
 from mercurial import (
     dirstate,
+    encoding,
     error,
     httpconnection,
     match as matchmod,
@@ -74,19 +75,19 @@ 
     if path:
         return path
     if pycompat.osname == 'nt':
-        appdata = pycompat.osgetenv('LOCALAPPDATA',\
-                        pycompat.osgetenv('APPDATA'))
+        appdata = encoding.environ.get('LOCALAPPDATA',\
+                        encoding.environ.get('APPDATA'))
         if appdata:
             return os.path.join(appdata, longname)
     elif platform.system() == 'Darwin':
-        home = pycompat.osgetenv('HOME')
+        home = encoding.environ.get('HOME')
         if home:
             return os.path.join(home, 'Library', 'Caches', longname)
     elif pycompat.osname == 'posix':
-        path = pycompat.osgetenv('XDG_CACHE_HOME')
+        path = encoding.environ.get('XDG_CACHE_HOME')
         if path:
             return os.path.join(path, longname)
-        home = pycompat.osgetenv('HOME')
+        home = encoding.environ.get('HOME')
         if home:
             return os.path.join(home, '.cache', longname)
     else:
diff -r 79f98c1b8d71 -r 8980743ae028 mercurial/profiling.py
--- a/mercurial/profiling.py	Sat Jan 14 20:05:15 2017 +0530
+++ b/mercurial/profiling.py	Sun Jan 15 13:17:05 2017 +0530
@@ -12,8 +12,8 @@ 
 
 from .i18n import _
 from . import (
+    encoding,
     error,
-    pycompat,
     util,
 )
 
@@ -120,7 +120,7 @@ 
     Profiling is active when the context manager is active. When the context
     manager exits, profiling results will be written to the configured output.
     """
-    profiler = pycompat.osgetenv('HGPROF')
+    profiler = encoding.environ.get('HGPROF')
     if profiler is None:
         profiler = ui.config('profiling', 'type', default='stat')
     if profiler not in ('ls', 'stat', 'flame'):
diff -r 79f98c1b8d71 -r 8980743ae028 mercurial/pycompat.py
--- a/mercurial/pycompat.py	Sat Jan 14 20:05:15 2017 +0530
+++ b/mercurial/pycompat.py	Sun Jan 15 13:17:05 2017 +0530
@@ -46,7 +46,6 @@ 
     ospathsep = os.pathsep.encode('ascii')
     ossep = os.sep.encode('ascii')
     osaltsep = os.altsep
-    osgetenv = os.getenvb
     if osaltsep:
         osaltsep = osaltsep.encode('ascii')
     # os.getcwd() on Python 3 returns string, but it has os.getcwdb() which
@@ -169,7 +168,6 @@ 
     sysargv = sys.argv
     sysplatform = sys.platform
     getcwd = os.getcwd
-    osgetenv = os.getenv
     sysexecutable = sys.executable
     shlexsplit = shlex.split
 
diff -r 79f98c1b8d71 -r 8980743ae028 mercurial/url.py
--- a/mercurial/url.py	Sat Jan 14 20:05:15 2017 +0530
+++ b/mercurial/url.py	Sun Jan 15 13:17:05 2017 +0530
@@ -15,10 +15,10 @@ 
 
 from .i18n import _
 from . import (
+    encoding,
     error,
     httpconnection as httpconnectionmod,
     keepalive,
-    pycompat,
     sslutil,
     util,
 )
@@ -81,7 +81,7 @@ 
 class proxyhandler(urlreq.proxyhandler):
     def __init__(self, ui):
         proxyurl = (ui.config("http_proxy", "host") or
-                        pycompat.osgetenv('http_proxy'))
+                        encoding.environ.get('http_proxy'))
         # XXX proxyauthinfo = None
 
         if proxyurl:
@@ -99,7 +99,7 @@ 
             no_list.extend([p.lower() for
                             p in ui.configlist("http_proxy", "no")])
             no_list.extend([p.strip().lower() for
-                            p in pycompat.osgetenv("no_proxy", '').split(',')
+                            p in encoding.environ.get("no_proxy", '').split(',')
                             if p.strip()])
             # "http_proxy.always" config is for running tests on localhost
             if ui.configbool("http_proxy", "always"):