Commit 3339c5e7 authored by Barry Warsaw's avatar Barry Warsaw

_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.
parent a5237a58
...@@ -218,7 +218,7 @@ class Header: ...@@ -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: ...@@ -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: ...@@ -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()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment