comparison 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
comparison
equal deleted inserted replaced
5541:e124d76311e0 5542:29346d92d80c
11 11
12 # TODO: test bcc 12 # TODO: test bcc
13 13
14 import email 14 import email
15 from . import gpgmelib 15 from . import gpgmelib
16 import unittest, tempfile, os, shutil, errno, imp, sys, difflib, time 16 import unittest, tempfile, os, shutil, errno, imp, sys, difflib, time, io
17 17
18 import pytest 18 import pytest
19 19
20 try: 20 try:
21 import gpg, gpg.core 21 import gpg, gpg.core
27 skip_pgp = mark_class(pytest.mark.skip( 27 skip_pgp = mark_class(pytest.mark.skip(
28 reason="Skipping PGP tests: 'gpg' not installed")) 28 reason="Skipping PGP tests: 'gpg' not installed"))
29 29
30 30
31 from roundup.anypy.email_ import message_from_bytes 31 from roundup.anypy.email_ import message_from_bytes
32 from roundup.anypy.strings import StringIO, b2s, u2s 32 from roundup.anypy.strings import b2s, u2s, s2b
33 33
34 if 'SENDMAILDEBUG' not in os.environ: 34 if 'SENDMAILDEBUG' not in os.environ:
35 os.environ['SENDMAILDEBUG'] = 'mail-test.log' 35 os.environ['SENDMAILDEBUG'] = 'mail-test.log'
36 SENDMAILDEBUG = os.environ['SENDMAILDEBUG'] 36 SENDMAILDEBUG = os.environ['SENDMAILDEBUG']
37 37
242 return handler 242 return handler
243 243
244 def _handle_mail(self, message, args=(), trap_exc=0): 244 def _handle_mail(self, message, args=(), trap_exc=0):
245 handler = self._create_mailgw(message, args) 245 handler = self._create_mailgw(message, args)
246 handler.trapExceptions = trap_exc 246 handler.trapExceptions = trap_exc
247 return handler.main(StringIO(message)) 247 return handler.main(io.BytesIO(s2b(message)))
248 248
249 def _get_mail(self): 249 def _get_mail(self):
250 """Reads an email that has been written to file via debug output. 250 """Reads an email that has been written to file via debug output.
251 251
252 Note: the resulting email will have three leading extra lines 252 Note: the resulting email will have three leading extra lines

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