https://github.com/python/cpython/commit/5924fd4d711e7157ec409044a4ff5e98ebf81b39
commit: 5924fd4d711e7157ec409044a4ff5e98ebf81b39
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2026-10-23T18:38:40Z
summary:

[3.14] gh-157880: Write visible headers of BabylMessage in mailbox.Babyl 
(GH-157881) (GH-160277)

(cherry picked from commit 1512f788afa37b209dde2769e33a96e85adf715d)

Co-authored-by: Vasiliy Kiryanov <[email protected]>

files:
A Misc/NEWS.d/next/Library/2026-09-20-21-24-05.gh-issue-157880.aE7wIu.rst
M Lib/mailbox.py
M Lib/test/test_mailbox.py

diff --git a/Lib/mailbox.py b/Lib/mailbox.py
index 364af6bb010959..8d7c7985017129 100644
--- a/Lib/mailbox.py
+++ b/Lib/mailbox.py
@@ -1488,6 +1488,7 @@ def _install_message(self, message):
                 vis_buffer = io.BytesIO()
                 vis_generator = email.generator.BytesGenerator(vis_buffer, 
False, 0)
                 vis_generator.flatten(message.get_visible())
+                vis_buffer.seek(0)
                 while True:
                     line = vis_buffer.readline()
                     self._file.write(line.replace(b'\n', linesep))
diff --git a/Lib/test/test_mailbox.py b/Lib/test/test_mailbox.py
index 0169948e453438..a3350ea4c2b293 100644
--- a/Lib/test/test_mailbox.py
+++ b/Lib/test/test_mailbox.py
@@ -1506,6 +1506,31 @@ def test_labels(self):
         self._box.remove(key1)
         self.assertEqual(set(self._box.get_labels()), set(['blah']))
 
+    def test_visible_headers(self):
+        # gh-157880: visible headers are written to the file and read back
+        msg = mailbox.BabylMessage(self._template % 0)
+        msg.update_visible()
+        key = self._box.add(msg)
+        for retrieved in (self._box.get_message(key), self._box[key]):
+            self.assertEqual(retrieved['from'], 'foo')
+            self.assertEqual(retrieved.get_payload(), '0\n')
+            self.assertEqual(retrieved.get_visible().keys(), ['From'])
+            self.assertEqual(retrieved.get_visible()['from'], 'foo')
+        self.assertEqual(self._box.get_bytes(key),
+                         (self._template % 0).encode('ascii'))
+
+    def test_no_visible_headers(self):
+        # gh-157880: empty visible headers are written to the file
+        # and read back
+        msg = mailbox.BabylMessage(self._template % 0)
+        key = self._box.add(msg)
+        for retrieved in (self._box.get_message(key), self._box[key]):
+            self.assertEqual(retrieved['from'], 'foo')
+            self.assertEqual(retrieved.get_payload(), '0\n')
+            self.assertEqual(retrieved.get_visible().keys(), [])
+        self.assertEqual(self._box.get_bytes(key),
+                         (self._template % 0).encode('ascii'))
+
 
 class FakeFileLikeObject:
 
diff --git 
a/Misc/NEWS.d/next/Library/2026-09-20-21-24-05.gh-issue-157880.aE7wIu.rst 
b/Misc/NEWS.d/next/Library/2026-09-20-21-24-05.gh-issue-157880.aE7wIu.rst
new file mode 100644
index 00000000000000..7fbb1080047989
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-09-20-21-24-05.gh-issue-157880.aE7wIu.rst
@@ -0,0 +1,2 @@
+Fix :class:`mailbox.Babyl` to write the visible headers of a
+:class:`mailbox.BabylMessage` to the mailbox file.

_______________________________________________
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