[Python-checkins] python/dist/src/Lib/email Header.py,1.1,1.2

bwarsaw@sourceforge.net bwarsaw@sourceforge.net
Sun, 19 May 2002 16:47:55 -0700


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

Modified Files:
	Header.py 
Log Message:
Fixed a bug in the splitting of lines, and improved the splitting for
single byte character sets.  Also fixed a semantic problem with the
constructor's default arguments.  Specifically,

__init__(): Change the maxlinelen argument default to None instead of
MAXLINELEN.  The semantics should have been (and now are) that if
maxlinelen is given it is always honored.  If it isn't given, but
header_name is given, then the maximum line length is calculated.  If
neither are given then the default 76 characters is used.

_split(): If the character set is a single byte character set then we
can split the line at the maxlinelen because we know that encoding the
header won't increase its length.  If the charset isn't a single byte
charset then we use the quicker divide-and-conquer line splitting
algorithm as before.


Index: Header.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/email/Header.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** Header.py	10 Apr 2002 21:01:30 -0000	1.1
--- Header.py	19 May 2002 23:47:53 -0000	1.2
***************
*** 9,12 ****
--- 9,18 ----
  from email.Charset import Charset
  
+ try:
+     from email._compat22 import _intdiv2
+ except SyntaxError:
+     # Python 2.1 spells integer division differently
+     from email._compat21 import _intdiv2
+ 
  CRLFSPACE = '\r\n '
  CRLF = '\r\n'
***************
*** 87,92 ****
  
  class Header:
!     def __init__(self, s, charset=None, maxlinelen=MAXLINELEN,
!                  header_name=None):
          """Create a MIME-compliant header that can contain many languages.
  
--- 93,97 ----
  
  class Header:
!     def __init__(self, s, charset=None, maxlinelen=None, header_name=None):
          """Create a MIME-compliant header that can contain many languages.
  
***************
*** 100,107 ****
          charset specified in the constructor.
  
!         The maximum line length can either be specified by maxlinelen, or you
!         can pass in the name of the header field (e.g. "Subject") to let this
!         class guess the best line length to use to prevent wrapping.  The
!         default maxlinelen is 76.
          """
          if charset is None:
--- 105,112 ----
          charset specified in the constructor.
  
!         The maximum line length can be specified explicitly via maxlinelen.
!         You can also pass None for maxlinelen and the name of a header field
!         (e.g. "Subject") to let the constructor guess the best line length to
!         use.  The default maxlinelen is 76.
          """
          if charset is None:
***************
*** 111,117 ****
          self._chunks = []
          self.append(s, charset)
!         self._maxlinelen = maxlinelen
!         if header_name is not None:
!             self.guess_maxlinelen(header_name)
  
      def __str__(self):
--- 116,126 ----
          self._chunks = []
          self.append(s, charset)
!         if maxlinelen is None:
!             if header_name is None:
!                 self._maxlinelen = MAXLINELEN
!             else:
!                 self.guess_maxlinelen(header_name)
!         else:
!             self._maxlinelen = maxlinelen
  
      def __str__(self):
***************
*** 147,157 ****
          splittable = charset.to_splittable(s)
          encoded = charset.from_splittable(splittable)
          
!         if charset.encoded_header_len(encoded) < self._maxlinelen:
              return [(encoded, charset)]
          else:
              # Divide and conquer.  BAW: halfway depends on integer division.
              # When porting to Python 2.2, use the // operator.
!             halfway = len(splittable) // 2
              first = charset.from_splittable(splittable[:halfway], 0)
              last = charset.from_splittable(splittable[halfway:], 0)
--- 156,175 ----
          splittable = charset.to_splittable(s)
          encoded = charset.from_splittable(splittable)
+         elen = charset.encoded_header_len(encoded)
          
!         if elen <= self._maxlinelen:
              return [(encoded, charset)]
+         # BAW: should we use encoded?
+         elif elen == len(s):
+             # We can split on _maxlinelen boundaries because we know that the
+             # encoding won't change the size of the string
+             splitpnt = self._maxlinelen
+             first = charset.from_splittable(splittable[:splitpnt], 0)
+             last = charset.from_splittable(splittable[splitpnt:], 0)
+             return self._split(first, charset) + self._split(last, charset)
          else:
              # Divide and conquer.  BAW: halfway depends on integer division.
              # When porting to Python 2.2, use the // operator.
!             halfway = _intdiv2(len(splittable))
              first = charset.from_splittable(splittable[:halfway], 0)
              last = charset.from_splittable(splittable[halfway:], 0)