Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/5404.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Implement `PyTypeInfo` on `PyWeakrefReference` when using the stable ABI
50 changes: 33 additions & 17 deletions src/types/weakref/reference.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
use crate::err::PyResult;
use crate::ffi_ptr_ext::FfiPtrExt;
use crate::py_result_ext::PyResultExt;
#[cfg(any(PyPy, GraalPy, Py_LIMITED_API))]
use crate::sync::PyOnceLock;
use crate::types::any::PyAny;
use crate::{ffi, Borrowed, Bound, BoundObject, IntoPyObject, IntoPyObjectExt};

#[cfg(any(PyPy, GraalPy, Py_LIMITED_API))]
use crate::type_object::PyTypeCheck;
use crate::types::typeobject::PyTypeMethods;
#[cfg(any(PyPy, GraalPy, Py_LIMITED_API))]
use crate::types::PyType;
#[cfg(any(PyPy, GraalPy, Py_LIMITED_API))]
use crate::Py;
use crate::{ffi, Borrowed, Bound, BoundObject, IntoPyObject, IntoPyObjectExt};

use super::PyWeakrefMethods;

Expand All @@ -16,7 +21,7 @@ use super::PyWeakrefMethods;
pub struct PyWeakrefReference(PyAny);

#[cfg(not(any(PyPy, GraalPy, Py_LIMITED_API)))]
pyobject_subclassable_native_type!(PyWeakrefReference, crate::ffi::PyWeakReference);
pyobject_subclassable_native_type!(PyWeakrefReference, ffi::PyWeakReference);

#[cfg(not(any(PyPy, GraalPy, Py_LIMITED_API)))]
pyobject_native_type!(
Expand All @@ -30,18 +35,17 @@ pyobject_native_type!(

// When targeting alternative or multiple interpreters, it is better to not use the internal API.
#[cfg(any(PyPy, GraalPy, Py_LIMITED_API))]
pyobject_native_type_named!(PyWeakrefReference);

#[cfg(any(PyPy, GraalPy, Py_LIMITED_API))]
impl PyTypeCheck for PyWeakrefReference {
const NAME: &'static str = "weakref.ReferenceType";
#[cfg(feature = "experimental-inspect")]
const PYTHON_TYPE: &'static str = "weakref.ReferenceType";

fn type_check(object: &Bound<'_, PyAny>) -> bool {
unsafe { ffi::PyWeakref_CheckRef(object.as_ptr()) > 0 }
}
}
pyobject_native_type_core!(
PyWeakrefReference,
|py| {
static TYPE: PyOnceLock<Py<PyType>> = PyOnceLock::new();
TYPE.import(py, "weakref", "ref")
.unwrap()
.as_type_ptr()
},
#module=Some("weakref"),
#checkfunction=ffi::PyWeakref_CheckRef
);

impl PyWeakrefReference {
/// Constructs a new Weak Reference (`weakref.ref`/`weakref.ReferenceType`) for the given object.
Expand Down Expand Up @@ -238,7 +242,7 @@ mod tests {

mod python_class {
use super::*;
use crate::ffi;
use crate::{ffi, PyTypeInfo};
use crate::{py_result_ext::PyResultExt, types::PyType};
use std::ptr;

Expand Down Expand Up @@ -372,6 +376,18 @@ mod tests {
Ok(())
})
}

#[test]
fn test_type_object() -> PyResult<()> {
Python::attach(|py| {
let class = get_type(py)?;
let object = class.call0()?;
let reference = PyWeakrefReference::new(&object)?;

assert!(reference.is_instance(&PyWeakrefReference::type_object(py))?);
Ok(())
})
}
}

// under 'abi3-py37' and 'abi3-py38' PyClass cannot be weakreferencable.
Expand Down
Loading