handling ExpatError exception raised from ElementTree.XML() method

M

mirandacascade

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
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.... root = ElemTree.XML(badxml)
.... except:
.... print 'some exception raised'
....
some exception raised.... 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.
 
P

Paul Boddie

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
 
M

mirandacascade

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'.
 
P

Paul Boddie

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top