[Python-Dev] refcounting vs PyModule_AddObject

"Martin v. Löwis" martin at v.loewis.de
Wed Jun 15 20:24:29 CEST 2005


Michael Hudson wrote:

>         if (ProfilerError == NULL)
>             ProfilerError = PyErr_NewException("hotshot.ProfilerError",
>                                                NULL, NULL);
>         if (ProfilerError != NULL) {
>             Py_INCREF(ProfilerError);
>             PyModule_AddObject(module, "ProfilerError", ProfilerError);
>         }
> 
> 
> I think the Py_INCREF should just be removed, but I'm wondering if I'm
> missing something...

It may be me who is missing something, but...

On reference is added to the dictionary, this is the one the explicit
INCREF creates. The other reference is held in the C variable
ProfilerError; this is the one that creating the exception object
creates.

It is convention that C variables which are explicitly used also
hold their own references, even if they are global, and even if there
is no procedure to clear them. The reason is that they would become
stale if the module object went away. As there is no way to protect
against this case, they just keep a garbage reference.

Regards,
Martin


More information about the Python-Dev mailing list