Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified src/allmydata/util/encodingutil.py

    rf47b45a r1504bec  
    99"""
    1010
    11 from past.builtins import unicode
    1211from six import ensure_str
    1312
     
    5453
    5554filesystem_encoding = None
    56 is_unicode_platform = True
    57 use_unicode_filepath = True
    5855
    5956def _reload():
     
    8380    This is the inverse of ``unicode_to_argv``.
    8481    """
    85     if isinstance(s, unicode):
     82    if isinstance(s, str):
    8683        return s
    8784
     
    8986
    9087    try:
    91         return unicode(s, io_encoding)
     88        return str(s, io_encoding)
    9289    except UnicodeDecodeError:
    9390        raise usage.UsageError("Argument %s cannot be decoded as %s." %
     
    113110    Windows, this returns the input unmodified.
    114111    """
    115     precondition(isinstance(s, unicode), s)
     112    precondition(isinstance(s, str), s)
    116113    warnings.warn("This is unnecessary.", DeprecationWarning)
    117114    if sys.platform == "win32":
     
    167164    the responsibility of stdout/stderr, they expect Unicode by default.
    168165    """
    169     precondition(isinstance(s, unicode), s)
     166    precondition(isinstance(s, str), s)
    170167    warnings.warn("This is unnecessary.", DeprecationWarning)
    171168    return s
     
    215212    """
    216213    result = quote_output(*args, **kwargs)
    217     if isinstance(result, unicode):
     214    if isinstance(result, str):
    218215        return result
    219216    # Since we're quoting, the assumption is this will be read by a human, and
     
    240237    On Python 3, returns Unicode strings.
    241238    """
    242     precondition(isinstance(s, (bytes, unicode)), s)
     239    precondition(isinstance(s, (bytes, str)), s)
    243240    # Since we're quoting, the assumption is this will be read by a human, and
    244241    # therefore printed, so stdout's encoding is the plausible one. io_encoding
     
    279276
    280277def quote_local_unicode_path(path, quotemarks=True):
    281     precondition(isinstance(path, unicode), path)
     278    precondition(isinstance(path, str), path)
    282279
    283280    if sys.platform == "win32" and path.startswith(u"\\\\?\\"):
     
    299296        fp = fp.child(segment)
    300297
    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
    305299
    306300def 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)
    312302
    313303    if sys.platform == "win32":
    314         _assert(isinstance(path, unicode), path=path)
     304        _assert(isinstance(path, str), path=path)
    315305        if path.startswith(u"\\\\?\\") and len(path) > 4:
    316306            # FilePath normally strips trailing path separators, but not in this case.
     
    320310
    321311def _decode(s):
    322     precondition(isinstance(s, (bytes, unicode)), s=s)
     312    precondition(isinstance(s, (bytes, str)), s=s)
    323313
    324314    if isinstance(s, bytes):
     
    341331    Does the current platform handle Unicode filenames natively?
    342332    """
    343     return is_unicode_platform
     333    return True
    344334
    345335class FilenameEncodingError(Exception):
     
    350340    pass
    351341
    352 def listdir_unicode_fallback(path):
    353     """
    354     This function emulates a fallback Unicode API similar to one available
    355     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 
    371342def listdir_unicode(path):
    372343    """
     
    374345    Unicode API even under platforms that don't provide one natively.
    375346    """
    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)
    385349
    386350def listdir_filepath(fp):
Note: See TracChangeset for help on using the changeset viewer.