Fix tar_contents() to handle UnicodeEncodeError by falling back to utf_8 if
authorZac Medico <zmedico@gentoo.org>
Thu, 27 Aug 2009 05:05:00 +0000 (05:05 -0000)
committerZac Medico <zmedico@gentoo.org>
Thu, 27 Aug 2009 05:05:00 +0000 (05:05 -0000)
appropriate.

svn path=/main/trunk/; revision=14163

pym/portage/dbapi/vartree.py

index a1891c37e7bca3cf06183e4ce697122561ff1f5f..c8a045dc4dfebecb98859f8104b095f9836d5b1c 100644 (file)
@@ -4400,6 +4400,26 @@ def write_contents(contents, root, f):
 
 def tar_contents(contents, root, tar, protect=None, onProgress=None):
        os = _os_merge
+
+       try:
+               for x in contents:
+                       _unicode_encode(x,
+                               encoding=_encodings['merge'],
+                               errors='strict')
+       except UnicodeEncodeError:
+               # The package appears to have been merged with a
+               # different value of sys.getfilesystemencoding(),
+               # so fall back to utf_8 if appropriate.
+               try:
+                       for x in contents:
+                               _unicode_encode(x,
+                                       encoding=_encodings['fs'],
+                                       errors='strict')
+               except UnicodeEncodeError:
+                       pass
+               else:
+                       os = portage.os
+
        from portage.util import normalize_path
        import tarfile
        root = normalize_path(root).rstrip(os.path.sep) + os.path.sep
@@ -4447,7 +4467,8 @@ def tar_contents(contents, root, tar, protect=None, onProgress=None):
                                tar.addfile(tarinfo)
                        else:
                                f = open(_unicode_encode(path,
-                                       encoding=_encodings['merge'], errors='strict'), 'rb')
+                                       encoding=object.__getattribute__(os, '_encoding'),
+                                       errors='strict'), 'rb')
                                try:
                                        tar.addfile(tarinfo, f)
                                finally: