[issue9124] Mailbox module should use binary I/O, not text I/O

Steffen Daode Nurpmeso report at bugs.python.org
Fri Jan 28 21:29:21 CET 2011


Steffen Daode Nurpmeso <sdaoden at googlemail.com> added the comment:

> What is the data type returned by your get_msg?  I bet it is string,
> and email can't handle messages in string format that have non-ASCII
> characters

(Now i see that the local names 'box', 'mbox' and 'mailbox' have become somewhat messed up, which may have been misleading.)
The answer is (somewhat) definitely no:

    class Ticket:
        @staticmethod
        def process_msg(msg):
                ticket = Ticket(msg)
        ...
        def __init__(self, msg):
                global _Ticket_Count
                _Ticket_Count += 1
                self._id = _Ticket_Count
                self._msg = msg
                log("    @ Creating ticket number ", self._id, ":")

... instantiated by either:
                        msg = mbox.get_message(nr) # It's a Mailbox
                        Ticket.process_msg(msg)

... or:
def openbsd_splitter(msg):
        if msg.is_multipart():
                log("      @ Multipart message: not splitting")
                return [msg]
        i = msg["Subject"]
        if i is None or "digest," not in i:
                log("      @ \"digest,\" not in Subject: not splitting")
                return [msg]
        # Real splitter: nl, SPLITTER, nl, Date: header..
        SPLITTER = "------------------------------"
        def __create_msg(charset, lines):
                try:
                        fp = email.feedparser.FeedParser()
                        headerok, lastnl = False, False
                        while len(lines) > 0:
                                l = lines.pop(0)
                                if SPLITTER in l and lastnl:
                                        break
                                lastnl = not len(l.strip())
                                if not headerok:
                                        if lastnl:
                                                headerok = True
                                        else:
                                                l = split_header_line_helper(.....)
                                fp.feed(l + "\n")
                        return fp.close()
                except Exception as e:
                        log("      @ Error - not splitting: ", str(e))
                        return None
        result = list()
        lines = msg.get_payload().splitlines()
        while len(lines):
                l = lines.pop(0)
                if SPLITTER in l:
                        break
        while len(lines):
                l = lines[0]
                if l.startswith("Date: "):
                        nm = __create_msg(charset, lines)
                        if not nm:
                                return [msg]
                        result.append(nm)
                else:
                        lines.pop(0)
        return result
... which then ends up as the shown
                for msg in splitter(msg):
                        ticket = Ticket(msg)
                        to_box.add_ticket(ticket) # This is 'class Box'

... and it's the very Box.add_ticket() which has been shown in msg127313.  That's all - note however that the email.message.Message headers may either be strings or 'Header' objects - this is work in transition (i somehow want to deal with these malformed mails and at least encapsulate all str() headers to 'Header' headers with the fallback 'quopri' encoding ISO-8859-1 - like this the mail will at least be clean on the disk ...)

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue9124>
_______________________________________


More information about the Python-bugs-list mailing list