Mercurial > p > roundup > code
diff roundup/mailgw.py @ 5542:29346d92d80c
Fix email interfaces with Python 3 (issue 2550974, issue 2551000).
This patch fixes various issues handling incoming email with
roundup-mailgw with Python 3.
Incoming email must always be handled as bytes, not strings, because
it may contain 8-bit-encoded MIME parts with different encodings in
each part. When handling piped input, that means using
sys.stdin.buffer in Python 3 for binary input, along with
message_from_binary_file, not sys.stdin which is text input and may be
for the wrong encoding and not message_from_file. (In turn, tests
that use MailGW.main with text input are affected so an s2b call is
inserted in the test code and it is made to use BytesIO not StringIO.
Properly all the test messages in test_mailgw.py ought to use b''
explicitly rather than having such an s2b conversion, and there ought
to be test messages using 8-bit encodings with non-ASCII characters to
verify that that case works.)
imaplib and poplib return bytes not strings with Python 3 (from
inspection of the code, not tested), as is necessary for the above
reasons. Thus, the handling of IMAP and POP messages must expect
bytes and handle the data accordingly.
For messages from mailboxes, I saw the same problem described in issue
2551000 for a multipart message with a single (non-ASCII) part. The
Roundup code requires RoundupMessage not email.message.Message to be
used recursively for all MIME parts of a message. Because the mailbox
module uses email.message_from_* directly without passing the _class
argument to them, fixing this requires temporarily patching the email
module to ensure _class=RoundupMessage gets passed to those methods.
| author | Joseph Myers <jsm@polyomino.org.uk> |
|---|---|
| date | Sun, 16 Sep 2018 13:55:53 +0000 |
| parents | b7fa56ced601 |
| children | 081be318661b |
line wrap: on
line diff
--- a/roundup/mailgw.py Sun Sep 16 13:42:03 2018 +0000 +++ b/roundup/mailgw.py Sun Sep 16 13:55:53 2018 +0000 @@ -95,7 +95,7 @@ from __future__ import print_function __docformat__ = 'restructuredtext' -import base64, re, os, smtplib, socket, binascii +import base64, re, os, smtplib, socket, binascii, io, functools import time, sys, logging import codecs import traceback @@ -103,7 +103,8 @@ import email.utils from email.generator import Generator -from roundup.anypy.email_ import decode_header, message_from_bytes +from roundup.anypy.email_ import decode_header, message_from_bytes, \ + message_from_binary_file from roundup.anypy.my_input import my_input from roundup import configuration, hyperdb, date, password, exceptions @@ -1230,8 +1231,12 @@ XXX: we may want to read this into a temporary file instead... """ - s = StringIO() - s.write(sys.stdin.read()) + s = io.BytesIO() + if sys.version_info[0] > 2: + stdin = sys.stdin.buffer + else: + stdin = sys.stdin + s.write(stdin.read()) s.seek(0) self.main(s) return 0 @@ -1245,23 +1250,42 @@ class mboxRoundupMessage(mailbox.mboxMessage, RoundupMessage): pass + # The mailbox class constructs email.message.Message objects + # using various email.message_from_* methods, without allowing + # control over the _class argument passed to them to specify a + # subclass to be used. We need RoundupMessage to be used for + # subparts of multipart messages, so patch those methods to + # pass _class. try: - mbox = mailbox.mbox(filename, factory=mboxRoundupMessage, - create=False) - mbox.lock() - except (mailbox.NoSuchMailboxError, mailbox.ExternalClashError) as e: - if isinstance(e, mailbox.ExternalClashError): + patch_methods = ('message_from_bytes', 'message_from_string', + 'message_from_file', 'message_from_binary_file') + orig_methods = {} + for method in patch_methods: + if hasattr(email, method): + orig = getattr(email, method) + orig_methods[method] = orig + setattr(email, method, + functools.partial(orig, _class=RoundupMessage)) + try: + mbox = mailbox.mbox(filename, factory=mboxRoundupMessage, + create=False) + mbox.lock() + except (mailbox.NoSuchMailboxError, mailbox.ExternalClashError) as e: + if isinstance(e, mailbox.ExternalClashError): + mbox.close() + traceback.print_exc() + return 1 + + try: + for key in mbox.keys(): + self.handle_Message(mbox.get(key)) + mbox.remove(key) + finally: + mbox.unlock() mbox.close() - traceback.print_exc() - return 1 - - try: - for key in mbox.keys(): - self.handle_Message(mbox.get(key)) - mbox.remove(key) finally: - mbox.unlock() - mbox.close() + for method in orig_methods: + setattr(email, method, orig) return 0 @@ -1322,9 +1346,9 @@ server.store(str(i), '+FLAGS', r'(\Deleted)') # process the message - s = StringIO(data[0][1]) + s = io.BytesIO(data[0][1]) s.seek(0) - self.handle_Message(Message(s)) + self.handle_Message(message_from_bytes(s, RoundupMessage)) server.close() finally: try: @@ -1392,7 +1416,7 @@ # number of octets ] lines = server.retr(i)[1] self.handle_Message( - email.message_from_string('\n'.join(lines), RoundupMessage)) + message_from_bytes(b'\n'.join(lines), RoundupMessage)) # delete the message server.dele(i) @@ -1403,7 +1427,7 @@ def main(self, fp): ''' fp - the file from which to read the Message. ''' - return self.handle_Message(email.message_from_file(fp, RoundupMessage)) + return self.handle_Message(message_from_binary_file(fp, RoundupMessage)) def handle_Message(self, message): """Handle an RFC822 Message
