[Python-Dev] Weird problem with exceptions raised in extension module

Greg Ewing greg@cosc.canterbury.ac.nz
Fri, 21 Jun 2002 18:36:23 +1200 (NZST)


Guido:

> It seems that this is just for
> 
>    raise TypeError, "Test-Exception"

Actually, it's

   raise TypeError("Test-Exception")

> But I think that you shouldn't be calling PyErr_SetNone() here -- I
> think you should call PyErr_SetObject(__pyx_1, __pyx_2).
>
> For details see do_raise() in ceval.c.

Hmmm. Having studied this routine *very* carefully,
I think I can see where things are going wrong.
Reading the C API docs led me to believe that the
equivalent of the Python statement

   raise x

would be

  PyErr_SetNone(x)

But it appears that is not the case, and what I
should actually be doing is

  PyErr_SetObject(
    ((PyInstanceObject*)x)->in_class, x)

This is... um... not very intuitive. Perhaps the
C API docs could be amended to mention this?

Also, it looks as if exceptions have to be
old-style instances, not new-style ones. Is
that correct?

Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | A citizen of NewZealandCorp, a	  |
Christchurch, New Zealand	   | wholly-owned subsidiary of USA Inc.  |
greg@cosc.canterbury.ac.nz	   +--------------------------------------+