2021-12-03 00:03:32 +00:00
|
|
|
#![cfg(feature = "macros")]
|
2022-02-06 11:54:35 +00:00
|
|
|
#![cfg(any(not(Py_LIMITED_API), Py_3_11))]
|
2021-03-31 21:37:41 +00:00
|
|
|
|
2023-07-30 20:58:21 +00:00
|
|
|
use pyo3::{buffer::PyBuffer, exceptions::PyBufferError, ffi, prelude::*};
|
2021-03-31 21:37:41 +00:00
|
|
|
use std::{
|
|
|
|
ffi::CStr,
|
|
|
|
os::raw::{c_int, c_void},
|
|
|
|
ptr,
|
|
|
|
};
|
|
|
|
|
|
|
|
#[macro_use]
|
2023-09-24 12:34:53 +00:00
|
|
|
#[path = "../src/tests/common.rs"]
|
2021-03-31 21:37:41 +00:00
|
|
|
mod common;
|
|
|
|
|
|
|
|
enum TestGetBufferError {
|
|
|
|
NullShape,
|
|
|
|
NullStrides,
|
|
|
|
IncorrectItemSize,
|
|
|
|
IncorrectFormat,
|
|
|
|
IncorrectAlignment,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pyclass]
|
|
|
|
struct TestBufferErrors {
|
|
|
|
buf: Vec<u32>,
|
|
|
|
error: Option<TestGetBufferError>,
|
|
|
|
}
|
|
|
|
|
2022-01-11 07:50:02 +00:00
|
|
|
#[pymethods]
|
|
|
|
impl TestBufferErrors {
|
|
|
|
unsafe fn __getbuffer__(
|
2022-03-23 07:07:28 +00:00
|
|
|
slf: PyRefMut<'_, Self>,
|
2022-01-11 07:50:02 +00:00
|
|
|
view: *mut ffi::Py_buffer,
|
|
|
|
flags: c_int,
|
|
|
|
) -> PyResult<()> {
|
2021-03-31 21:37:41 +00:00
|
|
|
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"));
|
|
|
|
}
|
|
|
|
|
|
|
|
let bytes = &slf.buf;
|
|
|
|
|
2022-01-11 07:50:02 +00:00
|
|
|
(*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 = &mut (*view).len;
|
|
|
|
|
|
|
|
(*view).strides = &mut (*view).itemsize;
|
|
|
|
|
|
|
|
(*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;
|
2021-03-31 21:37:41 +00:00
|
|
|
}
|
2022-01-11 07:50:02 +00:00
|
|
|
IncorrectFormat => {
|
|
|
|
(*view).format = CStr::from_bytes_with_nul(b"B\0").unwrap().as_ptr() as _;
|
|
|
|
}
|
|
|
|
IncorrectAlignment => (*view).buf = (*view).buf.add(1),
|
2021-03-31 21:37:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-27 11:27:36 +00:00
|
|
|
(*view).obj = slf.into_ptr();
|
|
|
|
|
2021-03-31 21:37:41 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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();
|
|
|
|
|
2024-02-14 22:10:59 +00:00
|
|
|
assert!(PyBuffer::<u32>::get_bound(instance.bind(py).as_any()).is_ok());
|
2021-03-31 21:37:41 +00:00
|
|
|
|
|
|
|
instance.borrow_mut(py).error = Some(TestGetBufferError::NullShape);
|
|
|
|
assert_eq!(
|
2024-02-14 22:10:59 +00:00
|
|
|
PyBuffer::<u32>::get_bound(instance.bind(py).as_any())
|
2021-03-31 21:37:41 +00:00
|
|
|
.unwrap_err()
|
|
|
|
.to_string(),
|
|
|
|
"BufferError: shape is null"
|
|
|
|
);
|
|
|
|
|
|
|
|
instance.borrow_mut(py).error = Some(TestGetBufferError::NullStrides);
|
|
|
|
assert_eq!(
|
2024-02-14 22:10:59 +00:00
|
|
|
PyBuffer::<u32>::get_bound(instance.bind(py).as_any())
|
2021-03-31 21:37:41 +00:00
|
|
|
.unwrap_err()
|
|
|
|
.to_string(),
|
|
|
|
"BufferError: strides is null"
|
|
|
|
);
|
|
|
|
|
|
|
|
instance.borrow_mut(py).error = Some(TestGetBufferError::IncorrectItemSize);
|
|
|
|
assert_eq!(
|
2024-02-14 22:10:59 +00:00
|
|
|
PyBuffer::<u32>::get_bound(instance.bind(py).as_any())
|
2021-03-31 21:37:41 +00:00
|
|
|
.unwrap_err()
|
|
|
|
.to_string(),
|
|
|
|
"BufferError: buffer contents are not compatible with u32"
|
|
|
|
);
|
|
|
|
|
|
|
|
instance.borrow_mut(py).error = Some(TestGetBufferError::IncorrectFormat);
|
|
|
|
assert_eq!(
|
2024-02-14 22:10:59 +00:00
|
|
|
PyBuffer::<u32>::get_bound(instance.bind(py).as_any())
|
2021-03-31 21:37:41 +00:00
|
|
|
.unwrap_err()
|
|
|
|
.to_string(),
|
|
|
|
"BufferError: buffer contents are not compatible with u32"
|
|
|
|
);
|
|
|
|
|
|
|
|
instance.borrow_mut(py).error = Some(TestGetBufferError::IncorrectAlignment);
|
|
|
|
assert_eq!(
|
2024-02-14 22:10:59 +00:00
|
|
|
PyBuffer::<u32>::get_bound(instance.bind(py).as_any())
|
2021-03-31 21:37:41 +00:00
|
|
|
.unwrap_err()
|
|
|
|
.to_string(),
|
|
|
|
"BufferError: buffer contents are insufficiently aligned for u32"
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|