https://github.com/python/cpython/commit/1512f788afa37b209dde2769e33a96e85adf715d
commit: 1512f788afa37b209dde2769e33a96e85adf715d
branch: main
author: Vasiliy Kiryanov <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2026-10-22T20:54:42+03:00
summary:
gh-157880: Write visible headers of BabylMessage in mailbox.Babyl (GH-157881)
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 99426220154360..5bb9843eba31b4 100644
--- a/Lib/mailbox.py
+++ b/Lib/mailbox.py
@@ -1495,6 +1495,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 019c699bff55c4..f1100f98ab9a06 100644
--- a/Lib/test/test_mailbox.py
+++ b/Lib/test/test_mailbox.py
@@ -1521,6 +1521,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]