Repository navigation
Conversation
…base A polymorphic derived class puts its vptr before a non-polymorphic base, so the base subobject is not at offset 0. The single-inheritance fast path in load_impl used the derived pointer as the base pointer, and base members read the wrong memory. Use the multiple-inheritance path for this case, which applies the offset. Assisted-by: ClaudeCode:claude-fable-5-2
|
— codex on behalf of Ralf — No actionable findings. When a polymorphic derived type has a non-polymorphic base, the registered C++ cast handles the potentially nonzero subobject offset. My probe read the wrong base member on the parent and the expected value at this head, including for a Python subclass of the bound derived class. All 21 tests in the two multiple-inheritance modules passed on regular and free-threaded CPython 3.15.2. Base registration, Regression |
|
At second look, somehow (even though I wasn't asking for a re-review) codex figured out that this PR has a compatibility regression. I'm posting the entire response, even only the first paragraph is actionable. The rest is mainly to document how my prodding unexpectedly produced the finding. I’d still merge the typed-base fix, but I’d hold the current #6296 head for one compatibility regression. I reproduced it with two classes sharing The nine years are misleading. Someone proposed a fix just two days after #1080 was reported in September 2017. It changed the internals ABI, so the author deferred it from v2.2.1 to v2.3. It’s still open, and the public discussion ends after an MSVC linker fix. #6296 is smaller because a template base gives pybind11 the C++ types it needs to reuse the existing pointer-adjusting cast, just as your virtual-inheritance fix did. The original #1080 example passes the base as a Python handle, and I confirmed that example still reads the wrong value at this head. Original report, 2017 fix attempt, Virtual-inheritance fix Your rarity hypothesis is plausible. The trigger requires mixed polymorphism, a nonzero base offset, and an operation that actually goes through the base binding. As Henry pointed out in the #6306 discussion, users can find workarounds and move on. The issue history gives us little basis for estimating frequency. Making |
Track the potential vptr offset on the polymorphic source class. Require simple source ancestry for the unadjusted load path so a sibling bound with a Python class handle can keep using that path.
PyPy raises "instance layout conflicts in multiple inheritance" when a Python class inherits from two bound classes and one has a bound C++ parent. Assisted-by: ClaudeCode:claude-fable-5-2
A class bound with a Python base handle has no registered C++ cast. When the parent has non-simple ancestry, the load of the child as the parent found no match and raised TypeError. Register an identity cast for this case, because the handle form already requires equal pointers. Add tests for template-bound and handle-bound children of the pybind#1080 class and for a shared_ptr load of the base. Document when the template form of the base is necessary. Assisted-by: ClaudeCode:claude-fable-5-2
|
I needed to do another 12 line fix on there. There's a speed regression too, we could only avoid that with an ABI break, if this goes in we can add that to the ABI list discussion. Here are options for simplification on this PR. The first one, which revert part of my fix and most of yours, would take the change out of load_impl, which is the hot path. 🤖 AI text below 🤖 Yes. The largest saving is to drop the source-side machinery from Option 1: return to the original 5-line fix plus the identity cast.
Option 2: keep the current design and trim it.
|
Each base given as a Python handle now gets an identity cast, so a handle-bound sibling of a polymorphic class can load through the registered casts. The polymorphic class sets multiple_inheritance again, and the type_record flag and the load_impl changes are removed. Assisted-by: ClaudeCode:claude-fable-5-2
|
I have the simpler version, can push. It's like the original path, but with the identity cast I think it is better than before. Less that have the LoC, and avoids the higher risk hot path changes. All the new tests pass unchanged. Two downsides, below. I think I'll push it once CI runs on this version. 🤖 AI text below 🤖
|
— codex on behalf of Ralf —
Description
A polymorphic C++ class can place a non-polymorphic base subobject at a nonzero offset. The single-inheritance fast path used the derived pointer as a base pointer, so inherited member access could read the wrong memory.
For
py::class_<Derived, Base>, the binding now records that the derived class may need pointer adjustment and uses the registered C++ upcast when converting to the base. A zero-offset sibling registered withpy::class_<Sibling>(m, "Sibling", base_handle)keeps its direct load path. The regression tests cover both classes, including in Python multiple inheritance.The offset repair requires the template-base form, which supplies the C++ types needed for the upcast. The new flag lives in the temporary
type_recordand feeds the existingtype_info::simple_ancestorsfield, so noPYBIND11_INTERNALS_VERSIONbump is needed.Historical background
The original report in September 2017 used a Python base handle. A fix proposed two days later required an internals ABI bump, so its author deferred it past v2.2.1 and suggested v2.3. That proposal remains open. Original report, 2017 proposal
Suggested changelog entry:
py::class_<Derived, Base>.