Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed unicode email subject in Python 3.2

This probably also fixed Python 3.1: will test later. In this case we can
drop the test guard.
  • Loading branch information...
commit e9699ef6621db14d512e40904618b9771b5ddfcc 1 parent 73f6d3d
@dvarrazzo dvarrazzo authored
Showing with 16 additions and 2 deletions.
  1. +12 −2 logbook/handlers.py
  2. +4 −0 logbook/testsuite/test_regular.py
View
14 logbook/handlers.py
@@ -1087,15 +1087,25 @@ def message_from_record(self, record, suppressed):
"""
try:
from email.message import Message
+ from email.header import Header
except ImportError: # Python 2.4
from email.Message import Message
+ from email.Header import Header
msg = Message()
lineiter = iter(self.format(record).splitlines())
for line in lineiter:
if not line:
break
- pieces = line.split(':', 1)
- msg.add_header(*[x.strip() for x in pieces])
+ h, v = line.split(':', 1)
+ # We could probably just encode everything. For the moment encode
+ # only what really needed to avoid breaking a couple of tests.
+ try:
+ v.encode('ascii')
+ except UnicodeEncodeError:
+ msg[h.strip()] = Header(v.strip(), 'utf-8')
+ else:
+ msg[h.strip()] = v.strip()
+
body = '\r\n'.join(lineiter)
if suppressed:
body += '\r\n\r\nThis message occurred additional %d ' \
View
4 logbook/testsuite/test_regular.py
@@ -385,6 +385,10 @@ def test_mail_handler(self):
finally:
handler.pop_thread()
+ if not handler.mails:
+ # if sending the mail failed, the reason should be on stderr
+ self.fail(fallback.getvalue())
+
self.assertEqual(len(handler.mails), 1)
sender, receivers, mail = handler.mails[0]
self.assertEqual(sender, handler.from_addr)
Please sign in to comment.
Something went wrong with that request. Please try again.