comparison mercurial/mail.py @ 48883:f0c445a8e324

mail: delete conditional code for Python 2 Differential Revision: https://phab.mercurial-scm.org/D12286
author Gregory Szorc <gregory.szorc@gmail.com>
date Mon, 21 Feb 2022 10:20:48 -0700
parents 6000f5b25c9b
children 1d6c6ad645e1
comparison
equal deleted inserted replaced
48882:640e1cb6a7de 48883:f0c445a8e324
465 if not display: 465 if not display:
466 s, cs = _encode(ui, s, charsets) 466 s, cs = _encode(ui, s, charsets)
467 return mimetextqp(s, 'plain', cs) 467 return mimetextqp(s, 'plain', cs)
468 468
469 469
470 if pycompat.ispy3: 470 Generator = email.generator.BytesGenerator
471 471
472 Generator = email.generator.BytesGenerator 472
473 473 def parse(fp):
474 def parse(fp): 474 # type: (Any) -> email.message.Message
475 # type: (Any) -> email.message.Message 475 ep = email.parser.Parser()
476 ep = email.parser.Parser() 476 # disable the "universal newlines" mode, which isn't binary safe.
477 # disable the "universal newlines" mode, which isn't binary safe. 477 # I have no idea if ascii/surrogateescape is correct, but that's
478 # I have no idea if ascii/surrogateescape is correct, but that's 478 # what the standard Python email parser does.
479 # what the standard Python email parser does. 479 fp = io.TextIOWrapper(
480 fp = io.TextIOWrapper( 480 fp, encoding='ascii', errors='surrogateescape', newline=chr(10)
481 fp, encoding='ascii', errors='surrogateescape', newline=chr(10) 481 )
482 ) 482 try:
483 try:
484 return ep.parse(fp)
485 finally:
486 fp.detach()
487
488 def parsebytes(data):
489 # type: (bytes) -> email.message.Message
490 ep = email.parser.BytesParser()
491 return ep.parsebytes(data)
492
493
494 else:
495
496 Generator = email.generator.Generator
497
498 def parse(fp):
499 # type: (Any) -> email.message.Message
500 ep = email.parser.Parser()
501 return ep.parse(fp) 483 return ep.parse(fp)
502 484 finally:
503 def parsebytes(data): 485 fp.detach()
504 # type: (str) -> email.message.Message 486
505 ep = email.parser.Parser() 487
506 return ep.parsestr(data) 488 def parsebytes(data):
489 # type: (bytes) -> email.message.Message
490 ep = email.parser.BytesParser()
491 return ep.parsebytes(data)
507 492
508 493
509 def headdecode(s): 494 def headdecode(s):
510 # type: (Union[email.header.Header, bytes]) -> bytes 495 # type: (Union[email.header.Header, bytes]) -> bytes
511 '''Decodes RFC-2047 header''' 496 '''Decodes RFC-2047 header'''