Repository navigation
[cpyrt] Raise C++ exceptions from destructors in Python - #170
Open
conrade-ctc wants to merge 1 commit into
Open
conrade-ctc wants to merge 1 commit into
conrade-ctc wants to merge 1 commit into
Conversation
aaronj0
reviewed
Oct 27, 2026
| PyObject *pytype, *pyvalue, *pytrace; | ||
| PyErr_Fetch(&pytype, &pyvalue, &pytrace); | ||
| if (!op_dealloc_nofree(pyobj)) | ||
| PyErr_WriteUnraisable((PyObject*)Py_TYPE(pyobj)); |
Collaborator
There was a problem hiding this comment.
Perhaps we could use PyErr_FormatUnraisable and propagate some info about the object being deallocated.
Contributor
Author
There was a problem hiding this comment.
Good idea, thanks. PyErr_FormatUnraisable is public only from Python 3.13, and cppjit still supports 3.12. I will use it on 3.13 and later, with a message that names the C++ type and address of the object. On 3.12 I will keep PyErr_WriteUnraisable.
A noexcept(false) destructor that throws aborted the process, because the destructor path did not catch C++ exceptions. __destruct__() now raises the exception as a Python exception. Deallocation reports it through sys.unraisablehook, as CPython does for __del__. On Python 3.13 and later, the message names the C++ type and address of the object. Move the exception conversion from CPPMethod into ProxyWrappers, so that both paths use it. Co-developed-with-the-help-of: Claude Code (Opus 5.5, human in the loop)
conrade-ctc
force-pushed
the
upstreaming/throwing-destructor
branch
from
October 28, 2026 16:33
ee4f5d3 to
13fe239
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A throwing
noexcept(false)destructor ended the process withstd::terminate. The destructor path (op_dealloc_nofree→Cpp::Destruct) had no handler, so the exception unwound into CPython's C frames.__destruct__()now raises the C++ exception in Python. The proxy gives up ownership, so a second call does not run the destructor again.sys.unraisablehook, as CPython does for__del__. On Python 3.13 and later,PyErr_FormatUnraisablenames the C++ type and address of the object in the message.CPPMethodtoProxyWrappers(SetPyErrFromCppException), so that both paths use it.test33_throwing_destructorcovers these cases. It is xfail on Apple Silicon, like the other tests that throw C++ exceptions.As far as I can tell,
InitializerListConverter::Clearhas the same gap. This PR does not change it.🤖 Done with the help of Claude Code (Opus 5.5, human in the loop)