Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
da65334
test(test_animal_cat_tiger): add virtual inheritance test
Aug 15, 2025
52f3431
style(test_animal_cat_tiger): format
Aug 15, 2025
bf0737b
style(test_animal_cat_tiger): remove extra header
Aug 15, 2025
ccc6100
test(test_animal_cat_tiger): Improve test code
Aug 15, 2025
35115c7
test(test_animal_cat_tiger): remove redundant virtual
Aug 15, 2025
9cefe93
Merge branch 'master' into mannix/test/animal_cat_tiger
rwgk Sep 2, 2025
41c37a3
[skip ci] Remove `valueptr = src_raw_void_ptr;` in smart_holder_from_…
rwgk Sep 2, 2025
91d34a7
style: pre-commit fixes
pre-commit-ci[bot] Sep 2, 2025
44272c9
Fix "🐍 3 • windows-latest • mingw64" job (apparently msys2/setup-msys…
rwgk Sep 2, 2025
0f39a06
Experiment: apply 41c37a3cc82c9340a3162aaf9a454564b0312cfe also in sm…
rwgk Sep 2, 2025
0a8bb71
Disable test that fails (after commit 0f39a06effce0ccfa628fde1e20cdd8…
rwgk Sep 2, 2025
5006beb
Revert "Fix "🐍 3 • windows-latest • mingw64" job (apparently msys2/se…
rwgk Sep 3, 2025
e41dfcd
Merge branch 'master' into mannix/test/animal_cat_tiger
rwgk Sep 3, 2025
6d89ed9
test_animal_cat_tiger.cpp: bind_using_shared_ptr(), bind_using_smart_…
rwgk Sep 2, 2025
ccdb6b7
Merge branch 'master' into mannix/test/animal_cat_tiger
rwgk Sep 12, 2025
90700fa
Merge branch 'master' into mannix/test/animal_cat_tiger
rwgk Sep 13, 2025
9bbb3dc
Undo NOtest
rwgk Sep 13, 2025
4ad0203
Rename misleading test_with_smart_holder → test_clone
rwgk Sep 12, 2025
32db87a
Add assert(ptr) in register_instance_impl, deregister_instance_impl
rwgk Sep 13, 2025
ab9693b
Undo changes in type_caster_base.h (the file is now identical to the …
rwgk Sep 13, 2025
fada0cb
Proper bug fix
rwgk Sep 13, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/pybind11/detail/class.h
Original file line number Diff line number Diff line change
Expand Up @@ -334,13 +334,15 @@ inline void enable_try_inc_ref(PyObject *obj) {
#endif

inline bool register_instance_impl(void *ptr, instance *self) {
assert(ptr);
#ifdef Py_GIL_DISABLED
enable_try_inc_ref(reinterpret_cast<PyObject *>(self));
#endif
with_instance_map(ptr, [&](instance_map &instances) { instances.emplace(ptr, self); });
return true; // unused, but gives the same signature as the deregister func
}
inline bool deregister_instance_impl(void *ptr, instance *self) {
assert(ptr);
return with_instance_map(ptr, [&](instance_map &instances) {
auto range = instances.equal_range(ptr);
for (auto it = range.first; it != range.second; ++it) {
Expand Down
6 changes: 2 additions & 4 deletions include/pybind11/detail/type_caster_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -657,9 +657,8 @@ handle smart_holder_from_shared_ptr(const std::shared_ptr<T> &src,
return none().release();
}

auto src_raw_ptr = src.get();
void *src_raw_void_ptr = const_cast<void *>(st.first);
assert(st.second != nullptr);
void *src_raw_void_ptr = static_cast<void *>(src_raw_ptr);
const detail::type_info *tinfo = st.second;
if (handle existing_inst = find_registered_python_instance(src_raw_void_ptr, tinfo)) {
// PYBIND11:REMINDER: MISSING: Enforcement of consistency with existing smart_holder.
Expand All @@ -673,8 +672,7 @@ handle smart_holder_from_shared_ptr(const std::shared_ptr<T> &src,
void *&valueptr = values_and_holders(inst_raw_ptr).begin()->value_ptr();
valueptr = src_raw_void_ptr;

auto smhldr
= smart_holder::from_shared_ptr(std::shared_ptr<void>(src, const_cast<void *>(st.first)));
auto smhldr = smart_holder::from_shared_ptr(std::shared_ptr<void>(src, src_raw_void_ptr));
tinfo->init_instance(inst_raw_ptr, static_cast<const void *>(&smhldr));

if (policy == return_value_policy::reference_internal) {
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ set(PYBIND11_TEST_FILES
test_scoped_critical_section
test_sequences_and_iterators
test_smart_ptr
test_animal_cat_tiger
test_stl
test_stl_binders
test_tagbased_polymorphic
Expand Down
71 changes: 71 additions & 0 deletions tests/test_animal_cat_tiger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include "pybind11_tests.h"

#include <memory>

namespace pybind11_tests {
namespace class_animal {

template <int> // Using int as a trick to easily generate a series of types.
struct Multi {

class Animal {
public:
Animal() = default;
Animal(const Animal &) = default;
Animal &operator=(const Animal &) = default;
virtual std::shared_ptr<Animal> clone() const = 0;
virtual ~Animal() = default;
};

class Cat : virtual public Animal {
public:
Cat() = default;
Cat(const Cat &) = default;
Cat &operator=(const Cat &) = default;
~Cat() override = default;
};

class Tiger : virtual public Cat {
public:
Tiger() = default;
Tiger(const Tiger &) = default;
Tiger &operator=(const Tiger &) = default;
~Tiger() override = default;
std::shared_ptr<Animal> clone() const override { return std::make_shared<Tiger>(*this); }
};
};

namespace py = pybind11;

void bind_using_shared_ptr(py::module_ &m) {
using M = Multi<0>;

py::class_<M::Animal, std::shared_ptr<M::Animal>>(m, "AnimalSP");

py::class_<M::Cat, M::Animal, std::shared_ptr<M::Cat>>(m, "CatSP");

py::class_<M::Tiger, M::Cat, std::shared_ptr<M::Tiger>>(
m, "TigerSP", py::multiple_inheritance())
.def(py::init<>())
.def("clone", &M::Tiger::clone);
}

void bind_using_smart_holder(py::module_ &m) {
using M = Multi<1>;

py::class_<M::Animal, py::smart_holder>(m, "AnimalSH");

py::class_<M::Cat, M::Animal, py::smart_holder>(m, "CatSH");

py::class_<M::Tiger, M::Cat, py::smart_holder>(m, "TigerSH", py::multiple_inheritance())
.def(py::init<>())
.def("clone", &M::Tiger::clone);
}

TEST_SUBMODULE(class_animal, m) {
bind_using_shared_ptr(m);
bind_using_smart_holder(m);
}

} // namespace class_animal
} // namespace pybind11_tests
12 changes: 12 additions & 0 deletions tests/test_animal_cat_tiger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from __future__ import annotations

import pytest

from pybind11_tests import class_animal as m


@pytest.mark.parametrize("tiger_type", [m.TigerSP, m.TigerSH])
def test_clone(tiger_type):
tiger = tiger_type()
cloned = tiger.clone()
assert isinstance(cloned, tiger_type)
Loading