Skip to main content

pyforge_ffi/
weakrefobject.rs

1use crate::object::*;
2use std::ffi::c_int;
3
4#[cfg(Py_LIMITED_API)]
5opaque_struct!(pub PyWeakReference);
6
7#[cfg(not(Py_LIMITED_API))]
8pub use crate::_PyWeakReference as PyWeakReference;
9
10extern_libpython! {
11    // TODO: PyForge is depending on this symbol in `reference.rs`, we should change this and
12    // remove the export as this is a private symbol.
13    pub static mut _PyWeakref_RefType: PyTypeObject;
14    static mut _PyWeakref_ProxyType: PyTypeObject;
15    static mut _PyWeakref_CallableProxyType: PyTypeObject;
16}
17
18#[inline]
19pub unsafe fn PyWeakref_CheckRef(op: *mut PyObject) -> c_int {
20    PyObject_TypeCheck(op, &raw mut _PyWeakref_RefType)
21}
22
23#[inline]
24pub unsafe fn PyWeakref_CheckRefExact(op: *mut PyObject) -> c_int {
25    (Py_TYPE(op) == &raw mut _PyWeakref_RefType) as c_int
26}
27
28#[inline]
29pub unsafe fn PyWeakref_CheckProxy(op: *mut PyObject) -> c_int {
30    ((Py_TYPE(op) == &raw mut _PyWeakref_ProxyType)
31        || (Py_TYPE(op) == &raw mut _PyWeakref_CallableProxyType)) as c_int
32}
33
34#[inline]
35pub unsafe fn PyWeakref_Check(op: *mut PyObject) -> c_int {
36    (PyWeakref_CheckRef(op) != 0 || PyWeakref_CheckProxy(op) != 0) as c_int
37}
38
39extern_libpython! {
40    pub fn PyWeakref_NewRef(ob: *mut PyObject, callback: *mut PyObject) -> *mut PyObject;
41    pub fn PyWeakref_NewProxy(ob: *mut PyObject, callback: *mut PyObject) -> *mut PyObject;
42    #[cfg_attr(
43        Py_3_13,
44        deprecated(note = "deprecated since Python 3.13. Use `PyWeakref_GetRef` instead.")
45    )]
46    pub fn PyWeakref_GetObject(reference: *mut PyObject) -> *mut PyObject;
47    #[cfg(Py_3_13)]
48    pub fn PyWeakref_GetRef(reference: *mut PyObject, pobj: *mut *mut PyObject) -> c_int;
49}