2021-12-03 00:03:32 +00:00
|
|
|
#![cfg(feature = "macros")]
|
2020-09-15 12:21:49 +00:00
|
|
|
#![cfg(not(Py_LIMITED_API))]
|
|
|
|
|
2020-02-20 20:48:22 +00:00
|
|
|
use pyo3::buffer::PyBuffer;
|
2018-09-21 21:32:48 +00:00
|
|
|
use pyo3::class::PyBufferProtocol;
|
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;
|
2020-02-25 11:15:17 +00:00
|
|
|
use pyo3::AsPyPointer;
|
2019-03-24 16:19:15 +00:00
|
|
|
use std::ffi::CStr;
|
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
|
|
|
}
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyproto]
|
2020-02-20 20:48:22 +00:00
|
|
|
impl PyBufferProtocol for TestBufferClass {
|
2020-02-25 11:15:17 +00:00
|
|
|
fn bf_getbuffer(slf: PyRefMut<Self>, view: *mut ffi::Py_buffer, flags: c_int) -> PyResult<()> {
|
2017-07-18 14:32:31 +00:00
|
|
|
if view.is_null() {
|
2020-08-25 19:33:36 +00:00
|
|
|
return Err(PyBufferError::new_err("View is null"));
|
2017-05-14 19:52:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & ffi::PyBUF_WRITABLE) == ffi::PyBUF_WRITABLE {
|
2020-08-25 19:33:36 +00:00
|
|
|
return Err(PyBufferError::new_err("Object is not writable"));
|
2017-05-14 19:52:30 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 20:48:22 +00:00
|
|
|
unsafe {
|
2021-09-26 12:45:47 +00:00
|
|
|
(*view).obj = ffi::_Py_NewRef(slf.as_ptr());
|
2020-02-20 20:48:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let bytes = &slf.vec;
|
2017-05-14 19:52:30 +00:00
|
|
|
|
|
|
|
unsafe {
|
|
|
|
(*view).buf = bytes.as_ptr() as *mut c_void;
|
|
|
|
(*view).len = bytes.len() as isize;
|
|
|
|
(*view).readonly = 1;
|
|
|
|
(*view).itemsize = 1;
|
|
|
|
|
|
|
|
(*view).format = ptr::null_mut();
|
|
|
|
if (flags & ffi::PyBUF_FORMAT) == ffi::PyBUF_FORMAT {
|
2019-03-20 20:48:36 +00:00
|
|
|
let msg = CStr::from_bytes_with_nul(b"B\0").unwrap();
|
2017-05-14 19:52:30 +00:00
|
|
|
(*view).format = msg.as_ptr() as *mut _;
|
|
|
|
}
|
|
|
|
|
|
|
|
(*view).ndim = 1;
|
|
|
|
(*view).shape = ptr::null_mut();
|
|
|
|
if (flags & ffi::PyBUF_ND) == ffi::PyBUF_ND {
|
2021-12-20 20:36:46 +00:00
|
|
|
(*view).shape = &mut (*view).len;
|
2017-05-14 19:52:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
(*view).strides = ptr::null_mut();
|
|
|
|
if (flags & ffi::PyBUF_STRIDES) == ffi::PyBUF_STRIDES {
|
2021-12-20 20:36:46 +00:00
|
|
|
(*view).strides = &mut (*view).itemsize;
|
2017-05-14 19:52:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
(*view).suboffsets = ptr::null_mut();
|
|
|
|
(*view).internal = ptr::null_mut();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-02-20 20:48:22 +00:00
|
|
|
|
2020-06-23 10:40:17 +00:00
|
|
|
fn bf_releasebuffer(_slf: PyRefMut<Self>, _view: *mut ffi::Py_buffer) {}
|
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));
|
|
|
|
|
|
|
|
{
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
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);
|
2021-03-14 07:34:05 +00:00
|
|
|
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'];
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
let instance: PyObject = TestBufferClass {
|
|
|
|
vec: input.clone(),
|
|
|
|
drop_called: drop_called.clone(),
|
|
|
|
}
|
|
|
|
.into_py(py);
|
|
|
|
|
2020-06-04 13:00:47 +00:00
|
|
|
let buf = PyBuffer::<u8>::get(instance.as_ref(py)).unwrap();
|
|
|
|
assert_eq!(buf.to_vec(py).unwrap(), input);
|
2020-02-20 20:48:22 +00:00
|
|
|
drop(instance);
|
|
|
|
buf
|
|
|
|
};
|
|
|
|
|
|
|
|
assert!(!drop_called.load(Ordering::Relaxed));
|
|
|
|
|
|
|
|
{
|
|
|
|
let _py = Python::acquire_gil().python();
|
|
|
|
drop(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(drop_called.load(Ordering::Relaxed));
|
2017-05-14 19:52:30 +00:00
|
|
|
}
|