From 9f73060cc7759b2375d71243a93dadf1ad996077 Mon Sep 17 00:00:00 2001 From: Wenzel Jakob Date: Fri, 9 Nov 2018 12:32:48 +0100 Subject: [PATCH] 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). --- include/pybind11/stl.h | 4 ++-- tests/test_stl.cpp | 2 ++ tests/test_stl.py | 4 ++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/include/pybind11/stl.h b/include/pybind11/stl.h index 659505d87..50828a012 100644 --- a/include/pybind11/stl.h +++ b/include/pybind11/stl.h @@ -199,9 +199,9 @@ private: public: bool load(handle src, bool convert) { - if (!isinstance(src)) + if (!isinstance(src)) return false; - auto l = reinterpret_borrow(src); + auto l = reinterpret_borrow(src); if (!require_size(l.size())) return false; size_t ctr = 0; diff --git a/tests/test_stl.cpp b/tests/test_stl.cpp index 8736ea86d..0bb6433d1 100644 --- a/tests/test_stl.cpp +++ b/tests/test_stl.cpp @@ -263,4 +263,6 @@ TEST_SUBMODULE(stl, m) { return result; }, py::return_value_policy::take_ownership); + + m.def("array_cast_sequence", [](std::array x) { return x; }); } diff --git a/tests/test_stl.py b/tests/test_stl.py index b78f86a24..9e58223c0 100644 --- a/tests/test_stl.py +++ b/tests/test_stl.py @@ -216,3 +216,7 @@ def test_stl_ownership(): assert len(r) == 1 del r assert cstats.alive() == 0 + + +def test_array_cast_sequence(): + assert m.array_cast_sequence((1, 2, 3)) == [1, 2, 3]