~aaron-whitehouse/duplicity/08-unicode

« back to all changes in this revision

Viewing changes to duplicity/util.py

  • Committer: Aaron A Whitehouse
  • Date: 2017-06-12 22:24:53 UTC
  • Revision ID: lists@whitehouse.kiwi.nz-20170612222453-5a5nnnp6pf9yvpm6
Replace util.py functions to and from unicode with direct calls to .encode and .decode, as it did not save much code, means the "strict"/"ignore"/"replace" decision can be made per call. Also helps narrow down on why sys.getfilesystemencoding() is not working as expected in some places.

Show diffs side-by-side

added added

removed removed

Lines of Context:
54
54
def escape(string):
55
55
    "Convert a (bytes) filename to a format suitable for logging (quoted utf8)"
56
56
    if isinstance(string, str) or isinstance(string, unicode):
57
 
        string = bytes_to_uc(string).encode('unicode-escape', 'replace')
 
57
        string = string.decode(sys.getfilesystemencoding(), 'replace').encode('unicode-escape', 'replace')
58
58
    return u"'%s'" % string.decode('utf8', 'replace')
59
59
 
60
60
 
63
63
    # This should be phased out, as path.uc_name is preferable for paths and
64
64
    # bytes_to_uc is clearer for everything else
65
65
    # ToDo: Delete when no longer used
66
 
    return bytes_to_uc(filename)
 
66
    return filename.decode(sys.getfilesystemencoding(), "replace")
67
67
 
68
68
 
69
69
def bytes_to_uc(bytes_str):
88
88
    return unicode_str
89
89
 
90
90
 
91
 
def uc_to_bytes(unicode_str):
92
 
    """Convert a unicode string to bytes, using filesystem encoding"""
93
 
    # This should not be used filenames, as path.name is preferable
94
 
    # Note that this is similar to, but distinct from, fsencode, because
95
 
    # fsencode assumes a path-like string and has special handling for
96
 
    # strange (Linux) filename quirks.
97
 
    bytes_str = unicode_str.encode(sys.getfilesystemencoding(), 'replace')
98
 
    return bytes_str
99
 
 
100
 
 
101
91
def uindex(index):
102
92
    "Convert an index (a tuple of path parts) to unicode for printing"
103
93
    if index: