-
Notifications
You must be signed in to change notification settings - Fork 82
Use try...finally... to ensure buffer is released
#728
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -130,36 +130,37 @@ cdef class Array: | |
| self.strides_mv = None | ||
| else: | ||
| PyObject_GetBuffer(obj, &pybuf, PyBUF_FULL_RO) | ||
|
|
||
| if pybuf.suboffsets != NULL: | ||
| raise NotImplementedError("Suboffsets are not supported") | ||
|
|
||
| self.ptr = <uintptr_t>pybuf.buf | ||
| self.obj = pybuf.obj | ||
| self.readonly = <bint>pybuf.readonly | ||
| self.ndim = <Py_ssize_t>pybuf.ndim | ||
| self.itemsize = <Py_ssize_t>pybuf.itemsize | ||
|
|
||
| if self.ndim > 0: | ||
| self.shape_mv = new_Py_ssize_t_array(self.ndim) | ||
| memcpy( | ||
| &self.shape_mv[0], | ||
| pybuf.shape, | ||
| self.ndim * sizeof(Py_ssize_t) | ||
| ) | ||
| if not PyBuffer_IsContiguous(&pybuf, b"C"): | ||
| self.strides_mv = new_Py_ssize_t_array(self.ndim) | ||
| try: | ||
| if pybuf.suboffsets != NULL: | ||
| raise NotImplementedError("Suboffsets are not supported") | ||
|
Comment on lines
+134
to
+135
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For example here is one exception we raise Though it is also possible to wind up with an exception due to some bug in the code below In any event we want to make sure normal resource cleanup can proceed as usual (without potentially causing segfaults) |
||
|
|
||
| self.ptr = <uintptr_t>pybuf.buf | ||
| self.obj = pybuf.obj | ||
| self.readonly = <bint>pybuf.readonly | ||
| self.ndim = <Py_ssize_t>pybuf.ndim | ||
| self.itemsize = <Py_ssize_t>pybuf.itemsize | ||
|
|
||
| if self.ndim > 0: | ||
| self.shape_mv = new_Py_ssize_t_array(self.ndim) | ||
| memcpy( | ||
| &self.strides_mv[0], | ||
| pybuf.strides, | ||
| &self.shape_mv[0], | ||
| pybuf.shape, | ||
| self.ndim * sizeof(Py_ssize_t) | ||
| ) | ||
| if not PyBuffer_IsContiguous(&pybuf, b"C"): | ||
| self.strides_mv = new_Py_ssize_t_array(self.ndim) | ||
| memcpy( | ||
| &self.strides_mv[0], | ||
| pybuf.strides, | ||
| self.ndim * sizeof(Py_ssize_t) | ||
| ) | ||
| else: | ||
| self.strides_mv = None | ||
| else: | ||
| self.shape_mv = None | ||
| self.strides_mv = None | ||
| else: | ||
| self.shape_mv = None | ||
| self.strides_mv = None | ||
| PyBuffer_Release(&pybuf) | ||
| finally: | ||
| PyBuffer_Release(&pybuf) | ||
|
Comment on lines
+162
to
+163
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then moving the That way we still release the underlying |
||
|
|
||
| cpdef bint _c_contiguous(self): | ||
| return _c_contiguous( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The core of this change involves adding a
tryfor the block of code right afterPyObject_GetBuffer