summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBarry Warsaw <barry@python.org>2003-03-07 15:43:17 (GMT)
committerBarry Warsaw <barry@python.org>2003-03-07 15:43:17 (GMT)
commitce6bf59b2d7583c6a904f98e199988d9933720a9 (patch)
treecf4f8975630386675ab1538ce92e2c455fac63e8
parent9f3fcd9c23be0b430e335d36ea374ce9838e390e (diff)
downloadcpython-ce6bf59b2d7583c6a904f98e199988d9933720a9.zip
cpython-ce6bf59b2d7583c6a904f98e199988d9933720a9.tar.gz
cpython-ce6bf59b2d7583c6a904f98e199988d9933720a9.tar.bz2
_write_headers(), _split_header(): All of the smarts for splitting
long header lines is now (properly) in the Header class. So we no longer need _split_header() and we'll just defer to Header.encode() when we have a plain string.
-rw-r--r--Lib/email/Generator.py55
1 files changed, 20 insertions, 35 deletions
diff --git a/Lib/email/Generator.py b/Lib/email/Generator.py
index 4f455a4..9cce51c 100644
--- a/Lib/email/Generator.py
+++ b/Lib/email/Generator.py
@@ -161,44 +161,29 @@ class Generator:
def _write_headers(self, msg):
for h, v in msg.items():
- # RFC 2822 says that lines SHOULD be no more than maxheaderlen
- # characters wide, so we're well within our rights to split long
- # headers.
- text = '%s: %s' % (h, v)
- if self.__maxheaderlen > 0 and len(text) > self.__maxheaderlen:
- text = self._split_header(text)
- print >> self._fp, text
+ print >> self._fp, '%s:' % h,
+ if self.__maxheaderlen == 0:
+ # Explicit no-wrapping
+ print >> self._fp, v
+ elif isinstance(v, Header):
+ # Header instances know what to do
+ print >> self._fp, v.encode()
+ elif _is8bitstring(v):
+ # If we have raw 8bit data in a byte string, we have no idea
+ # what the encoding is. There is no safe way to split this
+ # string. If it's ascii-subset, then we could do a normal
+ # ascii split, but if it's multibyte then we could break the
+ # string. There's no way to know so the least harm seems to
+ # be to not split the string and risk it being too long.
+ print >> self._fp, v
+ else:
+ # Header's got lots of smarts, so use it.
+ print >> self._fp, Header(
+ v, maxlinelen=self.__maxheaderlen,
+ header_name=h, continuation_ws='\t').encode()
# A blank line always separates headers from body
print >> self._fp
- def _split_header(self, text):
- maxheaderlen = self.__maxheaderlen
- # Find out whether any lines in the header are really longer than
- # maxheaderlen characters wide. There could be continuation lines
- # that actually shorten it. Also, replace hard tabs with 8 spaces.
- lines = [s.replace('\t', SPACE8) for s in text.splitlines()]
- for line in lines:
- if len(line) > maxheaderlen:
- break
- else:
- # No line was actually longer than maxheaderlen characters, so
- # just return the original unchanged.
- return text
- # If we have raw 8bit data in a byte string, we have no idea what the
- # encoding is. I think there is no safe way to split this string. If
- # it's ascii-subset, then we could do a normal ascii split, but if
- # it's multibyte then we could break the string. There's no way to
- # know so the least harm seems to be to not split the string and risk
- # it being too long.
- if _is8bitstring(text):
- return text
- # The `text' argument already has the field name prepended, so don't
- # provide it here or the first line will get folded too short.
- h = Header(text, maxlinelen=maxheaderlen,
- # For backwards compatibility, we use a hard tab here
- continuation_ws='\t')
- return h.encode()
-
#
# Handlers for writing types and subtypes
#