[Q] Extension: Refcount for exception types

"Martin v. Löwis" martin at v.loewis.de
Fri Apr 16 15:55:14 EDT 2004


Ames Andreas (MPA/DF) wrote:
>         Py_INCREF(ErrorObject);
>         ^^^^^^^^^^^^^^^^^^^^^^^ I mean this one.
>         PyModule_AddObject(m, "error", ErrorObject);
> 
> I know that PyModule_AddObject steals a reference, but now
> ErrorObject's refcount should be 2, isn't it?  I just fail to see
> how the refcount can return to 0.  I'm sure this is simple to explain,
> so please enlighten me, why this isn't a leak.

It is a leak, but that is intentional. If the INCREF was not there,
and somebody would delete "error" from the dictionary, then the
error class would go away, and ErrorObject would be a dangling pointer.

Since the module C code refers to the exception through ErrorObject,
it must keep a reference that cannot go away. This is indeed a leak
because there is no module shutdown code which could decref ErrorObject,
and set it to NULL.

Regards,
Martin




More information about the Python-list mailing list