mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-22 05:05:11 +00:00
Add a pybind function to clear a list. (#5153)
* Add a pybing function to clear a list. * Add required error handling. * Add `/* py-non-const */` as suggested by @Skylion007 --------- Co-authored-by: Ralf W. Grosse-Kunstleve <rwgk@google.com>
This commit is contained in:
parent
9b3a200065
commit
35ff42b56e
@ -2183,6 +2183,11 @@ public:
|
|||||||
throw error_already_set();
|
throw error_already_set();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
void clear() /* py-non-const */ {
|
||||||
|
if (PyList_SetSlice(m_ptr, 0, PyList_Size(m_ptr), nullptr) == -1) {
|
||||||
|
throw error_already_set();
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class args : public tuple {
|
class args : public tuple {
|
||||||
|
@ -135,6 +135,7 @@ TEST_SUBMODULE(pytypes, m) {
|
|||||||
m.def("list_size_t", []() { return py::list{(py::size_t) 0}; });
|
m.def("list_size_t", []() { return py::list{(py::size_t) 0}; });
|
||||||
m.def("list_insert_ssize_t", [](py::list *l) { return l->insert((py::ssize_t) 1, 83); });
|
m.def("list_insert_ssize_t", [](py::list *l) { return l->insert((py::ssize_t) 1, 83); });
|
||||||
m.def("list_insert_size_t", [](py::list *l) { return l->insert((py::size_t) 3, 57); });
|
m.def("list_insert_size_t", [](py::list *l) { return l->insert((py::size_t) 3, 57); });
|
||||||
|
m.def("list_clear", [](py::list *l) { l->clear(); });
|
||||||
m.def("get_list", []() {
|
m.def("get_list", []() {
|
||||||
py::list list;
|
py::list list;
|
||||||
list.append("value");
|
list.append("value");
|
||||||
|
@ -65,6 +65,8 @@ def test_list(capture, doc):
|
|||||||
assert lins == [1, 83, 2]
|
assert lins == [1, 83, 2]
|
||||||
m.list_insert_size_t(lins)
|
m.list_insert_size_t(lins)
|
||||||
assert lins == [1, 83, 2, 57]
|
assert lins == [1, 83, 2, 57]
|
||||||
|
m.list_clear(lins)
|
||||||
|
assert lins == []
|
||||||
|
|
||||||
with capture:
|
with capture:
|
||||||
lst = m.get_list()
|
lst = m.get_list()
|
||||||
|
Loading…
Reference in New Issue
Block a user