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

Guido van Rossum guido@python.org
Fri, 21 Jun 2002 08:50:54 -0400


> 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?

I guess so.  The rule is that all PyErr_SetXXX functions correspond
to a raise statement with a class as first argument.  raise with an
instance first argument is a shortcut.

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

Unfortunately so in the current code base.  I'm not sure if/when we
should lift this restriction.  I'm also not sure if, when we lift it,
we should make Exception and all other built-in exceptions new-style
classes.  New-style and classic classes aren't 100% compatible and I
don't like to break people's code who have subclassed a built-in
exception class and did something that doesn't work the same in
new-style classes.

--Guido van Rossum (home page: http://www.python.org/~guido/)