Synchronize with PyXML 1.10

Break cycle involving expat parser in close().
Add lex handler support to SAX2 pyexpat
This commit is contained in:
Martin v. Löwis 2001-01-27 08:56:24 +00:00
parent 52ce0d0837
commit 0591725bc5
1 changed files with 25 additions and 7 deletions

View File

@ -27,6 +27,7 @@ class ExpatParser(xmlreader.IncrementalParser, xmlreader.Locator):
self._source = xmlreader.InputSource() self._source = xmlreader.InputSource()
self._parser = None self._parser = None
self._namespaces = namespaceHandling self._namespaces = namespaceHandling
self._lex_handler_prop = None
self._parsing = 0 self._parsing = 0
self._entity_stack = [] self._entity_stack = []
@ -60,10 +61,15 @@ class ExpatParser(xmlreader.IncrementalParser, xmlreader.Locator):
name) name)
def getProperty(self, name): def getProperty(self, name):
if name == handler.property_lexical_handler:
return self._lex_handler_prop
raise SAXNotRecognizedException("Property '%s' not recognized" % name) raise SAXNotRecognizedException("Property '%s' not recognized" % name)
def setProperty(self, name, value): def setProperty(self, name, value):
raise SAXNotRecognizedException("Property '%s' not recognized" % name) if name == handler.property_lexical_handler:
self._lex_handler_prop = value
else:
raise SAXNotRecognizedException("Property '%s' not recognized" % name)
# IncrementalParser methods # IncrementalParser methods
@ -82,6 +88,7 @@ class ExpatParser(xmlreader.IncrementalParser, xmlreader.Locator):
except expat.error: except expat.error:
error_code = self._parser.ErrorCode error_code = self._parser.ErrorCode
exc = SAXParseException(expat.ErrorString(error_code), None, self) exc = SAXParseException(expat.ErrorString(error_code), None, self)
# FIXME: when to invoke error()?
self._err_handler.fatalError(exc) self._err_handler.fatalError(exc)
def close(self): def close(self):
@ -91,6 +98,8 @@ class ExpatParser(xmlreader.IncrementalParser, xmlreader.Locator):
self.feed("", isFinal = 1) self.feed("", isFinal = 1)
self._cont_handler.endDocument() self._cont_handler.endDocument()
self._parsing = 0 self._parsing = 0
# break cycle created by expat handlers pointing to our methods
self._parser = None
def reset(self): def reset(self):
if self._namespaces: if self._namespaces:
@ -109,12 +118,15 @@ class ExpatParser(xmlreader.IncrementalParser, xmlreader.Locator):
self._parser.NotationDeclHandler = self.notation_decl self._parser.NotationDeclHandler = self.notation_decl
self._parser.StartNamespaceDeclHandler = self.start_namespace_decl self._parser.StartNamespaceDeclHandler = self.start_namespace_decl
self._parser.EndNamespaceDeclHandler = self.end_namespace_decl self._parser.EndNamespaceDeclHandler = self.end_namespace_decl
# self._parser.CommentHandler =
# self._parser.StartCdataSectionHandler = self._decl_handler_prop = None
# self._parser.EndCdataSectionHandler = if self._lex_handler_prop:
# self._parser.DefaultHandler = self._parser.CommentHandler = self._lex_handler_prop.comment
# self._parser.DefaultHandlerExpand = self._parser.StartCdataSectionHandler = self._lex_handler_prop.startCDATA
# self._parser.NotStandaloneHandler = self._parser.EndCdataSectionHandler = self._lex_handler_prop.endCDATA
# self._parser.DefaultHandler =
# self._parser.DefaultHandlerExpand =
# self._parser.NotStandaloneHandler =
self._parser.ExternalEntityRefHandler = self.external_entity_ref self._parser.ExternalEntityRefHandler = self.external_entity_ref
self._parsing = 0 self._parsing = 0
@ -123,9 +135,13 @@ class ExpatParser(xmlreader.IncrementalParser, xmlreader.Locator):
# Locator methods # Locator methods
def getColumnNumber(self): def getColumnNumber(self):
if self._parser is None:
return None
return self._parser.ErrorColumnNumber return self._parser.ErrorColumnNumber
def getLineNumber(self): def getLineNumber(self):
if self._parser is None:
return 1
return self._parser.ErrorLineNumber return self._parser.ErrorLineNumber
def getPublicId(self): def getPublicId(self):
@ -165,6 +181,8 @@ class ExpatParser(xmlreader.IncrementalParser, xmlreader.Locator):
pair = string.split(name) pair = string.split(name)
if len(pair) == 1: if len(pair) == 1:
pair = (None, name) pair = (None, name)
else:
pair = tuple(pair)
self._cont_handler.endElementNS(pair, None) self._cont_handler.endElementNS(pair, None)