Skip to content

Commit c78dfe6

Browse files
authored
bugfix: Add error checking to list append and insert (pybind#4208)
1 parent da8c730 commit c78dfe6

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

include/pybind11/pytypes.h

+9-3
Original file line numberDiff line numberDiff line change
@@ -2022,14 +2022,20 @@ class list : public object {
20222022
detail::list_iterator end() const { return {*this, PyList_GET_SIZE(m_ptr)}; }
20232023
template <typename T>
20242024
void append(T &&val) /* py-non-const */ {
2025-
PyList_Append(m_ptr, detail::object_or_cast(std::forward<T>(val)).ptr());
2025+
if (PyList_Append(m_ptr, detail::object_or_cast(std::forward<T>(val)).ptr()) != 0) {
2026+
throw error_already_set();
2027+
}
20262028
}
20272029
template <typename IdxType,
20282030
typename ValType,
20292031
detail::enable_if_t<std::is_integral<IdxType>::value, int> = 0>
20302032
void insert(const IdxType &index, ValType &&val) /* py-non-const */ {
2031-
PyList_Insert(
2032-
m_ptr, ssize_t_cast(index), detail::object_or_cast(std::forward<ValType>(val)).ptr());
2033+
if (PyList_Insert(m_ptr,
2034+
ssize_t_cast(index),
2035+
detail::object_or_cast(std::forward<ValType>(val)).ptr())
2036+
!= 0) {
2037+
throw error_already_set();
2038+
}
20332039
}
20342040
};
20352041

0 commit comments

Comments
 (0)