changeset 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 5a88c514d4e0
children 298510ec8171
files configmix/compat.py configmix/config.py
diffstat 2 files changed, 17 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/configmix/compat.py	Fri Dec 17 15:56:53 2021 +0100
+++ b/configmix/compat.py	Fri Dec 17 16:06:49 2021 +0100
@@ -16,7 +16,8 @@
            "u",
            "u2fs",
            "uchr",
-           "n"]
+           "n",
+           "str_and_u"]
 
 
 import sys
@@ -78,6 +79,12 @@
         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):
@@ -116,3 +123,10 @@
             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)
--- a/configmix/config.py	Fri Dec 17 15:56:53 2021 +0100
+++ b/configmix/config.py	Fri Dec 17 16:06:49 2021 +0100
@@ -28,7 +28,7 @@
     from urlparse import urlsplit
 
 from .variables import lookup_varns, lookup_filter
-from .compat import u, uchr, n, PY2
+from .compat import u, uchr, n, str_and_u, PY2
 from .constants import REF_NAMESPACE, NONE_FILTER, EMPTY_FILTER
 
 
@@ -699,7 +699,7 @@
                 # None and/or empty str are handled equally here
                 pass
             else:
-                res_append(u(str(varvalue)))
+                res_append(str_and_u(varvalue))
             # don't re-evaluate because `self.getvar_s()` expands already
             rest = end + 2
             start = s.find(self._STARTTOK, rest)