mirror of
https://github.com/python/cpython.git
synced 2025-09-04 16:01:10 +00:00
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
This commit is contained in:
parent
3bcc42ad0f
commit
00b34228bb
2 changed files with 16 additions and 14 deletions
|
@ -29,7 +29,6 @@ MAXLINELEN = 78
|
||||||
|
|
||||||
USASCII = Charset('us-ascii')
|
USASCII = Charset('us-ascii')
|
||||||
UTF8 = Charset('utf-8')
|
UTF8 = Charset('utf-8')
|
||||||
TRANSITIONAL_SPACE = object()
|
|
||||||
|
|
||||||
# Match encoded-word strings in the form =?charset?q?Hello_World?=
|
# Match encoded-word strings in the form =?charset?q?Hello_World?=
|
||||||
ecre = re.compile(r'''
|
ecre = re.compile(r'''
|
||||||
|
@ -309,6 +308,7 @@ class Header:
|
||||||
formatter.feed(line, charset)
|
formatter.feed(line, charset)
|
||||||
if len(lines) > 1:
|
if len(lines) > 1:
|
||||||
formatter.newline()
|
formatter.newline()
|
||||||
|
formatter.add_transition()
|
||||||
return str(formatter)
|
return str(formatter)
|
||||||
|
|
||||||
def _normalize(self):
|
def _normalize(self):
|
||||||
|
@ -341,19 +341,20 @@ class _ValueFormatter:
|
||||||
self._current_line = _Accumulator(headerlen)
|
self._current_line = _Accumulator(headerlen)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
# Remove any trailing TRANSITIONAL_SPACE
|
|
||||||
if len(self._current_line) > 0:
|
|
||||||
last_line = self._current_line.pop()
|
|
||||||
if last_line is not TRANSITIONAL_SPACE:
|
|
||||||
self._current_line.push(last_line)
|
|
||||||
self.newline()
|
self.newline()
|
||||||
return NL.join(self._lines)
|
return NL.join(self._lines)
|
||||||
|
|
||||||
def newline(self):
|
def newline(self):
|
||||||
|
end_of_line = self._current_line.pop()
|
||||||
|
if end_of_line is not None:
|
||||||
|
self._current_line.push(end_of_line)
|
||||||
if len(self._current_line) > 0:
|
if len(self._current_line) > 0:
|
||||||
self._lines.append(str(self._current_line))
|
self._lines.append(str(self._current_line))
|
||||||
self._current_line.reset()
|
self._current_line.reset()
|
||||||
|
|
||||||
|
def add_transition(self):
|
||||||
|
self._current_line.push(None)
|
||||||
|
|
||||||
def feed(self, string, charset):
|
def feed(self, string, charset):
|
||||||
# If the string itself fits on the current line in its encoded format,
|
# If the string itself fits on the current line in its encoded format,
|
||||||
# then add it now and be done with it.
|
# then add it now and be done with it.
|
||||||
|
@ -408,7 +409,6 @@ class _ValueFormatter:
|
||||||
# There was only one line.
|
# There was only one line.
|
||||||
return
|
return
|
||||||
self._current_line.push(last_line)
|
self._current_line.push(last_line)
|
||||||
self._current_line.push(TRANSITIONAL_SPACE)
|
|
||||||
# Everything else are full lines in themselves.
|
# Everything else are full lines in themselves.
|
||||||
for line in encoded_lines:
|
for line in encoded_lines:
|
||||||
self._lines.append(self._continuation_ws + line)
|
self._lines.append(self._continuation_ws + line)
|
||||||
|
@ -554,18 +554,20 @@ class _Accumulator:
|
||||||
self._current.append(string)
|
self._current.append(string)
|
||||||
|
|
||||||
def pop(self):
|
def pop(self):
|
||||||
|
if not self._current:
|
||||||
|
return None
|
||||||
return self._current.pop()
|
return self._current.pop()
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return sum((len(string)
|
return sum(((1 if string is None else len(string))
|
||||||
for string in self._current
|
for string in self._current),
|
||||||
if string is not TRANSITIONAL_SPACE),
|
|
||||||
self._initial_size)
|
self._initial_size)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return EMPTYSTRING.join(
|
if self._current and self._current[-1] is None:
|
||||||
(' ' if string is TRANSITIONAL_SPACE else string)
|
self._current.pop()
|
||||||
for string in self._current)
|
return EMPTYSTRING.join((' ' if string is None else string)
|
||||||
|
for string in self._current)
|
||||||
|
|
||||||
def reset(self, string=None):
|
def reset(self, string=None):
|
||||||
self._current = []
|
self._current = []
|
||||||
|
|
|
@ -1556,7 +1556,7 @@ class TestRFC2047(TestEmailBase):
|
||||||
header = make_header(dh)
|
header = make_header(dh)
|
||||||
eq(str(header),
|
eq(str(header),
|
||||||
'Re: r\xe4ksm\xf6rg\xe5s baz foo bar r\xe4ksm\xf6rg\xe5s')
|
'Re: r\xe4ksm\xf6rg\xe5s baz foo bar r\xe4ksm\xf6rg\xe5s')
|
||||||
self.ndiffAssertEqual(header.encode(), """\
|
self.ndiffAssertEqual(header.encode(maxlinelen=76), """\
|
||||||
Re: =?mac-iceland?q?r=8Aksm=9Arg=8Cs?= baz foo bar =?mac-iceland?q?r=8Aksm?=
|
Re: =?mac-iceland?q?r=8Aksm=9Arg=8Cs?= baz foo bar =?mac-iceland?q?r=8Aksm?=
|
||||||
=?mac-iceland?q?=9Arg=8Cs?=""")
|
=?mac-iceland?q?=9Arg=8Cs?=""")
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue