[XML-SIG] PyExpat module swallowing exceptions

A.M. Kuchling akuchlin@cnri.reston.va.us
Sun, 13 Sep 1998 12:39:35 -0400


I've come across a curious bug; the SAX PyExpat module seems to
swallow exceptions, but I can't figure out why this is happening.
Here's a test program:

from xml.sax import saxexts,saxlib
class ExcHandler(saxlib.HandlerBase):
    def startElement(self, name, attrs):
        raise SystemError

import StringIO
h = ExcHandler()
p=saxexts.XMLParserFactory.make_parser("xml.sax.drivers.drv_pyexpat")
p.setDocumentHandler( h )
p.parseFile( StringIO.StringIO("<anything>data</anything>") )

Notice that the startElement method raises an exception.  Run the
above code, and it quietly runs to completion:

[amk@mira xbel]$ python t.py
[amk@mira xbel]$

Change it to use another parser, such as xmllib, and you get an
exception:

Traceback (innermost last):
  File "t.py", line 11, in ?
    p.parseFile( StringIO.StringIO("<anything>data</anything>") )
   ... lots of stack frames deleted ...
  File "t.py", line 5, in startElement
    raise SystemError
SystemError

It looks to me as if, in the event of an exception being raised from a
handler, there's no way to tell the Expat parser "Hey!  That handler
didn't work, so stop parsing!", and the handlers keep getting called,
the exception being discarded somewhere.  I came across this when
debugging my XBEL reading code; I had written self.add_folder instead
of self.bms.add_folder, but never saw the AttributeError exception
that would have pointed out the problem.  Obviously this is a bad
thing when debugging code and the Expat module is selected as the
parser.

This seems like a glaring flaw in Expat, that there's no way to end
parsing prematurely.  Has anyone told James Clark about this?  Failing
a change to Expat, an apparent fix would be to add "if
(PyErr_Occurred()) return;" to all the handler functions in pyexpat.c,
in order to do nothing.  However, I tried this, and the exception
still is never raised.

What's confusing me is: why is the exception just vanishing?  I
couldn't find an 'except:' responsible, or a PyErr_Clear() in
pyexpat.c.  Anyone got any clues? 

-- 
A.M. Kuchling			http://starship.skyport.net/crew/amk/
May you go safe, my friend, across that dizzy way / No wider than a hair, by
which your people go / From earth to Paradise; may you go safe today / With
stars and space above, and time and stars below.
    -- Lord Dunsany