[Python-checkins] CVS: python/dist/src/Lib xmllib.py,1.20,1.21

Sjoerd Mullender python-dev@python.org
Mon, 10 Jul 2000 01:09:51 -0700


Update of /cvsroot/python/python/dist/src/Lib
In directory slayer.i.sourceforge.net:/tmp/cvs-serv21011

Modified Files:
	xmllib.py 
Log Message:
Better error handling of bad entity references.  Before when an & in
an attribute value was not escaped, you could get two syntax errors:
one about a missing semicolon and one about an unknown entity.  Now
you get only one about a bogus ampersand.


Index: xmllib.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/xmllib.py,v
retrieving revision 1.20
retrieving revision 1.21
diff -C2 -r1.20 -r1.21
*** xmllib.py	2000/07/04 14:53:12	1.20
--- xmllib.py	2000/07/10 08:09:48	1.21
***************
*** 182,197 ****
              if res is None:
                  return data
!             res = ref.match(data, res.start(0))
              if res is None:
                  self.syntax_error("bogus `&'")
!                 i =i+1
                  continue
              i = res.end(0)
-             if data[i - 1] != ';':
-                 self.syntax_error("`;' missing after entity/char reference")
-                 i = i-1
              str = res.group(1)
!             pre = data[:res.start(0)]
!             post = data[i:]
              if str[0] == '#':
                  if str[1] == 'x':
--- 182,194 ----
              if res is None:
                  return data
!             s = res.start(0)
!             res = ref.match(data, s)
              if res is None:
                  self.syntax_error("bogus `&'")
!                 i = s+1
                  continue
              i = res.end(0)
              str = res.group(1)
!             rescan = 0
              if str[0] == '#':
                  if str[1] == 'x':
***************
*** 199,216 ****
                  else:
                      str = chr(string.atoi(str[1:]))
!                 data = pre + str + post
!                 i = res.start(0)+len(str)
              elif all:
                  if self.entitydefs.has_key(str):
!                     data = pre + self.entitydefs[str] + post
!                     i = res.start(0)    # rescan substituted text
                  else:
                      self.syntax_error("reference to unknown entity `&%s;'" % str)
!                     # can't do it, so keep the entity ref in
!                     data = pre + '&' + str + ';' + post
!                     i = res.start(0) + len(str) + 2
              else:
!                 # just translating character references
!                 pass                    # i is already postioned correctly
  
      # Internal -- handle data as far as reasonable.  May leave state
--- 196,225 ----
                  else:
                      str = chr(string.atoi(str[1:]))
!                 if data[i - 1] != ';':
!                     self.syntax_error("`;' missing after char reference")
!                     i = i-1
              elif all:
                  if self.entitydefs.has_key(str):
!                     str = self.entitydefs[str]
!                     rescan = 1
!                 elif data[i - 1] != ';':
!                     self.syntax_error("bogus `&'")
!                     i = s + 1 # just past the &
!                     continue
                  else:
                      self.syntax_error("reference to unknown entity `&%s;'" % str)
!                     str = '&' + str + ';'
!             elif data[i - 1] != ';':
!                 self.syntax_error("bogus `&'")
!                 i = s + 1 # just past the &
!                 continue
! 
!             # when we get here, str contains the translated text and i points
!             # to the end of the string that is to be replaced
!             data = data[:s] + str + data[i:]
!             if rescan:
!                 i = s
              else:
!                 i = s + len(str)
  
      # Internal -- handle data as far as reasonable.  May leave state