mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
_encode_chunks(), encode(): Don't modify self._chunks. As Ben says:
Also, it fixes a really egregious error in Header.encode() (really in Header._encode_chunks()) that could cause a header to grow and grow each time encode() was called if output_codec was different from input_codec. Also, fix a typo.
This commit is contained in:
parent
ab9439fdd4
commit
0c358258c9
1 changed files with 22 additions and 23 deletions
|
@ -218,7 +218,7 @@ class Header:
|
||||||
charset = Charset(charset)
|
charset = Charset(charset)
|
||||||
# Normalize and check the string
|
# Normalize and check the string
|
||||||
if isinstance(s, StringType):
|
if isinstance(s, StringType):
|
||||||
# Possibly raise UnicodeError if it can't e encoded
|
# Possibly raise UnicodeError if it can't be encoded
|
||||||
unicode(s, charset.get_output_charset())
|
unicode(s, charset.get_output_charset())
|
||||||
elif isinstance(s, UnicodeType):
|
elif isinstance(s, UnicodeType):
|
||||||
# Convert Unicode to byte string for later concatenation
|
# Convert Unicode to byte string for later concatenation
|
||||||
|
@ -346,27 +346,27 @@ class Header:
|
||||||
rtn.append(EMPTYSTRING.join(sublines))
|
rtn.append(EMPTYSTRING.join(sublines))
|
||||||
return [(chunk, charset) for chunk in rtn]
|
return [(chunk, charset) for chunk in rtn]
|
||||||
|
|
||||||
def _encode_chunks(self):
|
def _encode_chunks(self, newchunks):
|
||||||
"""MIME-encode a header with many different charsets and/or encodings.
|
# MIME-encode a header with many different charsets and/or encodings.
|
||||||
|
#
|
||||||
Given a list of pairs (string, charset), return a MIME-encoded string
|
# Given a list of pairs (string, charset), return a MIME-encoded
|
||||||
suitable for use in a header field. Each pair may have different
|
# string suitable for use in a header field. Each pair may have
|
||||||
charsets and/or encodings, and the resulting header will accurately
|
# different charsets and/or encodings, and the resulting header will
|
||||||
reflect each setting.
|
# accurately reflect each setting.
|
||||||
|
#
|
||||||
Each encoding can be email.Utils.QP (quoted-printable, for ASCII-like
|
# Each encoding can be email.Utils.QP (quoted-printable, for
|
||||||
character sets like iso-8859-1), email.Utils.BASE64 (Base64, for
|
# ASCII-like character sets like iso-8859-1), email.Utils.BASE64
|
||||||
non-ASCII like character sets like KOI8-R and iso-2022-jp), or None
|
# (Base64, for non-ASCII like character sets like KOI8-R and
|
||||||
(no encoding).
|
# iso-2022-jp), or None (no encoding).
|
||||||
|
#
|
||||||
Each pair will be represented on a separate line; the resulting string
|
# Each pair will be represented on a separate line; the resulting
|
||||||
will be in the format:
|
# string will be in the format:
|
||||||
|
#
|
||||||
"=?charset1?q?Mar=EDa_Gonz=E1lez_Alonso?=\n
|
# =?charset1?q?Mar=EDa_Gonz=E1lez_Alonso?=\n
|
||||||
=?charset2?b?SvxyZ2VuIEL2aW5n?="
|
# =?charset2?b?SvxyZ2VuIEL2aW5n?="
|
||||||
"""
|
#
|
||||||
chunks = []
|
chunks = []
|
||||||
for header, charset in self._chunks:
|
for header, charset in newchunks:
|
||||||
if charset is None or charset.header_encoding is None:
|
if charset is None or charset.header_encoding is None:
|
||||||
# There's no encoding for this chunk's charsets
|
# There's no encoding for this chunk's charsets
|
||||||
_max_append(chunks, header, self._maxlinelen)
|
_max_append(chunks, header, self._maxlinelen)
|
||||||
|
@ -397,5 +397,4 @@ class Header:
|
||||||
newchunks = []
|
newchunks = []
|
||||||
for s, charset in self._chunks:
|
for s, charset in self._chunks:
|
||||||
newchunks += self._split(s, charset, True)
|
newchunks += self._split(s, charset, True)
|
||||||
self._chunks = newchunks
|
return self._encode_chunks(newchunks)
|
||||||
return self._encode_chunks()
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue