Skip to content

Commit

Permalink
port PyAny tests to Bound API (#4140)
Browse files Browse the repository at this point in the history
  • Loading branch information
Icxolu committed Apr 30, 2024
1 parent 4616838 commit 82c00a2
Showing 1 changed file with 36 additions and 39 deletions.
75 changes: 36 additions & 39 deletions src/types/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2324,12 +2324,11 @@ impl<'py> Bound<'py, PyAny> {
}

#[cfg(test)]
#[cfg_attr(not(feature = "gil-refs"), allow(deprecated))]
mod tests {
use crate::{
basic::CompareOp,
types::{any::PyAnyMethods, IntoPyDict, PyAny, PyBool, PyList, PyLong, PyModule},
Bound, PyNativeType, PyTypeInfo, Python, ToPyObject,
types::{IntoPyDict, PyAny, PyAnyMethods, PyBool, PyList, PyLong, PyModule, PyTypeMethods},
Bound, PyTypeInfo, Python, ToPyObject,
};

#[test]
Expand Down Expand Up @@ -2407,7 +2406,7 @@ class NonHeapNonDescriptorInt:
#[test]
fn test_call_for_non_existing_method() {
Python::with_gil(|py| {
let a = py.eval("42", None, None).unwrap();
let a = py.eval_bound("42", None, None).unwrap();
a.call_method0("__str__").unwrap(); // ok
assert!(a.call_method("nonexistent_method", (1,), None).is_err());
assert!(a.call_method0("nonexistent_method").is_err());
Expand Down Expand Up @@ -2455,19 +2454,19 @@ class SimpleClass:
#[test]
fn test_type() {
Python::with_gil(|py| {
let obj = py.eval("42", None, None).unwrap();
let obj = py.eval_bound("42", None, None).unwrap();
assert_eq!(obj.get_type().as_type_ptr(), obj.get_type_ptr());
});
}

#[test]
fn test_dir() {
Python::with_gil(|py| {
let obj = py.eval("42", None, None).unwrap();
let obj = py.eval_bound("42", None, None).unwrap();
let dir = py
.eval("dir(42)", None, None)
.eval_bound("dir(42)", None, None)
.unwrap()
.downcast::<PyList>()
.downcast_into::<PyList>()
.unwrap();
let a = obj
.dir()
Expand All @@ -2482,7 +2481,7 @@ class SimpleClass:
#[test]
fn test_hasattr() {
Python::with_gil(|py| {
let x = 5.to_object(py).into_ref(py);
let x = 5.to_object(py).into_bound(py);
assert!(x.is_instance_of::<PyLong>());

assert!(x.hasattr("to_bytes").unwrap());
Expand All @@ -2509,7 +2508,7 @@ class SimpleClass:

Python::with_gil(|py| {
let obj = Py::new(py, GetattrFail).unwrap();
let obj = obj.as_ref(py).as_ref();
let obj = obj.bind(py).as_ref();

assert!(obj
.hasattr("foo")
Expand All @@ -2521,42 +2520,42 @@ class SimpleClass:
#[test]
fn test_nan_eq() {
Python::with_gil(|py| {
let nan = py.eval("float('nan')", None, None).unwrap();
assert!(nan.compare(nan).is_err());
let nan = py.eval_bound("float('nan')", None, None).unwrap();
assert!(nan.compare(&nan).is_err());
});
}

#[test]
fn test_any_is_instance_of() {
Python::with_gil(|py| {
let x = 5.to_object(py).into_ref(py);
let x = 5.to_object(py).into_bound(py);
assert!(x.is_instance_of::<PyLong>());

let l = vec![x, x].to_object(py).into_ref(py);
let l = vec![&x, &x].to_object(py).into_bound(py);
assert!(l.is_instance_of::<PyList>());
});
}

#[test]
fn test_any_is_instance() {
Python::with_gil(|py| {
let l = vec![1u8, 2].to_object(py).into_ref(py);
assert!(l.is_instance(py.get_type::<PyList>()).unwrap());
let l = vec![1u8, 2].to_object(py).into_bound(py);
assert!(l.is_instance(&py.get_type_bound::<PyList>()).unwrap());
});
}

#[test]
fn test_any_is_exact_instance_of() {
Python::with_gil(|py| {
let x = 5.to_object(py).into_ref(py);
let x = 5.to_object(py).into_bound(py);
assert!(x.is_exact_instance_of::<PyLong>());

let t = PyBool::new_bound(py, true);
assert!(t.is_instance_of::<PyLong>());
assert!(!t.is_exact_instance_of::<PyLong>());
assert!(t.is_exact_instance_of::<PyBool>());

let l = vec![x, x].to_object(py).into_ref(py);
let l = vec![&x, &x].to_object(py).into_bound(py);
assert!(l.is_exact_instance_of::<PyList>());
});
}
Expand All @@ -2565,19 +2564,17 @@ class SimpleClass:
fn test_any_is_exact_instance() {
Python::with_gil(|py| {
let t = PyBool::new_bound(py, true);
assert!(t
.is_instance(&py.get_type::<PyLong>().as_borrowed())
.unwrap());
assert!(!t.is_exact_instance(&py.get_type::<PyLong>().as_borrowed()));
assert!(t.is_exact_instance(&py.get_type::<PyBool>().as_borrowed()));
assert!(t.is_instance(&py.get_type_bound::<PyLong>()).unwrap());
assert!(!t.is_exact_instance(&py.get_type_bound::<PyLong>()));
assert!(t.is_exact_instance(&py.get_type_bound::<PyBool>()));
});
}

#[test]
fn test_any_contains() {
Python::with_gil(|py| {
let v: Vec<i32> = vec![1, 1, 2, 3, 5, 8];
let ob = v.to_object(py).into_ref(py);
let ob = v.to_object(py).into_bound(py);

let bad_needle = 7i32.to_object(py);
assert!(!ob.contains(&bad_needle).unwrap());
Expand All @@ -2589,7 +2586,7 @@ class SimpleClass:
assert!(ob.contains(&type_coerced_needle).unwrap());

let n: u32 = 42;
let bad_haystack = n.to_object(py).into_ref(py);
let bad_haystack = n.to_object(py).into_bound(py);
let irrelevant_needle = 0i32.to_object(py);
assert!(bad_haystack.contains(&irrelevant_needle).is_err());
});
Expand All @@ -2603,52 +2600,52 @@ class SimpleClass:
Python::with_gil(|py| {
for a in list {
for b in list {
let a_py = a.to_object(py).into_ref(py);
let b_py = b.to_object(py).into_ref(py);
let a_py = a.to_object(py).into_bound(py);
let b_py = b.to_object(py).into_bound(py);

assert_eq!(
a.lt(b),
a_py.lt(b_py).unwrap(),
a_py.lt(&b_py).unwrap(),
"{} < {} should be {}.",
a_py,
b_py,
a.lt(b)
);
assert_eq!(
a.le(b),
a_py.le(b_py).unwrap(),
a_py.le(&b_py).unwrap(),
"{} <= {} should be {}.",
a_py,
b_py,
a.le(b)
);
assert_eq!(
a.eq(b),
a_py.eq(b_py).unwrap(),
a_py.eq(&b_py).unwrap(),
"{} == {} should be {}.",
a_py,
b_py,
a.eq(b)
);
assert_eq!(
a.ne(b),
a_py.ne(b_py).unwrap(),
a_py.ne(&b_py).unwrap(),
"{} != {} should be {}.",
a_py,
b_py,
a.ne(b)
);
assert_eq!(
a.gt(b),
a_py.gt(b_py).unwrap(),
a_py.gt(&b_py).unwrap(),
"{} > {} should be {}.",
a_py,
b_py,
a.gt(b)
);
assert_eq!(
a.ge(b),
a_py.ge(b_py).unwrap(),
a_py.ge(&b_py).unwrap(),
"{} >= {} should be {}.",
a_py,
b_py,
Expand Down Expand Up @@ -2695,10 +2692,10 @@ class SimpleClass:
#[test]
fn test_rich_compare_type_error() {
Python::with_gil(|py| {
let py_int = 1.to_object(py).into_ref(py);
let py_str = "1".to_object(py).into_ref(py);
let py_int = 1.to_object(py).into_bound(py);
let py_str = "1".to_object(py).into_bound(py);

assert!(py_int.rich_compare(py_str, CompareOp::Lt).is_err());
assert!(py_int.rich_compare(&py_str, CompareOp::Lt).is_err());
assert!(!py_int
.rich_compare(py_str, CompareOp::Eq)
.unwrap()
Expand Down Expand Up @@ -2736,13 +2733,13 @@ class SimpleClass:
#[test]
fn test_is_empty() {
Python::with_gil(|py| {
let empty_list: &PyAny = PyList::empty(py);
let empty_list = PyList::empty_bound(py).into_any();
assert!(empty_list.is_empty().unwrap());

let list: &PyAny = PyList::new(py, vec![1, 2, 3]);
let list = PyList::new_bound(py, vec![1, 2, 3]).into_any();
assert!(!list.is_empty().unwrap());

let not_container = 5.to_object(py).into_ref(py);
let not_container = 5.to_object(py).into_bound(py);
assert!(not_container.is_empty().is_err());
});
}
Expand Down

0 comments on commit 82c00a2

Please sign in to comment.