[XML-SIG] Using PyExpat.py
Don Wakefield
don_wakefield@mentorg.com
Sat, 10 Feb 2001 17:45:20 -0800 (PST)
>>>>> "Uche" == Uche Ogbuji <uche.ogbuji@fourthought.com> writes:
Uche> I do recommend the upgrade, and 0.6.4 is on its way.
I installed 0.6.3, and immediately encountered several problems. Part of
this may be my freshness to Python. My environment may not be complete
in some way. First things first:
- Using the code supplied by Uche below, I got a complaint of 'os' not
being visible within PyExpat.py. It isn't imported there, and my
importing it into my calling script didn't help. I had to add the
'import os' to the top of PyExpat.py to eliminate this error.
- The next problem was this:
> python Timediag.py cs39.xml
Traceback (innermost last):
File "Timediag.py", line 19, in ?
x = Cells(sys.argv[1])
File "Timediag.py", line 11, in __init__
xml_dom_object = reader.fromUri(filename)
File "/user/donw/src/local/ss5/obj/lib/python1.5/site-packages/xml/dom/ext/reader/PyExpat.py", line 80, in fromUri
rt = self.fromStream(stream, doc,stripElements)
File "/user/donw/src/local/ss5/obj/lib/python1.5/site-packages/xml/dom/ext/reader/PyExpat.py", line 64, in fromStream
if not self._override:
AttributeError: _override
And indeed this variable is not defined in PyExpat.py. I added a line to my own script thusly:
reader._override = None
This eliminated that error, allowing me to move on to the next one.
- Here is the next traceback:
> python Timediag.py cs39.xml
Traceback (innermost last):
File "Timediag.py", line 20, in ?
x = Cells(sys.argv[1])
File "Timediag.py", line 12, in __init__
xml_dom_object = reader.fromUri(filename)
File "/user/donw/src/local/ss5/obj/lib/python1.5/site-packages/xml/dom/ext/reader/PyExpat.py", line 80, in fromUri
rt = self.fromStream(stream, doc,stripElements)
File "/user/donw/src/local/ss5/obj/lib/python1.5/site-packages/xml/dom/ext/reader/PyExpat.py", line 65, in fromStream
self.initParser()
File "/user/donw/src/local/ss5/obj/lib/python1.5/site-packages/xml/dom/ext/reader/PyExpat.py", line 51, in initParser
self.parser=pyexpat.ParserCreate()
NameError: pyexpat
Based on my experience with 'os', I placed the line 'from xml.parsers import pyexpat'
directly into PyExpat.py. Now that error has gone away...
- I ran again, and got:
> python Timediag.py cs39.xml
Traceback (innermost last):
File "Timediag.py", line 20, in ?
x = Cells(sys.argv[1])
File "Timediag.py", line 12, in __init__
xml_dom_object = reader.fromUri(filename)
File "/user/donw/src/local/ss5/obj/lib/python1.5/site-packages/xml/dom/ext/reader/PyExpat.py", line 81, in fromUri
rt = self.fromStream(stream, doc,stripElements)
File "/user/donw/src/local/ss5/obj/lib/python1.5/site-packages/xml/dom/ext/reader/PyExpat.py", line 67, in fromStream
self.initState(doc, stripElements)
TypeError: too many arguments; expected 2, got 3
Checking in PyExpat.py, I indeed discovered that the caller was
supplying stripElements, while the method did not take such an
argument. I.e.:
class Reader:
:
def initState(self, doc=None):
:
def fromStream(self, stream, doc=None, stripElements=None):
if not self._override:
self.initParser()
self.initState(doc, stripElements)
I've stopped here.
Admittedly some of these problems may stem from my limited understanding
of Python modules and/or namespaces. But the checking of an undefined
variable, and the calling of a method with more than the defined number
of arguments, leads me to believe that I've somehow managed to pick up a
corrupted/scrambled version of PyXML-0.6.3.tar.gz. Is this possible? Or
do 0.6.2 and 0.6.3 just work better with Python 2.0? I'm currently stuck
with 1.5.2, so I hope not.
Uche> As a forewarning, the 0.6.3 and up way is
Uche> from xml.dom.ext.reader import PyExpat #or Sax2
Uche> reader = PyExpat.Reader()
Uche> xml_dom_object = reader.fromUri(filename) #should work for either URL or file
By the way, thanks for all the friendly advice so far. I've noticed that
this list has more traffic by far relating to development work than
questions like mine, so I hope this isn't an intrusion.
--
Don Wakefield Mentor Graphics Corporation
(503) 685-1262 8005 S.W. Boeckman Road
don_wakefield@mentorg.com Wilsonville, OR 97070-7777