# HG changeset patch # User Gregory Szorc # Date 2018-02-11 22:17:23 # Node ID 04984f2e50ae53840f8628ca203f03000f4de222 # Parent 3b4d14beac3d0d3ca6a8755beda6d31709a69a06 py3: use email parser that operates on bytes email.parser.Parser() operates on str in both Python 2 and 3. Python 3.2 introduced the email.parser.BytesParser(), which works like Parser except it accepts bytes. We implement the pycompat helper as a function so we lazily import the "email" module. Differential Revision: https://phab.mercurial-scm.org/D2147 diff --git a/mercurial/patch.py b/mercurial/patch.py --- a/mercurial/patch.py +++ b/mercurial/patch.py @@ -12,7 +12,6 @@ import collections import copy import difflib import email -import email.parser as emailparser import errno import hashlib import os @@ -109,7 +108,7 @@ def split(stream): cur.append(line) c = chunk(cur) - m = emailparser.Parser().parse(c) + m = pycompat.emailparser().parse(c) if not m.is_multipart(): yield msgfp(m) else: @@ -218,7 +217,7 @@ def extract(ui, fileobj): fd, tmpname = tempfile.mkstemp(prefix='hg-patch-') tmpfp = os.fdopen(fd, pycompat.sysstr('w')) try: - msg = emailparser.Parser().parse(fileobj) + msg = pycompat.emailparser().parse(fileobj) subject = msg['Subject'] and mail.headdecode(msg['Subject']) data['user'] = msg['From'] and mail.headdecode(msg['From']) diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py --- a/mercurial/pycompat.py +++ b/mercurial/pycompat.py @@ -267,6 +267,10 @@ if ispy3: ret = shlex.split(s.decode('latin-1')) return [a.encode('latin-1') for a in ret] + def emailparser(*args, **kwargs): + import email.parser + return email.parser.BytesParser(*args, **kwargs) + else: import cStringIO @@ -327,6 +331,10 @@ else: ziplist = zip rawinput = raw_input + def emailparser(*args, **kwargs): + import email.parser + return email.parser.Parser(*args, **kwargs) + isjython = sysplatform.startswith('java') isdarwin = sysplatform == 'darwin'