[Python-checkins] python/dist/src/Lib/email Message.py,1.15,1.16 Parser.py,1.10,1.11

bwarsaw@users.sourceforge.net bwarsaw@users.sourceforge.net
Thu, 18 Jul 2002 16:09:12 -0700


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

Modified Files:
	Message.py Parser.py 
Log Message:
Anthony Baxter's cleanup patch.  Python project SF patch # 583190,
quoting:

  in non-strict mode, messages don't require a blank line at the end 
  with a missing end-terminator. A single newline is sufficient now. 

  Handle trailing whitespace at the end of a boundary. Had to switch 
  from using string.split() to re.split()

  Handle whitespace on the end of a parameter list for Content-type.

  Handle whitespace on the end of a plain content-type header.

Specifically,

get_type(): Strip the content type string.

_get_params_preserve(): Strip the parameter names and values on both
sides.

_parsebody(): Lots of changes as described above, with some stylistic
changes by Barry (who hopefully didn't screw things up ;).


Index: Message.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/email/Message.py,v
retrieving revision 1.15
retrieving revision 1.16
diff -C2 -d -r1.15 -r1.16
*** Message.py	9 Jul 2002 02:46:12 -0000	1.15
--- Message.py	18 Jul 2002 23:09:09 -0000	1.16
***************
*** 374,378 ****
          if value is missing:
              return failobj
!         return paramre.split(value)[0].lower()
  
      def get_main_type(self, failobj=None):
--- 374,378 ----
          if value is missing:
              return failobj
!         return paramre.split(value)[0].lower().strip()
  
      def get_main_type(self, failobj=None):
***************
*** 429,437 ****
              try:
                  name, val = p.split('=', 1)
!                 name = name.rstrip()
!                 val = val.lstrip()
              except ValueError:
                  # Must have been a bare attribute
!                 name = p
                  val = ''
              params.append((name, val))
--- 429,437 ----
              try:
                  name, val = p.split('=', 1)
!                 name = name.strip()
!                 val = val.strip()
              except ValueError:
                  # Must have been a bare attribute
!                 name = p.strip()
                  val = ''
              params.append((name, val))

Index: Parser.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/email/Parser.py,v
retrieving revision 1.10
retrieving revision 1.11
diff -C2 -d -r1.10 -r1.11
*** Parser.py	9 Jul 2002 02:50:02 -0000	1.10
--- Parser.py	18 Jul 2002 23:09:09 -0000	1.11
***************
*** 125,141 ****
              preamble = epilogue = None
              # Split into subparts.  The first boundary we're looking for won't
!             # have the leading newline since we're at the start of the body
!             # text.
              separator = '--' + boundary
              payload = fp.read()
!             start = payload.find(separator)
!             if start < 0:
                  raise Errors.BoundaryError(
                      "Couldn't find starting boundary: %s" % boundary)
              if start > 0:
                  # there's some pre-MIME boundary preamble
                  preamble = payload[0:start]
              # Find out what kind of line endings we're using
!             start += len(separator)
              cre = re.compile('\r\n|\r|\n')
              mo = cre.search(payload, start)
--- 125,147 ----
              preamble = epilogue = None
              # Split into subparts.  The first boundary we're looking for won't
!             # always have a leading newline since we're at the start of the
!             # body text, and there's not always a preamble before the first
!             # boundary.
              separator = '--' + boundary
              payload = fp.read()
!             # We use an RE here because boundaries can have trailing 
!             # whitespace.
!             mo = re.search(
!                 r'(?P<sep>' + re.escape(separator) + r')(?P<ws>[ \t]*)',
!                 payload)
!             if not mo:
                  raise Errors.BoundaryError(
                      "Couldn't find starting boundary: %s" % boundary)
+             start = mo.start()
              if start > 0:
                  # there's some pre-MIME boundary preamble
                  preamble = payload[0:start]
              # Find out what kind of line endings we're using
!             start += len(mo.group('sep')) + len(mo.group('ws'))
              cre = re.compile('\r\n|\r|\n')
              mo = cre.search(payload, start)
***************
*** 152,156 ****
                  linesep = mo.group('sep')
                  if mo.end() < len(payload):
!                     # there's some post-MIME boundary epilogue
                      epilogue = payload[mo.end():]
              elif self._strict:
--- 158,162 ----
                  linesep = mo.group('sep')
                  if mo.end() < len(payload):
!                     # There's some post-MIME boundary epilogue
                      epilogue = payload[mo.end():]
              elif self._strict:
***************
*** 158,172 ****
                          "Couldn't find terminating boundary: %s" % boundary)
              else:
!                 # handle the case of no trailing boundary. I hate mail clients.
!                 # check that it ends in a blank line
!                 endre = re.compile('(?P<sep>\r\n|\r|\n){2}$')
!                 mo = endre.search(payload)
                  if not mo:
!                     raise Errors.BoundaryError(
!                         "Couldn't find terminating boundary, and no "+
!                         "trailing empty line")
!                 else:
!                     linesep = mo.group('sep')
!                     terminator = len(payload)
              # We split the textual payload on the boundary separator, which
              # includes the trailing newline. If the container is a
--- 164,178 ----
                          "Couldn't find terminating boundary: %s" % boundary)
              else:
!                 # Handle the case of no trailing boundary.  Check that it ends
!                 # in a blank line.  Some cases (spamspamspam) don't even have
!                 # that!
!                 mo = re.search('(?P<sep>\r\n|\r|\n){2}$', payload)
                  if not mo:
!                     mo = re.search('(?P<sep>\r\n|\r|\n)$', payload)
!                     if not mo:
!                         raise Errors.BoundaryError(
!                           'No terminating boundary and no trailing empty line')
!                 linesep = mo.group('sep')
!                 terminator = len(payload)
              # We split the textual payload on the boundary separator, which
              # includes the trailing newline. If the container is a
***************
*** 175,180 ****
              # block of MIME headers, then an empty line followed by the 
              # message headers.
!             separator += linesep
!             parts = payload[start:terminator].split(linesep + separator)
              for part in parts:
                  if isdigest: 
--- 181,187 ----
              # block of MIME headers, then an empty line followed by the 
              # message headers.
!             parts = re.split(
!                 linesep + re.escape(separator) + r'[ \t]*' + linesep,
!                 payload[start:terminator])
              for part in parts:
                  if isdigest: