Skip to content

mailbox.Babyl returns labels as bytes after reading a mailbox file #160598

Description

@devdavidejesus

Bug report

When a Babyl mailbox is read back from its file, the message labels are bytes instead of str:

import mailbox

box = mailbox.Babyl('box.babyl')
msg = mailbox.BabylMessage('Subject: one\n\nbody\n')
msg.set_labels(['unseen', 'work'])
box.add(msg)
box.add(mailbox.BabylMessage('Subject: two\n\nbody\n'))
box.close()

box = mailbox.Babyl('box.babyl')
msg = box[0]
print(sorted(msg.get_labels()))  # [b'unseen', b'work']
print(sorted(box.get_labels()))  # [b'unseen', b'work']
print(mailbox.MaildirMessage(msg).get_flags(), mailbox.mboxMessage(msg).get_flags())  # S RO
box.remove(1)
box.flush()  # AttributeError: 'bytes' object has no attribute 'encode'

As a result:

  • 'unseen' in msg.get_labels() is false, so converting the message to MaildirMessage or mboxMessage marks an unseen message as read (S, R).
  • Babyl.get_labels() does not leave out the special labels such as unseen.
  • If any message has labels, a change that makes flush() rewrite the file, such as remove(), fails with AttributeError, and so does adding such a message to another Babyl mailbox.

Babyl._generate_toc() reads the labels line in binary mode and keeps the labels as bytes, while _install_message() and _pre_mailbox_hook() write them with label.encode(). Decoding them in _generate_toc() fixes all of the above. Using errors='surrogateescape' for both the decoding and the encoding keeps files with labels that are not valid UTF-8 readable (today they can be read, only rewriting them fails). The existing test_labels never reopens the mailbox, so it doesn't catch this.

In the releases I tried (3.13.17, 3.14.9, 3.15.0) the example fails even earlier, with an AssertionError in get_message(), because of gh-157880, which is fixed on main, 3.15 and 3.14 after those releases. With the gh-157880 fix applied, 3.13 shows the same label problem.

CPython versions tested on:

3.13, 3.14, CPython main branch

Operating systems tested on:

macOS

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    stdlibStandard Library Python modules in the Lib/ directory
    on Oct 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions