Skip to content

Commit

Permalink
Fix implicit conversion of accessors to types derived from py::object
Browse files Browse the repository at this point in the history
  • Loading branch information
dean0x7d committed Sep 11, 2017
1 parent 953d242 commit 2cf87a5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
8 changes: 5 additions & 3 deletions include/pybind11/pytypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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) \
Expand Down
6 changes: 6 additions & 0 deletions tests/test_pytypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});

Expand Down
2 changes: 2 additions & 0 deletions tests/test_pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ def func(self, x, *args):
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)

Expand Down

0 comments on commit 2cf87a5

Please sign in to comment.