2.0b2 xml -- bugs, or misunderstandings of mine?

Alex Martelli aleaxit at yahoo.com
Fri Oct 6 07:14:24 EDT 2000


Trying out 2.0b2's XML support, I'm hitting into
things that look like bugs to me, but, before
reporting them as such, I'd like some confirmation,
since the stuff is rather underdocumented so I could
easily be badly misunderstanding something.


Try (simplest example I can manufacture):

C:\>python
Python 2.0b2 (#6, Sep 26 2000, 14:59:21) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
>>> import xml.dom.minidom
>>> adom = xml.dom.minidom.parseString("<a b='c'/>")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "D:\Python20\lib\xml\dom\minidom.py", line 463, in parseString
    return _doparse(pulldom.parseString, args, kwargs)
  File "D:\Python20\lib\xml\dom\minidom.py", line 453, in _doparse
    toktype, rootNode = events.getEvent()
  File "D:\Python20\lib\xml\dom\pulldom.py", line 174, in getEvent
    self.parser.feed(buf)
  File "D:\Python20\lib\xml\sax\expatreader.py", line 77, in feed
    if not self._parser.Parse(data, 0):
  File "D:\Python20\lib\xml\sax\expatreader.py", line 151, in
start_element_ns
    AttributesNSImpl(newattrs, {}))
  File "D:\Python20\lib\xml\dom\pulldom.py", line 49, in startElementNS
    node.setAttributeNode(qname,attr)
UnboundLocalError: Local variable 'qname' referenced before assignment
>>>

And indeed, peeking around line 49 of the pulldom.py file, one
sees variable qname assigned only along one branch of an if
statement, but then used unconditionally at line 49.  But trying
to fix that by giving qname a value anyway can't work, since the
setAttributeNode method only wants one argument (the attr) and
is here being called with two.

Just removing the first argument and setting that line 49 to:
    node.setAttributeNode(attr)
does "work" in letting the DOM object be built.
But then another bug is hit when trying to check out the XML
contents of the DOM:

C:\>python
Python 2.0b2 (#6, Sep 26 2000, 14:59:21) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
>>> import xml.dom.minidom
>>> adom = xml.dom.minidom.parseString("<a b='c'/>")
>>> import sys
>>> adom.writexml(sys.stdout)
<a None="Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "D:\Python20\lib\xml\dom\minidom.py", line 449, in writexml
    node.writexml(writer)
  File "D:\Python20\lib\xml\dom\minidom.py", line 335, in writexml
    _write_data(writer, self._get_attributes()[a_name])
  File "D:\Python20\lib\xml\dom\minidom.py", line 145, in _write_data
    data = string.replace(data, "&", "&")
  File "D:\Python20\lib\string.py", line 363, in replace
    return s.replace(old, new, maxsplit)
  File "D:\Python20\lib\xml\dom\minidom.py", line 74, in __getattr__
    raise AttributeError, key
AttributeError: replace
>>>

The "data" argument to method _write_data is not really a string
object -- so it doesn't have a .replace method, and string.replace
now uses the object's replace method, so it fails.


So, am I misunderstanding things totally, or are there indeed bugs
in these modules in 2.0b2?  Are there places I could download
updated code and/or docs to help me?  I'm not sure how currently
relevant the various URL's for Python-and-XML one can find around
now are, so I thought I'd better check.  I have to DOM-process quite
a few smallish XML files and I'd love to use Python 2 for that...


Alex






More information about the Python-list mailing list