From 7370c7aedb171a3b85a0cc5fcb7863e3e3d24b04 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Mon, 3 Oct 2022 10:49:25 -0400 Subject: [PATCH] chore: Add error checking to list append and insert --- include/pybind11/pytypes.h | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index f4ba459f20..565df4375b 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -2022,14 +2022,20 @@ class list : public object { detail::list_iterator end() const { return {*this, PyList_GET_SIZE(m_ptr)}; } template void append(T &&val) /* py-non-const */ { - PyList_Append(m_ptr, detail::object_or_cast(std::forward(val)).ptr()); + if (PyList_Append(m_ptr, detail::object_or_cast(std::forward(val)).ptr()) != 0) { + throw error_already_set(); + } } template ::value, int> = 0> void insert(const IdxType &index, ValType &&val) /* py-non-const */ { - PyList_Insert( - m_ptr, ssize_t_cast(index), detail::object_or_cast(std::forward(val)).ptr()); + if (PyList_Insert(m_ptr, + ssize_t_cast(index), + detail::object_or_cast(std::forward(val)).ptr()) + != 0) { + throw error_already_set(); + } } };