Changes in src/allmydata/util/encodingutil.py [f47b45a:1504bec] in trunk
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified src/allmydata/util/encodingutil.py ¶
rf47b45a r1504bec 9 9 """ 10 10 11 from past.builtins import unicode12 11 from six import ensure_str 13 12 … … 54 53 55 54 filesystem_encoding = None 56 is_unicode_platform = True57 use_unicode_filepath = True58 55 59 56 def _reload(): … … 83 80 This is the inverse of ``unicode_to_argv``. 84 81 """ 85 if isinstance(s, unicode):82 if isinstance(s, str): 86 83 return s 87 84 … … 89 86 90 87 try: 91 return unicode(s, io_encoding)88 return str(s, io_encoding) 92 89 except UnicodeDecodeError: 93 90 raise usage.UsageError("Argument %s cannot be decoded as %s." % … … 113 110 Windows, this returns the input unmodified. 114 111 """ 115 precondition(isinstance(s, unicode), s)112 precondition(isinstance(s, str), s) 116 113 warnings.warn("This is unnecessary.", DeprecationWarning) 117 114 if sys.platform == "win32": … … 167 164 the responsibility of stdout/stderr, they expect Unicode by default. 168 165 """ 169 precondition(isinstance(s, unicode), s)166 precondition(isinstance(s, str), s) 170 167 warnings.warn("This is unnecessary.", DeprecationWarning) 171 168 return s … … 215 212 """ 216 213 result = quote_output(*args, **kwargs) 217 if isinstance(result, unicode):214 if isinstance(result, str): 218 215 return result 219 216 # Since we're quoting, the assumption is this will be read by a human, and … … 240 237 On Python 3, returns Unicode strings. 241 238 """ 242 precondition(isinstance(s, (bytes, unicode)), s)239 precondition(isinstance(s, (bytes, str)), s) 243 240 # Since we're quoting, the assumption is this will be read by a human, and 244 241 # therefore printed, so stdout's encoding is the plausible one. io_encoding … … 279 276 280 277 def quote_local_unicode_path(path, quotemarks=True): 281 precondition(isinstance(path, unicode), path)278 precondition(isinstance(path, str), path) 282 279 283 280 if sys.platform == "win32" and path.startswith(u"\\\\?\\"): … … 299 296 fp = fp.child(segment) 300 297 301 if isinstance(fp.path, unicode) and not use_unicode_filepath: 302 return FilePath(fp.path.encode(filesystem_encoding)) 303 else: 304 return fp 298 return fp 305 299 306 300 def to_filepath(path): 307 precondition(isinstance(path, unicode if use_unicode_filepath else (bytes, unicode)), 308 path=path) 309 310 if isinstance(path, unicode) and not use_unicode_filepath: 311 path = path.encode(filesystem_encoding) 301 precondition(isinstance(path, str), path=path) 312 302 313 303 if sys.platform == "win32": 314 _assert(isinstance(path, unicode), path=path)304 _assert(isinstance(path, str), path=path) 315 305 if path.startswith(u"\\\\?\\") and len(path) > 4: 316 306 # FilePath normally strips trailing path separators, but not in this case. … … 320 310 321 311 def _decode(s): 322 precondition(isinstance(s, (bytes, unicode)), s=s)312 precondition(isinstance(s, (bytes, str)), s=s) 323 313 324 314 if isinstance(s, bytes): … … 341 331 Does the current platform handle Unicode filenames natively? 342 332 """ 343 return is_unicode_platform333 return True 344 334 345 335 class FilenameEncodingError(Exception): … … 350 340 pass 351 341 352 def listdir_unicode_fallback(path):353 """354 This function emulates a fallback Unicode API similar to one available355 under Windows or MacOS X.356 357 If badly encoded filenames are encountered, an exception is raised.358 """359 precondition(isinstance(path, unicode), path)360 361 try:362 byte_path = path.encode(filesystem_encoding)363 except (UnicodeEncodeError, UnicodeDecodeError):364 raise FilenameEncodingError(path)365 366 try:367 return [unicode(fn, filesystem_encoding) for fn in os.listdir(byte_path)]368 except UnicodeDecodeError as e:369 raise FilenameEncodingError(e.object)370 371 342 def listdir_unicode(path): 372 343 """ … … 374 345 Unicode API even under platforms that don't provide one natively. 375 346 """ 376 precondition(isinstance(path, unicode), path) 377 378 # On Windows and MacOS X, the Unicode API is used 379 # On other platforms (ie. Unix systems), the byte-level API is used 380 381 if is_unicode_platform: 382 return os.listdir(path) 383 else: 384 return listdir_unicode_fallback(path) 347 precondition(isinstance(path, str), path) 348 return os.listdir(path) 385 349 386 350 def listdir_filepath(fp):
Note: See TracChangeset
for help on using the changeset viewer.