Skip to content

Commit

Permalink
Adjust tests to be compatible with py-clone feature
Browse files Browse the repository at this point in the history
  • Loading branch information
adamreichold committed Apr 21, 2024
1 parent 721212e commit f288495
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 6 deletions.
7 changes: 5 additions & 2 deletions guide/src/faq.md
Expand Up @@ -127,12 +127,10 @@ If you don't want that cloning to happen, a workaround is to allocate the field
```rust
# use pyo3::prelude::*;
#[pyclass]
#[derive(Clone)]
struct Inner {/* fields omitted */}

#[pyclass]
struct Outer {
#[pyo3(get)]
inner: Py<Inner>,
}

Expand All @@ -144,6 +142,11 @@ impl Outer {
inner: Py::new(py, Inner {})?,
})
}

#[getter]
fn inner(&self, py: Python<'_>) -> Py<Inner> {
self.inner.clone_ref(py)
}
}
```
This time `a` and `b` *are* the same object:
Expand Down
2 changes: 1 addition & 1 deletion src/conversions/std/option.rs
Expand Up @@ -61,7 +61,7 @@ mod tests {
assert_eq!(option.as_ptr(), std::ptr::null_mut());

let none = py.None();
option = Some(none.clone());
option = Some(none.clone_ref(py));

let ref_cnt = none.get_refcnt(py);
assert_eq!(option.as_ptr(), none.as_ptr());
Expand Down
2 changes: 2 additions & 0 deletions src/gil.rs
Expand Up @@ -724,6 +724,7 @@ mod tests {
assert!(!gil_is_acquired());
}

#[cfg(feature = "py-clone")]
#[test]
#[should_panic]
fn test_allow_threads_updates_refcounts() {
Expand All @@ -747,6 +748,7 @@ mod tests {
})
}

#[cfg(feature = "py-clone")]
#[test]
fn test_clone_with_gil() {
Python::with_gil(|py| {
Expand Down
4 changes: 4 additions & 0 deletions src/instance.rs
Expand Up @@ -863,15 +863,19 @@ impl<T> IntoPy<PyObject> for Borrowed<'_, '_, T> {
/// // All of these are valid syntax
/// let second = Py::clone_ref(&first, py);
/// let third = first.clone_ref(py);
/// #[cfg(feature = "py-clone")]
/// let fourth = Py::clone(&first);
/// #[cfg(feature = "py-clone")]
/// let fifth = first.clone();
///
/// // Disposing of our original `Py<PyDict>` just decrements the reference count.
/// drop(first);
///
/// // They all point to the same object
/// assert!(second.is(&third));
/// #[cfg(feature = "py-clone")]
/// assert!(fourth.is(&fifth));
/// #[cfg(feature = "py-clone")]
/// assert!(second.is(&fourth));
/// });
/// # }
Expand Down
3 changes: 3 additions & 0 deletions src/pybacked.rs
Expand Up @@ -336,6 +336,7 @@ mod test {
is_sync::<PyBackedBytes>();
}

#[cfg(feature = "py-clone")]
#[test]
fn test_backed_str_clone() {
Python::with_gil(|py| {
Expand Down Expand Up @@ -398,6 +399,7 @@ mod test {
})
}

#[cfg(feature = "py-clone")]
#[test]
fn test_backed_bytes_from_bytes_clone() {
Python::with_gil(|py| {
Expand All @@ -410,6 +412,7 @@ mod test {
});
}

#[cfg(feature = "py-clone")]
#[test]
fn test_backed_bytes_from_bytearray_clone() {
Python::with_gil(|py| {
Expand Down
2 changes: 1 addition & 1 deletion src/tests/hygiene/pyclass.rs
Expand Up @@ -25,7 +25,7 @@ pub struct Bar {
a: u8,
#[pyo3(get, set)]
b: Foo,
#[pyo3(get, set)]
#[pyo3(set)]
c: ::std::option::Option<crate::Py<Foo2>>,
}

Expand Down
2 changes: 1 addition & 1 deletion tests/test_buffer_protocol.rs
Expand Up @@ -122,7 +122,7 @@ fn test_releasebuffer_unraisable_error() {
let capture = UnraisableCapture::install(py);

let instance = Py::new(py, ReleaseBufferError {}).unwrap();
let env = [("ob", instance.clone())].into_py_dict_bound(py);
let env = [("ob", instance.clone_ref(py))].into_py_dict_bound(py);

assert!(capture.borrow(py).capture.is_none());

Expand Down
5 changes: 4 additions & 1 deletion tests/test_class_basics.rs
Expand Up @@ -172,6 +172,7 @@ fn empty_class_in_module() {
});
}

#[cfg(feature = "py-clone")]
#[pyclass]
struct ClassWithObjectField {
// It used to be that PyObject was not supported with (get, set)
Expand All @@ -180,6 +181,7 @@ struct ClassWithObjectField {
value: PyObject,
}

#[cfg(feature = "py-clone")]
#[pymethods]
impl ClassWithObjectField {
#[new]
Expand All @@ -188,6 +190,7 @@ impl ClassWithObjectField {
}
}

#[cfg(feature = "py-clone")]
#[test]
fn class_with_object_field() {
Python::with_gil(|py| {
Expand Down Expand Up @@ -242,7 +245,7 @@ fn test_unsendable<T: PyClass + 'static>() -> PyResult<()> {

assert!(!caught_panic);

Ok((obj.clone(), obj))
Ok((obj.clone_ref(py), obj))
})?;

let caught_panic = std::thread::spawn(move || {
Expand Down
4 changes: 4 additions & 0 deletions tests/test_class_conversion.rs
Expand Up @@ -54,12 +54,14 @@ impl SubClass {
}
}

#[cfg(feature = "py-clone")]
#[pyclass]
struct PolymorphicContainer {
#[pyo3(get, set)]
inner: Py<BaseClass>,
}

#[cfg(feature = "py-clone")]
#[test]
fn test_polymorphic_container_stores_base_class() {
Python::with_gil(|py| {
Expand All @@ -76,6 +78,7 @@ fn test_polymorphic_container_stores_base_class() {
});
}

#[cfg(feature = "py-clone")]
#[test]
fn test_polymorphic_container_stores_sub_class() {
Python::with_gil(|py| {
Expand Down Expand Up @@ -103,6 +106,7 @@ fn test_polymorphic_container_stores_sub_class() {
});
}

#[cfg(feature = "py-clone")]
#[test]
fn test_polymorphic_container_does_not_accept_other_types() {
Python::with_gil(|py| {
Expand Down
3 changes: 3 additions & 0 deletions tests/test_methods.rs
Expand Up @@ -877,6 +877,7 @@ fn test_from_sequence() {
});
}

#[cfg(feature = "py-clone")]
#[pyclass]
struct r#RawIdents {
#[pyo3(get, set)]
Expand All @@ -885,6 +886,7 @@ struct r#RawIdents {
r#subsubtype: PyObject,
}

#[cfg(feature = "py-clone")]
#[pymethods]
impl r#RawIdents {
#[new]
Expand Down Expand Up @@ -949,6 +951,7 @@ impl r#RawIdents {
}
}

#[cfg(feature = "py-clone")]
#[test]
fn test_raw_idents() {
Python::with_gil(|py| {
Expand Down
3 changes: 3 additions & 0 deletions tests/test_no_imports.rs
Expand Up @@ -139,12 +139,14 @@ fn test_basic() {
});
}

#[cfg(feature = "py-clone")]
#[pyo3::pyclass]
struct NewClassMethod {
#[pyo3(get)]
cls: pyo3::PyObject,
}

#[cfg(feature = "py-clone")]
#[pyo3::pymethods]
impl NewClassMethod {
#[new]
Expand All @@ -156,6 +158,7 @@ impl NewClassMethod {
}
}

#[cfg(feature = "py-clone")]
#[test]
fn test_new_class_method() {
pyo3::Python::with_gil(|py| {
Expand Down
3 changes: 3 additions & 0 deletions tests/test_sequence.rs
Expand Up @@ -247,12 +247,14 @@ fn test_inplace_repeat() {

// Check that #[pyo3(get, set)] works correctly for Vec<PyObject>

#[cfg(feature = "py-clone")]
#[pyclass]
struct GenericList {
#[pyo3(get, set)]
items: Vec<PyObject>,
}

#[cfg(feature = "py-clone")]
#[test]
fn test_generic_list_get() {
Python::with_gil(|py| {
Expand All @@ -265,6 +267,7 @@ fn test_generic_list_get() {
});
}

#[cfg(feature = "py-clone")]
#[test]
fn test_generic_list_set() {
Python::with_gil(|py| {
Expand Down

0 comments on commit f288495

Please sign in to comment.