11// /
22// / Copyright (c) 2016-2024 CNRS INRIA
3+ // / Copyright (c) 2025-2025 Heriot-Watt University
34// / This file was taken from Pinocchio (header
45// / <pinocchio/bindings/python/utils/std-vector.hpp>)
56// /
@@ -84,11 +85,12 @@ struct overload_base_get_item_for_std_vector
8485
8586 template <class Class >
8687 void visit (Class &cl) const {
87- cl.def (" __getitem__" , &base_get_item);
88+ cl.def (" __getitem__" , &base_get_item_int)
89+ .def (" __getitem__" , &base_get_item_slice);
8890 }
8991
9092 private:
91- static boost::python::object base_get_item (
93+ static boost::python::object base_get_item_int (
9294 boost::python::back_reference<Container &> container, PyObject *i_) {
9395 index_type idx = convert_index (container.get (), i_);
9496 typename Container::iterator i = container.get ().begin ();
@@ -104,6 +106,29 @@ struct overload_base_get_item_for_std_vector
104106 return bp::object (bp::handle<>(convert (*i)));
105107 }
106108
109+ static boost::python::object base_get_item_slice (
110+ boost::python::back_reference<Container&> container,
111+ boost::python::slice slice) {
112+
113+ namespace bp = boost::python;
114+ Py_ssize_t start, stop, step, slicelen;
115+ const Py_ssize_t n = static_cast <Py_ssize_t>(container.get ().size ());
116+
117+ if (PySlice_GetIndicesEx (slice.ptr (), n, &start, &stop, &step, &slicelen) != 0 )
118+ bp::throw_error_already_set ();
119+
120+ bp::list out;
121+ for (Py_ssize_t k = 0 ; k < slicelen; ++k) {
122+ Py_ssize_t idx = start + k * step;
123+
124+ // return a reference (not a copy), like your int-getitem
125+ typename bp::to_python_indirect<value_type&,
126+ bp::detail::make_reference_holder> convert;
127+ out.append (bp::object (bp::handle<>(convert (container.get ()[std::size_t (idx)]))));
128+ }
129+ return out;
130+ }
131+
107132 static index_type convert_index (Container &container, PyObject *i_) {
108133 bp::extract<long > i (i_);
109134 if (i.check ()) {
0 commit comments