Permalink
Browse files

be explicit about where bytes are used, Python 2 and 3 compat.

  • Loading branch information...
1 parent 9b5ac4b commit 2c984c45a06514a2ae52dd7cede065bebfd7f108 @rpatterson rpatterson committed Mar 13, 2012
Showing with 10 additions and 9 deletions.
  1. +2 −2 pyramid_mailer/response.py
  2. +8 −7 pyramid_mailer/tests.py
View
4 pyramid_mailer/response.py
@@ -424,7 +424,7 @@ def header_to_mime_encoding(value, not_email=False):
encoder = Charset(DEFAULT_ENCODING)
if is_nonstr_iter(value): # not a string
- return ", ".join(properly_encode_header(
+ return b", ".join(properly_encode_header(
v, encoder, not_email) for v in value)
else:
return properly_encode_header(value, encoder, not_email)
@@ -448,7 +448,7 @@ def properly_encode_header(value, encoder, not_email):
if not_email is False and VALUE_IS_EMAIL_ADDRESS(value):
# this could have an email address, make sure we don't screw it up
name, address = parseaddr(value)
- return '"%s" <%s>' % (
+ return b'"%s" <%s>' % (
encoder.header_encode(name.encode("utf-8")), address)
return encoder.header_encode(value.encode("utf-8"))
View
15 pyramid_mailer/tests.py
@@ -826,7 +826,8 @@ def test_to_message_multiple_to_recipients(self):
From='From', Subject='Subject',
Body='Body', Html='Html')
message = response.to_message()
- self.assertEqual(message['To'], 'chrism@plope.com, billg@microsoft.com')
+ self.assertEqual(message[b'To'],
+ b'chrism@plope.com, billg@microsoft.com')
def test_to_message_multipart(self):
from pyramid_mailer.response import MIMEPart
@@ -957,12 +958,12 @@ def test_empty_value(self):
def test_list_value(self):
L = ['chrism@plope.com', 'billg@microsoft.com']
result = self._callFUT(L)
- self.assertEqual(result, 'chrism@plope.com, billg@microsoft.com')
+ self.assertEqual(result, b'chrism@plope.com, billg@microsoft.com')
def test_nonempty_nonlist_value(self):
val = 'chrism@plope.com'
result = self._callFUT(val)
- self.assertEqual(result, 'chrism@plope.com')
+ self.assertEqual(result, b'chrism@plope.com')
class Test_properly_encode_header(unittest.TestCase):
def _callFUT(self, value, encoder, not_email):
@@ -971,13 +972,13 @@ def _callFUT(self, value, encoder, not_email):
def test_ascii_encodable(self):
result = self._callFUT('a', None, None)
- self.assertEqual(result, 'a')
+ self.assertEqual(result, b'a')
def test_not_ascii_encodable_email(self):
la = b'LaPe\xc3\xb1a@plope.com'.decode('utf-8')
class Encoder(object):
def header_encode(self, val):
- return 'encoded'
+ return b'encoded'
encoder = Encoder()
result = self._callFUT(la, encoder, False)
self.assertEqual(result, '"encoded" <LaPe\xf1a@plope.com>')
@@ -986,10 +987,10 @@ def test_not_ascii_encodable(self):
la = b'LaPe\xc3\xb1a'.decode('utf-8')
class Encoder(object):
def header_encode(self, val):
- return 'encoded'
+ return b'encoded'
encoder = Encoder()
result = self._callFUT(la, encoder, False)
- self.assertEqual(result, 'encoded')
+ self.assertEqual(result, b'encoded')
class Dummy(object):
pass

0 comments on commit 2c984c4

Please sign in to comment.