Skip to content

Commit 00b3422

Browse files
committed
More email package fixes.
This repairs the linear whitespace insertion between RFC 2047 encoded words without leaving bogus trailing spaces at the end lines that end in encoded words. Current status: 7F/9E
1 parent 3bcc42a commit 00b3422

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

Lib/email/header.py

+15-13
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
USASCII = Charset('us-ascii')
3131
UTF8 = Charset('utf-8')
32-
TRANSITIONAL_SPACE = object()
3332

3433
# Match encoded-word strings in the form =?charset?q?Hello_World?=
3534
ecre = re.compile(r'''
@@ -309,6 +308,7 @@ def encode(self, splitchars=';, \t', maxlinelen=None):
309308
formatter.feed(line, charset)
310309
if len(lines) > 1:
311310
formatter.newline()
311+
formatter.add_transition()
312312
return str(formatter)
313313

314314
def _normalize(self):
@@ -341,19 +341,20 @@ def __init__(self, headerlen, maxlen, continuation_ws, splitchars):
341341
self._current_line = _Accumulator(headerlen)
342342

343343
def __str__(self):
344-
# Remove any trailing TRANSITIONAL_SPACE
345-
if len(self._current_line) > 0:
346-
last_line = self._current_line.pop()
347-
if last_line is not TRANSITIONAL_SPACE:
348-
self._current_line.push(last_line)
349344
self.newline()
350345
return NL.join(self._lines)
351346

352347
def newline(self):
348+
end_of_line = self._current_line.pop()
349+
if end_of_line is not None:
350+
self._current_line.push(end_of_line)
353351
if len(self._current_line) > 0:
354352
self._lines.append(str(self._current_line))
355353
self._current_line.reset()
356354

355+
def add_transition(self):
356+
self._current_line.push(None)
357+
357358
def feed(self, string, charset):
358359
# If the string itself fits on the current line in its encoded format,
359360
# then add it now and be done with it.
@@ -408,7 +409,6 @@ def feed(self, string, charset):
408409
# There was only one line.
409410
return
410411
self._current_line.push(last_line)
411-
self._current_line.push(TRANSITIONAL_SPACE)
412412
# Everything else are full lines in themselves.
413413
for line in encoded_lines:
414414
self._lines.append(self._continuation_ws + line)
@@ -554,18 +554,20 @@ def push(self, string):
554554
self._current.append(string)
555555

556556
def pop(self):
557+
if not self._current:
558+
return None
557559
return self._current.pop()
558560

559561
def __len__(self):
560-
return sum((len(string)
561-
for string in self._current
562-
if string is not TRANSITIONAL_SPACE),
562+
return sum(((1 if string is None else len(string))
563+
for string in self._current),
563564
self._initial_size)
564565

565566
def __str__(self):
566-
return EMPTYSTRING.join(
567-
(' ' if string is TRANSITIONAL_SPACE else string)
568-
for string in self._current)
567+
if self._current and self._current[-1] is None:
568+
self._current.pop()
569+
return EMPTYSTRING.join((' ' if string is None else string)
570+
for string in self._current)
569571

570572
def reset(self, string=None):
571573
self._current = []

Lib/email/test/test_email.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1556,7 +1556,7 @@ def test_rfc2047_multiline(self):
15561556
header = make_header(dh)
15571557
eq(str(header),
15581558
'Re: r\xe4ksm\xf6rg\xe5s baz foo bar r\xe4ksm\xf6rg\xe5s')
1559-
self.ndiffAssertEqual(header.encode(), """\
1559+
self.ndiffAssertEqual(header.encode(maxlinelen=76), """\
15601560
Re: =?mac-iceland?q?r=8Aksm=9Arg=8Cs?= baz foo bar =?mac-iceland?q?r=8Aksm?=
15611561
=?mac-iceland?q?=9Arg=8Cs?=""")
15621562

0 commit comments

Comments
 (0)