comparison 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
comparison
equal deleted inserted replaced
282:d507ae4943d5 283:99b78fa04bc1
141 """ 141 """
142 return WalkDirEntry.alt_fs(self._path) 142 return WalkDirEntry.alt_fs(self._path)
143 143
144 @staticmethod 144 @staticmethod
145 def alt_fs(what): 145 def alt_fs(what):
146 if PY2: 146 #
147 if isinstance(what, bytes): 147 # Prevent double encoding ...
148 s = what 148 # ... and hope that the current FS encoding is compatible
149 # with it
150 #
151 if isinstance(what, bytes):
152 s = (what.replace(b'\\', b"\\x5c")
153 .replace(b'\n', b"\\x0a")
154 .replace(b'\r', b"\\x0d"))
155 else:
156 s = (what.replace(u'\\', u"\\x5c")
157 .replace(u'\n', u"\\x0a")
158 .replace(u'\r', u"\\x0d"))
159 if PY2:
160 if isinstance(s, bytes):
161 return s
149 else: 162 else:
150 # 163 return s.encode(_FSENCODING, "backslashreplace")
151 # Prevent double encoding ... 164 else:
152 # ... and hope that the current FS encoding is compatible 165 return os.fsencode(s)
153 # with it
154 #
155 s = what.replace(u'\\', u"\\x5c")
156 s = s.encode(_FSENCODING, "backslashreplace")
157 return s.replace(b'\n', b"\\x0a").replace(b'\r', b"\\x0d")
158 else:
159 s = os.fsencode(what)
160 return (s.replace(b'\\', b"\\x5c")
161 .replace(b'\n', b"\\x0a")
162 .replace(b'\r', b"\\x0d"))
163 166
164 @property 167 @property
165 def uname(self): 168 def uname(self):
166 """Always "real", strictly encoded Unicode or `None` if this is not 169 """Always "real", strictly encoded Unicode or `None` if this is not
167 possible. 170 possible.
245 def alt_u8path(self): 248 def alt_u8path(self):
246 return WalkDirEntry.alt_u8(self._path) 249 return WalkDirEntry.alt_u8(self._path)
247 250
248 @staticmethod 251 @staticmethod
249 def alt_u8(what): 252 def alt_u8(what):
250 if PY2: 253 #
251 if isinstance(what, bytes): 254 # Prevent double encoding ...
255 # ... and hope that the current UTF-8 is compatible
256 # with it
257 #
258 if isinstance(what, bytes):
259 s = (what.replace(b'\\', b"\\x5c")
260 .replace(b'\n', b"\\x0a")
261 .replace(b'\r', b"\\x0d"))
262 else:
263 s = (what.replace(u'\\', u"\\x5c")
264 .replace(u'\n', u"\\x0a")
265 .replace(u'\r', u"\\x0d"))
266 if PY2:
267 if isinstance(s, bytes):
252 try: 268 try:
253 s = (what.decode(_FSENCODING, "strict") 269 return (s.decode(_FSENCODING, "strict")
254 .encode("utf-8", "strict")) 270 .encode("utf-8", "strict"))
255 except UnicodeError: 271 except UnicodeError:
256 s = (WalkDirEntry.surrogate_decode(what) 272 return (WalkDirEntry.surrogate_decode(s)
257 .encode("ascii", "backslashreplace")) 273 .encode("ascii", "backslashreplace"))
258 else: 274 else:
259 s = what.encode("ascii", "backslashreplace") 275 return s.encode("ascii", "backslashreplace")
260 else: 276 else:
261 s = what.encode("utf-8", "backslashreplace") 277 return s.encode("utf-8", "backslashreplace")
262 return (s.replace(b'\\', b"\\x5c")
263 .replace(b'\n', b"\\x0a")
264 .replace(b'\r', b"\\x0d"))
265 278
266 @property 279 @property
267 def is_symlink(self): 280 def is_symlink(self):
268 return self._is_symlink 281 return self._is_symlink
269 282