From 0927c4d19e33687b463346ecb7d194493ee53b4c Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Tue, 11 Oct 2022 16:07:42 -0400 Subject: [PATCH] chore: Improve PyCapsule exception handling (#4232) * Improve pycapsule error handling corner cases * Handle another corner case * Simplify err handling code --- include/pybind11/pytypes.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 29506b7eaf..2e6b755caf 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -1829,7 +1829,7 @@ class capsule : public object { // guard if destructor called while err indicator is set error_scope error_guard; auto destructor = reinterpret_cast(PyCapsule_GetContext(o)); - if (PyErr_Occurred()) { + if (destructor == nullptr && PyErr_Occurred()) { throw error_already_set(); } const char *name = get_name_in_error_scope(o); @@ -1843,7 +1843,7 @@ class capsule : public object { } }); - if (!m_ptr || PyCapsule_SetContext(m_ptr, (void *) destructor) != 0) { + if (!m_ptr || PyCapsule_SetContext(m_ptr, reinterpret_cast(destructor)) != 0) { throw error_already_set(); } }