← Back to team overview

duplicity-team team mailing list archive

[Merge] lp:~ed.so/duplicity/UnicodeDecodeError into lp:duplicity

 

edso has proposed merging lp:~ed.so/duplicity/UnicodeDecodeError into lp:duplicity.

Requested reviews:
  duplicity-team (duplicity-team)

For more details, see:
https://code.launchpad.net/~ed.so/duplicity/UnicodeDecodeError/+merge/78840
-- 
https://code.launchpad.net/~ed.so/duplicity/UnicodeDecodeError/+merge/78840
Your team duplicity-team is requested to review the proposed merge of lp:~ed.so/duplicity/UnicodeDecodeError into lp:duplicity.
=== modified file 'duplicity/commandline.py'
--- duplicity/commandline.py	2011-10-08 16:09:27 +0000
+++ duplicity/commandline.py	2011-10-10 13:28:56 +0000
@@ -169,6 +169,30 @@
             optparse.Option.take_action(
                 self, action, dest, opt, value, values, parser)
 
+""" 
+Fix:
+File "/usr/lib/pythonX.X/optparse.py", line XXXX, in print_help
+    file.write(self.format_help().encode(encoding, "replace"))
+    UnicodeDecodeError: 'ascii' codec can't decode byte 0xXX in position XXXX: 
+"""
+class OPHelpFix(optparse.OptionParser):
+    def _get_encoding(self, file):
+        """
+        try to get the encoding or switch to UTF-8
+        which is default encoding in python3 and most recent unixes
+        """
+        encoding = getattr(file, "encoding", "UTF-8")
+        return encoding
+
+    def print_help(self, file=None):
+        """
+        overwrite method with proper utf-8 decoding
+        """
+        if file is None:
+            file = sys.stdout
+        encoding = self._get_encoding(file)
+        file.write(self.format_help().decode('utf-8').encode(encoding, "replace"))
+
 def parse_cmdline_options(arglist):
     """Parse argument list"""
     global select_opts, select_files, full_backup
@@ -211,7 +235,7 @@
     def add_rename(o, s, v, p):
         globals.rename[os.path.normcase(os.path.normpath(v[0]))] = v[1]
 
-    parser = optparse.OptionParser(option_class=DupOption, usage=usage())
+    parser = OPHelpFix( option_class=DupOption, usage=usage() )
 
     # If this is true, only warn and don't raise fatal error when backup
     # source directory doesn't match previous backup source directory.


Follow ups