comparison mercurial/patch.py @ 36083:04984f2e50ae

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
author Gregory Szorc <gregory.szorc@gmail.com>
date Sun, 11 Feb 2018 14:17:23 -0800
parents 95791b275b73
children a5cf79755eff
comparison
equal deleted inserted replaced
36082:3b4d14beac3d 36083:04984f2e50ae
10 10
11 import collections 11 import collections
12 import copy 12 import copy
13 import difflib 13 import difflib
14 import email 14 import email
15 import email.parser as emailparser
16 import errno 15 import errno
17 import hashlib 16 import hashlib
18 import os 17 import os
19 import posixpath 18 import posixpath
20 import re 19 import re
107 106
108 for line in stream: 107 for line in stream:
109 cur.append(line) 108 cur.append(line)
110 c = chunk(cur) 109 c = chunk(cur)
111 110
112 m = emailparser.Parser().parse(c) 111 m = pycompat.emailparser().parse(c)
113 if not m.is_multipart(): 112 if not m.is_multipart():
114 yield msgfp(m) 113 yield msgfp(m)
115 else: 114 else:
116 ok_types = ('text/plain', 'text/x-diff', 'text/x-patch') 115 ok_types = ('text/plain', 'text/x-diff', 'text/x-patch')
117 for part in m.walk(): 116 for part in m.walk():
216 215
217 data = {} 216 data = {}
218 fd, tmpname = tempfile.mkstemp(prefix='hg-patch-') 217 fd, tmpname = tempfile.mkstemp(prefix='hg-patch-')
219 tmpfp = os.fdopen(fd, pycompat.sysstr('w')) 218 tmpfp = os.fdopen(fd, pycompat.sysstr('w'))
220 try: 219 try:
221 msg = emailparser.Parser().parse(fileobj) 220 msg = pycompat.emailparser().parse(fileobj)
222 221
223 subject = msg['Subject'] and mail.headdecode(msg['Subject']) 222 subject = msg['Subject'] and mail.headdecode(msg['Subject'])
224 data['user'] = msg['From'] and mail.headdecode(msg['From']) 223 data['user'] = msg['From'] and mail.headdecode(msg['From'])
225 if not subject and not data['user']: 224 if not subject and not data['user']:
226 # Not an email, restore parsed headers if any 225 # Not an email, restore parsed headers if any