Mercurial > hgrepos > Python > libs > ConfigMix
comparison configmix/compat.py @ 487:d7f6f2afcee2
Instead of using u(str(v)) use specialized functions for PY2 and PY3
| author | Franz Glasner <f.glasner@feldmann-mg.com> |
|---|---|
| date | Fri, 17 Dec 2021 16:06:49 +0100 |
| parents | b96f49c9c76b |
| children | f454889e41fa |
comparison
equal
deleted
inserted
replaced
| 486:5a88c514d4e0 | 487:d7f6f2afcee2 |
|---|---|
| 14 "text_to_native_os_str", | 14 "text_to_native_os_str", |
| 15 "native_os_str_to_text", | 15 "native_os_str_to_text", |
| 16 "u", | 16 "u", |
| 17 "u2fs", | 17 "u2fs", |
| 18 "uchr", | 18 "uchr", |
| 19 "n"] | 19 "n", |
| 20 "str_and_u"] | |
| 20 | 21 |
| 21 | 22 |
| 22 import sys | 23 import sys |
| 23 import os | 24 import os |
| 24 import locale | 25 import locale |
| 76 if isinstance(s, str): | 77 if isinstance(s, str): |
| 77 return s | 78 return s |
| 78 else: | 79 else: |
| 79 return s.encode(encoding) | 80 return s.encode(encoding) |
| 80 | 81 |
| 82 def str_and_u(v): | |
| 83 if isinstance(v, unicode): # noqa: F821 | |
| 84 return v | |
| 85 else: | |
| 86 return u(str(v)) | |
| 87 | |
| 81 else: | 88 else: |
| 82 | 89 |
| 83 def text_to_native_os_str(s, encoding=None): | 90 def text_to_native_os_str(s, encoding=None): |
| 84 return s | 91 return s |
| 85 | 92 |
| 114 def n(s, encoding="utf-8"): | 121 def n(s, encoding="utf-8"): |
| 115 if isinstance(s, str): | 122 if isinstance(s, str): |
| 116 return s | 123 return s |
| 117 else: | 124 else: |
| 118 return s.decode(encoding) | 125 return s.decode(encoding) |
| 126 | |
| 127 def str_and_u(v): | |
| 128 """Convert the value in `v` of any type to a native string and then | |
| 129 to text (Unicode) | |
| 130 | |
| 131 """ | |
| 132 return str(v) |
