Mercurial > hgrepos > Python > libs > ConfigMix
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 165:6ca90e80f4f4 | 166:b5ce9a8461bf |
|---|---|
| 8 """ | 8 """ |
| 9 | 9 |
| 10 from __future__ import division, absolute_import, print_function | 10 from __future__ import division, absolute_import, print_function |
| 11 | 11 |
| 12 import sys | 12 import sys |
| 13 import os | |
| 13 import locale | 14 import locale |
| 14 | 15 |
| 15 | 16 |
| 16 __all__ = ["PY2", | 17 __all__ = ["PY2", |
| 17 "text_to_native_os_str", | 18 "text_to_native_os_str", |
| 18 "native_os_str_to_text", | 19 "native_os_str_to_text", |
| 19 "u"] | 20 "u", |
| 21 "u2fs"] | |
| 20 | 22 |
| 21 | 23 |
| 22 PY2 = sys.version_info[0] <= 2 | 24 PY2 = sys.version_info[0] <= 2 |
| 23 | 25 |
| 24 | 26 |
| 25 if PY2: | 27 if PY2: |
| 26 | 28 |
| 27 _OS_ENCODING = locale.getpreferredencoding() | 29 _OS_ENCODING = locale.getpreferredencoding() |
| 30 | |
| 31 _FS_ENCODING = sys.getfilesystemencoding() or _OS_ENCODING | |
| 32 | |
| 28 | 33 |
| 29 def text_to_native_os_str(s, encoding=None): | 34 def text_to_native_os_str(s, encoding=None): |
| 30 if isinstance(s, unicode): | 35 if isinstance(s, unicode): |
| 31 return s.encode(encoding or _OS_ENCODING) | 36 return s.encode(encoding or _OS_ENCODING) |
| 32 else: | 37 else: |
| 41 if isinstance(s, unicode): | 46 if isinstance(s, unicode): |
| 42 return s | 47 return s |
| 43 else: | 48 else: |
| 44 return s.decode(encoding) | 49 return s.decode(encoding) |
| 45 | 50 |
| 51 | |
| 52 def u2fs(s, force=False): | |
| 53 """Convert a text (Unicode) string to the filesystem encoding. | |
| 54 | |
| 55 .. note:: If `s` is already a byte string be permissive and | |
| 56 return `s` unchanged. | |
| 57 | |
| 58 """ | |
| 59 if isinstance(s, str): | |
| 60 return s | |
| 61 if not force and os.name in ("nt", "ce"): | |
| 62 # WinNT and CE have native Unicode support: nothing to convert | |
| 63 return s | |
| 64 return s.encode(_FS_ENCODING) | |
| 65 | |
| 46 else: | 66 else: |
| 47 | 67 |
| 48 def text_to_native_os_str(s, encoding=None): | 68 def text_to_native_os_str(s, encoding=None): |
| 49 return s | 69 return s |
| 50 | 70 |
| 51 | 71 |
| 52 def native_os_str_to_text(s, encoding=None): | 72 def native_os_str_to_text(s, encoding=None): |
| 53 return s | 73 return s |
| 54 | 74 |
| 75 | |
| 55 def u(s, encoding="utf-8"): | 76 def u(s, encoding="utf-8"): |
| 56 if isinstance(s, str): | 77 if isinstance(s, str): |
| 57 return s | 78 return s |
| 58 else: | 79 else: |
| 59 return s.decode(encoding) | 80 return s.decode(encoding) |
| 81 | |
| 82 | |
| 83 def u2fs(s, force=False): | |
| 84 """Convert a text (Unicode) string to the filesystem encoding. | |
| 85 | |
| 86 .. note:: The filesystem encoding on Python 3 is a Unicode text | |
| 87 string. The function is a noop when called on Python 3. | |
| 88 | |
| 89 """ | |
| 90 assert isinstance(s, str) | |
| 91 return s |
