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
|
|
|
|
2023-09-24 12:34:53 +00:00
|
|
|
#[path = "../src/tests/common.rs"]
|
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() {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
2024-03-12 22:57:03 +00:00
|
|
|
let f = wrap_pyfunction_bound!(bytes_pybytes_conversion)(py).unwrap();
|
2022-07-19 17:34:23 +00:00
|
|
|
py_assert!(py, f, "f(b'Hello World') == b'Hello World'");
|
|
|
|
});
|
2020-05-17 22:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[pyfunction]
|
2023-11-27 13:50:54 +00:00
|
|
|
fn bytes_vec_conversion(py: Python<'_>, bytes: Vec<u8>) -> Bound<'_, PyBytes> {
|
|
|
|
PyBytes::new_bound(py, bytes.as_slice())
|
2020-05-17 22:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_pybytes_vec_conversion() {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
2024-03-12 22:57:03 +00:00
|
|
|
let f = wrap_pyfunction_bound!(bytes_vec_conversion)(py).unwrap();
|
2022-07-19 17:34:23 +00:00
|
|
|
py_assert!(py, f, "f(b'Hello World') == b'Hello World'");
|
|
|
|
});
|
2020-05-17 22:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_bytearray_vec_conversion() {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
2024-03-12 22:57:03 +00:00
|
|
|
let f = wrap_pyfunction_bound!(bytes_vec_conversion)(py).unwrap();
|
2022-07-19 17:34:23 +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() {
|
2022-07-19 17:34:23 +00:00
|
|
|
let pyobj: pyo3::Py<pyo3::types::PyBytes> =
|
2023-11-27 13:50:54 +00:00
|
|
|
Python::with_gil(|py| pyo3::types::PyBytes::new_bound(py, b"abc").unbind());
|
2022-07-19 17:34:23 +00:00
|
|
|
|
|
|
|
let data = Python::with_gil(|py| pyobj.as_bytes(py));
|
2022-03-19 15:46:40 +00:00
|
|
|
|
|
|
|
assert_eq!(data, b"abc");
|
2024-05-11 14:48:45 +00:00
|
|
|
|
|
|
|
Python::with_gil(move |_py| drop(pyobj));
|
2022-03-19 15:46:40 +00:00
|
|
|
}
|