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

Skip to content

Commit f43cf31

Browse files
committed
Updated to new *NS signatures (patch 101573).
1 parent 1258049 commit f43cf31

1 file changed

Lines changed: 25 additions & 25 deletions

File tree

Lib/xml/sax/expatreader.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
version = "0.20"
1818

19+
from xml.sax._exceptions import *
1920
from xml.parsers import expat
2021
from xml.sax import xmlreader
21-
import xml.sax
2222

2323
# --- ExpatParser
2424

@@ -31,6 +31,7 @@ def __init__(self, namespaceHandling=0, bufsize=2**16-20):
3131
self._parser = None
3232
self._namespaces = namespaceHandling
3333
self._parsing = 0
34+
self._attrs = xmlreader.AttributesImpl({}, {})
3435

3536
# XMLReader methods
3637

@@ -47,8 +48,7 @@ def parse(self, stream_or_string):
4748
xmlreader.IncrementalParser.parse(self, stream)
4849
except expat.error:
4950
error_code = self._parser.ErrorCode
50-
raise xml.sax.SAXParseException(expat.ErrorString(error_code),
51-
None, self)
51+
raise SAXParseException(expat.ErrorString(error_code), None, self)
5252

5353
self._cont_handler.endDocument()
5454

@@ -59,19 +59,23 @@ def prepareParser(self, filename=None):
5959
self._parser.SetBase(self._source)
6060

6161
def getFeature(self, name):
62-
"Looks up and returns the state of a SAX2 feature."
62+
if name == feature_namespaces:
63+
return self._namespaces
6364
raise SAXNotRecognizedException("Feature '%s' not recognized" % name)
6465

6566
def setFeature(self, name, state):
66-
"Sets the state of a SAX2 feature."
67-
raise SAXNotRecognizedException("Feature '%s' not recognized" % name)
67+
if self._parsing:
68+
raise SAXNotSupportedException("Cannot set features while parsing")
69+
if name == feature_namespaces:
70+
self._namespaces = state
71+
else:
72+
raise SAXNotRecognizedException("Feature '%s' not recognized" %
73+
name)
6874

6975
def getProperty(self, name):
70-
"Looks up and returns the value of a SAX2 property."
7176
raise SAXNotRecognizedException("Property '%s' not recognized" % name)
7277

7378
def setProperty(self, name, value):
74-
"Sets the value of a SAX2 property."
7579
raise SAXNotRecognizedException("Property '%s' not recognized" % name)
7680

7781
# IncrementalParser methods
@@ -81,8 +85,10 @@ def feed(self, data):
8185
self._parsing = 1
8286
self.reset()
8387
self._cont_handler.startDocument()
84-
# FIXME: error checking and endDocument()
85-
self._parser.Parse(data, 0)
88+
89+
if not self._parser.Parse(data, 0):
90+
msg = pyexpat.ErrorString(self._parser.ErrorCode)
91+
raise SAXParseException(msg, None, self)
8692

8793
def close(self):
8894
if self._parsing:
@@ -131,36 +137,30 @@ def getSystemId(self):
131137

132138
# event handlers
133139
def start_element(self, name, attrs):
134-
self._cont_handler.startElement(name, name,
135-
xmlreader.AttributesImpl(attrs, attrs))
140+
self._cont_handler.startElement(name, self._attrs)
136141

137142
def end_element(self, name):
138-
self._cont_handler.endElement(name, name)
143+
self._cont_handler.endElement(name)
139144

140145
def start_element_ns(self, name, attrs):
141146
pair = name.split()
142147
if len(pair) == 1:
143-
tup = (None, name)
144-
else:
145-
tup = pair
148+
pair = (None, name)
146149

147-
self._cont_handler.startElement(tup, None,
148-
xmlreader.AttributesImpl(attrs, None))
150+
self._cont_handler.startElementNS(pair, None, self._attrs)
149151

150152
def end_element_ns(self, name):
151153
pair = name.split()
152154
if len(pair) == 1:
153-
name = (None, name, None)
154-
else:
155-
name = pair + [None] # prefix is not implemented yet!
155+
name = (None, name)
156156

157-
self._cont_handler.endElement(name, None)
157+
self._cont_handler.endElementNS(pair, None)
158158

159-
# this is not used
159+
# this is not used (call directly to ContentHandler)
160160
def processing_instruction(self, target, data):
161161
self._cont_handler.processingInstruction(target, data)
162162

163-
# this is not used
163+
# this is not used (call directly to ContentHandler)
164164
def character_data(self, data):
165165
self._cont_handler.characters(data)
166166

@@ -177,7 +177,7 @@ def notation_decl(self, name, base, sysid, pubid):
177177
self._dtd_handler.notationDecl(name, pubid, sysid)
178178

179179
def external_entity_ref(self, context, base, sysid, pubid):
180-
assert 0 # not implemented
180+
raise NotImplementedError()
181181
source = self._ent_handler.resolveEntity(pubid, sysid)
182182
source = saxutils.prepare_input_source(source)
183183
# FIXME: create new parser, stack self._source and self._parser

0 commit comments

Comments
 (0)