[ python-Bugs-847665 ] XMLGenerator.startElementNS dies on EMPTY_NAMESPACE attribut
SourceForge.net
noreply at sourceforge.net
Sun Dec 4 21:01:13 CET 2005
Bugs item #847665, was opened at 2003-11-23 10:21
Message generated for change (Comment added) made by akuchling
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=847665&group_id=5470
Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: XML
Group: Python 2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: Stuart Bishop (zenzen)
Assigned to: Nobody/Anonymous (nobody)
Summary: XMLGenerator.startElementNS dies on EMPTY_NAMESPACE attribut
Initial Comment:
from xml.sax.saxutils import XMLGenerator
g = XMLGenerator(encoding='utf8')
STREAM_NAMESPACE = 'http://etherx.jabber.org/streams'
CLIENT_NAMESPACE = 'jabber:client'
g.startDocument()
g.startPrefixMapping('stream', STREAM_NAMESPACE)
g.startPrefixMapping('client', CLIENT_NAMESPACE)
g.startElementNS(
(STREAM_NAMESPACE, 'stream'), 'stream',
{(None,'xmlns'): CLIENT_NAMESPACE}
)
Dies with:
Traceback (most recent call last):
File "tst.py", line 11, in ?
g.startElementNS(
File "/System/Library/Frameworks/Python.framework/
Versions/2.3/lib/python2.3/xml/sax/saxutils.py", line 124, in
startElementNS
name = self._current_context[name[0]] + ":" + name[1]
KeyError: 'x'
Changing the end of XMLGenerator.startElementNS to the
following makes it work the way I expect:
for (name, value) in attrs.items():
if name[0] is None:
name = name[1]
else:
name = self._current_context[name[0]] + ":" +
name[1]
self._out.write(' %s=%s' % (name,
quoteattr(value)))
self._out.write('>')
----------------------------------------------------------------------
>Comment By: A.M. Kuchling (akuchling)
Date: 2005-12-04 15:01
Message:
Logged In: YES
user_id=11375
I don't understand your second example; it uses saxutils to
parse a file. What's it got to do with XMLGenerator?
----------------------------------------------------------------------
Comment By: Stuart Bishop (zenzen)
Date: 2003-11-24 14:36
Message:
Logged In: YES
user_id=46639
This method also appears to have trouble emmitting tags without a
namespace prefix:
import xml.sax
import xml.sax.handler
import xml.sax.saxutils
x = '''<?xml version="1.0"?><mechanisms xmlns='urn:ietf:
params:xml:ns:xmpp-sasl'
>'''
parser = xml.sax.make_parser()
parser.setFeature(xml.sax.handler.feature_namespaces, True)
parser.setContentHandler(xml.sax.saxutils.XMLGenerator())
parser.feed(x)
dies with:
Traceback (most recent call last):
File "tst.py", line 30, in ?
parser.feed(x)
File "/System/Library/Frameworks/Python.framework/Versions/
2.3/lib/python2.3/xml/sax/expatreader.py", line 207, in feed
self._parser.Parse(data, isFinal)
File "/System/Library/Frameworks/Python.framework/Versions/
2.3/lib/python2.3/xml/sax/expatreader.py", line 337, in
start_element_ns
AttributesNSImpl(newattrs, qnames))
File "/System/Library/Frameworks/Python.framework/Versions/
2.3/lib/python2.3/xml/sax/saxutils.py", line 116, in startElementNS
name = self._current_context[name[0]] + ":" + name[1]
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
I've attached my current replacement startElementNS method
which appears to fix both this and the previous issue.
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=847665&group_id=5470
More information about the Python-bugs-list
mailing list