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

def_readwrite/readonly: bind using class type, not inferred type #911

Closed
wants to merge 1 commit 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
8 changes: 5 additions & 3 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -1042,15 +1042,17 @@ class class_ : public detail::generic_type {

template <typename C, typename D, typename... Extra>
class_ &def_readwrite(const char *name, D C::*pm, const Extra&... extra) {
cpp_function fget([pm](const C &c) -> const D &{ return c.*pm; }, is_method(*this)),
fset([pm](C &c, const D &value) { c.*pm = value; }, is_method(*this));
static_assert(std::is_base_of<C, type>::value, "def_readwrite() requires a class member (or base class member)");
cpp_function fget([pm](const type &c) -> const D &{ return c.*pm; }, is_method(*this)),
fset([pm](type &c, const D &value) { c.*pm = value; }, is_method(*this));
def_property(name, fget, fset, return_value_policy::reference_internal, extra...);
return *this;
}

template <typename C, typename D, typename... Extra>
class_ &def_readonly(const char *name, const D C::*pm, const Extra& ...extra) {
cpp_function fget([pm](const C &c) -> const D &{ return c.*pm; }, is_method(*this));
static_assert(std::is_base_of<C, type>::value, "def_readonly() requires a class member (or base class member)");
cpp_function fget([pm](const type &c) -> const D &{ return c.*pm; }, is_method(*this));
def_property_readonly(name, fget, return_value_policy::reference_internal, extra...);
return *this;
}
Expand Down
13 changes: 13 additions & 0 deletions tests/test_methods_and_attributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ struct TestPropRVP {
SimpleValue TestPropRVP::sv1{};
SimpleValue TestPropRVP::sv2{};

struct UnregisteredBaseWithMember { int ro = 6; int rw = 13; };
struct RegisteredDerivedInheritsMember : UnregisteredBaseWithMember {};

class DynamicClass {
public:
DynamicClass() { print_default_created(this); }
Expand Down Expand Up @@ -258,6 +261,16 @@ test_initializer methods_and_attributes([](py::module &m) {
py::class_<SimpleValue>(m, "SimpleValue")
.def_readwrite("value", &SimpleValue::value);

// Unnumbered issue: can't expose unregistered base class member with def_readwrite/def_readonly
py::class_<RegisteredDerivedInheritsMember>(m, "RegisteredDerivedInheritsMember")
.def(py::init<>())
.def_readonly("ro", &UnregisteredBaseWithMember::ro)
.def_readwrite("rw", &UnregisteredBaseWithMember::rw)
// These should trigger a static_assert if uncommented
//.def_readwrite("fails", &SimpleValue::value) // should trigger a static_assert if uncommented
//.def_readonly("fails", &SimpleValue::value) // should trigger a static_assert if uncommented
;

auto static_get1 = [](py::object) -> const SimpleValue & { return TestPropRVP::sv1; };
auto static_get2 = [](py::object) -> const SimpleValue & { return TestPropRVP::sv2; };
auto static_set1 = [](py::object, int v) { TestPropRVP::sv1.value = v; };
Expand Down
8 changes: 7 additions & 1 deletion tests/test_methods_and_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_methods_and_attributes():


def test_properties():
from pybind11_tests import TestProperties
from pybind11_tests import TestProperties, RegisteredDerivedInheritsMember

instance = TestProperties()

Expand All @@ -79,6 +79,12 @@ def test_properties():
instance.def_property = 3
assert instance.def_property == 3

z = RegisteredDerivedInheritsMember()
assert z.ro == 6
assert z.rw == 13
z.rw += z.ro
assert z.ro == 6 and z.rw == 19


def test_copy_method():
"""Issue #443: calling copied methods fails in Python 3"""
Expand Down