view configmix/compat.py @ 542:f71d34dda19f

Add an optional C-implementation for configmix.config.unquote and configmix.config.pathstr2path. This is currently for Python 3.5+. It is tested with Python 3.7 and Python3.8 (FreeBSD 12.2 amd64, LLVM 10.0.1). A build for the stable API ("abi3") fails because PyUnicode_New() is currently not in the stable API. Also includes are extended tests for unquote() and pathstr2path().
author Franz Glasner <fzglas.hg@dom66.de>
date Fri, 31 Dec 2021 21:24:16 +0100
parents d7f6f2afcee2
children f454889e41fa
line wrap: on
line source

# -*- coding: utf-8 -*-
# :-
# :Copyright: (c) 2015-2021, Franz Glasner. All rights reserved.
# :License:   BSD-3-Clause. See LICENSE.txt for details.
# :-
"""Some minimal compatibility shim between Python2 and Python3

"""

from __future__ import division, absolute_import, print_function


__all__ = ["PY2",
           "text_to_native_os_str",
           "native_os_str_to_text",
           "u",
           "u2fs",
           "uchr",
           "n",
           "str_and_u"]


import sys
import os
import locale


PY2 = sys.version_info[0] <= 2


if PY2:

    _OS_ENCODING = locale.getpreferredencoding()

    _FS_ENCODING = sys.getfilesystemencoding() or _OS_ENCODING


    def text_to_native_os_str(s, encoding=None):
        if isinstance(s, unicode):                       # noqa: F821
            return s.encode(encoding or _OS_ENCODING)
        else:
            return s


    def native_os_str_to_text(s, encoding=None):
        return s.decode(encoding or _OS_ENCODING)


    def u(s, encoding="utf-8"):
        if isinstance(s, unicode):                       # noqa: F821
            return s
        else:
            return s.decode(encoding)


    def u2fs(s, force=False):
        """Convert a text (Unicode) string to the filesystem encoding.

        .. note:: The filesystem encoding on Python 3 is a Unicode text
                  string. The function is a noop when called on Python 3.

        .. note:: If `s` is already a byte string be permissive and
                  return `s` unchanged.

        """
        if isinstance(s, str):
            return s
        if not force and os.name in ("nt", "ce"):
            # WinNT and CE have native Unicode support: nothing to convert
            return s
        return s.encode(_FS_ENCODING)

    def uchr(n):
        return unichr(n)      # noqa: F821

    def n(s, encoding="utf-8"):
        if isinstance(s, str):
            return s
        else:
            return s.encode(encoding)

    def str_and_u(v):
        if isinstance(v, unicode):                        # noqa: F821
            return v
        else:
            return u(str(v))

else:

    def text_to_native_os_str(s, encoding=None):
        return s


    def native_os_str_to_text(s, encoding=None):
        return s


    def u(s, encoding="utf-8"):
        if isinstance(s, str):
            return s
        else:
            return s.decode(encoding)


    def u2fs(s, force=False):
        """Convert a text (Unicode) string to the filesystem encoding.

        .. note:: The filesystem encoding on Python 3 is a Unicode text
                  string. The function is a noop when called on Python 3.

        .. note:: If `s` is already a byte string be permissive and
                  return `s` unchanged.

        """
        assert isinstance(s, str)
        return s

    def uchr(n):
        return chr(n)

    def n(s, encoding="utf-8"):
        if isinstance(s, str):
            return s
        else:
            return s.decode(encoding)

    def str_and_u(v):
        """Convert the value in `v` of any type to a native string and then
        to text (Unicode)

        """
        return str(v)