Re: setjmp() usage in Extensions/X11 code

Sjoerd Mullender (Sjoerd.Mullender@cwi.nl)
Mon, 09 Jan 1995 11:04:57 +0100

On Wed, Dec 28 1994 Bob Novas wrote:

> I have a question regarding the use of setjmp() in the python
> Extensions/X11 implementation code.
>
> Take the following code, from Xtgenerated.h, for example.
>
> static object *
> widget_Parent(self, args)
> widgetobject *self;
> object *args;
> {
> Widget result;
> if (!getargs(args, ""))
> return NULL;
> if (!setjmp(jump_where)) {
> jump_flag = 1;
> result = XtParent(self->ob_widget);
> jump_flag = 0;
> }
> if (jump_flag) { jump_flag = 0; return NULL; }
> return (object *)newwidgetobject(result, widget_methodlists);
> }
>
> I don't think that there's any way an exception can ever occur in XtParent()
> that invokes longjmp(). It would have to be as a result of some code catchin
> g
> a signal, which I can't find. Certainly, none of the python implementation
> code that does a longjmp() could ever get executed as a result of the call to
> XtParent().

The longjmp may be called from the error handler that may be called
from the Xt library. Whether this can happen in the case of XtParent,
I don't know. If not, it is a artifact of the generation process.

> The only time that the setjmp code could be effective is if some python
> implementation code gets executed as a result of a setjmp()-wrapped call.
> So, I believe that the following setjmp() could be effective:
>
> static object *
> Xt_DispatchEvent(self, args)
> object *self;
> object *args;
> {
> char *event;
> int event_length;
> if (!getargs(args, "s#", &event, &event_length))
> return NULL;
> if (event_length != sizeof(XEvent)) {
> err_setstr(TypeError, "bad arg");
> return NULL;
> }
> if (!setjmp(jump_where)) {
> jump_flag = 1;
> XtDispatchEvent((XEvent *) event);
> jump_flag = 0;
> }
> if (jump_flag) { jump_flag = 0; return NULL; }
> INCREF(None);
> return None;
> }
>
> Since the event dispatched could invoke python implementation code that
> longjmp()'s, the setjmp is justified.

This is not a good idea. The idea behind the current usage of
setjmp/longjmp is that when an X or Xt error occurs, the call from
Python to the library raises an exception. XtDispatchEvent may
cause Python code to be executed (callbacks), so if a call to the Xt
library from the callback would fail, the exception wouldn't be raised
there, but at the call of Xt.DispatchEvent. This would make debugging
a lot harder.

Sjoerd Mullender, CWI, P.O. Box 94079, 1090 GB Amsterdam, The Netherlands
E-Mail: Sjoerd.Mullender@cwi.nl; Phone: +31 20 592 4127; Fax: +31 20 592 4199
URL: http://www.cwi.nl/cwi/people/Sjoerd.Mullender.html