# HG changeset patch # User Franz Glasner # Date 1740318326 -3600 # Node ID 99b78fa04bc1223737f9e7858e0b9be1b144b080 # Parent d507ae4943d546ceec8ed170bb34d263e2f16b3b FIX: Unittests in test_walk.py: adjust to the new escaping behaviour diff -r d507ae4943d5 -r 99b78fa04bc1 cutils/util/walk.py --- 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): diff -r d507ae4943d5 -r 99b78fa04bc1 tests/test_walk.py --- a/tests/test_walk.py Sun Feb 23 12:40:28 2025 +0100 +++ b/tests/test_walk.py Sun Feb 23 14:45:26 2025 +0100 @@ -127,8 +127,8 @@ b"tests", b"test-\xc4", _do_stat=False) self.assertEqual(b"test-\xc4", entry.name) self.assertEqual(b"tests/test-\xc4", entry.path) - self.assertEqual(b"test-\xc4", entry.fsname) - self.assertEqual(b"tests/test-\xc4", entry.fspath) + self.assertIsNone(entry.fsname) + self.assertIsNone(entry.fspath) self.assertEqual(b"test-\xc4", entry.alt_fsname) self.assertEqual(b"tests/test-\xc4", entry.alt_fspath) @@ -147,7 +147,7 @@ self.assertEqual(b"test", entry.name) self.assertEqual(b"tests\xc5/test", entry.path) self.assertEqual(b"test", entry.fsname) - self.assertEqual(b"tests\xc5/test", entry.fspath) + self.assertIsNone(entry.fspath) self.assertEqual(b"test", entry.alt_fsname) self.assertEqual(b"tests\xc5/test", entry.alt_fspath)