[XML-SIG] Expat strategy

Jack Jansen jack@oratrix.nl
Mon, 31 Jan 2000 22:21:04 +0100


Recently, Paul Prescod <paul@prescod.net> said:
> PyExpat is one of a very few modules in the library to use setjmp. It
> uses it for error handling and I'm not sure if there is any way around
> it so I won't advocate its removal unless someone can propose a better
> way.  I'm not clear how to signal to expat that it should quit parsing
> other than through setjmp/longjmp.

I think I put in the setjmp/longjmp, basically because I could see no
other way to stop the parser, indeed. There's a couple of other
libraries I embedded in Python that have the same problem (jpeg and
pbm spring to mind).

Aside from the cross-segment longjmps, which needed a bit of massaging 
on the Mac, so assume the same is could be true on Windows, there's
one very big problem with setjmp/longjmp and that is that they're not
thread-safe.

However, in the case of the use in Pyexpat the Python programmer will
have to do something pretty gross to invoke this bug: as the
jmpbuf_valid flag is saved in the parser object and set/reset around
the Parse() call you'll have to create one parser and call
parser.Parse() on the one object simultaneously in two threads.
Still, putting a mutex in the object is probably a good idea.

> if (rv == NULL) {
> 	if (self->jmpbuf_valid)
> 		longjmp(self->jmpbuf, 1);
> 	My_WriteStderr("Exception in CharacterDataHandler()\n");
> 	PyErr_Clear();
> }
> 
> One funny thing is the code after the longjmp. I guess maybe its a
> fallback for when the long-jump doesn't work. It doesn't seem to work on
> Windows, though.

I think the code is better replaced by an abort(): if
myStartElementHandler and myEndElementHandler are called outside of a
Parse() invocation there's something pretty basic about expat that I
didn't understand when I wrote this code:-)

But please note that all this is based on how Pyexpat looked when I
maintained it, I haven't had the time to track developments since
then... 
--
Jack Jansen             | ++++ stop the execution of Mumia Abu-Jamal ++++
Jack.Jansen@oratrix.com | ++++ if you agree copy these lines to your sig ++++
www.oratrix.nl/~jack    | see http://www.xs4all.nl/~tank/spg-l/sigaction.htm