std::array<> caster: support arbitrary sequences (#1602)

This PR brings the std::array<> caster in sync with the other STL type
casters: to accept an arbitrary sequence as input (rather than a list,
which is too restrictive).
This commit is contained in:
Wenzel Jakob 2018-11-09 12:32:48 +01:00 committed by GitHub
parent 978d439e92
commit 9f73060cc7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 2 deletions

View File

@ -199,9 +199,9 @@ private:
public: public:
bool load(handle src, bool convert) { bool load(handle src, bool convert) {
if (!isinstance<list>(src)) if (!isinstance<sequence>(src))
return false; return false;
auto l = reinterpret_borrow<list>(src); auto l = reinterpret_borrow<sequence>(src);
if (!require_size(l.size())) if (!require_size(l.size()))
return false; return false;
size_t ctr = 0; size_t ctr = 0;

View File

@ -263,4 +263,6 @@ TEST_SUBMODULE(stl, m) {
return result; return result;
}, },
py::return_value_policy::take_ownership); py::return_value_policy::take_ownership);
m.def("array_cast_sequence", [](std::array<int, 3> x) { return x; });
} }

View File

@ -216,3 +216,7 @@ def test_stl_ownership():
assert len(r) == 1 assert len(r) == 1
del r del r
assert cstats.alive() == 0 assert cstats.alive() == 0
def test_array_cast_sequence():
assert m.array_cast_sequence((1, 2, 3)) == [1, 2, 3]