C API: module cleanup function

Stefan Behnel stefan_ml at behnel.de
Sat Jan 30 01:09:26 EST 2010


Mr.M, 29.01.2010 23:50:
> 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
> reload).
> 
> Is there a way to make this work?

Gabriel already pointed you to the module cleanup support in Py3, which can
be used to provide reload capabilities to your module.

In Py2, there are at least some ways to free resources when terminating the
interpreter. See the "atexit" module and the Py_AtExit() function:

http://docs.python.org/library/atexit.html
http://docs.python.org/c-api/sys.html

Note that both have their specific limitations, though, as you can see from
the docs.

Also note that it might help you to take a look at Cython, a Python-to-C
compiler for writing fast C extensions. It has an option for generating
module-level cleanup code automatically, and generally simplifies writing
binary extension modules quite a bit.

Stefan



More information about the Python-list mailing list