Skip to content

Commit

Permalink
Add test cases for new TryFrom impls
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Oct 15, 2023
1 parent d4ed66f commit f4e64aa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
4 changes: 2 additions & 2 deletions pytests/src/buf_and_str.rs
Expand Up @@ -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)
}

Expand Down
13 changes: 12 additions & 1 deletion src/types/bytearray.rs
Expand Up @@ -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]
Expand Down Expand Up @@ -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| {
Expand Down

0 comments on commit f4e64aa

Please sign in to comment.