summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBarry Warsaw <barry@python.org>2003-03-07 15:39:37 (GMT)
committerBarry Warsaw <barry@python.org>2003-03-07 15:39:37 (GMT)
commit9f3fcd9c23be0b430e335d36ea374ce9838e390e (patch)
tree03f801e51d7b8e2a1a973ac5183b81d04f5c30a3
parentc4d6bdd58a58b365556abfe60c9f5b5f73c555f8 (diff)
downloadcpython-9f3fcd9c23be0b430e335d36ea374ce9838e390e.zip
cpython-9f3fcd9c23be0b430e335d36ea374ce9838e390e.tar.gz
cpython-9f3fcd9c23be0b430e335d36ea374ce9838e390e.tar.bz2
More internal refinements of the ascii splitting algorithm.
_encode_chunks(): Pass maxlinelen in instead of always using self._maxlinelen, so we can adjust for shorter initial lines. Pass this value through to _max_append(). encode(): Weave maxlinelen through to the _encode_chunks() call. _split_ascii(): When recursively splitting a line on spaces (i.e. lower level syntactic split), don't append the whole returned string. Instead, split it on linejoiners and extend the lines up to the last line (for proper packing). Calculate the linelen based on the last element in the this list.
-rw-r--r--Lib/email/Header.py17
1 files changed, 10 insertions, 7 deletions
diff --git a/Lib/email/Header.py b/Lib/email/Header.py
index cef734c..afd815f 100644
--- a/Lib/email/Header.py
+++ b/Lib/email/Header.py
@@ -342,7 +342,7 @@ class Header:
lines = line.splitlines()
return zip(lines, [charset]*len(lines))
- def _encode_chunks(self, newchunks):
+ def _encode_chunks(self, newchunks, maxlinelen):
# MIME-encode a header with many different charsets and/or encodings.
#
# Given a list of pairs (string, charset), return a MIME-encoded
@@ -367,7 +367,7 @@ class Header:
s = header
else:
s = charset.header_encode(header)
- _max_append(chunks, s, self._maxlinelen, ' ')
+ _max_append(chunks, s, maxlinelen, ' ')
joiner = NL + self._continuation_ws
return joiner.join(chunks)
@@ -407,11 +407,12 @@ class Header:
newchunks += self._split(s, charset, targetlen, splitchars)
lastchunk, lastcharset = newchunks[-1]
lastlen = lastcharset.encoded_header_len(lastchunk)
- return self._encode_chunks(newchunks)
+ return self._encode_chunks(newchunks, maxlinelen)
def _split_ascii(s, firstlen, restlen, continuation_ws, splitchars):
+ linejoiner = '\n' + continuation_ws
lines = []
maxlen = firstlen
for line in s.splitlines():
@@ -460,11 +461,14 @@ def _split_ascii(s, firstlen, restlen, continuation_ws, splitchars):
# splitting on whitespace, try to recursively split this line
# on whitespace.
if partlen > maxlen and ch <> ' ':
- this = [_split_ascii(part, maxlen, restlen,
- continuation_ws, ' ')]
+ subs = _split_ascii(part, maxlen, restlen,
+ continuation_ws, ' ')
+ subl = re.split(linejoiner, subs)
+ lines.extend(subl[:-1])
+ this = [subl[-1]]
else:
this = [part]
- linelen = wslen + partlen
+ linelen = wslen + len(this[-1])
maxlen = restlen
else:
this.append(part)
@@ -472,7 +476,6 @@ def _split_ascii(s, firstlen, restlen, continuation_ws, splitchars):
# Put any left over parts on a line by themselves
if this:
lines.append(joiner.join(this))
- linejoiner = '\n' + continuation_ws
return linejoiner.join(lines)