Move translation from expat.error to SAXParseException into feed, so that

callers of feed will get a SAXException.
In close, feed the last chunk first before calling endDocument, so that
the parser may report errors before the end of the document. Don't do
anything in a nested parser.
Don't call endDocument in parse; that will be called in close.
Use self._source for finding the SystemID; XML_GetBase will be cleared in
case of an error.
This commit is contained in:
Martin v. Löwis 2000-10-06 21:08:59 +00:00
parent c2bac8745d
commit ee1dc157d7
1 changed files with 19 additions and 17 deletions

View File

@ -39,13 +39,7 @@ class ExpatParser(xmlreader.IncrementalParser, xmlreader.Locator):
self._source = source self._source = source
self.reset() self.reset()
self._cont_handler.setDocumentLocator(self) self._cont_handler.setDocumentLocator(self)
try: xmlreader.IncrementalParser.parse(self, source)
xmlreader.IncrementalParser.parse(self, source)
except expat.error:
error_code = self._parser.ErrorCode
raise SAXParseException(expat.ErrorString(error_code), None, self)
self._cont_handler.endDocument()
def prepareParser(self, source): def prepareParser(self, source):
if source.getSystemId() != None: if source.getSystemId() != None:
@ -73,21 +67,29 @@ class ExpatParser(xmlreader.IncrementalParser, xmlreader.Locator):
# IncrementalParser methods # IncrementalParser methods
def feed(self, data): def feed(self, data, isFinal = 0):
if not self._parsing: if not self._parsing:
self._parsing = 1 self._parsing = 1
self.reset() self.reset()
self._cont_handler.startDocument() self._cont_handler.startDocument()
if not self._parser.Parse(data, 0): try:
msg = pyexpat.ErrorString(self._parser.ErrorCode) # The isFinal parameter is internal to the expat reader.
raise SAXParseException(msg, None, self) # If it is set to true, expat will check validity of the entire
# document. When feeding chunks, they are not normally final -
# except when invoked from close.
self._parser.Parse(data, isFinal)
except expat.error:
error_code = self._parser.ErrorCode
raise SAXParseException(expat.ErrorString(error_code), None, self)
def close(self): def close(self):
if self._parsing: if self._entity_stack:
self._cont_handler.endDocument() # If we are completing an external entity, do nothing here
self._parsing = 0 return
self._parser.Parse("", 1) self.feed("", isFinal = 1)
self._cont_handler.endDocument()
self._parsing = 0
def reset(self): def reset(self):
if self._namespaces: if self._namespaces:
@ -128,7 +130,7 @@ class ExpatParser(xmlreader.IncrementalParser, xmlreader.Locator):
return self._source.getPublicId() return self._source.getPublicId()
def getSystemId(self): def getSystemId(self):
return self._parser.GetBase() return self._source.getSystemId()
# event handlers # event handlers
def start_element(self, name, attrs): def start_element(self, name, attrs):
@ -194,7 +196,7 @@ class ExpatParser(xmlreader.IncrementalParser, xmlreader.Locator):
try: try:
xmlreader.IncrementalParser.parse(self, source) xmlreader.IncrementalParser.parse(self, source)
self.close() self._parser.Parse("",1)
except: except:
return 0 # FIXME: save error info here? return 0 # FIXME: save error info here?