]> granicus.if.org Git - python/commitdiff
Issue #13639: Accept unicode filenames in tarfile.open(mode="w|gz").
authorLars Gustäbel <lars@gustaebel.de>
Wed, 21 Dec 2011 18:27:50 +0000 (19:27 +0100)
committerLars Gustäbel <lars@gustaebel.de>
Wed, 21 Dec 2011 18:27:50 +0000 (19:27 +0100)
Passing a unicode filename to tarfile.open() along with mode "w|gz" failed
with a UnicodeError because the filename was not encoded properly before being
written to the gzipped stream in the FNAME extra field.

Lib/tarfile.py
Lib/test/test_tarfile.py
Misc/NEWS

index ed5bcf20ee8d4feddfb758bf904b18d44dd89812..bd73965abf85d7f02fa371839a0a87ecfdd91091 100644 (file)
@@ -454,6 +454,8 @@ class _Stream:
                                             0)
         timestamp = struct.pack("<L", long(time.time()))
         self.__write("\037\213\010\010%s\002\377" % timestamp)
+        if type(self.name) is unicode:
+            self.name = self.name.encode("iso-8859-1", "replace")
         if self.name.endswith(".gz"):
             self.name = self.name[:-3]
         self.__write(self.name + NUL)
index 49d2d07d90a8fc81e4dbddfd6248e8e6dc83bd85..d5b864e238b41c1ceeb82de6356612fb08d44a96 100644 (file)
@@ -994,6 +994,13 @@ class StreamWriteTest(WriteTestBase):
         finally:
             os.umask(original_umask)
 
+    def test_issue13639(self):
+        try:
+            with tarfile.open(unicode(tmpname, sys.getfilesystemencoding()), self.mode):
+                pass
+        except UnicodeDecodeError:
+            self.fail("_Stream failed to write unicode filename")
+
 
 class GNUWriteTest(unittest.TestCase):
     # This testcase checks for correct creation of GNU Longname
index d2082a6568f81917bd8adb5b7da03859d28e034c..7b6dbd26ed7ea2896f5760dc07da0a32fcf4b542 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -86,6 +86,8 @@ Core and Builtins
 Library
 -------
 
+- Issue #13639: Accept unicode filenames in tarfile.open(mode="w|gz").
+
 - Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
 
 - Issue #7502: Fix equality comparison for DocTestCase instances.  Patch by