mercurial/mail.py
changeset 43627 af3e341dbf03
parent 43626 bdb0ddab7bb3
child 43628 ddb5d097d561
equal deleted inserted replaced
43626:bdb0ddab7bb3 43627:af3e341dbf03
   261     if cs.startswith("iso") and not cs.startswith("iso-"):
   261     if cs.startswith("iso") and not cs.startswith("iso-"):
   262         return "iso-" + cs[3:]
   262         return "iso-" + cs[3:]
   263     return cs
   263     return cs
   264 
   264 
   265 
   265 
   266 def mimetextpatch(s, subtype=b'plain', display=False):
   266 def mimetextpatch(s, subtype='plain', display=False):
   267     # type: (bytes, bytes, bool) -> email.message.Message
   267     # type: (bytes, str, bool) -> email.message.Message
   268     '''Return MIME message suitable for a patch.
   268     '''Return MIME message suitable for a patch.
   269     Charset will be detected by first trying to decode as us-ascii, then utf-8,
   269     Charset will be detected by first trying to decode as us-ascii, then utf-8,
   270     and finally the global encodings. If all those fail, fall back to
   270     and finally the global encodings. If all those fail, fall back to
   271     ISO-8859-1, an encoding with that allows all byte sequences.
   271     ISO-8859-1, an encoding with that allows all byte sequences.
   272     Transfer encodings will be used if necessary.'''
   272     Transfer encodings will be used if necessary.'''
   288 
   288 
   289     return mimetextqp(s, subtype, "iso-8859-1")
   289     return mimetextqp(s, subtype, "iso-8859-1")
   290 
   290 
   291 
   291 
   292 def mimetextqp(body, subtype, charset):
   292 def mimetextqp(body, subtype, charset):
   293     # type: (bytes, bytes, str) -> email.message.Message
   293     # type: (bytes, str, str) -> email.message.Message
   294     '''Return MIME message.
   294     '''Return MIME message.
   295     Quoted-printable transfer encoding will be used if necessary.
   295     Quoted-printable transfer encoding will be used if necessary.
   296     '''
   296     '''
   297     cs = email.charset.Charset(charset)
   297     cs = email.charset.Charset(charset)
   298     msg = email.message.Message()
   298     msg = email.message.Message()
   299     msg.set_type(pycompat.sysstr(b'text/' + subtype))
   299     msg.set_type('text/' + subtype)
   300 
   300 
   301     for line in body.splitlines():
   301     for line in body.splitlines():
   302         if len(line) > 950:
   302         if len(line) > 950:
   303             cs.body_encoding = email.charset.QP
   303             cs.body_encoding = email.charset.QP
   304             break
   304             break
   448     '''creates mime text object, encodes it if needed, and sets
   448     '''creates mime text object, encodes it if needed, and sets
   449     charset and transfer-encoding accordingly.'''
   449     charset and transfer-encoding accordingly.'''
   450     cs = 'us-ascii'
   450     cs = 'us-ascii'
   451     if not display:
   451     if not display:
   452         s, cs = _encode(ui, s, charsets)
   452         s, cs = _encode(ui, s, charsets)
   453     return mimetextqp(s, b'plain', cs)
   453     return mimetextqp(s, 'plain', cs)
   454 
   454 
   455 
   455 
   456 if pycompat.ispy3:
   456 if pycompat.ispy3:
   457 
   457 
   458     Generator = email.generator.BytesGenerator
   458     Generator = email.generator.BytesGenerator