From 1c5556334dc6fde832db9c8fe232efe117e2381e Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Mon, 15 Apr 2013 01:05:37 -0700 Subject: [PATCH] Try to fix UnicodeDecodeError in create_manpage_completions.py https://github.com/fish-shell/fish-shell/issues/658 --- share/tools/create_manpage_completions.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/share/tools/create_manpage_completions.py b/share/tools/create_manpage_completions.py index 4548dd1d2..d861886a0 100755 --- a/share/tools/create_manpage_completions.py +++ b/share/tools/create_manpage_completions.py @@ -93,13 +93,28 @@ def fish_escape_single_quote(str): str = str.replace("'", "\\'") # Replace one single quote with a backslash-single-quote return "'" + str + "'" +# Make a string Unicode by attempting to decode it as latin-1, or UTF8. See #658 +def lossy_unicode(s): + # All strings are unicode in Python 3 + if IS_PY3 or isinstance(s, unicode): return s + try: + return s.decode('latin-1') + except UnicodeEncodeError: + pass + try: + return s.decode('utf-8') + except UnicodeEncodeError: + pass + return s.decode('latin-1', 'ignore') + + def output_complete_command(cmdname, args, description, output_list): comps = ['complete -c', cmdname] comps.extend(args) if description: comps.append('--description') comps.append(description) - output_list.append(' '.join(comps)) + output_list.append(lossy_unicode(' ').join([lossy_unicode(c) for c in comps])) def built_command(options, description): # print "Options are: ", options