-
Notifications
You must be signed in to change notification settings - Fork 672
/
test_bytes.rs
58 lines (45 loc) · 1.31 KB
/
test_bytes.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#![cfg(feature = "macros")]
use pyo3::prelude::*;
use pyo3::types::PyBytes;
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();
let f = wrap_pyfunction!(bytes_pybytes_conversion)(py).unwrap();
py_assert!(py, f, "f(b'Hello World') == b'Hello World'");
}
#[pyfunction]
fn bytes_vec_conversion(py: Python, bytes: Vec<u8>) -> &PyBytes {
PyBytes::new(py, bytes.as_slice())
}
#[test]
fn test_pybytes_vec_conversion() {
let gil = Python::acquire_gil();
let py = gil.python();
let f = wrap_pyfunction!(bytes_vec_conversion)(py).unwrap();
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();
let f = wrap_pyfunction!(bytes_vec_conversion)(py).unwrap();
py_assert!(py, f, "f(bytearray(b'Hello World')) == b'Hello World'");
}
#[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");
}