Exception Handling (C - extending python)

Stefan Behnel stefan_ml at behnel.de
Sun Oct 23 08:41:05 EDT 2011


Hi,

note that I reformatted your posting to get the replies back into order.

Lee, 23.10.2011 13:32:
> On Oct 23, 10:06 pm, Stefan Behnel wrote:
>> Lee, 23.10.2011 06:09:
>>> Where does PyExc_TypeError (and alike) points to? I can see its
>>> declaration - PyAPI_DATA(PyObject *) PyExc_TypeError; - in pyerrors.h
>>> but I cannot figure out what it is its value, where it is
>>> initialized.
>>
>> It gets initialised inside of the interpreter core and then points to a
>> Python object.
>
> If it points to an object that means I can defered it (through
> ob_type).

That will give you the "type" object, because PyExc_TypeError points to the 
type "TypeError". Note that types are also objects in Python.


> From there, how a function like PyErr_SetString knows what exception
> is?

The type object that you pass in must inherit from the BaseException type. 
You can read the code in Python/errors.c, function PyErr_SetObject().


>>> Any help is greatly appreciated.
>>
>> The question is: why do you ask? What exactly do you want to do?
>>
>> If you ask a more targeted question, you will get an answer that will help
>> you further.
>
> I am just interested to understand the mechanism inside python.

That's just fine. If you are interested in the inner mechanics of the 
CPython runtime, reading the source is a very good way to start getting 
involved with the project.

However, many extension module authors don't care about these inner 
mechanics and just use Cython instead. That keeps them from having to learn 
the C-API of CPython, and from tying their code too deeply into the CPython 
runtime itself.

Stefan




More information about the Python-list mailing list