Add test cases for new TryFrom impls

This commit is contained in:
messense 2023-10-15 21:53:31 +08:00
parent d4ed66fff0
commit f4e64aadef
No known key found for this signature in database
GPG key ID: BB41A8A2C716CCA9
2 changed files with 14 additions and 3 deletions

View file

@ -43,8 +43,8 @@ impl BytesExtractor {
#[pyfunction]
fn return_memoryview(py: Python<'_>) -> PyResult<&PyMemoryView> {
let bytes = PyBytes::new(py, b"hello world");
let memoryview = PyMemoryView::from(bytes)?;
let bytes: &PyAny = PyBytes::new(py, b"hello world").into();
let memoryview = TryInto::try_into(bytes)?;
Ok(memoryview)
}

View file

@ -253,8 +253,8 @@ impl<'py> TryFrom<&'py PyAny> for &'py PyByteArray {
#[cfg(test)]
mod tests {
use crate::exceptions;
use crate::types::PyByteArray;
use crate::{exceptions, PyAny};
use crate::{PyObject, Python};
#[test]
@ -332,6 +332,17 @@ mod tests {
});
}
#[test]
fn test_try_from() {
Python::with_gil(|py| {
let src = b"Hello Python";
let bytearray: &PyAny = PyByteArray::new(py, src).into();
let bytearray: &PyByteArray = TryInto::try_into(bytearray).unwrap();
assert_eq!(src, unsafe { bytearray.as_bytes() });
});
}
#[test]
fn test_resize() {
Python::with_gil(|py| {