Kaydet (Commit) 8b2af27d authored tarafından Barry Warsaw's avatar Barry Warsaw

More email package fixes.

MIMEApplication() requires a bytes object for its _data, so fix the tests.

We no longer need utils._identity() or utils._bdecode().  The former isn't
used anywhere AFAICT (where's "make test's" lint? <wink>) and the latter is a
kludge that is eliminated by base64.b64encode().

Current status: 5F/5E
üst 00b34228
...@@ -11,8 +11,8 @@ __all__ = [ ...@@ -11,8 +11,8 @@ __all__ = [
'encode_quopri', 'encode_quopri',
] ]
import base64
from base64 import b64encode as _bencode
from quopri import encodestring as _encodestring from quopri import encodestring as _encodestring
...@@ -23,19 +23,6 @@ def _qencode(s): ...@@ -23,19 +23,6 @@ def _qencode(s):
return enc.replace(' ', '=20') return enc.replace(' ', '=20')
def _bencode(s):
# We can't quite use base64.encodestring() since it tacks on a "courtesy
# newline". Blech!
if not s:
return s
hasnewline = (s[-1] == '\n')
value = base64.encodestring(s)
if not hasnewline and value[-1] == '\n':
return value[:-1]
return value
def encode_base64(msg): def encode_base64(msg):
"""Encode the message's payload in Base64. """Encode the message's payload in Base64.
......
...@@ -8,6 +8,7 @@ __all__ = ['Message'] ...@@ -8,6 +8,7 @@ __all__ = ['Message']
import re import re
import uu import uu
import base64
import binascii import binascii
import warnings import warnings
from io import BytesIO, StringIO from io import BytesIO, StringIO
...@@ -196,12 +197,14 @@ class Message: ...@@ -196,12 +197,14 @@ class Message:
return utils._qdecode(payload) return utils._qdecode(payload)
elif cte == 'base64': elif cte == 'base64':
try: try:
return utils._bdecode(payload) if isinstance(payload, str):
payload = payload.encode('raw-unicode-escape')
return base64.b64decode(payload)
#return utils._bdecode(payload)
except binascii.Error: except binascii.Error:
# Incorrect padding # Incorrect padding
pass pass
elif cte in ('x-uuencode', 'uuencode', 'uue', 'x-uue'): elif cte in ('x-uuencode', 'uuencode', 'uue', 'x-uue'):
payload += '\n'
in_file = BytesIO(payload.encode('raw-unicode-escape')) in_file = BytesIO(payload.encode('raw-unicode-escape'))
out_file = BytesIO() out_file = BytesIO()
try: try:
...@@ -212,7 +215,9 @@ class Message: ...@@ -212,7 +215,9 @@ class Message:
pass pass
# Is there a better way to do this? We can't use the bytes # Is there a better way to do this? We can't use the bytes
# constructor. # constructor.
return bytes(payload, 'raw-unicode-escape') if isinstance(payload, str):
return payload.encode('raw-unicode-escape')
return payload
def set_payload(self, payload, charset=None): def set_payload(self, payload, charset=None):
"""Set the payload to the given value. """Set the payload to the given value.
......
...@@ -1012,7 +1012,7 @@ class TestMIMEImage(unittest.TestCase): ...@@ -1012,7 +1012,7 @@ class TestMIMEImage(unittest.TestCase):
class TestMIMEApplication(unittest.TestCase): class TestMIMEApplication(unittest.TestCase):
def test_headers(self): def test_headers(self):
eq = self.assertEqual eq = self.assertEqual
msg = MIMEApplication('\xfa\xfb\xfc\xfd\xfe\xff') msg = MIMEApplication(b'\xfa\xfb\xfc\xfd\xfe\xff')
eq(msg.get_content_type(), 'application/octet-stream') eq(msg.get_content_type(), 'application/octet-stream')
eq(msg['content-transfer-encoding'], 'base64') eq(msg['content-transfer-encoding'], 'base64')
......
...@@ -55,22 +55,6 @@ escapesre = re.compile(r'[][\\()"]') ...@@ -55,22 +55,6 @@ escapesre = re.compile(r'[][\\()"]')
# Helpers # Helpers
def _identity(s):
return s
def _bdecode(s):
# We can't quite use base64.encodestring() since it tacks on a "courtesy
# newline". Blech!
if not s:
return s
value = base64.decodestring(s)
if not s.endswith('\n') and value.endswith('\n'):
return value[:-1]
return value
def formataddr(pair): def formataddr(pair):
"""The inverse of parseaddr(), this takes a 2-tuple of the form """The inverse of parseaddr(), this takes a 2-tuple of the form
(realname, email_address) and returns the string value suitable (realname, email_address) and returns the string value suitable
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment