Fix implicit conversion of accessors to types derived from py::object

Fixes #1069.
This commit is contained in:
Dean Moldovan 2017-09-10 12:21:21 +02:00
parent 953d2422b3
commit 2cf87a54d8
3 changed files with 13 additions and 3 deletions

View File

@ -431,8 +431,8 @@ class accessor : public object_api<accessor<Policy>> {
public:
accessor(handle obj, key_type key) : obj(obj), key(std::move(key)) { }
accessor(const accessor &a) = default;
accessor(accessor &&a) = default;
accessor(const accessor &) = default;
accessor(accessor &&) = default;
// accessor overload required to override default assignment operator (templates are not allowed
// to replace default compiler-generated assignments).
@ -743,7 +743,9 @@ NAMESPACE_END(detail)
{ if (!m_ptr) throw error_already_set(); } \
Name(object &&o) \
: Parent(check_(o) ? o.release().ptr() : ConvertFun(o.ptr()), stolen_t{}) \
{ if (!m_ptr) throw error_already_set(); }
{ if (!m_ptr) throw error_already_set(); } \
template <typename Policy_> \
Name(const ::pybind11::detail::accessor<Policy_> &a) : Name(object(a)) { }
#define PYBIND11_OBJECT(Name, Parent, CheckFun) \
PYBIND11_OBJECT_COMMON(Name, Parent, CheckFun) \

View File

@ -128,6 +128,12 @@ TEST_SUBMODULE(pytypes, m) {
d["operator()"] = o.attr("func")(1);
d["operator*"] = o.attr("func")(*o.attr("begin_end"));
// Test implicit conversion
py::list implicit_list = o.attr("begin_end");
d["implicit_list"] = implicit_list;
py::dict implicit_dict = o.attr("__dict__");
d["implicit_dict"] = implicit_dict;
return d;
});

View File

@ -144,6 +144,8 @@ def test_accessors():
assert d["is_none"] is False
assert d["operator()"] == 2
assert d["operator*"] == 7
assert d["implicit_list"] == [1, 2, 3]
assert all(x in TestObject.__dict__ for x in d["implicit_dict"])
assert m.tuple_accessor(tuple()) == (0, 1, 2)