[Python-checkins] python/dist/src/Lib/email Header.py,1.16,1.17

bwarsaw@users.sourceforge.net bwarsaw@users.sourceforge.net
Mon, 30 Dec 2002 11:13:02 -0800


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

Modified Files:
	Header.py 
Log Message:
Header.__init__(), .append(): Add an optional argument `errors' which
is passed straight through to the unicode() and ustr.encode() calls.
I think it's the best we can do to address the UnicodeErrors in badly
encoded headers such as is described in SF bug #648119.


Index: Header.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/email/Header.py,v
retrieving revision 1.16
retrieving revision 1.17
diff -C2 -d -r1.16 -r1.17
*** Header.py	14 Oct 2002 16:52:41 -0000	1.16
--- Header.py	30 Dec 2002 19:13:00 -0000	1.17
***************
*** 128,132 ****
  class Header:
      def __init__(self, s=None, charset=None, maxlinelen=None, header_name=None,
!                  continuation_ws=' '):
          """Create a MIME-compliant header that can contain many character sets.
  
--- 128,132 ----
  class Header:
      def __init__(self, s=None, charset=None, maxlinelen=None, header_name=None,
!                  continuation_ws=' ', errors='strict'):
          """Create a MIME-compliant header that can contain many character sets.
  
***************
*** 151,154 ****
--- 151,156 ----
          either a space or a hard tab) which will be prepended to continuation
          lines.
+ 
+         errors is passed through to the .append() call.
          """
          if charset is None:
***************
*** 162,166 ****
          self._chunks = []
          if s is not None:
!             self.append(s, charset)
          if maxlinelen is None:
              maxlinelen = MAXLINELEN
--- 164,168 ----
          self._chunks = []
          if s is not None:
!             self.append(s, charset, errors)
          if maxlinelen is None:
              maxlinelen = MAXLINELEN
***************
*** 197,201 ****
          return not self == other
  
!     def append(self, s, charset=None):
          """Append a string to the MIME header.
  
--- 199,203 ----
          return not self == other
  
!     def append(self, s, charset=None, errors='strict'):
          """Append a string to the MIME header.
  
***************
*** 214,217 ****
--- 216,222 ----
          following charsets in order: us-ascii, the charset hint, utf-8.  The
          first character set not to provoke a UnicodeError is used.
+ 
+         Optional `errors' is passed as the third argument to any unicode() or
+         ustr.encode() call.
          """
          if charset is None:
***************
*** 228,237 ****
                  # converted to a unicode with the input codec of the charset.
                  incodec = charset.input_codec or 'us-ascii'
!                 ustr = unicode(s, incodec)
                  # Now make sure that the unicode could be converted back to a
                  # byte string with the output codec, which may be different
                  # than the iput coded.  Still, use the original byte string.
                  outcodec = charset.output_codec or 'us-ascii'
!                 ustr.encode(outcodec)
              elif isinstance(s, UnicodeType):
                  # Now we have to be sure the unicode string can be converted
--- 233,242 ----
                  # converted to a unicode with the input codec of the charset.
                  incodec = charset.input_codec or 'us-ascii'
!                 ustr = unicode(s, incodec, errors)
                  # Now make sure that the unicode could be converted back to a
                  # byte string with the output codec, which may be different
                  # than the iput coded.  Still, use the original byte string.
                  outcodec = charset.output_codec or 'us-ascii'
!                 ustr.encode(outcodec, errors)
              elif isinstance(s, UnicodeType):
                  # Now we have to be sure the unicode string can be converted
***************
*** 241,245 ****
                      try:
                          outcodec = charset.output_codec or 'us-ascii'
!                         s = s.encode(outcodec)
                          break
                      except UnicodeError:
--- 246,250 ----
                      try:
                          outcodec = charset.output_codec or 'us-ascii'
!                         s = s.encode(outcodec, errors)
                          break
                      except UnicodeError: