Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for #3788 __getattr__ issue #3795

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 4 additions & 3 deletions include/pybind11/detail/smart_holder_type_casters.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,6 @@ class modified_type_caster_generic_load_impl {
if (!src) {
return false;
}
if (cpptype && try_as_void_ptr_capsule(src)) {
return true;
}
if (!typeinfo) {
return try_load_foreign_module_local(src);
}
Expand Down Expand Up @@ -296,6 +293,10 @@ class modified_type_caster_generic_load_impl {
return true;
}

if (cpptype && try_as_void_ptr_capsule(src)) {
return true;
}

return false;
}

Expand Down
13 changes: 13 additions & 0 deletions tests/test_class_sh_void_ptr_capsule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ int get_from_no_conversion_capsule(const NoConversion *c) { return c->get(); }

int get_from_no_capsule_returned(const NoCapsuleReturned *c) { return c->get(); }

// https://github.com/pybind/pybind11/issues/3788
struct TypeWithGetattr {
TypeWithGetattr() = default;
int get_42() const { return 42; }
};

} // namespace class_sh_void_ptr_capsule
} // namespace pybind11_tests

Expand All @@ -83,6 +89,7 @@ PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_void_ptr_capsule::Va
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_void_ptr_capsule::NoConversion)
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_void_ptr_capsule::NoCapsuleReturned)
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_void_ptr_capsule::AsAnotherObject)
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_void_ptr_capsule::TypeWithGetattr)

TEST_SUBMODULE(class_sh_void_ptr_capsule, m) {
using namespace pybind11_tests::class_sh_void_ptr_capsule;
Expand Down Expand Up @@ -128,4 +135,10 @@ TEST_SUBMODULE(class_sh_void_ptr_capsule, m) {
m.def("get_from_unique_ptr_valid_capsule", &get_from_unique_ptr_valid_capsule);
m.def("get_from_no_conversion_capsule", &get_from_no_conversion_capsule);
m.def("get_from_no_capsule_returned", &get_from_no_capsule_returned);

py::classh<TypeWithGetattr>(m, "TypeWithGetattr")
.def(py::init<>())
.def("get_42", &TypeWithGetattr::get_42)
.def("__getattr__",
[](TypeWithGetattr &, const std::string &key) { return "GetAttr: " + key; });
}
10 changes: 8 additions & 2 deletions tests/test_class_sh_void_ptr_capsule.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
@pytest.mark.parametrize(
"ctor, caller, expected, capsule_generated",
[
(m.Valid, m.get_from_valid_capsule, 101, True),
(m.Valid, m.get_from_valid_capsule, 101, False),
(m.NoConversion, m.get_from_no_conversion_capsule, 102, False),
(m.NoCapsuleReturned, m.get_from_no_capsule_returned, 103, True),
(m.NoCapsuleReturned, m.get_from_no_capsule_returned, 103, False),
(m.AsAnotherObject, m.get_from_valid_capsule, 104, True),
],
)
Expand All @@ -31,3 +31,9 @@ def test_as_void_ptr_capsule_unsupported(ctor, caller, pointer_type, capsule_gen
caller(obj)
assert pointer_type in str(excinfo.value)
assert obj.capsule_generated == capsule_generated


def test_type_with_getattr():
obj = m.TypeWithGetattr()
assert obj.get_42() == 42
assert obj.something == "GetAttr: something"