From 961aa0f6baf7d6cb88c9fa51c6d6312e3b4635a9 Mon Sep 17 00:00:00 2001 From: Sesh Sadasivam Date: Fri, 18 Jan 2019 22:48:24 -0500 Subject: [PATCH] Fixed bug in email Message.set_boundary() Also added tests that would expose the bug. --- src/future/backports/email/message.py | 6 ++--- tests/test_future/test_email_multipart.py | 31 +++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 tests/test_future/test_email_multipart.py diff --git a/src/future/backports/email/message.py b/src/future/backports/email/message.py index 99715fcc..d8d9615d 100644 --- a/src/future/backports/email/message.py +++ b/src/future/backports/email/message.py @@ -800,7 +800,7 @@ def set_boundary(self, boundary): # There was no Content-Type header, and we don't know what type # to set it to, so raise an exception. raise errors.HeaderParseError('No Content-Type header found') - newparams = [] + newparams = list() foundp = False for pk, pv in params: if pk.lower() == 'boundary': @@ -814,10 +814,10 @@ def set_boundary(self, boundary): # instead??? newparams.append(('boundary', '"%s"' % boundary)) # Replace the existing Content-Type header with the new value - newheaders = [] + newheaders = list() for h, v in self._headers: if h.lower() == 'content-type': - parts = [] + parts = list() for k, v in newparams: if v == '': parts.append(k) diff --git a/tests/test_future/test_email_multipart.py b/tests/test_future/test_email_multipart.py new file mode 100644 index 00000000..cbd93b89 --- /dev/null +++ b/tests/test_future/test_email_multipart.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- +"""Tests for multipart emails.""" + +from future.tests.base import unittest +import future.backports.email as email +import future.backports.email.mime.multipart +from future.builtins import list + +class EmailMultiPartTests(unittest.TestCase): + """Tests for handling multipart email Messages.""" + + def test_multipart_serialize_without_boundary(self): + """Tests that serializing an empty multipart email does not fail.""" + multipart_message = email.mime.multipart.MIMEMultipart() + self.assertIsNot(multipart_message.as_string(), None) + + def test_multipart_set_boundary_does_not_change_header_type(self): + """ + Tests that Message.set_boundary() does not cause Python2 errors. + + In particular, tests that set_boundary does not cause the type of the + message headers list to be changed from the future built-in list. + """ + multipart_message = email.mime.multipart.MIMEMultipart() + headers_type = type(multipart_message._headers) + self.assertEqual(headers_type, type(list())) + + boundary = '===============6387699881409002085==' + multipart_message.set_boundary(boundary) + headers_type = type(multipart_message._headers) + self.assertEqual(headers_type, type(list()))