[Python-checkins] bpo-41068: Fix read after write in zipfile for non-ASCII files names. (GH-21040)

Miss Islington (bot) webhook-mailer at python.org
Mon Jun 22 04:40:19 EDT 2020


https://github.com/python/cpython/commit/d7f37d1ed4fd38555e3e5aad32d515c96b528df5
commit: d7f37d1ed4fd38555e3e5aad32d515c96b528df5
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2020-06-22T01:40:05-07:00
summary:

bpo-41068: Fix read after write in zipfile for non-ASCII files names. (GH-21040)

(cherry picked from commit 36ff513f82e372ed3cea0bf7cbdf15a1ef6dab9e)

Co-authored-by: Serhiy Storchaka <storchaka at gmail.com>

files:
A Misc/NEWS.d/next/Library/2020-06-22-10-25-39.bpo-41068._bX2BW.rst
M Lib/test/test_zipfile.py
M Lib/zipfile.py

diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py
index 28e62dc5c61c5..e2e37a1770418 100644
--- a/Lib/test/test_zipfile.py
+++ b/Lib/test/test_zipfile.py
@@ -1589,6 +1589,11 @@ def test_write_unicode_filenames(self):
             self.assertEqual(zf.filelist[0].filename, "foo.txt")
             self.assertEqual(zf.filelist[1].filename, "\xf6.txt")
 
+    def test_read_after_write_unicode_filenames(self):
+        with zipfile.ZipFile(TESTFN2, 'w') as zipfp:
+            zipfp.writestr('приклад', b'sample')
+            self.assertEqual(zipfp.read('приклад'), b'sample')
+
     def test_exclusive_create_zip_file(self):
         """Test exclusive creating a new zipfile."""
         unlink(TESTFN2)
diff --git a/Lib/zipfile.py b/Lib/zipfile.py
index f7a2a2e8b8ab9..73e89666309ff 100644
--- a/Lib/zipfile.py
+++ b/Lib/zipfile.py
@@ -1546,7 +1546,7 @@ def open(self, name, mode="r", pwd=None, *, force_zip64=False):
                 # strong encryption
                 raise NotImplementedError("strong encryption (flag bit 6)")
 
-            if zinfo.flag_bits & 0x800:
+            if fheader[_FH_GENERAL_PURPOSE_FLAG_BITS] & 0x800:
                 # UTF-8 filename
                 fname_str = fname.decode("utf-8")
             else:
diff --git a/Misc/NEWS.d/next/Library/2020-06-22-10-25-39.bpo-41068._bX2BW.rst b/Misc/NEWS.d/next/Library/2020-06-22-10-25-39.bpo-41068._bX2BW.rst
new file mode 100644
index 0000000000000..20580c7886fac
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-06-22-10-25-39.bpo-41068._bX2BW.rst
@@ -0,0 +1,2 @@
+Fixed reading files with non-ASCII names from ZIP archive directly after
+writing them.



More information about the Python-checkins mailing list