-
Notifications
You must be signed in to change notification settings - Fork 802
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
buffer: tidy up exceptions #1534
Merged
Merged
Changes from all commits
Commits
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
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
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 |
---|---|---|
@@ -0,0 +1,145 @@ | ||
#![cfg(not(Py_LIMITED_API))] | ||
|
||
use pyo3::{ | ||
buffer::PyBuffer, class::PyBufferProtocol, exceptions::PyBufferError, ffi, prelude::*, | ||
AsPyPointer, | ||
}; | ||
use std::{ | ||
ffi::CStr, | ||
os::raw::{c_int, c_void}, | ||
ptr, | ||
}; | ||
|
||
#[macro_use] | ||
mod common; | ||
|
||
enum TestGetBufferError { | ||
NullShape, | ||
NullStrides, | ||
IncorrectItemSize, | ||
IncorrectFormat, | ||
IncorrectAlignment, | ||
} | ||
|
||
#[pyclass] | ||
struct TestBufferErrors { | ||
buf: Vec<u32>, | ||
error: Option<TestGetBufferError>, | ||
} | ||
|
||
#[pyproto] | ||
impl PyBufferProtocol for TestBufferErrors { | ||
fn bf_getbuffer(slf: PyRefMut<Self>, view: *mut ffi::Py_buffer, flags: c_int) -> PyResult<()> { | ||
if view.is_null() { | ||
return Err(PyBufferError::new_err("View is null")); | ||
} | ||
|
||
if (flags & ffi::PyBUF_WRITABLE) == ffi::PyBUF_WRITABLE { | ||
return Err(PyBufferError::new_err("Object is not writable")); | ||
} | ||
|
||
unsafe { | ||
(*view).obj = slf.as_ptr(); | ||
ffi::Py_INCREF((*view).obj); | ||
} | ||
|
||
let bytes = &slf.buf; | ||
|
||
unsafe { | ||
(*view).buf = bytes.as_ptr() as *mut c_void; | ||
(*view).len = bytes.len() as isize; | ||
(*view).readonly = 1; | ||
(*view).itemsize = std::mem::size_of::<u32>() as isize; | ||
|
||
let msg = CStr::from_bytes_with_nul(b"I\0").unwrap(); | ||
(*view).format = msg.as_ptr() as *mut _; | ||
|
||
(*view).ndim = 1; | ||
(*view).shape = (&((*view).len)) as *const _ as *mut _; | ||
|
||
(*view).strides = &((*view).itemsize) as *const _ as *mut _; | ||
|
||
(*view).suboffsets = ptr::null_mut(); | ||
(*view).internal = ptr::null_mut(); | ||
|
||
if let Some(err) = &slf.error { | ||
use TestGetBufferError::*; | ||
match err { | ||
NullShape => { | ||
(*view).shape = std::ptr::null_mut(); | ||
} | ||
NullStrides => { | ||
(*view).strides = std::ptr::null_mut(); | ||
} | ||
IncorrectItemSize => { | ||
(*view).itemsize += 1; | ||
} | ||
IncorrectFormat => { | ||
(*view).format = CStr::from_bytes_with_nul(b"B\0").unwrap().as_ptr() as _; | ||
} | ||
IncorrectAlignment => (*view).buf = (*view).buf.add(1), | ||
} | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn bf_releasebuffer(_slf: PyRefMut<Self>, _view: *mut ffi::Py_buffer) {} | ||
} | ||
|
||
#[test] | ||
fn test_get_buffer_errors() { | ||
Python::with_gil(|py| { | ||
let instance = Py::new( | ||
py, | ||
TestBufferErrors { | ||
buf: vec![0, 1, 2, 3], | ||
error: None, | ||
}, | ||
) | ||
.unwrap(); | ||
|
||
assert!(PyBuffer::<u32>::get(instance.as_ref(py)).is_ok()); | ||
|
||
instance.borrow_mut(py).error = Some(TestGetBufferError::NullShape); | ||
assert_eq!( | ||
PyBuffer::<u32>::get(instance.as_ref(py)) | ||
.unwrap_err() | ||
.to_string(), | ||
"BufferError: shape is null" | ||
); | ||
|
||
instance.borrow_mut(py).error = Some(TestGetBufferError::NullStrides); | ||
assert_eq!( | ||
PyBuffer::<u32>::get(instance.as_ref(py)) | ||
.unwrap_err() | ||
.to_string(), | ||
"BufferError: strides is null" | ||
); | ||
|
||
instance.borrow_mut(py).error = Some(TestGetBufferError::IncorrectItemSize); | ||
assert_eq!( | ||
PyBuffer::<u32>::get(instance.as_ref(py)) | ||
.unwrap_err() | ||
.to_string(), | ||
"BufferError: buffer contents are not compatible with u32" | ||
); | ||
|
||
instance.borrow_mut(py).error = Some(TestGetBufferError::IncorrectFormat); | ||
assert_eq!( | ||
PyBuffer::<u32>::get(instance.as_ref(py)) | ||
.unwrap_err() | ||
.to_string(), | ||
"BufferError: buffer contents are not compatible with u32" | ||
); | ||
|
||
instance.borrow_mut(py).error = Some(TestGetBufferError::IncorrectAlignment); | ||
assert_eq!( | ||
PyBuffer::<u32>::get(instance.as_ref(py)) | ||
.unwrap_err() | ||
.to_string(), | ||
"BufferError: buffer contents are insufficiently aligned for u32" | ||
); | ||
}); | ||
} |
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.
If you take this approach, then you can remove
Py_buffer::new()
.Though I'm a bit doubtful about how effective
MaybeUninit
is (I mean that this is effective only when we raise an error, but thenbuf
is immediately dropped...).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.
In the future I would like to remove
Py_buffer::new()
, but I think to do so right now would be inconvenient for users becausePyBufferProtocol
basically forces you to handle the raw ffi pointer (which probably points to uninitialized memory which C created).I chose
MaybeUninit
here because that's pretty much identical to what C would do: we create a slab of memory and pass its location toPyObject_GetBuffer
to initialize the contents.