pyo3/tests/test_buffer_protocol.rs

79 lines
2 KiB
Rust
Raw Normal View History

2018-09-21 21:32:48 +00:00
use pyo3::class::PyBufferProtocol;
use pyo3::exceptions::BufferError;
use pyo3::ffi;
use pyo3::prelude::*;
use pyo3::types::IntoPyDict;
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;
#[pyclass]
2017-05-20 06:18:54 +00:00
struct TestClass {
vec: Vec<u8>,
}
#[pyproto]
impl PyBufferProtocol for TestClass {
fn bf_getbuffer(&self, view: *mut ffi::Py_buffer, flags: c_int) -> PyResult<()> {
if view.is_null() {
2018-08-21 21:51:13 +00:00
return Err(BufferError::py_err("View is null"));
}
unsafe {
(*view).obj = ptr::null_mut();
}
if (flags & ffi::PyBUF_WRITABLE) == ffi::PyBUF_WRITABLE {
2018-08-21 21:51:13 +00:00
return Err(BufferError::py_err("Object is not writable"));
}
let bytes = &self.vec;
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();
(*view).format = msg.as_ptr() as *mut _;
}
(*view).ndim = 1;
(*view).shape = ptr::null_mut();
if (flags & ffi::PyBUF_ND) == ffi::PyBUF_ND {
(*view).shape = (&((*view).len)) as *const _ as *mut _;
}
(*view).strides = ptr::null_mut();
if (flags & ffi::PyBUF_STRIDES) == ffi::PyBUF_STRIDES {
(*view).strides = &((*view).itemsize) as *const _ as *mut _;
}
(*view).suboffsets = ptr::null_mut();
(*view).internal = ptr::null_mut();
}
Ok(())
}
}
#[test]
fn test_buffer() {
let gil = Python::acquire_gil();
let py = gil.python();
let t = Py::new(
py,
TestClass {
vec: vec![b' ', b'2', b'3'],
},
)
.unwrap();
let d = [("ob", t)].into_py_dict(py);
2017-06-21 21:08:16 +00:00
py.run("assert bytes(ob) == b' 23'", None, Some(d)).unwrap();
}