[PATCH] py3: add an os.fsencode backport to ease path handling

Yuya Nishihara yuya at tcha.org
Sun Oct 9 16:51:40 EDT 2016


On Sun, 09 Oct 2016 17:45:16 +0200, Martijn Pieters wrote:
> # HG changeset patch
> # User Martijn Pieters <mjpieters at fb.com>
> # Date 1476027863 -7200
> #      Sun Oct 09 17:44:23 2016 +0200
> # Node ID 1fef9008cbd2098f058f1458df4d59552da88c16
> # Parent  82489cd912f332be976cf432673ad47af0d04cd7
> py3: add an os.fsencode backport to ease path handling
> 
> diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
> --- a/mercurial/pycompat.py
> +++ b/mercurial/pycompat.py
> @@ -34,6 +34,7 @@
>  if ispy3:
>      import builtins
>      import functools
> +    from os import fsencode
>  
>      def sysstr(s):
>          """Return a keyword str to be passed to Python functions such as
> @@ -64,6 +65,34 @@
>      def sysstr(s):
>          return s
>  
> +    # Partial backport from os.py in Python 3
> +    def _fscodec():
> +        encoding = sys.getfilesystemencoding()
> +        if encoding == 'mbcs':
> +            errors = 'strict'
> +        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)

It appears that 'surrogateescape' is a Python 3 thing. We have
encoding.fromutf8b/toutf8b() which should do the same thing for 'utf-8', but
I have no idea how 'surrogateescape' works for the other encodings.

https://docs.python.org/3/library/codecs.html


More information about the Mercurial-devel mailing list