[Python-checkins] CVS: python/dist/src/Lib/email Generator.py,1.4,1.5

Barry Warsaw bwarsaw@users.sourceforge.net
Wed, 17 Oct 2001 13:51:44 -0700


Update of /cvsroot/python/python/dist/src/Lib/email
In directory usw-pr-cvs1:/tmp/cvs-serv18469

Modified Files:
	Generator.py 
Log Message:
Two merges from the mimelib project:

    _split_header(): Split on folding whitespace if the attempt to split
    on semi-colons failed.

    _split_header(): Patch by Matthew Cowles for fixing SF bug # 471918,
    Generator splitting long headers.


Index: Generator.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/email/Generator.py,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** Generator.py	2001/10/04 17:05:11	1.4
--- Generator.py	2001/10/17 20:51:42	1.5
***************
*** 16,23 ****
--- 16,25 ----
  import Errors
  
+ EMPTYSTRING = ''
  SEMISPACE = '; '
  BAR = '|'
  UNDERSCORE = '_'
  NL = '\n'
+ NLTAB = '\n\t'
  SEMINLTAB = ';\n\t'
  SPACE8 = ' ' * 8
***************
*** 172,187 ****
              if len(line.replace('\t', SPACE8)) <= maxheaderlen:
                  rtn.append(line)
              else:
                  # Try to break the line on semicolons, but if that doesn't
!                 # work, then just leave it alone.
                  while len(text) > maxheaderlen:
                      i = text.rfind(';', 0, maxheaderlen)
                      if i < 0:
-                         rtn.append(text)
                          break
                      rtn.append(text[:i])
                      text = text[i+1:].lstrip()
!                 rtn.append(text)
!         return SEMINLTAB.join(rtn)
  
      #
--- 174,218 ----
              if len(line.replace('\t', SPACE8)) <= maxheaderlen:
                  rtn.append(line)
+                 SEMINLTAB.join(rtn)
              else:
+                 oldlen = len(text)
                  # Try to break the line on semicolons, but if that doesn't
!                 # work, try to split on folding whitespace.
                  while len(text) > maxheaderlen:
                      i = text.rfind(';', 0, maxheaderlen)
                      if i < 0:
                          break
                      rtn.append(text[:i])
                      text = text[i+1:].lstrip()
!                 if len(text) <> oldlen:
!                     # Splitting on semis worked
!                     rtn.append(text)
!                     return SEMINLTAB.join(rtn)
!                 # Splitting on semis didn't help, so try to split on
!                 # whitespace.
!                 parts = re.split(r'(\s+)', text)
!                 # Watch out though for "Header: longnonsplittableline"
!                 if parts[0].endswith(':') and len(parts) == 3:
!                     return text
!                 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
!                         rtn.append(EMPTYSTRING.join(sublines))
!                         del parts[0]
!                         first = parts.pop(0)
!                         sublines = [first]
!                         acc = len(first)
!                 rtn.append(EMPTYSTRING.join(sublines))
!                 return NLTAB.join(rtn)
  
      #