From 8d597a42bce1fc99aa27e27635ed3e8f5d702f81 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 22 Jun 2020 11:24:11 +0300 Subject: [PATCH] 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 --- Lib/test/test_zipfile.py | 5 +++++ Lib/zipfile.py | 2 +- .../next/Library/2020-06-22-10-25-39.bpo-41068._bX2BW.rst | 2 ++ 3 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2020-06-22-10-25-39.bpo-41068._bX2BW.rst diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py index 7e8e8d2c89f080..fad4a82a23b50a 100644 --- a/Lib/test/test_zipfile.py +++ b/Lib/test/test_zipfile.py @@ -1548,6 +1548,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 63748d371f0e75..6ef62527d7e2fe 100644 --- a/Lib/zipfile.py +++ b/Lib/zipfile.py @@ -1535,7 +1535,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 00000000000000..20580c7886fac5 --- /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.