chore: Improve PyCapsule exception handling (#4232)

* Improve pycapsule error handling corner cases

* Handle another corner case

* Simplify err handling code
This commit is contained in:
Aaron Gokaslan 2022-10-11 16:07:42 -04:00 committed by GitHub
parent ff7b69714d
commit 0927c4d19e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1829,7 +1829,7 @@ public:
// guard if destructor called while err indicator is set // guard if destructor called while err indicator is set
error_scope error_guard; error_scope error_guard;
auto destructor = reinterpret_cast<void (*)(void *)>(PyCapsule_GetContext(o)); auto destructor = reinterpret_cast<void (*)(void *)>(PyCapsule_GetContext(o));
if (PyErr_Occurred()) { if (destructor == nullptr && PyErr_Occurred()) {
throw error_already_set(); throw error_already_set();
} }
const char *name = get_name_in_error_scope(o); const char *name = get_name_in_error_scope(o);
@ -1843,7 +1843,7 @@ public:
} }
}); });
if (!m_ptr || PyCapsule_SetContext(m_ptr, (void *) destructor) != 0) { if (!m_ptr || PyCapsule_SetContext(m_ptr, reinterpret_cast<void *>(destructor)) != 0) {
throw error_already_set(); throw error_already_set();
} }
} }