Skip to main content

pyo3_ffi/
weakrefobject.rs

1use crate::object::*;
2use core::ffi::c_int;
3
4#[cfg(all(not(PyPy), Py_LIMITED_API, not(GraalPy)))]
5opaque_struct!(pub PyWeakReference);
6
7#[cfg(all(not(PyPy), not(Py_LIMITED_API), not(GraalPy)))]
8pub use crate::_PyWeakReference as PyWeakReference;
9
10extern_libpython! {
11    // TODO: PyO3 is depending on this symbol in `reference.rs`, we should change this and
12    // remove the export as this is a private symbol.
13    #[cfg(not(RustPython))]
14    pub static mut _PyWeakref_RefType: PyTypeObject;
15    #[cfg(not(RustPython))]
16    static mut _PyWeakref_ProxyType: PyTypeObject;
17    #[cfg(not(RustPython))]
18    static mut _PyWeakref_CallableProxyType: PyTypeObject;
19
20    #[cfg(any(PyPy, RustPython))]
21    #[cfg_attr(PyPy, link_name = "PyPyWeakref_CheckRef")]
22    pub fn PyWeakref_CheckRef(op: *mut PyObject) -> c_int;
23
24    #[cfg(any(PyPy, RustPython))]
25    #[cfg_attr(PyPy, link_name = "PyPyWeakref_CheckRefExact")]
26    pub fn PyWeakref_CheckRefExact(op: *mut PyObject) -> c_int;
27
28    #[cfg(any(PyPy, RustPython))]
29    #[cfg_attr(PyPy, link_name = "PyPyWeakref_CheckProxy")]
30    pub fn PyWeakref_CheckProxy(op: *mut PyObject) -> c_int;
31}
32
33#[inline]
34#[cfg(not(any(PyPy, RustPython)))]
35#[cfg(not(RustPython))]
36pub unsafe fn PyWeakref_CheckRef(op: *mut PyObject) -> c_int {
37    PyObject_TypeCheck(op, &raw mut _PyWeakref_RefType)
38}
39
40#[inline]
41#[cfg(not(any(PyPy, RustPython)))]
42pub unsafe fn PyWeakref_CheckRefExact(op: *mut PyObject) -> c_int {
43    Py_IS_TYPE(op, &raw mut _PyWeakref_RefType)
44}
45
46#[inline]
47#[cfg(not(any(PyPy, RustPython)))]
48pub unsafe fn PyWeakref_CheckProxy(op: *mut PyObject) -> c_int {
49    (Py_IS_TYPE(op, &raw mut _PyWeakref_ProxyType) > 0
50        || Py_IS_TYPE(op, &raw mut _PyWeakref_CallableProxyType) > 0) as c_int
51}
52
53#[inline]
54pub unsafe fn PyWeakref_Check(op: *mut PyObject) -> c_int {
55    (PyWeakref_CheckRef(op) != 0 || PyWeakref_CheckProxy(op) != 0) as c_int
56}
57
58extern_libpython! {
59    #[cfg_attr(PyPy, link_name = "PyPyWeakref_NewRef")]
60    pub fn PyWeakref_NewRef(ob: *mut PyObject, callback: *mut PyObject) -> *mut PyObject;
61    #[cfg_attr(PyPy, link_name = "PyPyWeakref_NewProxy")]
62    pub fn PyWeakref_NewProxy(ob: *mut PyObject, callback: *mut PyObject) -> *mut PyObject;
63    #[cfg(not(Py_3_15))]
64    #[cfg_attr(PyPy, link_name = "PyPyWeakref_GetObject")]
65    #[cfg_attr(
66        Py_3_13,
67        deprecated(note = "deprecated since Python 3.13. Use `PyWeakref_GetRef` instead.")
68    )]
69    pub fn PyWeakref_GetObject(reference: *mut PyObject) -> *mut PyObject;
70    #[cfg(Py_3_13)]
71    #[cfg_attr(PyPy, link_name = "PyPyWeakref_GetRef")]
72    pub fn PyWeakref_GetRef(reference: *mut PyObject, pobj: *mut *mut PyObject) -> c_int;
73}
⚠️ Internal Docs ⚠️ Not Public API 👉 Official Docs Here