I am dealing with an external library that does not handle its resources in a pythonic way. The library is natively a C++ library with a Python wrapper around it. Whenever a library function returns a C++ object, what I actually get is a string handle to it. The object itself persists in memory until I explicitly call a delete()
function with that handle as the argument. I tried to introduce my own wrapper around the handle to make sure it is deleted when the wrapper goes out of scope. Something like this:
#import some_module as sm
class MyWrapper(str):
def __new__(*args, **kwargs):
# do something
def __del__(self):
sm.delete(self)
This works most of the time but on script completion I often get an exception that says sm
has no attribute delete
. It looks like the module object gets destroyed before my wrapper around the handle. Is there a way to make sure my __del__
is called before the module is unloaded?