Python 3.5.1 C API, the global available available is not destroyed when delete the module

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Sep 20 03:26:50 EDT 2016


On Tuesday 20 September 2016 16:19, dl l wrote:

> Hi Steven,
> 
> Thanks for reply.
> 
> I logged bug https://bugs.python.org/issue28202. I added more info in this
> bug :).
> 
> Yes, it's a workaround to set the global variables to None. But My app just
> provide a framework for my customers to run python scripts. That's means
> the workaround requires my customers to update their python scripts. That
> may make them unhappy :(. Is there a workaround in my code C++ side to call
> Python C APIs to resolve the memory leak issue?

Sorry, I wasn't clear. I meant to say that from your C++ side you should try 
explicitly setting the object to None to see if the __del__ method runs.

I don't know C++, which is why I wrote it in Python.


Assuming that this really is a memory leak, then you could try walking the 
module and deleting everything by hand. Again, I'm writing this in Python, but 
you should write it in C++.

# instead of just this:
del sys.modules['mymodule']  # PyDict_DelItemString


# try this instead
mod = sys.modules['mymodule']
namespace = vars(mod)
for name in list(namespace.keys()):
    del namespace[name]  # PyDict_DelItemString ?
del mod
del sys.modules['mymodule']  # PyDict_DelItemString


But it might be easiest to just update once the bug is fixed.




-- 
Steven
git gets easier once you get the basic idea that branches are homeomorphic 
endofunctors mapping submanifolds of a Hilbert space.




More information about the Python-list mailing list