From 68bef91d733aaf8f08df5f3daf0821a4c59d4d87 Mon Sep 17 00:00:00 2001 From: Olivier Dony Date: Fri, 20 Oct 2017 18:40:22 +0200 Subject: [PATCH] [FIX] P3: mail: message parsing must return native strings There was a leftover explicit conversion to bytestrings during the parsing of HTML email parts. This translated into a parse result with `msg_dict['body']` as a `bytes` instance in Python 3, instead of the expected text type. Coercing the result of etree.tostring() with UTF-8 encoding to a native string should preserve the semantics in both Python versions without this side-effect. + simplify another instance of native string use --- addons/mail/models/mail_thread.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/addons/mail/models/mail_thread.py b/addons/mail/models/mail_thread.py index fd736269968..ef606db3f78 100644 --- a/addons/mail/models/mail_thread.py +++ b/addons/mail/models/mail_thread.py @@ -1387,6 +1387,7 @@ class MailThread(models.AbstractModel): node.getparent().remove(node) if postprocessed: body = etree.tostring(root, pretty_print=False, encoding='UTF-8') + body = pycompat.to_native(body) return body, attachments def _message_extract_payload(self, message, save_original=False): @@ -1495,10 +1496,8 @@ class MailThread(models.AbstractModel): 'message_type': 'email', } if not isinstance(message, Message): - # message_from_string works on a native str, so on py2 we need to - # encode incoming unicode strings - if pycompat.PY2 and not isinstance(message, str): - message = message.encode('utf-8') + # message_from_string works on a native str + message = pycompat.to_native(message) message = email.message_from_string(message) message_id = message['message-id']