[PATCH 1 of 2] py3: use pycompat.fsencode() to convert __file__ to bytes

Pulkit Goyal 7895pulkit at gmail.com
Thu Feb 23 11:59:00 UTC 2017


# HG changeset patch
# User Pulkit Goyal <7895pulkit at gmail.com>
# Date 1487596242 -19800
#      Mon Feb 20 18:40:42 2017 +0530
# Node ID 80693cfda5a63dee28cf9e63cf0e8cef3f27f23a
# Parent  37ab9e20991c1d39014db8297065357fbded0213
py3: use pycompat.fsencode() to convert __file__ to bytes

__file__ returns unicodes on Python 3. This patch uses pycompat.fsenocde() to
convert them to bytes.

diff -r 37ab9e20991c -r 80693cfda5a6 mercurial/debugcommands.py
--- a/mercurial/debugcommands.py	Sat Feb 18 21:30:28 2017 +1100
+++ 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:
diff -r 37ab9e20991c -r 80693cfda5a6 mercurial/extensions.py
--- a/mercurial/extensions.py	Sat Feb 18 21:30:28 2017 +1100
+++ 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:
diff -r 37ab9e20991c -r 80693cfda5a6 mercurial/i18n.py
--- a/mercurial/i18n.py	Sat Feb 18 21:30:28 2017 +1100
+++ 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
diff -r 37ab9e20991c -r 80693cfda5a6 mercurial/sslutil.py
--- a/mercurial/sslutil.py	Sat Feb 18 21:30:28 2017 +1100
+++ 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
 
diff -r 37ab9e20991c -r 80693cfda5a6 mercurial/statprof.py
--- a/mercurial/statprof.py	Sat Feb 18 21:30:28 2017 +1100
+++ 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):
diff -r 37ab9e20991c -r 80693cfda5a6 mercurial/util.py
--- a/mercurial/util.py	Sat Feb 18 21:30:28 2017 +1100
+++ 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)


More information about the Mercurial-devel mailing list