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))]
|
2020-09-15 12:21:49 +00:00
|
|
|
|
2020-02-20 20:48:22 +00:00
|
|
|
use pyo3::buffer::PyBuffer;
|
2020-07-04 15:55:26 +00:00
|
|
|
use pyo3::exceptions::PyBufferError;
|
2018-05-02 18:49:40 +00:00
|
|
|
use pyo3::ffi;
|
2018-06-15 19:21:12 +00:00
|
|
|
use pyo3::prelude::*;
|
2019-03-20 18:37:27 +00:00
|
|
|
use pyo3::types::IntoPyDict;
|
2021-12-21 07:01:11 +00:00
|
|
|
use std::ffi::CString;
|
2019-02-01 13:01:18 +00:00
|
|
|
use std::os::raw::{c_int, c_void};
|
|
|
|
use std::ptr;
|
2020-02-20 20:48:22 +00:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
use std::sync::Arc;
|
2017-05-14 19:52:30 +00:00
|
|
|
|
2021-03-14 07:34:05 +00:00
|
|
|
mod common;
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyclass]
|
2020-02-20 20:48:22 +00:00
|
|
|
struct TestBufferClass {
|
2017-05-20 06:18:54 +00:00
|
|
|
vec: Vec<u8>,
|
2020-02-20 20:48:22 +00:00
|
|
|
drop_called: Arc<AtomicBool>,
|
2017-05-20 06:18:54 +00:00
|
|
|
}
|
|
|
|
|
2021-12-21 07:01:11 +00:00
|
|
|
#[pymethods]
|
|
|
|
impl TestBufferClass {
|
|
|
|
unsafe fn __getbuffer__(
|
2023-01-19 07:45:05 +00:00
|
|
|
slf: &PyCell<Self>,
|
2021-12-21 07:01:11 +00:00
|
|
|
view: *mut ffi::Py_buffer,
|
|
|
|
flags: c_int,
|
|
|
|
) -> PyResult<()> {
|
2023-01-19 07:45:05 +00:00
|
|
|
fill_view_from_readonly_data(view, flags, &slf.borrow().vec, slf)
|
2017-05-14 19:52:30 +00:00
|
|
|
}
|
2020-02-20 20:48:22 +00:00
|
|
|
|
2021-12-21 07:01:11 +00:00
|
|
|
unsafe fn __releasebuffer__(&self, view: *mut ffi::Py_buffer) {
|
|
|
|
// Release memory held by the format string
|
|
|
|
drop(CString::from_raw((*view).format));
|
|
|
|
}
|
2020-02-20 20:48:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for TestBufferClass {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
print!("dropped");
|
|
|
|
self.drop_called.store(true, Ordering::Relaxed);
|
|
|
|
}
|
2017-05-14 19:52:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_buffer() {
|
2020-02-20 20:48:22 +00:00
|
|
|
let drop_called = Arc::new(AtomicBool::new(false));
|
|
|
|
|
2023-01-19 07:45:05 +00:00
|
|
|
Python::with_gil(|py| {
|
|
|
|
let instance = Py::new(
|
|
|
|
py,
|
|
|
|
TestBufferClass {
|
|
|
|
vec: vec![b' ', b'2', b'3'],
|
|
|
|
drop_called: drop_called.clone(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let env = [("ob", instance)].into_py_dict(py);
|
|
|
|
py_assert!(py, *env, "bytes(ob) == b' 23'");
|
|
|
|
});
|
2020-02-20 20:48:22 +00:00
|
|
|
|
|
|
|
assert!(drop_called.load(Ordering::Relaxed));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_buffer_referenced() {
|
|
|
|
let drop_called = Arc::new(AtomicBool::new(false));
|
|
|
|
|
|
|
|
let buf = {
|
|
|
|
let input = vec![b' ', b'2', b'3'];
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
|
|
|
let instance: PyObject = TestBufferClass {
|
|
|
|
vec: input.clone(),
|
|
|
|
drop_called: drop_called.clone(),
|
|
|
|
}
|
|
|
|
.into_py(py);
|
|
|
|
|
|
|
|
let buf = PyBuffer::<u8>::get(instance.as_ref(py)).unwrap();
|
|
|
|
assert_eq!(buf.to_vec(py).unwrap(), input);
|
|
|
|
drop(instance);
|
|
|
|
buf
|
|
|
|
})
|
2020-02-20 20:48:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
assert!(!drop_called.load(Ordering::Relaxed));
|
|
|
|
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|_| {
|
2020-02-20 20:48:22 +00:00
|
|
|
drop(buf);
|
2022-07-19 17:34:23 +00:00
|
|
|
});
|
2020-02-20 20:48:22 +00:00
|
|
|
|
|
|
|
assert!(drop_called.load(Ordering::Relaxed));
|
2017-05-14 19:52:30 +00:00
|
|
|
}
|
2023-01-19 07:45:05 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(Py_3_8)] // sys.unraisablehook not available until Python 3.8
|
|
|
|
fn test_releasebuffer_unraisable_error() {
|
2023-05-24 09:04:05 +00:00
|
|
|
use common::UnraisableCapture;
|
2023-01-19 07:45:05 +00:00
|
|
|
use pyo3::exceptions::PyValueError;
|
|
|
|
|
|
|
|
#[pyclass]
|
|
|
|
struct ReleaseBufferError {}
|
|
|
|
|
|
|
|
#[pymethods]
|
|
|
|
impl ReleaseBufferError {
|
|
|
|
unsafe fn __getbuffer__(
|
|
|
|
slf: &PyCell<Self>,
|
|
|
|
view: *mut ffi::Py_buffer,
|
|
|
|
flags: c_int,
|
|
|
|
) -> PyResult<()> {
|
|
|
|
static BUF_BYTES: &[u8] = b"hello world";
|
|
|
|
fill_view_from_readonly_data(view, flags, BUF_BYTES, slf)
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn __releasebuffer__(&self, _view: *mut ffi::Py_buffer) -> PyResult<()> {
|
|
|
|
Err(PyValueError::new_err("oh dear"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Python::with_gil(|py| {
|
2023-05-24 09:04:05 +00:00
|
|
|
let capture = UnraisableCapture::install(py);
|
2023-01-19 07:45:05 +00:00
|
|
|
|
|
|
|
let instance = Py::new(py, ReleaseBufferError {}).unwrap();
|
|
|
|
let env = [("ob", instance.clone())].into_py_dict(py);
|
|
|
|
|
|
|
|
assert!(capture.borrow(py).capture.is_none());
|
|
|
|
|
|
|
|
py_assert!(py, *env, "bytes(ob) == b'hello world'");
|
|
|
|
|
|
|
|
let (err, object) = capture.borrow_mut(py).capture.take().unwrap();
|
|
|
|
assert_eq!(err.to_string(), "ValueError: oh dear");
|
|
|
|
assert!(object.is(&instance));
|
|
|
|
|
2023-05-24 09:04:05 +00:00
|
|
|
capture.borrow_mut(py).uninstall(py);
|
2023-01-19 07:45:05 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// `view` must be a valid pointer to ffi::Py_buffer, or null
|
|
|
|
/// `data` must outlive the Python lifetime of `owner` (i.e. data must be owned by owner, or data
|
|
|
|
/// must be static data)
|
|
|
|
unsafe fn fill_view_from_readonly_data(
|
|
|
|
view: *mut ffi::Py_buffer,
|
|
|
|
flags: c_int,
|
|
|
|
data: &[u8],
|
|
|
|
owner: &PyAny,
|
|
|
|
) -> 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"));
|
|
|
|
}
|
|
|
|
|
|
|
|
(*view).obj = ffi::_Py_NewRef(owner.as_ptr());
|
|
|
|
|
|
|
|
(*view).buf = data.as_ptr() as *mut c_void;
|
|
|
|
(*view).len = data.len() as isize;
|
|
|
|
(*view).readonly = 1;
|
|
|
|
(*view).itemsize = 1;
|
|
|
|
|
|
|
|
(*view).format = if (flags & ffi::PyBUF_FORMAT) == ffi::PyBUF_FORMAT {
|
|
|
|
let msg = CString::new("B").unwrap();
|
|
|
|
msg.into_raw()
|
|
|
|
} else {
|
|
|
|
ptr::null_mut()
|
|
|
|
};
|
|
|
|
|
|
|
|
(*view).ndim = 1;
|
|
|
|
(*view).shape = if (flags & ffi::PyBUF_ND) == ffi::PyBUF_ND {
|
|
|
|
&mut (*view).len
|
|
|
|
} else {
|
|
|
|
ptr::null_mut()
|
|
|
|
};
|
|
|
|
|
|
|
|
(*view).strides = if (flags & ffi::PyBUF_STRIDES) == ffi::PyBUF_STRIDES {
|
|
|
|
&mut (*view).itemsize
|
|
|
|
} else {
|
|
|
|
ptr::null_mut()
|
|
|
|
};
|
|
|
|
|
|
|
|
(*view).suboffsets = ptr::null_mut();
|
|
|
|
(*view).internal = ptr::null_mut();
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|