From ac945cf52b06fbcc5520cc839ed56551fa564d38 Mon Sep 17 00:00:00 2001 From: Thierry Coppey Date: Sun, 2 Jun 2024 13:55:23 +0200 Subject: [PATCH 1/3] Add a pybing function to clear a list. --- include/pybind11/pytypes.h | 1 + tests/test_pytypes.cpp | 1 + tests/test_pytypes.py | 2 ++ 3 files changed, 4 insertions(+) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index a27d6aed09..6233856b15 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -2183,6 +2183,7 @@ class list : public object { throw error_already_set(); } } + void clear() { PyList_SetSlice(m_ptr, 0, PyList_Size(m_ptr), nullptr); } }; class args : public tuple { diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 12e6151812..f3709d40c6 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -135,6 +135,7 @@ TEST_SUBMODULE(pytypes, m) { 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_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", []() { py::list list; list.append("value"); diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index d215b4e8a6..38edfd9998 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -65,6 +65,8 @@ def test_list(capture, doc): assert lins == [1, 83, 2] m.list_insert_size_t(lins) assert lins == [1, 83, 2, 57] + m.list_clear(lins) + assert lins == [] with capture: lst = m.get_list() From da4a108d2eeb6a429c0097f3f5f0b33f6124a8ce Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Thu, 6 Jun 2024 14:01:59 -0700 Subject: [PATCH 2/3] Add required error handling. --- include/pybind11/pytypes.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 6233856b15..5caff995c8 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -2183,7 +2183,11 @@ class list : public object { throw error_already_set(); } } - void clear() { PyList_SetSlice(m_ptr, 0, PyList_Size(m_ptr), nullptr); } + void clear() { + if (PyList_SetSlice(m_ptr, 0, PyList_Size(m_ptr), nullptr) == -1) { + throw error_already_set(); + } + } }; class args : public tuple { From 627ca30f5e4f0b5d0a40c2485e9c172c411f8a70 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Fri, 7 Jun 2024 13:20:56 -0700 Subject: [PATCH 3/3] Add `/* py-non-const */` as suggested by @Skylion007 --- include/pybind11/pytypes.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 5caff995c8..3d2d0c2dac 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -2183,7 +2183,7 @@ class list : public object { throw error_already_set(); } } - void clear() { + void clear() /* py-non-const */ { if (PyList_SetSlice(m_ptr, 0, PyList_Size(m_ptr), nullptr) == -1) { throw error_already_set(); }