Skip to content

[cpyrt] Raise C++ exceptions from destructors in Python - #170

Open
conrade-ctc wants to merge 1 commit into
compiler-research:mainfrom
chicagotrading:upstreaming/throwing-destructor
Open

conrade-ctc wants to merge 1 commit into
compiler-research:mainfrom
chicagotrading:upstreaming/throwing-destructor

Conversation

@conrade-ctc

Copy link
Copy Markdown
Contributor

A throwing noexcept(false) destructor ended the process with std::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.
  • Deallocation cannot raise. It reports the exception through sys.unraisablehook, as CPython does for __del__.
  • For a by-value object, the storage is freed also when the destructor throws ([expr.delete]).
  • The C++-to-Python exception conversion moves from CPPMethod to ProxyWrappers (SetPyErrFromCppException), so that both paths use it.

test33_throwing_destructor covers these cases. It is xfail on Apple Silicon, like the other tests that throw C++ exceptions.

As far as I can tell, InitializerListConverter::Clear has the same gap. This PR does not change it.

🤖 Done with the help of Claude Code (Opus 5.5, human in the loop)

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__.

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)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant