◐ Shell
clean mode source ↗

gh-104371: Fix calls to __release_buffer__ while an exception is active by JelleZijlstra · Pull Request #104378 · python/cpython

Expand Up @@ -9117,14 +9117,20 @@ releasebuffer_maybe_call_super(PyObject *self, Py_buffer *buffer) static void releasebuffer_call_python(PyObject *self, Py_buffer *buffer) { // bf_releasebuffer may be called while an exception is already active. // We have no way to report additional errors up the stack, because // this slot returns void, so we simply stash away the active exception // and restore it after the call to Python returns. PyObject *exc = PyErr_GetRaisedException();
PyObject *mv; bool is_buffer_wrapper = Py_TYPE(buffer->obj) == &_PyBufferWrapper_Type; if (is_buffer_wrapper) { // Make sure we pass the same memoryview to // __release_buffer__() that __buffer__() returned. PyBufferWrapper *bw = (PyBufferWrapper *)buffer->obj; if (bw->mv == NULL) { return; goto end; } mv = Py_NewRef(bw->mv); } Expand All @@ -9134,7 +9140,7 @@ releasebuffer_call_python(PyObject *self, Py_buffer *buffer) mv = PyMemoryView_FromBuffer(buffer); if (mv == NULL) { PyErr_WriteUnraisable(self); return; goto end; } // Set the memoryview to restricted mode, which forbids // users from saving any reference to the underlying buffer Expand All @@ -9155,6 +9161,10 @@ releasebuffer_call_python(PyObject *self, Py_buffer *buffer) PyObject_CallMethodNoArgs(mv, &_Py_ID(release));
Comment thread

kumaraditya303 marked this conversation as resolved.

} Py_DECREF(mv); end: assert(!PyErr_Occurred());
Comment thread

kumaraditya303 marked this conversation as resolved.


PyErr_SetRaisedException(exc); }
/* Expand Down