Mercurial > hgrepos > Python > libs > ConfigMix
diff configmix/compat.py @ 166:b5ce9a8461bf
Use the filesystem encoding explicitely where appropriate.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Thu, 14 Mar 2019 01:35:16 +0100 |
| parents | e2e8d21b4122 |
| children | b3b5ed34d180 |
line wrap: on
line diff
--- a/configmix/compat.py Thu Mar 14 00:21:30 2019 +0100 +++ b/configmix/compat.py Thu Mar 14 01:35:16 2019 +0100 @@ -10,13 +10,15 @@ from __future__ import division, absolute_import, print_function import sys +import os import locale __all__ = ["PY2", "text_to_native_os_str", "native_os_str_to_text", - "u"] + "u", + "u2fs"] PY2 = sys.version_info[0] <= 2 @@ -26,6 +28,9 @@ _OS_ENCODING = locale.getpreferredencoding() + _FS_ENCODING = sys.getfilesystemencoding() or _OS_ENCODING + + def text_to_native_os_str(s, encoding=None): if isinstance(s, unicode): return s.encode(encoding or _OS_ENCODING) @@ -43,6 +48,21 @@ else: return s.decode(encoding) + + def u2fs(s, force=False): + """Convert a text (Unicode) string to the filesystem encoding. + + .. 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) + else: def text_to_native_os_str(s, encoding=None): @@ -52,8 +72,20 @@ 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. + + """ + assert isinstance(s, str) + return s
