Patchwork pycompat: only accept a bytestring filepath in Python 2

login
register
mail settings
Submitter Martijn Pieters
Date Oct. 10, 2016, 10:11 p.m.
Message ID <d74c594b68768d14d941.1476137480@mjpieters-mbp>
Download mbox | patch
Permalink /patch/17025/
State Accepted
Headers show

Comments

Martijn Pieters - Oct. 10, 2016, 10:11 p.m.
# HG changeset patch
# User Martijn Pieters <mjpieters@fb.com>
# Date 1476137475 -3600
#      Mon Oct 10 23:11:15 2016 +0100
# Node ID d74c594b68768d14d9414b0ebe062d1b0bdd542a
# Parent  8d079c0594b35dfbf57baf8d83fde686a946920a
pycompat: only accept a bytestring filepath in Python 2
Yuya Nishihara - Oct. 11, 2016, 3:23 p.m.
On Mon, 10 Oct 2016 23:11:20 +0100, Martijn Pieters wrote:
> # HG changeset patch
> # User Martijn Pieters <mjpieters@fb.com>
> # Date 1476137475 -3600
> #      Mon Oct 10 23:11:15 2016 +0100
> # Node ID d74c594b68768d14d9414b0ebe062d1b0bdd542a
> # Parent  8d079c0594b35dfbf57baf8d83fde686a946920a
> pycompat: only accept a bytestring filepath in Python 2

Queued, thanks.

Patch

diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
--- a/mercurial/pycompat.py
+++ b/mercurial/pycompat.py
@@ -66,33 +66,15 @@ 
     def sysstr(s):
         return s
 
-    # Partial backport from os.py in Python 3
-    def _fscodec():
-        encoding = sys.getfilesystemencoding()
-        if encoding == 'mbcs':
-            errors = 'strict'
+    # Partial backport from os.py in Python 3, which only accepts bytes.
+    # In Python 2, our paths should only ever be bytes, a unicode path
+    # indicates a bug.
+    def fsencode(filename):
+        if isinstance(filename, str):
+            return filename
         else:
-            errors = 'surrogateescape'
-
-        def fsencode(filename):
-            """
-            Encode filename to the filesystem encoding with 'surrogateescape'
-            error handler, return bytes unchanged. On Windows, use 'strict'
-            error handler if the file system encoding is 'mbcs' (which is the
-            default encoding).
-            """
-            if isinstance(filename, str):
-                return filename
-            elif isinstance(filename, unicode):
-                return filename.encode(encoding, errors)
-            else:
-                raise TypeError(
-                    "expect str or unicode, not %s" % type(filename).__name__)
-
-        return fsencode
-
-    fsencode = _fscodec()
-    del _fscodec
+            raise TypeError(
+                "expect str, not %s" % type(filename).__name__)
 
 stringio = io.StringIO
 empty = _queue.Empty