Skip to content

Commit 5bcc847

Browse files
committed
Use atexit for cleanup of Python-extended prototypes
Use standard atexit handler to remove Python-extended prototype objects from C++ registry. Ensure cleanup is truly executed instead of relying on garbage collector to call `__del__`. This prevents the following error message on Python exit Fatal Python error: PyThreadState_Get: the function must be called with the GIL held, but the GIL is released (the current Python thread state is NULL)
1 parent a0b9f7f commit 5bcc847

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/diffpy/srreal/_cleanup.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class prototypes is implemented in libdiffpy. Any Python-extended classes
2727
"""
2828

2929

30+
import atexit
3031
import weakref
3132

3233

@@ -62,7 +63,7 @@ def add(self, obj):
6263
return
6364

6465

65-
def __del__(self):
66+
def clean(self):
6667
while self._references:
6768
wr = self._references.pop()
6869
obj = wr()
@@ -75,7 +76,8 @@ def __del__(self):
7576

7677
# create singleton instance of the cleanup handler
7778
_cleanup_handler = _DerivedClassesCleanUpHandler()
78-
del _DerivedClassesCleanUpHandler
79+
atexit.register(_cleanup_handler.clean)
7980

81+
del _DerivedClassesCleanUpHandler
8082

8183
# End of file.

0 commit comments

Comments
 (0)