def _write_headers(self, msg): # This is almost the same as the string version, except for handling # strings with 8bit bytes. for h in msg._headers: self.write('%s: ' % h.name) if isinstance(h, str): v = h.source if h.source is not None else h if _has_surrogates(v): if not self.policy.must_be_7bit: # If we have raw 8bit data in a byte string, we have no idea # what the encoding is. There is no safe way to split this # string. If it's ascii-subset, then we could do a normal # ascii split, but if it's multibyte then we could break the # string. There's no way to know so the least harm seems to # be to not split the string and risk it being too long. self.write(v + NL) continue h = Header(v, charset=_charset.UNKNOWN8BIT, header_name=h.name) else: h = Header(v, header_name=h.name) self.write( h.encode(linesep=self._NL, maxlinelen=self._maxheaderlen) + self._NL) # A blank line always separates headers from body self.write(self._NL)
def _write_headers(self, msg): for h, v in msg.items(): self.write('%s: ' % h) if isinstance(v, Header): self.write( v.encode(maxlinelen=self._maxheaderlen, linesep=self._NL) + self._NL) else: v = v.source if v.source is not None else v # Header's got lots of smarts, so use it. header = Header(v, maxlinelen=self._maxheaderlen, header_name=h) self.write(header.encode(linesep=self._NL) + self._NL) # A blank line always separates headers from body self.write(self._NL)
def test_japanese_codecs(self): eq = self.ndiffAssertEqual jcode = "euc-jp" gcode = "iso-8859-1" j = Charset(jcode) g = Charset(gcode) h = Header("Hello World!") jhello = str(b'\xa5\xcf\xa5\xed\xa1\xbc\xa5\xef\xa1\xbc' b'\xa5\xeb\xa5\xc9\xa1\xaa', jcode) ghello = str(b'Gr\xfc\xdf Gott!', gcode) h.append(jhello, j) h.append(ghello, g) # BAW: This used to -- and maybe should -- fold the two iso-8859-1 # chunks into a single encoded word. However it doesn't violate the # standard to have them as two encoded chunks and maybe it's # reasonable <wink> for each .append() call to result in a separate # encoded word. eq(h.encode(), """\ Hello World! =?iso-2022-jp?b?GyRCJU8lbSE8JW8hPCVrJUkhKhsoQg==?= =?iso-8859-1?q?Gr=FC=DF_Gott!?=""") eq(decode_header(h.encode()), [(b'Hello World!', None), (b'\x1b$B%O%m!<%o!<%k%I!*\x1b(B', 'iso-2022-jp'), (b'Gr\xfc\xdf Gott!', gcode)]) subject_bytes = (b'test-ja \xa4\xd8\xc5\xea\xb9\xc6\xa4\xb5' b'\xa4\xec\xa4\xbf\xa5\xe1\xa1\xbc\xa5\xeb\xa4\xcf\xbb\xca\xb2' b'\xf1\xbc\xd4\xa4\xce\xbe\xb5\xc7\xa7\xa4\xf2\xc2\xd4\xa4\xc3' b'\xa4\xc6\xa4\xa4\xa4\xde\xa4\xb9') subject = str(subject_bytes, jcode) h = Header(subject, j, header_name="Subject") # test a very long header enc = h.encode() # TK: splitting point may differ by codec design and/or Header encoding eq(enc , """\ =?iso-2022-jp?b?dGVzdC1qYSAbJEIkWEVqOUYkNSRsJD8lYSE8JWskTztKGyhC?= =?iso-2022-jp?b?GyRCMnE8VCROPjVHJyRyQlQkQyRGJCQkXiQ5GyhC?=""") # TK: full decode comparison eq(str(h).encode(jcode), subject_bytes)