diff cutils/util/walk.py @ 283:99b78fa04bc1

FIX: Unittests in test_walk.py: adjust to the new escaping behaviour
author Franz Glasner <fzglas.hg@dom66.de>
date Sun, 23 Feb 2025 14:45:26 +0100
parents d507ae4943d5
children 48430941c18c
line wrap: on
line diff
--- a/cutils/util/walk.py	Sun Feb 23 12:40:28 2025 +0100
+++ b/cutils/util/walk.py	Sun Feb 23 14:45:26 2025 +0100
@@ -143,23 +143,26 @@
 
     @staticmethod
     def alt_fs(what):
+        #
+        # Prevent double encoding ...
+        # ... and hope that the current FS encoding is compatible
+        #     with it
+        #
+        if isinstance(what, bytes):
+            s = (what.replace(b'\\', b"\\x5c")
+                 .replace(b'\n', b"\\x0a")
+                 .replace(b'\r', b"\\x0d"))
+        else:
+            s = (what.replace(u'\\', u"\\x5c")
+                 .replace(u'\n', u"\\x0a")
+                 .replace(u'\r', u"\\x0d"))
         if PY2:
-            if isinstance(what, bytes):
-                s = what
+            if isinstance(s, bytes):
+                return s
             else:
-                #
-                # Prevent double encoding ...
-                # ... and hope that the current FS encoding is compatible
-                #     with it
-                #
-                s = what.replace(u'\\', u"\\x5c")
-                s = s.encode(_FSENCODING, "backslashreplace")
-                return s.replace(b'\n', b"\\x0a").replace(b'\r', b"\\x0d")
+                return s.encode(_FSENCODING, "backslashreplace")
         else:
-            s = os.fsencode(what)
-        return (s.replace(b'\\', b"\\x5c")
-                .replace(b'\n', b"\\x0a")
-                .replace(b'\r', b"\\x0d"))
+            return os.fsencode(s)
 
     @property
     def uname(self):
@@ -247,21 +250,31 @@
 
     @staticmethod
     def alt_u8(what):
+        #
+        # Prevent double encoding ...
+        # ... and hope that the current UTF-8 is compatible
+        #     with it
+        #
+        if isinstance(what, bytes):
+            s = (what.replace(b'\\', b"\\x5c")
+                 .replace(b'\n', b"\\x0a")
+                 .replace(b'\r', b"\\x0d"))
+        else:
+            s = (what.replace(u'\\', u"\\x5c")
+                 .replace(u'\n', u"\\x0a")
+                 .replace(u'\r', u"\\x0d"))
         if PY2:
-            if isinstance(what, bytes):
+            if isinstance(s, bytes):
                 try:
-                    s = (what.decode(_FSENCODING, "strict")
-                         .encode("utf-8", "strict"))
+                    return (s.decode(_FSENCODING, "strict")
+                            .encode("utf-8", "strict"))
                 except UnicodeError:
-                    s = (WalkDirEntry.surrogate_decode(what)
-                         .encode("ascii", "backslashreplace"))
+                    return (WalkDirEntry.surrogate_decode(s)
+                            .encode("ascii", "backslashreplace"))
             else:
-                s = what.encode("ascii", "backslashreplace")
+                return s.encode("ascii", "backslashreplace")
         else:
-            s = what.encode("utf-8", "backslashreplace")
-        return (s.replace(b'\\', b"\\x5c")
-                .replace(b'\n', b"\\x0a")
-                .replace(b'\r', b"\\x0d"))
+            return s.encode("utf-8", "backslashreplace")
 
     @property
     def is_symlink(self):