diff test/test_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 19bd4b413ed6
children 4523fe3cf04c
line wrap: on
line diff
--- a/test/test_mailgw.py	Sun Sep 16 13:42:03 2018 +0000
+++ b/test/test_mailgw.py	Sun Sep 16 13:55:53 2018 +0000
@@ -13,7 +13,7 @@
 
 import email
 from . import gpgmelib
-import unittest, tempfile, os, shutil, errno, imp, sys, difflib, time
+import unittest, tempfile, os, shutil, errno, imp, sys, difflib, time, io
 
 import pytest
 
@@ -29,7 +29,7 @@
 
 
 from roundup.anypy.email_ import message_from_bytes
-from roundup.anypy.strings import StringIO, b2s, u2s
+from roundup.anypy.strings import b2s, u2s, s2b
 
 if 'SENDMAILDEBUG' not in os.environ:
     os.environ['SENDMAILDEBUG'] = 'mail-test.log'
@@ -244,7 +244,7 @@
     def _handle_mail(self, message, args=(), trap_exc=0):
         handler = self._create_mailgw(message, args)
         handler.trapExceptions = trap_exc
-        return handler.main(StringIO(message))
+        return handler.main(io.BytesIO(s2b(message)))
 
     def _get_mail(self):
         """Reads an email that has been written to file via debug output.

Roundup Issue Tracker: http://roundup-tracker.org/