[Python-checkins] cpython (3.3): #14360: make encoders.encode_quopri work.
r.david.murray
python-checkins at python.org
Fri Jun 28 00:39:04 CEST 2013
http://hg.python.org/cpython/rev/1d5856849e64
changeset: 84353:1d5856849e64
branch: 3.3
parent: 84350:d7ae8a84f443
user: R David Murray <rdmurray at bitdance.com>
date: Thu Jun 27 18:37:00 2013 -0400
summary:
#14360: make encoders.encode_quopri work.
There were no tests for the encoders module. encode_base64 worked
because it is the default and so got tested implicitly elsewhere, and
we use encode_7or8bit internally, so that worked, too. I previously
fixed encode_noop, so this fix means that everythign in the encoders
module now works, hopefully correctly. Also added an explicit test
for encode_base64.
files:
Lib/email/encoders.py | 8 +++-
Lib/test/test_email/test_email.py | 29 +++++++++++++++++++
2 files changed, 35 insertions(+), 2 deletions(-)
diff --git a/Lib/email/encoders.py b/Lib/email/encoders.py
--- a/Lib/email/encoders.py
+++ b/Lib/email/encoders.py
@@ -20,7 +20,7 @@
def _qencode(s):
enc = _encodestring(s, quotetabs=True)
# Must encode spaces, which quopri.encodestring() doesn't do
- return enc.replace(' ', '=20')
+ return enc.replace(b' ', b'=20')
def encode_base64(msg):
@@ -41,8 +41,12 @@
Also, add an appropriate Content-Transfer-Encoding header.
"""
orig = msg.get_payload()
+ if isinstance(orig, str):
+ # If it is a string, the model data may have binary data encoded in via
+ # surrogateescape. Convert back to bytes so we can CTE encode it.
+ orig = orig.encode('ascii', 'surrogateescape')
encdata = _qencode(orig)
- msg.set_payload(encdata)
+ msg.set_payload(encdata.decode('ascii', 'surrogateescape'))
msg['Content-Transfer-Encoding'] = 'quoted-printable'
diff --git a/Lib/test/test_email/test_email.py b/Lib/test/test_email/test_email.py
--- a/Lib/test/test_email/test_email.py
+++ b/Lib/test/test_email/test_email.py
@@ -1474,6 +1474,35 @@
self.assertEqual(msg.get_payload(), '\uFFFD' * len(bytesdata))
self.assertEqual(msg2.get_payload(decode=True), bytesdata)
+ def test_binary_body_with_encode_quopri(self):
+ # Issue 14360.
+ bytesdata = b'\xfa\xfb\xfc\xfd\xfe\xff '
+ msg = MIMEApplication(bytesdata, _encoder=encoders.encode_quopri)
+ self.assertEqual(msg.get_payload(), '=FA=FB=FC=FD=FE=FF=20')
+ self.assertEqual(msg.get_payload(decode=True), bytesdata)
+ self.assertEqual(msg['Content-Transfer-Encoding'], 'quoted-printable')
+ s = BytesIO()
+ g = BytesGenerator(s)
+ g.flatten(msg)
+ wireform = s.getvalue()
+ msg2 = email.message_from_bytes(wireform)
+ self.assertEqual(msg.get_payload(), '=FA=FB=FC=FD=FE=FF=20')
+ self.assertEqual(msg2.get_payload(decode=True), bytesdata)
+ self.assertEqual(msg2['Content-Transfer-Encoding'], 'quoted-printable')
+
+ def test_binary_body_with_encode_base64(self):
+ bytesdata = b'\xfa\xfb\xfc\xfd\xfe\xff'
+ msg = MIMEApplication(bytesdata, _encoder=encoders.encode_base64)
+ self.assertEqual(msg.get_payload(), '+vv8/f7/\n')
+ self.assertEqual(msg.get_payload(decode=True), bytesdata)
+ s = BytesIO()
+ g = BytesGenerator(s)
+ g.flatten(msg)
+ wireform = s.getvalue()
+ msg2 = email.message_from_bytes(wireform)
+ self.assertEqual(msg.get_payload(), '+vv8/f7/\n')
+ self.assertEqual(msg2.get_payload(decode=True), bytesdata)
+
# Test the basic MIMEText class
class TestMIMEText(unittest.TestCase):
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list