changeset 30133:f6dcda7505f9

pycompat: only accept a bytestring filepath in Python 2
author Martijn Pieters <mjpieters@fb.com>
date Mon, 10 Oct 2016 23:11:15 +0100
parents 72f75eda02ba
children 165c9526e594
files mercurial/pycompat.py
diffstat 1 files changed, 8 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/pycompat.py	Mon Oct 10 05:31:31 2016 -0700
+++ b/mercurial/pycompat.py	Mon Oct 10 23:11:15 2016 +0100
@@ -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