Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Text encoding should only be handled for messages using encoders.enco…

…de_base64.

Also use the same logic for trying to find the best encoding that we
use for headers.
  • Loading branch information...
commit 201899a858b7784b10c26a9de04723f74efdf301 1 parent bc7ae3a
@rpatterson rpatterson authored
Showing with 19 additions and 32 deletions.
  1. +19 −21 pyramid_mailer/response.py
  2. +0 −11 pyramid_mailer/tests.py
View
40 pyramid_mailer/response.py
@@ -368,18 +368,6 @@ def __init__(self, type, **params):
self.maintype, self.subtype = type.split('/')
MIMEBase.__init__(self, self.maintype, self.subtype, **params)
- def add_text(self, content):
- # this is text, so encode it in canonical form
- try:
- encoded = content.encode('ascii')
- charset = 'ascii'
- except UnicodeError:
- encoded = content.encode('utf-8')
- charset = 'utf-8'
-
- self.set_payload(encoded, charset=charset)
-
-
def extract_payload(self, mail):
if mail.body == None: return # only None, '' is still ok
@@ -390,13 +378,19 @@ def extract_payload(self, mail):
"have a valid Content-Type.")
if ctype.startswith("text/"):
- self.add_text(mail.body)
+ self.set_payload(mail.body)
else:
if cdisp:
# replicate the content-disposition settings
self.add_header('Content-Disposition', cdisp, **cdisp_params)
- self.set_payload(mail.body)
+ charset = None
+ body = mail.body
+ if hasattr(mail.body, 'encode'):
+ charset = best_charset(mail.body)
+ if charset:
+ body = body.encode(charset)
+ self.set_payload(body, charset=charset)
encoders.encode_base64(self)
def __repr__(self):
@@ -408,6 +402,16 @@ def __repr__(self):
self.is_multipart())
+def best_charset(text):
+ for charset in 'ascii', 'latin_1', 'utf_8':
+ try:
+ text.encode(charset)
+ except UnicodeError:
+ pass
+ else:
+ return charset
+
+
def is_nonstr_iter(v):
if isinstance(v, str):
return False
@@ -423,11 +427,5 @@ def header_to_mime_encoding(value, charset=DEFAULT_ENCODING):
if is_nonstr_iter(value): # not a string
value = ", ".join(value)
if value:
- for charset in 'ascii', 'latin_1', 'utf_8':
- try:
- value.encode(charset)
- except UnicodeError:
- pass
- else:
- break
+ charset = best_charset(value)
return header.Header(value, charset=charset)
View
11 pyramid_mailer/tests.py
@@ -921,17 +921,6 @@ def _makeOne(self, type, **params):
from pyramid_mailer.response import MIMEPart
return MIMEPart(type, **params)
- def test_add_text_string(self):
- part = self._makeOne('text/html')
- part.add_text('a')
- self.assertEqual(part.get_payload(), b'a')
-
- def test_add_text_unicode(self):
- part = self._makeOne('text/html')
- la = b'LaPe\xc3\xb1a'.decode('utf-8')
- part.add_text(la)
- self.assertEqual(part.get_payload(), 'TGFQZcOxYQ==\n')
-
def test_extract_payload(self):
mail = DummyPart()
mail.content_encoding['Content-Type'] = ('application/json', {})
Please sign in to comment.
Something went wrong with that request. Please try again.