py3: use pycompat.fsencode() to convert __file__ to bytes
authorPulkit Goyal <7895pulkit@gmail.com>
Mon, 20 Feb 2017 18:40:42 +0530
changeset 31074 2912b06905dc
parent 31073 2cf1e5207fdf
child 31075 21f1f97ab212
py3: use pycompat.fsencode() to convert __file__ to bytes __file__ returns unicodes on Python 3. This patch uses pycompat.fsencode() to convert them to bytes.
mercurial/debugcommands.py
mercurial/extensions.py
mercurial/i18n.py
mercurial/sslutil.py
mercurial/statprof.py
mercurial/util.py
--- a/mercurial/debugcommands.py	Wed Feb 22 10:14:18 2017 -0800
+++ b/mercurial/debugcommands.py	Mon Feb 20 18:40:42 2017 +0530
@@ -692,7 +692,7 @@
     fm = ui.formatter('debugextensions', opts)
     for extname, extmod in sorted(exts, key=operator.itemgetter(0)):
         isinternal = extensions.ismoduleinternal(extmod)
-        extsource = extmod.__file__
+        extsource = pycompat.fsencode(extmod.__file__)
         if isinternal:
             exttestedwith = []  # never expose magic string to users
         else:
@@ -970,7 +970,7 @@
     fm.write('hgmodulepolicy', _("checking module policy (%s)\n"),
              policy.policy)
     fm.write('hgmodules', _("checking installed modules (%s)...\n"),
-             os.path.dirname(__file__))
+             os.path.dirname(pycompat.fsencode(__file__)))
 
     err = None
     try:
--- a/mercurial/extensions.py	Wed Feb 22 10:14:18 2017 -0800
+++ b/mercurial/extensions.py	Mon Feb 20 18:40:42 2017 +0530
@@ -362,7 +362,8 @@
     '''find paths of disabled extensions. returns a dict of {name: path}
     removes /__init__.py from packages if strip_init is True'''
     import hgext
-    extpath = os.path.dirname(os.path.abspath(hgext.__file__))
+    extpath = os.path.dirname(
+        os.path.abspath(pycompat.fsencode(hgext.__file__)))
     try: # might not be a filesystem path
         files = os.listdir(extpath)
     except OSError:
--- a/mercurial/i18n.py	Wed Feb 22 10:14:18 2017 -0800
+++ b/mercurial/i18n.py	Mon Feb 20 18:40:42 2017 +0530
@@ -21,7 +21,7 @@
 if getattr(sys, 'frozen', None) is not None:
     module = pycompat.sysexecutable
 else:
-    module = __file__
+    module = pycompat.fsencode(__file__)
 
 try:
     unicode
--- a/mercurial/sslutil.py	Wed Feb 22 10:14:18 2017 -0800
+++ b/mercurial/sslutil.py	Mon Feb 20 18:40:42 2017 +0530
@@ -720,7 +720,8 @@
     # to load the system CA store. If we're running on Apple Python, use this
     # trick.
     if _plainapplepython():
-        dummycert = os.path.join(os.path.dirname(__file__), 'dummycert.pem')
+        dummycert = os.path.join(
+            os.path.dirname(pycompat.fsencode(__file__)), 'dummycert.pem')
         if os.path.exists(dummycert):
             return dummycert
 
--- a/mercurial/statprof.py	Wed Feb 22 10:14:18 2017 -0800
+++ b/mercurial/statprof.py	Mon Feb 20 18:40:42 2017 +0530
@@ -724,7 +724,7 @@
 
     if path in _pathcache:
         return _pathcache[path]
-    hgpath = encoding.__file__.rsplit(os.sep, 2)[0]
+    hgpath = pycompat.fsencode(encoding.__file__).rsplit(os.sep, 2)[0]
     for p in [hgpath] + sys.path:
         prefix = p + os.sep
         if path.startswith(prefix):
--- a/mercurial/util.py	Wed Feb 22 10:14:18 2017 -0800
+++ b/mercurial/util.py	Mon Feb 20 18:40:42 2017 +0530
@@ -955,10 +955,7 @@
     # executable version (py2exe) doesn't support __file__
     datapath = os.path.dirname(pycompat.sysexecutable)
 else:
-    datapath = os.path.dirname(__file__)
-
-if not isinstance(datapath, bytes):
-    datapath = pycompat.fsencode(datapath)
+    datapath = os.path.dirname(pycompat.fsencode(__file__))
 
 i18n.setdatapath(datapath)
 
@@ -980,8 +977,9 @@
                 _sethgexecutable(encoding.environ['EXECUTABLEPATH'])
             else:
                 _sethgexecutable(pycompat.sysexecutable)
-        elif os.path.basename(getattr(mainmod, '__file__', '')) == 'hg':
-            _sethgexecutable(mainmod.__file__)
+        elif (os.path.basename(
+            pycompat.fsencode(getattr(mainmod, '__file__', ''))) == 'hg'):
+            _sethgexecutable(pycompat.fsencode(mainmod.__file__))
         else:
             exe = findexe('hg') or os.path.basename(sys.argv[0])
             _sethgexecutable(exe)