-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Fix buffer protocol implementation #5407
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9f36b54
Fix buffer protocol implementation
QuLogic d150159
Apply suggestions from review
QuLogic d98c86c
Obey contiguity requests for buffer protocol
QuLogic ea8c424
Handle review comments
QuLogic 83f6ec5
Test buffer protocol against NumPy
QuLogic 63d6fd2
Also check PyBUF_FORMAT results
QuLogic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -601,24 +601,70 @@ extern "C" inline int pybind11_getbuffer(PyObject *obj, Py_buffer *view, int fla | |
set_error(PyExc_BufferError, "Writable buffer requested for readonly storage"); | ||
return -1; | ||
} | ||
|
||
// Fill in all the information, and then downgrade as requested by the caller, or raise an | ||
// error if that's not possible. | ||
view->obj = obj; | ||
view->ndim = 1; | ||
view->internal = info; | ||
view->buf = info->ptr; | ||
view->itemsize = info->itemsize; | ||
view->len = view->itemsize; | ||
for (auto s : info->shape) { | ||
view->len *= s; | ||
} | ||
view->ndim = static_cast<int>(info->ndim); | ||
view->shape = info->shape.data(); | ||
view->strides = info->strides.data(); | ||
view->readonly = static_cast<int>(info->readonly); | ||
if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) { | ||
view->format = const_cast<char *>(info->format.c_str()); | ||
} | ||
if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES) { | ||
view->ndim = (int) info->ndim; | ||
view->strides = info->strides.data(); | ||
view->shape = info->shape.data(); | ||
|
||
// Note, all contiguity flags imply PyBUF_STRIDES and lower. | ||
if ((flags & PyBUF_C_CONTIGUOUS) == PyBUF_C_CONTIGUOUS) { | ||
if (PyBuffer_IsContiguous(view, 'C') == 0) { | ||
std::memset(view, 0, sizeof(Py_buffer)); | ||
delete info; | ||
set_error(PyExc_BufferError, | ||
"C-contiguous buffer requested for discontiguous storage"); | ||
return -1; | ||
} | ||
} else if ((flags & PyBUF_F_CONTIGUOUS) == PyBUF_F_CONTIGUOUS) { | ||
if (PyBuffer_IsContiguous(view, 'F') == 0) { | ||
std::memset(view, 0, sizeof(Py_buffer)); | ||
delete info; | ||
set_error(PyExc_BufferError, | ||
"Fortran-contiguous buffer requested for discontiguous storage"); | ||
return -1; | ||
} | ||
} else if ((flags & PyBUF_ANY_CONTIGUOUS) == PyBUF_ANY_CONTIGUOUS) { | ||
if (PyBuffer_IsContiguous(view, 'A') == 0) { | ||
std::memset(view, 0, sizeof(Py_buffer)); | ||
delete info; | ||
set_error(PyExc_BufferError, "Contiguous buffer requested for discontiguous storage"); | ||
return -1; | ||
} | ||
|
||
} else if ((flags & PyBUF_STRIDES) != PyBUF_STRIDES) { | ||
// If no strides are requested, the buffer must be C-contiguous. | ||
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. Maybe move this comment to the top of the |
||
// https://docs.python.org/3/c-api/buffer.html#contiguity-requests | ||
if (PyBuffer_IsContiguous(view, 'C') == 0) { | ||
std::memset(view, 0, sizeof(Py_buffer)); | ||
delete info; | ||
set_error(PyExc_BufferError, | ||
"C-contiguous buffer requested for discontiguous storage"); | ||
return -1; | ||
} | ||
|
||
view->strides = nullptr; | ||
|
||
// Since this is a contiguous buffer, it can also pretend to be 1D. | ||
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. Unsure, maybe better to follow numpy (IIUC?) and make this |
||
if ((flags & PyBUF_ND) != PyBUF_ND) { | ||
view->shape = nullptr; | ||
view->ndim = 0; | ||
} | ||
} | ||
|
||
Py_INCREF(view->obj); | ||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
For a follow-on PR: We should use
std::unique_ptr
to make sure there aren't leaks (e.g. when there are C++ exceptions).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.
Will that work when put into
view->internal
?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.
@QuLogic Is there a chance that you could try this in a follow-on PR, now that this one is merged?