2021-12-03 00:03:32 +00:00
|
|
|
#![cfg(feature = "macros")]
|
|
|
|
|
2019-10-20 11:01:32 +00:00
|
|
|
use pyo3::prelude::*;
|
2020-05-17 22:13:56 +00:00
|
|
|
use pyo3::types::PyBytes;
|
2019-10-20 11:01:32 +00:00
|
|
|
|
|
|
|
mod common;
|
|
|
|
|
|
|
|
#[pyfunction]
|
|
|
|
fn bytes_pybytes_conversion(bytes: &[u8]) -> &[u8] {
|
|
|
|
bytes
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_pybytes_bytes_conversion() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2020-09-03 13:48:32 +00:00
|
|
|
let f = wrap_pyfunction!(bytes_pybytes_conversion)(py).unwrap();
|
2020-05-17 22:13:56 +00:00
|
|
|
py_assert!(py, f, "f(b'Hello World') == b'Hello World'");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pyfunction]
|
2022-03-23 07:07:28 +00:00
|
|
|
fn bytes_vec_conversion(py: Python<'_>, bytes: Vec<u8>) -> &PyBytes {
|
2020-05-17 22:13:56 +00:00
|
|
|
PyBytes::new(py, bytes.as_slice())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_pybytes_vec_conversion() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2020-09-03 13:48:32 +00:00
|
|
|
let f = wrap_pyfunction!(bytes_vec_conversion)(py).unwrap();
|
2020-05-17 22:13:56 +00:00
|
|
|
py_assert!(py, f, "f(b'Hello World') == b'Hello World'");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_bytearray_vec_conversion() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2020-09-03 13:48:32 +00:00
|
|
|
let f = wrap_pyfunction!(bytes_vec_conversion)(py).unwrap();
|
2020-05-17 22:13:56 +00:00
|
|
|
py_assert!(py, f, "f(bytearray(b'Hello World')) == b'Hello World'");
|
2019-10-20 11:01:32 +00:00
|
|
|
}
|
2022-03-19 15:46:40 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_py_as_bytes() {
|
|
|
|
let pyobj: pyo3::Py<pyo3::types::PyBytes>;
|
|
|
|
let data: &[u8];
|
|
|
|
|
|
|
|
{
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
pyobj = pyo3::types::PyBytes::new(py, b"abc").into_py(py);
|
|
|
|
data = pyobj.as_bytes(py);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(data, b"abc");
|
|
|
|
}
|