Skip to content

cppinterface: show C++ frames in Python tracebacks again - #1836

Open
JotaJazz wants to merge 1 commit into
SFTtech:masterfrom
JotaJazz:pyinterface-cpp-tracebacks
Open

JotaJazz wants to merge 1 commit into
SFTtech:masterfrom
JotaJazz:pyinterface-cpp-tracebacks

Conversation

@JotaJazz

Copy link
Copy Markdown
Contributor

Fixes #1700.

Since _PyTraceback_Add became private in Python 3.13, translated C++ exceptions lost their C++ frames in Python tracebacks. This builds them as real traceback entries instead, using only public API: for each C++ frame, a one-line raise is compiled into a code object that carries the C++ file, line and function name (no column info). Running it gives a frame, and the entries are chained with types.TracebackType. The translated exception gets this chain via with_traceback() before it is raised.

This happens in raise_cpp_error, the translator that Cython's except + handler calls via __Pyx_CppExn2PyErr, so there is no overhead unless a C++ exception is actually translated.

cpp_to_py_demo with Python 3.14:

Traceback (most recent call last):
  File "openage/cppinterface/exctranslate_tests.pyx", line 42, in openage.cppinterface.exctranslate_tests.cpp_to_py_demo
  File "openage/cppinterface/exctranslate_tests.pyx", line 26, in openage.cppinterface.exctranslate_tests.call_cpp_thrower
  ...
  File "", line 0, in void openage::pyinterface::tests::throw_foo<0>()+0x7d
  File "", line 0, in openage::error::Error::Error(openage::log::message const&, bool, bool)+0x11f
  File "libopenage/pyinterface/exctranslate_tests.cpp", line 23, in void openage::pyinterface::tests::throw_foo() [with int i = 0]
openage.cppinterface.exctranslate.CPPException: foo

cppinterface.exctranslate_tests.cpp_to_py now also checks that the C++ throw location is part of the traceback.

🤖 Generated with Claude Code

The C++ frames of translated exceptions were added with
_PyTraceback_Add, which became private in Python 3.13, so tracebacks
lost them.

Build them as real traceback entries instead: for each C++ frame,
compile a one-line raise whose code object carries the C++ file, line
and function name, run it to get a frame, and chain the entries with
types.TracebackType. The translated exception gets this chain before
it is raised. This only runs when a C++ exception is translated.

Fixes SFTtech#1700

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

Propagate C++ expections in Python (again)

1 participant