Search code examples
pythoncpythonpython-c-extension

What's the proper way to clean up static python object references in a CPython extension module?


The CPython headers define a macro to declare a method that is run to initialize your module on import: PyMODINIT_FUNC

My initializer creates references to other python objects, what is the best way to ensure that these objects are properly cleaned up / dereferenced when my module is unloaded?


Solution

  • You can't unload C extension modules at all. There is just no way to do it, and I know for sure that most of the standard extension modules would leak like crazy if there was.