bugfix: Add error checking to list append and insert (#4208)

This commit is contained in:
Aaron Gokaslan 2022-10-03 13:44:09 -04:00 committed by GitHub
parent da8c730a62
commit c78dfe6964
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2022,14 +2022,20 @@ public:
detail::list_iterator end() const { return {*this, PyList_GET_SIZE(m_ptr)}; } detail::list_iterator end() const { return {*this, PyList_GET_SIZE(m_ptr)}; }
template <typename T> template <typename T>
void append(T &&val) /* py-non-const */ { void append(T &&val) /* py-non-const */ {
PyList_Append(m_ptr, detail::object_or_cast(std::forward<T>(val)).ptr()); if (PyList_Append(m_ptr, detail::object_or_cast(std::forward<T>(val)).ptr()) != 0) {
throw error_already_set();
}
} }
template <typename IdxType, template <typename IdxType,
typename ValType, typename ValType,
detail::enable_if_t<std::is_integral<IdxType>::value, int> = 0> detail::enable_if_t<std::is_integral<IdxType>::value, int> = 0>
void insert(const IdxType &index, ValType &&val) /* py-non-const */ { void insert(const IdxType &index, ValType &&val) /* py-non-const */ {
PyList_Insert( if (PyList_Insert(m_ptr,
m_ptr, ssize_t_cast(index), detail::object_or_cast(std::forward<ValType>(val)).ptr()); ssize_t_cast(index),
detail::object_or_cast(std::forward<ValType>(val)).ptr())
!= 0) {
throw error_already_set();
}
} }
}; };