Skip to main content

pyo3_ffi/compat/
py_3_13.rs

1compat_function!(
2    originally_defined_for(Py_3_13);
3
4    #[inline]
5    pub unsafe fn PyDict_GetItemRef(
6        dp: *mut crate::PyObject,
7        key: *mut crate::PyObject,
8        result: *mut *mut crate::PyObject,
9    ) -> core::ffi::c_int {
10        use crate::{compat::Py_NewRef, PyDict_GetItemWithError, PyErr_Occurred};
11
12        let item = PyDict_GetItemWithError(dp, key);
13        if !item.is_null() {
14            *result = Py_NewRef(item);
15            return 1; // found
16        }
17        *result = core::ptr::null_mut();
18        if PyErr_Occurred().is_null() {
19            return 0; // not found
20        }
21        -1
22    }
23);
24
25compat_function!(
26    originally_defined_for(Py_3_13);
27
28    #[inline]
29    pub unsafe fn PyList_GetItemRef(
30        arg1: *mut crate::PyObject,
31        arg2: crate::Py_ssize_t,
32    ) -> *mut crate::PyObject {
33        use crate::{PyList_GetItem, Py_XINCREF};
34
35        let item = PyList_GetItem(arg1, arg2);
36        Py_XINCREF(item);
37        item
38    }
39);
40
41compat_function!(
42    originally_defined_for(Py_3_13);
43
44    #[inline]
45    pub unsafe fn PyImport_AddModuleRef(
46        name: *const core::ffi::c_char,
47    ) -> *mut crate::PyObject {
48        use crate::{compat::Py_XNewRef, PyImport_AddModule};
49
50        Py_XNewRef(PyImport_AddModule(name))
51    }
52);
53
54compat_function!(
55    originally_defined_for(Py_3_13);
56
57    #[inline]
58    pub unsafe fn PyWeakref_GetRef(
59        reference: *mut crate::PyObject,
60        pobj: *mut *mut crate::PyObject,
61    ) -> core::ffi::c_int {
62        use crate::{
63            compat::Py_NewRef, PyErr_SetString, PyExc_TypeError, PyWeakref_Check,
64            PyWeakref_GetObject, Py_None,
65        };
66
67        if !reference.is_null() && PyWeakref_Check(reference) == 0 {
68            *pobj = core::ptr::null_mut();
69            PyErr_SetString(PyExc_TypeError, c"expected a weakref".as_ptr());
70            return -1;
71        }
72        let obj = PyWeakref_GetObject(reference);
73        if obj.is_null() {
74            // SystemError if reference is NULL
75            *pobj = core::ptr::null_mut();
76            return -1;
77        }
78        if obj == Py_None() {
79            *pobj = core::ptr::null_mut();
80            return 0;
81        }
82        *pobj = Py_NewRef(obj);
83        1
84    }
85);
86
87compat_function!(
88    originally_defined_for(Py_3_13);
89
90    #[inline]
91    pub unsafe fn PyList_Extend(
92        list: *mut crate::PyObject,
93        iterable: *mut crate::PyObject,
94    ) -> core::ffi::c_int {
95        crate::PyList_SetSlice(list, crate::PY_SSIZE_T_MAX, crate::PY_SSIZE_T_MAX, iterable)
96    }
97);
98
99compat_function!(
100    originally_defined_for(Py_3_13);
101
102    #[inline]
103    pub unsafe fn PyList_Clear(list: *mut crate::PyObject) -> core::ffi::c_int {
104        crate::PyList_SetSlice(list, 0, crate::PY_SSIZE_T_MAX, core::ptr::null_mut())
105    }
106);
107
108compat_function!(
109    originally_defined_for(Py_3_13);
110
111    #[inline]
112    pub unsafe fn PyModule_Add(
113        module: *mut crate::PyObject,
114        name: *const core::ffi::c_char,
115        value: *mut crate::PyObject,
116    ) -> core::ffi::c_int {
117        let result = crate::compat::PyModule_AddObjectRef(module, name, value);
118        crate::Py_XDECREF(value);
119        result
120    }
121);
122
123#[cfg(not(Py_LIMITED_API))]
124compat_function!(
125    originally_defined_for(Py_3_13);
126
127    #[inline]
128    pub unsafe fn PyThreadState_GetUnchecked(
129    ) -> *mut crate::PyThreadState {
130        crate::_PyThreadState_UncheckedGet()
131    }
132);
133
134compat_function!(
135    originally_defined_for(all(Py_3_13, any(not(Py_LIMITED_API), Py_3_15)));
136
137    #[inline]
138    pub unsafe fn PyDict_SetDefaultRef(
139        mp: *mut crate::PyObject,
140        key: *mut crate::PyObject,
141        default_value: *mut crate::PyObject,
142        result: *mut *mut crate::PyObject,
143    ) -> core::ffi::c_int {
144        use crate::{
145            compat::{PyDict_GetItemRef, Py_NewRef},
146            PyDict_SetItem, PyObject, Py_DECREF,
147        };
148        let mut value: *mut PyObject = core::ptr::null_mut();
149        if PyDict_GetItemRef(mp, key, &mut value) < 0 {
150            // get error
151            if !result.is_null() {
152                *result = core::ptr::null_mut();
153            }
154            return -1;
155        }
156        if !value.is_null() {
157            // present
158            if !result.is_null() {
159                *result = value;
160            } else {
161                Py_DECREF(value);
162            }
163            return 1;
164        }
165
166        // missing, set the item
167        if PyDict_SetItem(mp, key, default_value) < 0 {
168            // set error
169            if !result.is_null() {
170                *result = core::ptr::null_mut();
171            }
172            return -1;
173        }
174        if !result.is_null() {
175            *result = Py_NewRef(default_value);
176        }
177        0
178    }
179);
180
181compat_function!(
182    originally_defined_for(Py_3_13);
183
184    #[inline]
185    pub unsafe fn PyObject_GetOptionalAttr(
186        obj: *mut crate::PyObject,
187        name: *mut crate::PyObject,
188        result: *mut *mut crate::PyObject,
189    ) -> core::ffi::c_int {
190        use crate::{PyErr_Clear, PyErr_ExceptionMatches, PyExc_AttributeError, PyObject_GetAttr};
191
192        let attr = PyObject_GetAttr(obj, name);
193        if !attr.is_null() {
194            *result = attr;
195            return 1; // found
196        }
197        *result = core::ptr::null_mut();
198        if PyErr_ExceptionMatches(PyExc_AttributeError) != 0 {
199            PyErr_Clear();
200            return 0; // not found
201        }
202        -1 // other error
203    }
204);
205
206compat_function!(
207    originally_defined_for(Py_3_13);
208
209    #[inline]
210    pub unsafe fn PyObject_HasAttrWithError(obj: *mut crate::PyObject, attr_name: *mut crate::PyObject) -> core::ffi::c_int {
211        let mut res: *mut crate::PyObject = core::ptr::null_mut();
212        let rc = crate::compat::PyObject_GetOptionalAttr(obj, attr_name, &mut res);
213        crate::Py_XDECREF(res);
214        rc
215    }
216);
⚠️ Internal Docs ⚠️ Not Public API 👉 Official Docs Here