Patchwork [07,of,10,STABLE] py3: use a BytesParser in notify extension

login
register
mail settings
Submitter Denis Laxalde
Date Oct. 24, 2019, 3:30 p.m.
Message ID <187e1e54ef5cf5a30ea6.1571931038@steppe.local>
Download mbox | patch
Permalink /patch/42566/
State Accepted
Headers show

Comments

Denis Laxalde - Oct. 24, 2019, 3:30 p.m.
# HG changeset patch
# User Denis Laxalde <denis.laxalde@logilab.fr>
# Date 1571923680 -7200
#      Thu Oct 24 15:28:00 2019 +0200
# Branch stable
# Node ID 187e1e54ef5cf5a30ea690d69369876004aef443
# Parent  aaacb57e24c54398dfdd56bf93b68b7bad510b4c
py3: use a BytesParser in notify extension

This is the first step to make the "long line" case in test-notify.t
pass by fixing a UnicodeDecodeError on Python 3.

We alias a parsebytes() in mail module, similarly as we already have a
parse() function for Python 2 and Python 3 compatibility.

Patch

diff --git a/hgext/notify.py b/hgext/notify.py
--- a/hgext/notify.py
+++ b/hgext/notify.py
@@ -148,7 +148,6 @@  web.baseurl
 from __future__ import absolute_import
 
 import email.errors as emailerrors
-import email.parser as emailparser
 import fnmatch
 import hashlib
 import socket
@@ -382,9 +381,8 @@  class notifier(object):
             )
             return
 
-        p = emailparser.Parser()
         try:
-            msg = p.parsestr(encoding.strfromlocal(data))
+            msg = mail.parsebytes(data)
         except emailerrors.MessageParseError as inst:
             raise error.Abort(inst)
 
diff --git a/mercurial/mail.py b/mercurial/mail.py
--- a/mercurial/mail.py
+++ b/mercurial/mail.py
@@ -441,6 +441,9 @@  if pycompat.ispy3:
         finally:
             fp.detach()
 
+    def parsebytes(data):
+        ep = email.parser.BytesParser()
+        return ep.parsebytes(data)
 
 else:
 
@@ -450,6 +453,10 @@  else:
         ep = email.parser.Parser()
         return ep.parse(fp)
 
+    def parsebytes(data):
+        ep = email.parser.Parser()
+        return ep.parsestr(data)
+
 
 def headdecode(s):
     '''Decodes RFC-2047 header'''