C API: module cleanup function

Mr.M mrm at unknown.nospam
Fri Jan 29 23:50:52 CET 2010


I can't figure out if there is a way to run a specialized cleanup 
function when a module needs to be "unloaded" (i.e. just before a 
reload() or when i quit the interpreter).

I'm thinking of something like tp_dealloc.

If I call Py_InitModule3 and look at module->ob_type->tp_dealloc, I find 
that Python provides a default tp_dealloc for me.

Now, suppose my module needs to allocate some resources at startup, I'm 
not sure, but I think I'd have to do it in my PyMODINIT_FUNC, right?

But, if I reload() my module or if I quit the Python interpreter, I'd 
like to free those resources (before allocate them again, in case of a 

Is there a way to make this work?

Thank you, Luca.

More information about the Python-list mailing list