Repository navigation
fix: raise instead of aborting when instantiating pybind11_object - #6244
kroplabeskidu wants to merge 7 commits into
Conversation
eec0084 to
e54d6b1
Compare
|
🤖 AI text below 🤖 I pushed bf2e776 on top of your commit. It replaces the cleanup approach with a check before allocation:
Your test is unchanged and still passes. Without the fix, Python aborts on it. |
|
Points to know that are all fine:
And the only one I could be concerned about:
I measured, it's at most 7% for an empty class. If we want to fix it, it's pretty easy, |
|
— codex on behalf of Ralf — Preserve CPython's object-destruction bookkeeping on a layout allocation failure. Calling One way to handle this is to allocate the non-simple layout before |
c0ec259 to
92f5b11
Compare
|
— codex on behalf of Ralf — Handle the null object in the reference-tracer removal event. Starting in CPython 3.14, restoring the original tracer invokes the test's current callback with Generated by Codex. Ralf has yet to review these findings. |
|
🤖 AI text below 🤖 Fixed in 9668a4b: the tracer callback now checks One correction: |
Raise TypeError before allocation instead of tearing down a half-built instance, and translate other C++ exceptions (bad_alloc -> MemoryError). Assisted-by: ClaudeCode:claude-opus-5-5
Assisted-by: ClaudeCode:claude-fable-5-2
Pass the type info into make_new_instance and instance::allocate_layout. Assisted-by: ClaudeCode:claude-fable-5-2
Also use a fixed TypeError message, so that PyPy cannot fail while it makes the qualified type name. Assisted-by: ClaudeCode:claude-fable-5-2
A failure in allocate_layout() released the object with tp_free. That skipped the interpreter's destruction bookkeeping (reference tracer, Py_TRACE_REFS). The layout storage is now allocated before tp_alloc, so no object exists when the allocation fails. Assisted-by: ClaudeCode:claude-fable-5-2
CPython 3.15 calls the tracer with a null object for PyRefTracer_TRACKER_REMOVED when the tracer is replaced. Assisted-by: ClaudeCode:claude-fable-5-2
9668a4b to
a9178b8
Compare
Description
Item 11 from #6159. Calling
pybind11_object()from Python, or a Python subclass of it with no registered C++ base, aborts the process because a C++ exception reaches the interpreter through theextern "C"tp_new:pybind11_object_newnow checks for a registered C++ base before it allocates. If there is none, it raisesTypeError, so no half-built instance is created.pybind11_object_newsends all other C++ exceptions throughtry_translate_exceptions(), so they become Python errors (std::bad_allocbecomesMemoryError).make_new_instancechecks thetp_allocresult.make_new_instancereleases the object ifallocate_layout()throws. It does not runtp_dealloc, because the layout is incomplete.pybind11_object_newlooks up the type info one time and passes it tomake_new_instanceandinstance::allocate_layout, so construction has no extra lookup.The new test aborts the test process without this fix. I ran it in both release and debug builds.
A second test makes
PyMem_Callocfail and checks that the instance does not leak.Suggested changelog entry:
pybind11_objectfrom Python now raisesTypeErrorinstead of aborting the process.