py3: replace pycompat.getenv with encoding.environ.get
authorPulkit Goyal <7895pulkit@gmail.com>
Sun, 15 Jan 2017 13:17:05 +0530
changeset 30820 6a70cf94d1b5
parent 30819 897726622877
child 30821 7005c03f7387
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.
contrib/check-code.py
hgext/largefiles/lfutil.py
mercurial/profiling.py
mercurial/pycompat.py
mercurial/url.py
--- a/contrib/check-code.py	Sun Jan 15 16:33:15 2017 +0900
+++ b/contrib/check-code.py	Sun Jan 15 13:17:05 2017 +0530
@@ -465,9 +465,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
   [],
--- a/hgext/largefiles/lfutil.py	Sun Jan 15 16:33:15 2017 +0900
+++ 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:
--- a/mercurial/profiling.py	Sun Jan 15 16:33:15 2017 +0900
+++ 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'):
--- a/mercurial/pycompat.py	Sun Jan 15 16:33:15 2017 +0900
+++ 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
 
--- a/mercurial/url.py	Sun Jan 15 16:33:15 2017 +0900
+++ 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"):