467,168 Members | 1,035 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,168 developers. It's quick & easy.

handling ExpatError exception raised from ElementTree.XML() method

Verion of Python: 2.4
O/S: Windows XP
ElementTree resides in the c:\python24\lib\site-packages\elementtree\
folder

When a string that does not contain well-formed XML is passed as an
argument to the XML() method in ElementTree.py, an ExpatError exception
is raised. I can trap the exception with a try/except where the except
does not specify a specific exception, but I cannot figure out how to
construct the except clause in a try/except statement to catch the
ExpatError exception. Interactive window illustrates
import elementtree.ElementTree as ElemTree
badxml = '<response>abc</responsez>'
root = ElemTree.XML(badxml) Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "C:\Python24\Lib\site-packages\elementtree\ElementTree.py", line
960, in XML
parser.feed(text)
File "C:\Python24\Lib\site-packages\elementtree\ElementTree.py", line
1242, in feed
self._parser.Parse(data, 0)
ExpatError: mismatched tag: line 1, column 15 try: .... root = ElemTree.XML(badxml)
.... except:
.... print 'some exception raised'
....
some exception raised try:

.... root = ElemTree.XML(badxml)
.... except ExpatError:
.... print 'ExpatError exception raised'
....
Traceback (most recent call last):
File "<interactive input>", line 3, in ?
NameError: name 'ExpatError' is not defined

I'm guessing that I need to define/describe the ExpatError exception
class and then refer to that defined exception class after the keyword
'except' and before the ':', but I cannot figure out how to do that.

Oct 27 '05 #1
  • viewed: 3586
Share:
3 Replies
mirandacasc...@yahoo.com wrote:

NameError: name 'ExpatError' is not defined

I'm guessing that I need to define/describe the ExpatError exception
class and then refer to that defined exception class after the keyword
'except' and before the ':', but I cannot figure out how to do that.


You just need to know how to reference ExpatError yourself. Doing a
grep on the site-packages directory revealed that the most likely
location of the ExpatError class is xml.parsers.expat. So, first you
need to import that package:

import xml.parsers.expat

Then, you can refer to the class as an attribute of that package.
Alternatively...

from xml.parsers.expat import ExpatError

....lets you use the name directly.

I think that it can be useful when making a library to make common
exception classes attributes of certain principal classes/objects, in
order to simplify this kind of situation and to avoid the need to find
the originating module of some exception or other.

Paul

Oct 27 '05 #2
Mr. Boddie's suggestions work as indicated...many thanks.

It's not clear how a grep of the site-packages directory revealed the
most likely location of the ExpatError class is xml.parsers.expat.

Using XP's search utility, I searched for any files within the
c:\python24\ folder structure that had 'ExpatError' somewhere in the
file (I specified a case sensitive search.) That search found nothing.
I modified the search to look for any files with 'expat' (not case
sensitive.)...it located the file Mr. Boddie
mentioned...\lib\xml\parsers\expat.py. That file has only a few lines
of code...it appears to import all the objects from what I'm guessing
is the pyexpat extension module. I'm guessing that ExpatError is
embedded within that extension module...is that a correct guess?

It's not clear how Mr. Boddie was able to surmise where ExpatError
resides given that a case sensitive search of the folder structure
didn't find any files containing 'ExpatError'.

Oct 28 '05 #3
mirandacasc...@yahoo.com wrote:

It's not clear how a grep of the site-packages directory revealed the
most likely location of the ExpatError class is xml.parsers.expat.
Here's what I did:

grep -e ExpatError -r /usr/lib/python2.4/site-packages/

What is interesting is that most of the "hits" belonged to packages
which you may not have on your system: pyxdg and smartpm, for example,
which mention the ExpatError class. I did get the following message,
though:

Binary file
/usr/lib/python2.4/site-packages/_xmlplus/parsers/pyexpat.so matches
...it located the file Mr. Boddie mentioned...\lib\xml\parsers\expat.py. That file has
only a few lines of code...it appears to import all the objects from what I'm guessing
is the pyexpat extension module. I'm guessing that ExpatError is
embedded within that extension module...is that a correct guess?


I believe so.

Paul

Oct 31 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by python newbie | last post: by
8 posts views Thread by StepH | last post: by
4 posts views Thread by alainpoint@yahoo.fr | last post: by
1 post views Thread by mirandacascade@yahoo.com | last post: by
9 posts views Thread by C# Learner | last post: by
2 posts views Thread by mirandacascade@yahoo.com | last post: by
3 posts views Thread by Shuaib | last post: by
17 posts views Thread by frederic.pica@gmail.com | last post: by
3 posts views Thread by gray.bowman@gmail.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.