Context
I have a troublesome object from third-party code in a Python (3.10) program. The object is not written in pure Python; it's provided by an extension module.
Due to a bug in the third party code, when the object's destructor runs, the program blocks indefinitely (waiting for a mutex it will never get).
Attempts to fix the bug causing the indefinite blocking have failed, though I'm still trying.
Question
How can I intentionally prevent a destructor from being called, ever, when Python shuts down?
For example, let's say I have the following code in test.py:
class MyClass:
def __del__(self):
print("bye")
x = MyClass()
What can I do to make sure that x's destructor never runs, and "bye" is never printed--either when the program shuts down or when x goes out of scope?
What I've tried
- I've tried forcing the program to exit via
os._exitin a destructor that's guaranteed to run before the troublesome object's destructor. However, that causes other destructors that I do want to run to not do so. While I know destructor execution can't be guaranteed, this approach is throwing out rather a lot of good with the bad. - I've tried intentionally creating a reference cycle involving this object, but the cyclic GC is too good for me to defeat simply.
- I've tried code like this, but it did not work:
import ctypes
incref = ctypes.pythonapi.Py_IncRef
incref.argtypes = [ctypes.py_object]
incref.restype = None
incref(x)