[Python-checkins] python/dist/src/Lib/email Generator.py,1.9,1.10
bwarsaw@users.sourceforge.net
bwarsaw@users.sourceforge.net
Fri, 28 Jun 2002 16:41:44 -0700
Update of /cvsroot/python/python/dist/src/Lib/email
In directory usw-pr-cvs1:/tmp/cvs-serv15842/email
Modified Files:
Generator.py
Log Message:
_split_header(): The code here was terminally broken because it didn't
know anything about RFC 2047 encoded headers. Fortunately we have a
perfectly good header splitter in Header.encode(). So we just call
that to give us a properly formatted and split header.
Header.encode() didn't know about "highest-level syntactic breaks" but
that's been fixed now too.
Index: Generator.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/email/Generator.py,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -d -r1.9 -r1.10
*** Generator.py 2 Jun 2002 19:02:37 -0000 1.9
--- Generator.py 28 Jun 2002 23:41:42 -0000 1.10
***************
*** 12,15 ****
--- 12,17 ----
from cStringIO import StringIO
+ from email.Header import Header
+
EMPTYSTRING = ''
SEMISPACE = '; '
***************
*** 150,164 ****
text = '%s: %s' % (h, v)
if self.__maxheaderlen > 0 and len(text) > self.__maxheaderlen:
! text = self._split_header(text)
print >> self._fp, text
# 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.split('\n')]
for line in lines:
if len(line) > maxheaderlen:
--- 152,166 ----
text = '%s: %s' % (h, v)
if self.__maxheaderlen > 0 and len(text) > self.__maxheaderlen:
! text = self._split_header(h, text)
print >> self._fp, text
# A blank line always separates headers from body
print >> self._fp
! def _split_header(self, name, 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:
***************
*** 168,221 ****
# just return the original unchanged.
return text
! rtn = []
! for line in text.split('\n'):
! splitline = []
! # Short lines can remain unchanged
! if len(line.replace('\t', SPACE8)) <= maxheaderlen:
! splitline.append(line)
! rtn.append(SEMINLTAB.join(splitline))
! else:
! oldlen = len(line)
! # Try to break the line on semicolons, but if that doesn't
! # work, try to split on folding whitespace.
! while len(line) > maxheaderlen:
! i = line.rfind(';', 0, maxheaderlen)
! if i < 0:
! break
! splitline.append(line[:i])
! line = line[i+1:].lstrip()
! if len(line) <> oldlen:
! # Splitting on semis worked
! splitline.append(line)
! rtn.append(SEMINLTAB.join(splitline))
! continue
! # Splitting on semis didn't help, so try to split on
! # whitespace.
! parts = re.split(r'(\s+)', line)
! # Watch out though for "Header: longnonsplittableline"
! if parts[0].endswith(':') and len(parts) == 3:
! rtn.append(line)
! continue
! first = parts.pop(0)
! sublines = [first]
! acc = len(first)
! while parts:
! len0 = len(parts[0])
! len1 = len(parts[1])
! if acc + len0 + len1 < maxheaderlen:
! sublines.append(parts.pop(0))
! sublines.append(parts.pop(0))
! acc += len0 + len1
! else:
! # Split it here, but don't forget to ignore the
! # next whitespace-only part
! splitline.append(EMPTYSTRING.join(sublines))
! del parts[0]
! first = parts.pop(0)
! sublines = [first]
! acc = len(first)
! splitline.append(EMPTYSTRING.join(sublines))
! rtn.append(NLTAB.join(splitline))
! return NL.join(rtn)
#
--- 170,179 ----
# just return the original unchanged.
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()
#