classification
Title: mailbox does not treat external factories the same
Type: behavior Stage: patch review
Components: email Versions: Python 3.11, Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Cryvate, barry, bpoaugust, cryvate, r.david.murray
Priority: normal Keywords: patch

Created on 2017-09-21 01:37 by bpoaugust, last changed 2021-06-17 19:43 by cryvate.

Files
File name Uploaded Description Edit
mb.py bpoaugust, 2017-09-21 01:37 Show external factory is treated differently
Pull Requests
URL Status Linked Edit
PR 26776 open Cryvate, 2021-06-17 19:43
Messages (2)
msg302667 - (view) Author: (bpoaugust) Date: 2017-09-21 01:37
The default mailbox factory is mailbox.mboxMessage so I expect the following two statements to work the same:

messages = mailbox.mbox("test.mbox")
messages = mailbox.mbox("test.mbox", mailbox.mboxMessage)

However they do not.

The attached file generates the output:

<class 'mailbox.mboxMessage'>
Test
sender@invalid Thu Nov 17 00:49:30 2016
None
<class 'mailbox.mboxMessage'>
Test
MAILER-DAEMON Thu Sep 21 01:31:15 2017
None

Note that the original from has been lost in the second parse.
msg302682 - (view) Author: Henk-Jaap Wagenaar (cryvate) * Date: 2017-09-21 10:41
To me the documentation doesn't quite look right, in the case of no factory being passed, it runs:

    def __getitem__(self, key):
        """Return the keyed message; raise KeyError if it doesn't exist."""
        if not self._factory:
            return self.get_message(key)
        else:
            return self._factory(self.get_file(key))

from the Mailbox base class. The get_message is thus:

    def get_message(self, key):
        """Return a Message representation or raise a KeyError."""
        start, stop = self._lookup(key)
        self._file.seek(start)
        from_line = self._file.readline().replace(linesep, b'')
        string = self._file.read(stop - self._file.tell())
        msg = self._message_factory(string.replace(linesep, b'\n'))
        msg.set_from(from_line[5:].decode('ascii'))
        return msg

where self._message_factory is set to mailbox.mboxMessage.

I am not familiar with this module, so am unsure whether the documentation is ill-worded or it is a bug.
History
Date User Action Args
2021-06-17 19:43:45cryvatesetversions: + Python 3.10, Python 3.11, - Python 3.5, Python 3.6, Python 3.7
2021-06-17 19:43:21Cryvatesetkeywords: + patch
nosy: + Cryvate

pull_requests: + pull_request25362
stage: patch review
2017-09-21 10:41:29cryvatesetnosy: + cryvate
messages: + msg302682
2017-09-21 01:37:52bpoaugustcreate