https://github.com/python/cpython/commit/fc2f9137fe913cc5600b913e3d6afa3344a8baa2
commit: fc2f9137fe913cc5600b913e3d6afa3344a8baa2
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: ambv <[email protected]>
date: 2026-11-13T17:31:43-07:00
summary:

[3.14] gh-152845: Preserve ZIP encoding flags when appending (gh-161950) 
(gh-161961)

(cherry picked from commit 5151e7f2a3dabf74941f2c3857e17886d87cb5dd)

Co-authored-by: Kasra Ghodsi <[email protected]>

files:
A Misc/NEWS.d/next/Library/2026-11-13-09-49-13.gh-issue-152845.ZipFlags.rst
M Lib/test/test_zipfile/test_core.py
M Lib/zipfile/__init__.py

diff --git a/Lib/test/test_zipfile/test_core.py 
b/Lib/test/test_zipfile/test_core.py
index ee93e27f2bff8f..08ec019c12eb20 100644
--- a/Lib/test/test_zipfile/test_core.py
+++ b/Lib/test/test_zipfile/test_core.py
@@ -2371,6 +2371,66 @@ def test_empty_zipfile(self):
             self.fail("Unable to create empty ZIP file in 'a' mode")
         zipf.close()
 
+    def test_append_preserves_ascii_utf8_flag(self):
+        comments = (b'', 'caf\N{LATIN SMALL LETTER E WITH 
ACUTE}'.encode('utf-8'))
+        updates = (
+            ('append', lambda zf: zf.writestr('added.txt', b'added')),
+            ('comment', lambda zf: setattr(zf, 'comment', b'archive comment')),
+        )
+        for comment, (operation, update) in itertools.product(comments, 
updates):
+            with self.subTest(comment=comment, operation=operation):
+                stream = io.BytesIO()
+                with zipfile.ZipFile(stream, 'w') as archive:
+                    entry = zipfile.ZipInfo('file.txt')
+                    entry.comment = comment
+                    archive.writestr(entry, b'payload')
+                original = bytearray(stream.getvalue())
+                central_offset = original.index(b'PK\x01\x02')
+                struct.pack_into('<H', original, 6, 1 << 11)
+                struct.pack_into('<H', original, central_offset + 8, 1 << 11)
+                stream = io.BytesIO(original)
+                with zipfile.ZipFile(stream, 'a') as archive:
+                    update(archive)
+                with zipfile.ZipFile(stream) as archive:
+                    entry = archive.getinfo('file.txt')
+                    self.assertEqual(entry.flag_bits, 1 << 11)
+                    self.assertEqual(entry.comment, comment)
+                    self.assertEqual(archive.read('file.txt'), b'payload')
+                    self.assertIsNone(archive.testzip())
+                self.assertEqual(stream.getvalue()[:central_offset],
+                                 original[:central_offset])
+
+    def test_new_entry_filename_encoding_flags(self):
+        cases = (
+            ('file.txt', b'\x82', 0, 'cp437', 0,
+             '\N{LATIN SMALL LETTER E WITH ACUTE}'),
+            ('caf\N{LATIN SMALL LETTER E WITH ACUTE}.txt', b'\xc3\xa9',
+             0, 'utf-8', 1 << 11,
+             '\N{LATIN SMALL LETTER E WITH ACUTE}'),
+            ('file.txt', b'\xc3\xa9', 1 << 11, 'utf-8', 1 << 11,
+             '\N{LATIN SMALL LETTER E WITH ACUTE}'),
+            ('file.txt', b'', 1 << 11, 'utf-8', 1 << 11, ''),
+            ('file.txt', b'ascii', 1 << 11, 'utf-8', 1 << 11, 'ascii'),
+        )
+        for (name, comment, initial_flags, encoding, expected_flags,
+             expected_comment) in cases:
+            with self.subTest(name=name, comment=comment,
+                              initial_flags=initial_flags):
+                stream = io.BytesIO()
+                with zipfile.ZipFile(stream, 'w') as archive:
+                    entry = zipfile.ZipInfo(name)
+                    entry.flag_bits = initial_flags
+                    entry.comment = comment
+                    archive.writestr(entry, b'payload')
+                with zipfile.ZipFile(stream) as archive:
+                    entry = archive.getinfo(name)
+                    self.assertEqual(entry.flag_bits, expected_flags)
+                    self.assertEqual(entry.comment, comment)
+                    self.assertEqual(entry.comment.decode(encoding),
+                                     expected_comment)
+                    self.assertEqual(archive.read(name), b'payload')
+                    self.assertIsNone(archive.testzip())
+
     def test_open_empty_file(self):
         # Issue 1710703: Check that opening a file with less than 22 bytes
         # raises a BadZipFile exception (rather than the previously unhelpful
diff --git a/Lib/zipfile/__init__.py b/Lib/zipfile/__init__.py
index d4b104056115f8..27e2deceb3eb82 100644
--- a/Lib/zipfile/__init__.py
+++ b/Lib/zipfile/__init__.py
@@ -568,11 +568,11 @@ def FileHeader(self, zip64=None):
 
     def _encodeFilenameFlags(self):
         if self.flag_bits & _MASK_UTF_FILENAME:
-            encoding = 'ascii'
+            encoding = 'utf-8'
         else:
             encoding = 'cp437'
         try:
-            return self.filename.encode(encoding), self.flag_bits & 
~_MASK_UTF_FILENAME
+            return self.filename.encode(encoding), self.flag_bits
         except UnicodeEncodeError:
             return self.filename.encode('utf-8'), self.flag_bits | 
_MASK_UTF_FILENAME
 
@@ -1842,7 +1842,9 @@ def _open_to_write(self, zinfo, force_zip64=False):
         zinfo.compress_size = 0
         zinfo.CRC = 0
 
-        zinfo.flag_bits = _MASK_UTF_FILENAME
+        zinfo.flag_bits &= _MASK_UTF_FILENAME
+        if not zinfo.filename.isascii():
+            zinfo.flag_bits |= _MASK_UTF_FILENAME
         if zinfo.compress_type == ZIP_LZMA:
             # Compressed data includes an end-of-stream (EOS) marker
             zinfo.flag_bits |= _MASK_COMPRESS_OPTION_1
diff --git 
a/Misc/NEWS.d/next/Library/2026-11-13-09-49-13.gh-issue-152845.ZipFlags.rst 
b/Misc/NEWS.d/next/Library/2026-11-13-09-49-13.gh-issue-152845.ZipFlags.rst
new file mode 100644
index 00000000000000..70cf5134d3fe9f
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-11-13-09-49-13.gh-issue-152845.ZipFlags.rst
@@ -0,0 +1,3 @@
+Fix :mod:`zipfile` dropping the UTF-8 (EFS) flag for existing entries
+when rewriting an archive in append mode, and honor an explicitly set
+flag on new entries. Patch by Kasra Ghodsi.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to