2018-08-19 18:42:17 +00:00
|
|
|
#![feature(specialization)]
|
2017-05-14 19:52:30 +00:00
|
|
|
|
2018-09-21 21:32:48 +00:00
|
|
|
use pyo3::class::PyBufferProtocol;
|
|
|
|
use pyo3::exceptions::BufferError;
|
2018-05-02 18:49:40 +00:00
|
|
|
use pyo3::ffi;
|
2018-06-15 19:21:12 +00:00
|
|
|
use pyo3::prelude::*;
|
2018-09-21 21:32:48 +00:00
|
|
|
use pyo3::types::PyDict;
|
2019-02-01 13:01:18 +00:00
|
|
|
use std::os::raw::{c_int, c_void};
|
|
|
|
use std::ptr;
|
2017-05-14 19:52:30 +00:00
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyclass]
|
2017-05-20 06:18:54 +00:00
|
|
|
struct TestClass {
|
|
|
|
vec: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyproto]
|
2018-05-02 18:49:40 +00:00
|
|
|
impl PyBufferProtocol for TestClass {
|
2017-06-22 19:32:01 +00:00
|
|
|
fn bf_getbuffer(&self, view: *mut ffi::Py_buffer, flags: c_int) -> PyResult<()> {
|
2017-07-18 14:32:31 +00:00
|
|
|
if view.is_null() {
|
2018-08-21 21:51:13 +00:00
|
|
|
return Err(BufferError::py_err("View is null"));
|
2017-05-14 19:52:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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"));
|
2017-05-14 19:52:30 +00:00
|
|
|
}
|
|
|
|
|
2017-05-29 09:47:27 +00:00
|
|
|
let bytes = &self.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 {
|
|
|
|
let msg = ::std::ffi::CStr::from_ptr("B\0".as_ptr() as *const _);
|
|
|
|
(*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(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-12 05:23:49 +00:00
|
|
|
#[cfg(Py_3)]
|
2017-05-14 19:52:30 +00:00
|
|
|
#[test]
|
|
|
|
fn test_buffer() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2018-07-30 21:01:46 +00:00
|
|
|
let t = py
|
2018-11-12 13:15:11 +00:00
|
|
|
.init(|| TestClass {
|
2018-06-15 19:21:12 +00:00
|
|
|
vec: vec![b' ', b'2', b'3'],
|
2018-09-28 21:34:57 +00:00
|
|
|
})
|
|
|
|
.unwrap();
|
2017-05-14 19:52:30 +00:00
|
|
|
|
|
|
|
let d = PyDict::new(py);
|
2017-06-21 21:08:16 +00:00
|
|
|
d.set_item("ob", t).unwrap();
|
|
|
|
py.run("assert bytes(ob) == b' 23'", None, Some(d)).unwrap();
|
2017-05-14 19:52:30 +00:00
|
|
|
}
|
2017-06-12 05:23:49 +00:00
|
|
|
|
|
|
|
#[cfg(not(Py_3))]
|
|
|
|
#[test]
|
|
|
|
fn test_buffer() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2018-07-30 21:01:46 +00:00
|
|
|
let t = py
|
2018-11-12 13:15:11 +00:00
|
|
|
.init(|| TestClass {
|
2018-06-15 19:21:12 +00:00
|
|
|
vec: vec![b' ', b'2', b'3'],
|
2018-09-28 21:34:57 +00:00
|
|
|
})
|
|
|
|
.unwrap();
|
2017-06-12 05:23:49 +00:00
|
|
|
|
|
|
|
let d = PyDict::new(py);
|
2017-06-22 17:26:07 +00:00
|
|
|
d.set_item("ob", t).unwrap();
|
2018-06-15 19:21:12 +00:00
|
|
|
py.run("assert memoryview(ob).tobytes() == ' 23'", None, Some(d))
|
|
|
|
.unwrap();
|
2017-06-12 05:23:49 +00:00
|
|
|
}
|