From: <am...@us...> - 2010-04-17 18:16:51
|
Revision: 7032 http://jython.svn.sourceforge.net/jython/?rev=7032&view=rev Author: amak Date: 2010-04-17 18:16:45 +0000 (Sat, 17 Apr 2010) Log Message: ----------- Fix for bug 1375: XML SAX: attrs.get((None, 'attr')) gives NPE Modified Paths: -------------- trunk/jython/Lib/test/test_sax.py trunk/jython/Lib/xml/sax/drivers2/drv_javasax.py Modified: trunk/jython/Lib/test/test_sax.py =================================================================== --- trunk/jython/Lib/test/test_sax.py 2010-04-17 16:51:11 UTC (rev 7031) +++ trunk/jython/Lib/test/test_sax.py 2010-04-17 18:16:45 UTC (rev 7032) @@ -407,6 +407,29 @@ attrs.getValue((ns_uri, "attr")) == "val" and \ attrs[(ns_uri, "attr")] == "val" +def test_expat_nsattrs_no_namespace(): + parser = make_parser() + parser.setFeature(handler.feature_namespaces, 1) + gather = AttrGatherer() + parser.setContentHandler(gather) + + parser.parse(StringIO("<doc attr='val'/>")) + + attrs = gather._attrs + + return attrs.getLength() == 1 and \ + attrs.getNames() == [(None, "attr")] and \ + attrs.getQNames() == ["attr"] and \ + len(attrs) == 1 and \ + attrs.has_key((None, "attr")) and \ + attrs.keys() == [(None, "attr")] and \ + attrs.get((None, "attr")) == "val" and \ + attrs.get((None, "attr"), 25) == "val" and \ + attrs.items() == [((None, "attr"), "val")] and \ + attrs.values() == ["val"] and \ + attrs.getValue((None, "attr")) == "val" and \ + attrs[(None, "attr")] == "val" + # ===== InputSource support xml_test_out = open(findfile("test.xml.out")).read() Modified: trunk/jython/Lib/xml/sax/drivers2/drv_javasax.py =================================================================== --- trunk/jython/Lib/xml/sax/drivers2/drv_javasax.py 2010-04-17 16:51:11 UTC (rev 7031) +++ trunk/jython/Lib/xml/sax/drivers2/drv_javasax.py 2010-04-17 18:16:45 UTC (rev 7032) @@ -237,7 +237,22 @@ def endEntity(self, name): pass # TODO +def _fixTuple(nsTuple, frm, to): + if len(nsTuple) == 2: + nsUri, localName = nsTuple + if nsUri == frm: + nsUri = to + return (nsUri, localName) + return nsTuple + +def _makeJavaNsTuple(nsTuple): + return _fixTuple(nsTuple, None, '') + +def _makePythonNsTuple(nsTuple): + return _fixTuple(nsTuple, '', None) + class AttributesImpl: + def __init__(self, attrs = None): self._attrs = attrs @@ -245,16 +260,16 @@ return self._attrs.getLength() def getType(self, name): - return self._attrs.getType(name) + return self._attrs.getType(_makeJavaNsTuple(name)) def getValue(self, name): - value = self._attrs.getValue(name) + value = self._attrs.getValue(_makeJavaNsTuple(name)) if value == None: raise KeyError(name) return value def getNames(self): - return [self._attrs.getQName(index) for index in range(len(self))] + return [_makePythonNsTuple(self._attrs.getQName(index)) for index in range(len(self))] def getQNames(self): return [self._attrs.getQName(index) for index in range(len(self))] @@ -272,7 +287,7 @@ return qname def getQNameByName(self, name): - idx = self._attrs.getIndex(name) + idx = self._attrs.getIndex(_makeJavaNsTuple(name)) if idx == -1: raise KeyError, name return name @@ -316,10 +331,12 @@ AttributesImpl.__init__(self, attrs) def getType(self, name): + name = _makeJavaNsTuple(name) return self._attrs.getType(name[0], name[1]) def getValue(self, name): - value = self._attrs.getValue(name[0], name[1]) + jname = _makeJavaNsTuple(name) + value = self._attrs.getValue(jname[0], jname[1]) if value == None: raise KeyError(name) return value @@ -327,17 +344,17 @@ def getNames(self): names = [] for idx in range(len(self)): - names.append((self._attrs.getURI(idx), - self._attrs.getLocalName(idx))) + names.append(_makePythonNsTuple( (self._attrs.getURI(idx), self._attrs.getLocalName(idx)) )) return names def getNameByQName(self, qname): idx = self._attrs.getIndex(qname) if idx == -1: raise KeyError, qname - return (self._attrs.getURI(idx), self._attrs.getLocalName(idx)) + return _makePythonNsTuple( (self._attrs.getURI(idx), self._attrs.getLocalName(idx)) ) def getQNameByName(self, name): + name = _makeJavaNsTuple(name) idx = self._attrs.getIndex(name[0], name[1]) if idx == -1: raise KeyError, name This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |