1
0
mirror of https://github.com/wting/autojump synced 2026-03-02 03:49:26 +00:00

Fix encoding issues.

The original implementation used str.encode() on input and str.decode() on
output. However this would cause UnicodeDecodeError since certain characters
can't be encoded / decoded in ASCII.

The new solution is to use unicode() on all input strings and output UTF-8
encoded strings. This makes the assumption that the shell can handle UTF-8
strings.
This commit is contained in:
William Ting
2014-01-07 11:44:44 -06:00
parent 3f460fb3e9
commit 1a0003d852
4 changed files with 24 additions and 44 deletions

View File

@@ -28,26 +28,10 @@ def create_dir(path):
raise
def decode(string):
"""Converts byte string to Unicode string."""
if is_python2():
# Python 2.6 does not support kwargs
return string.decode('utf-8', 'replace')
return string
def encode(string):
"""Converts Unicode string to byte string."""
if is_python2():
# Python 2.6 does not support kwargs
return string.encode('utf-8', 'replace')
return string
def encode_local(string, encoding=None):
"""Converts string into local filesystem encoding."""
if is_python2():
return decode(string).encode(encoding or sys.getfilesystemencoding())
"""Converts into Unicode string."""
if is_python2() and not isinstance(string, unicode):
return unicode(string, encoding='utf-8', errors='replace')
return string
@@ -153,7 +137,7 @@ def move_file(src, dst):
def print_entry(entry):
print(encode_local("%.1f:\t%s" % (entry.weight, entry.path)))
print("%.1f:\t%s" % (entry.weight, entry.path))
def print_tab_menu(needle, tab_entries, separator):
@@ -166,17 +150,18 @@ def print_tab_menu(needle, tab_entries, separator):
on subsequent calls.
"""
for i, entry in enumerate(tab_entries):
print(encode_local(
print(
'%s%s%d%s%s' % (
needle,
separator,
i + 1,
separator,
entry.path)))
entry.path))
def sanitize(directories):
clean = lambda x: decode(x) if len(x) == 1 else decode(x).rstrip(os.sep)
# edge case to allow '/' as a valid path
clean = lambda x: encode(x) if x == os.sep else encode(x).rstrip(os.sep)
return list(imap(clean, directories))