diff options
| author | Florian Klink <flokli@flokli.de> | 2017-09-24 14:36:11 +0200 |
|---|---|---|
| committer | David Bremner <david@tethera.net> | 2017-10-02 07:21:33 -0300 |
| commit | 91fe20cd90ce46bf80c416a5c4451f76b4d69ec5 (patch) | |
| tree | c9d6427716c3b016115a3ddd2873d03413dd7631 /bindings/python | |
| parent | 073188e6905f1b5a065f696c4d02d0d1b3a7d769 (diff) | |
python: open messages in binary mode
currently, notmuch's get_message_parts() opens the file in text mode and passes
the file object to email.message_from_file(fp). In case the email contains
UTF-8 characters, reading might fail inside email.parser with the following exception:
File "/usr/lib/python3.6/site-packages/notmuch/message.py", line 591, in get_message_parts
email_msg = email.message_from_binary_file(fp)
File "/usr/lib/python3.6/email/__init__.py", line 62, in message_from_binary_file
return BytesParser(*args, **kws).parse(fp)
File "/usr/lib/python3.6/email/parser.py", line 110, in parse
return self.parser.parse(fp, headersonly)
File "/usr/lib/python3.6/email/parser.py", line 54, in parse
data = fp.read(8192)
File "/usr/lib/python3.6/codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe4 in position 1865: invalid continuation byte
To fix this, read file in binary mode and pass to
email.message_from_binary_file(fp).
Unfortunately, Python 2 doesn't support
email.message_from_binary_file(fp), so keep using
email.message_from_file(fp) there.
Signed-off-by: Florian Klink <flokli@flokli.de>
Diffstat (limited to 'bindings/python')
| -rw-r--r-- | bindings/python/notmuch/message.py | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/bindings/python/notmuch/message.py b/bindings/python/notmuch/message.py index cce377d0..d5b98e4f 100644 --- a/bindings/python/notmuch/message.py +++ b/bindings/python/notmuch/message.py @@ -41,6 +41,7 @@ from .tag import Tags from .filenames import Filenames import email +import sys class Message(Python3StringMixIn): @@ -587,8 +588,11 @@ class Message(Python3StringMixIn): def get_message_parts(self): """Output like notmuch show""" - fp = open(self.get_filename()) - email_msg = email.message_from_file(fp) + fp = open(self.get_filename(), 'rb') + if sys.version_info[0] < 3: + email_msg = email.message_from_file(fp) + else: + email_msg = email.message_from_binary_file(fp) fp.close() out = [] |
