Skip to content

Commit

Permalink
Change PyWeakrefReference to only use type pointer when it exists
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperJappie08 committed Apr 18, 2024
1 parent 97d73e9 commit c7ab375
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion newsfragments/3835.added.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Add `PyWeakRef`, `PyWeakProxy` and `PyWeakCallableProxy`.
Add `PyWeakref`, `PyWeakrefReference` and `PyWeakrefProxy`.
1 change: 0 additions & 1 deletion src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,4 @@ pub use crate::types::string::PyStringMethods;
pub use crate::types::traceback::PyTracebackMethods;
pub use crate::types::tuple::PyTupleMethods;
pub use crate::types::typeobject::PyTypeMethods;
#[cfg(not(PyPy))]
pub use crate::types::weakref::PyWeakrefMethods;
2 changes: 0 additions & 2 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ pub use self::string::{PyString, PyString as PyUnicode, PyStringMethods};
pub use self::traceback::{PyTraceback, PyTracebackMethods};
pub use self::tuple::{PyTuple, PyTupleMethods};
pub use self::typeobject::{PyType, PyTypeMethods};
#[cfg(not(PyPy))]
pub use self::weakref::{PyWeakref, PyWeakrefMethods, PyWeakrefProxy, PyWeakrefReference};

/// Iteration over Python collections.
Expand Down Expand Up @@ -352,5 +351,4 @@ pub(crate) mod string;
pub(crate) mod traceback;
pub(crate) mod tuple;
pub(crate) mod typeobject;
#[cfg(not(any(PyPy, GraalPy)))] // FIXME: Remove this soon
pub(crate) mod weakref;
16 changes: 16 additions & 0 deletions src/types/weakref/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use super::PyWeakrefMethods;
#[repr(transparent)]
pub struct PyWeakrefReference(PyAny);

#[cfg(not(any(PyPy, GraalPy, Py_LIMITED_API)))]
pyobject_native_type!(
PyWeakrefReference,
ffi::PyWeakReference,
Expand All @@ -21,6 +22,21 @@ pyobject_native_type!(
#checkfunction=ffi::PyWeakref_CheckRefExact
);

// When targetting 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))]
pyobject_native_type_extract!(PyWeakrefReference);

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

fn type_check(object: &Bound<'_, PyAny>) -> bool {
unsafe { ffi::PyWeakref_CheckRef(object.as_ptr()) > 0 }
}
}

impl PyWeakrefReference {
/// Deprecated form of [`PyWeakrefReference::new_bound`].
#[inline]
Expand Down

0 comments on commit c7ab375

Please sign in to comment.