Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fb73bb1

Browse files
committed
Synchronize with 1.13 of PyXML:
Allow application to set a new content handler and lex_prop handler during parsing. Closes bug #433761. Small hack to make expat be ignored in Jython.
1 parent d083839 commit fb73bb1

1 file changed

Lines changed: 28 additions & 6 deletions

File tree

Lib/xml/sax/expatreader.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
version = "0.20"
77

88
from xml.sax._exceptions import *
9+
10+
# xml.parsers.expat does not raise ImportError in Jython
11+
import sys
12+
if sys.platform[ : 4] == "java":
13+
raise SAXReaderNotAvailable("expat not available in Java", None)
14+
del sys
15+
916
try:
1017
from xml.parsers import expat
1118
except ImportError:
@@ -46,6 +53,13 @@ def prepareParser(self, source):
4653
if source.getSystemId() != None:
4754
self._parser.SetBase(source.getSystemId())
4855

56+
# Redefined setContentHandle to allow changing handlers during parsing
57+
58+
def setContentHandler(self, handler):
59+
xmlreader.IncrementalParser.setContentHandler(self, handler)
60+
if self._parsing:
61+
self._reset_cont_handler()
62+
4963
def getFeature(self, name):
5064
if name == handler.feature_namespaces:
5165
return self._namespaces
@@ -68,6 +82,8 @@ def getProperty(self, name):
6882
def setProperty(self, name, value):
6983
if name == handler.property_lexical_handler:
7084
self._lex_handler_prop = value
85+
if self._parsing:
86+
self._reset_lex_handler_prop()
7187
else:
7288
raise SAXNotRecognizedException("Property '%s' not recognized" % name)
7389

@@ -101,6 +117,16 @@ def close(self):
101117
# break cycle created by expat handlers pointing to our methods
102118
self._parser = None
103119

120+
def _reset_cont_handler(self):
121+
self._parser.ProcessingInstructionHandler = \
122+
self._cont_handler.processingInstruction
123+
self._parser.CharacterDataHandler = self._cont_handler.characters
124+
125+
def _reset_lex_handler_prop(self):
126+
self._parser.CommentHandler = self._lex_handler_prop.comment
127+
self._parser.StartCdataSectionHandler = self._lex_handler_prop.startCDATA
128+
self._parser.EndCdataSectionHandler = self._lex_handler_prop.endCDATA
129+
104130
def reset(self):
105131
if self._namespaces:
106132
self._parser = expat.ParserCreate(None, " ")
@@ -111,19 +137,15 @@ def reset(self):
111137
self._parser.StartElementHandler = self.start_element
112138
self._parser.EndElementHandler = self.end_element
113139

114-
self._parser.ProcessingInstructionHandler = \
115-
self._cont_handler.processingInstruction
116-
self._parser.CharacterDataHandler = self._cont_handler.characters
140+
self._reset_cont_handler()
117141
self._parser.UnparsedEntityDeclHandler = self.unparsed_entity_decl
118142
self._parser.NotationDeclHandler = self.notation_decl
119143
self._parser.StartNamespaceDeclHandler = self.start_namespace_decl
120144
self._parser.EndNamespaceDeclHandler = self.end_namespace_decl
121145

122146
self._decl_handler_prop = None
123147
if self._lex_handler_prop:
124-
self._parser.CommentHandler = self._lex_handler_prop.comment
125-
self._parser.StartCdataSectionHandler = self._lex_handler_prop.startCDATA
126-
self._parser.EndCdataSectionHandler = self._lex_handler_prop.endCDATA
148+
self._reset_lex_handler_prop()
127149
# self._parser.DefaultHandler =
128150
# self._parser.DefaultHandlerExpand =
129151
# self._parser.NotStandaloneHandler =

0 commit comments

Comments
 (0)