pyo3_ffi/
object.rs

1use crate::pyport::{Py_hash_t, Py_ssize_t};
2#[cfg(Py_GIL_DISABLED)]
3use crate::refcount;
4#[cfg(Py_GIL_DISABLED)]
5use crate::PyMutex;
6use std::ffi::{c_char, c_int, c_uint, c_ulong, c_void};
7use std::mem;
8use std::ptr;
9#[cfg(Py_GIL_DISABLED)]
10use std::sync::atomic::{AtomicIsize, AtomicU32};
11
12#[cfg(Py_LIMITED_API)]
13opaque_struct!(pub PyTypeObject);
14
15#[cfg(not(Py_LIMITED_API))]
16pub use crate::cpython::object::PyTypeObject;
17
18// skip PyObject_HEAD
19
20#[repr(C)]
21#[derive(Copy, Clone)]
22#[cfg(all(
23    target_pointer_width = "64",
24    Py_3_14,
25    not(Py_GIL_DISABLED),
26    target_endian = "big"
27))]
28/// This struct is anonymous in CPython, so the name was given by PyO3 because
29/// Rust structs need a name.
30pub struct PyObjectObFlagsAndRefcnt {
31    pub ob_flags: u16,
32    pub ob_overflow: u16,
33    pub ob_refcnt: u32,
34}
35
36#[repr(C)]
37#[derive(Copy, Clone)]
38#[cfg(all(
39    target_pointer_width = "64",
40    Py_3_14,
41    not(Py_GIL_DISABLED),
42    target_endian = "little"
43))]
44/// This struct is anonymous in CPython, so the name was given by PyO3 because
45/// Rust structs need a name.
46pub struct PyObjectObFlagsAndRefcnt {
47    pub ob_refcnt: u32,
48    pub ob_overflow: u16,
49    pub ob_flags: u16,
50}
51
52#[repr(C)]
53#[derive(Copy, Clone)]
54#[cfg(all(Py_3_12, not(Py_GIL_DISABLED)))]
55/// This union is anonymous in CPython, so the name was given by PyO3 because
56/// Rust union need a name.
57pub union PyObjectObRefcnt {
58    #[cfg(all(target_pointer_width = "64", Py_3_14))]
59    pub ob_refcnt_full: crate::PY_INT64_T,
60    #[cfg(all(target_pointer_width = "64", Py_3_14))]
61    pub refcnt_and_flags: PyObjectObFlagsAndRefcnt,
62    pub ob_refcnt: Py_ssize_t,
63    #[cfg(all(target_pointer_width = "64", not(Py_3_14)))]
64    pub ob_refcnt_split: [crate::PY_UINT32_T; 2],
65}
66
67#[cfg(all(Py_3_12, not(Py_GIL_DISABLED)))]
68impl std::fmt::Debug for PyObjectObRefcnt {
69    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
70        write!(f, "{}", unsafe { self.ob_refcnt })
71    }
72}
73
74#[cfg(all(not(Py_3_12), not(Py_GIL_DISABLED)))]
75pub type PyObjectObRefcnt = Py_ssize_t;
76
77// PyObject_HEAD_INIT comes before the PyObject definition in object.h
78// but we put it after PyObject because HEAD_INIT uses PyObject
79
80#[repr(C)]
81#[derive(Debug)]
82pub struct PyObject {
83    #[cfg(py_sys_config = "Py_TRACE_REFS")]
84    pub _ob_next: *mut PyObject,
85    #[cfg(py_sys_config = "Py_TRACE_REFS")]
86    pub _ob_prev: *mut PyObject,
87    #[cfg(Py_GIL_DISABLED)]
88    pub ob_tid: libc::uintptr_t,
89    #[cfg(all(Py_GIL_DISABLED, not(Py_3_14)))]
90    pub _padding: u16,
91    #[cfg(all(Py_GIL_DISABLED, Py_3_14))]
92    pub ob_flags: u16,
93    #[cfg(Py_GIL_DISABLED)]
94    pub ob_mutex: PyMutex, // per-object lock
95    #[cfg(Py_GIL_DISABLED)]
96    pub ob_gc_bits: u8, // gc-related state
97    #[cfg(Py_GIL_DISABLED)]
98    pub ob_ref_local: AtomicU32, // local reference count
99    #[cfg(Py_GIL_DISABLED)]
100    pub ob_ref_shared: AtomicIsize, // shared reference count
101    #[cfg(not(Py_GIL_DISABLED))]
102    pub ob_refcnt: PyObjectObRefcnt,
103    #[cfg(PyPy)]
104    pub ob_pypy_link: Py_ssize_t,
105    pub ob_type: *mut PyTypeObject,
106}
107
108#[allow(
109    clippy::declare_interior_mutable_const,
110    reason = "contains atomic refcount on free-threaded builds"
111)]
112pub const PyObject_HEAD_INIT: PyObject = PyObject {
113    #[cfg(py_sys_config = "Py_TRACE_REFS")]
114    _ob_next: std::ptr::null_mut(),
115    #[cfg(py_sys_config = "Py_TRACE_REFS")]
116    _ob_prev: std::ptr::null_mut(),
117    #[cfg(Py_GIL_DISABLED)]
118    ob_tid: 0,
119    #[cfg(all(Py_GIL_DISABLED, Py_3_14))]
120    ob_flags: 0,
121    #[cfg(all(Py_GIL_DISABLED, not(Py_3_14)))]
122    _padding: 0,
123    #[cfg(Py_GIL_DISABLED)]
124    ob_mutex: PyMutex::new(),
125    #[cfg(Py_GIL_DISABLED)]
126    ob_gc_bits: 0,
127    #[cfg(Py_GIL_DISABLED)]
128    ob_ref_local: AtomicU32::new(refcount::_Py_IMMORTAL_REFCNT_LOCAL),
129    #[cfg(Py_GIL_DISABLED)]
130    ob_ref_shared: AtomicIsize::new(0),
131    #[cfg(all(not(Py_GIL_DISABLED), Py_3_12))]
132    ob_refcnt: PyObjectObRefcnt { ob_refcnt: 1 },
133    #[cfg(not(Py_3_12))]
134    ob_refcnt: 1,
135    #[cfg(PyPy)]
136    ob_pypy_link: 0,
137    ob_type: std::ptr::null_mut(),
138};
139
140// skipped _Py_UNOWNED_TID
141
142// skipped _PyObject_CAST
143
144#[repr(C)]
145#[derive(Debug)]
146pub struct PyVarObject {
147    pub ob_base: PyObject,
148    #[cfg(not(GraalPy))]
149    pub ob_size: Py_ssize_t,
150    // On GraalPy the field is physically there, but not always populated. We hide it to prevent accidental misuse
151    #[cfg(GraalPy)]
152    pub _ob_size_graalpy: Py_ssize_t,
153}
154
155// skipped private _PyVarObject_CAST
156
157#[inline]
158#[cfg(not(any(GraalPy, PyPy)))]
159#[cfg_attr(docsrs, doc(cfg(all())))]
160pub unsafe fn Py_Is(x: *mut PyObject, y: *mut PyObject) -> c_int {
161    (x == y).into()
162}
163
164#[cfg(any(GraalPy, PyPy))]
165#[cfg_attr(docsrs, doc(cfg(all())))]
166extern "C" {
167    #[cfg_attr(PyPy, link_name = "PyPy_Is")]
168    pub fn Py_Is(x: *mut PyObject, y: *mut PyObject) -> c_int;
169}
170
171// skipped _Py_GetThreadLocal_Addr
172
173// skipped _Py_ThreadID
174
175// skipped _Py_IsOwnedByCurrentThread
176
177#[cfg(GraalPy)]
178extern "C" {
179    #[cfg(GraalPy)]
180    fn _Py_TYPE(arg1: *const PyObject) -> *mut PyTypeObject;
181
182    #[cfg(GraalPy)]
183    fn _Py_SIZE(arg1: *const PyObject) -> Py_ssize_t;
184}
185
186#[inline]
187#[cfg(not(Py_3_14))]
188pub unsafe fn Py_TYPE(ob: *mut PyObject) -> *mut PyTypeObject {
189    #[cfg(not(GraalPy))]
190    return (*ob).ob_type;
191    #[cfg(GraalPy)]
192    return _Py_TYPE(ob);
193}
194
195#[cfg_attr(windows, link(name = "pythonXY"))]
196#[cfg(Py_3_14)]
197extern "C" {
198    #[cfg_attr(PyPy, link_name = "PyPy_TYPE")]
199    pub fn Py_TYPE(ob: *mut PyObject) -> *mut PyTypeObject;
200}
201
202// skip _Py_TYPE compat shim
203
204#[cfg_attr(windows, link(name = "pythonXY"))]
205extern "C" {
206    #[cfg_attr(PyPy, link_name = "PyPyLong_Type")]
207    pub static mut PyLong_Type: PyTypeObject;
208    #[cfg_attr(PyPy, link_name = "PyPyBool_Type")]
209    pub static mut PyBool_Type: PyTypeObject;
210}
211
212#[inline]
213pub unsafe fn Py_SIZE(ob: *mut PyObject) -> Py_ssize_t {
214    #[cfg(not(GraalPy))]
215    {
216        debug_assert_ne!((*ob).ob_type, std::ptr::addr_of_mut!(crate::PyLong_Type));
217        debug_assert_ne!((*ob).ob_type, std::ptr::addr_of_mut!(crate::PyBool_Type));
218        (*ob.cast::<PyVarObject>()).ob_size
219    }
220    #[cfg(GraalPy)]
221    _Py_SIZE(ob)
222}
223
224#[inline]
225pub unsafe fn Py_IS_TYPE(ob: *mut PyObject, tp: *mut PyTypeObject) -> c_int {
226    (Py_TYPE(ob) == tp) as c_int
227}
228
229// skipped Py_SET_TYPE
230
231// skipped Py_SET_SIZE
232
233pub type unaryfunc = unsafe extern "C" fn(*mut PyObject) -> *mut PyObject;
234pub type binaryfunc = unsafe extern "C" fn(*mut PyObject, *mut PyObject) -> *mut PyObject;
235pub type ternaryfunc =
236    unsafe extern "C" fn(*mut PyObject, *mut PyObject, *mut PyObject) -> *mut PyObject;
237pub type inquiry = unsafe extern "C" fn(*mut PyObject) -> c_int;
238pub type lenfunc = unsafe extern "C" fn(*mut PyObject) -> Py_ssize_t;
239pub type ssizeargfunc = unsafe extern "C" fn(*mut PyObject, Py_ssize_t) -> *mut PyObject;
240pub type ssizessizeargfunc =
241    unsafe extern "C" fn(*mut PyObject, Py_ssize_t, Py_ssize_t) -> *mut PyObject;
242pub type ssizeobjargproc = unsafe extern "C" fn(*mut PyObject, Py_ssize_t, *mut PyObject) -> c_int;
243pub type ssizessizeobjargproc =
244    unsafe extern "C" fn(*mut PyObject, Py_ssize_t, Py_ssize_t, arg4: *mut PyObject) -> c_int;
245pub type objobjargproc = unsafe extern "C" fn(*mut PyObject, *mut PyObject, *mut PyObject) -> c_int;
246
247pub type objobjproc = unsafe extern "C" fn(*mut PyObject, *mut PyObject) -> c_int;
248pub type visitproc = unsafe extern "C" fn(object: *mut PyObject, arg: *mut c_void) -> c_int;
249pub type traverseproc =
250    unsafe extern "C" fn(slf: *mut PyObject, visit: visitproc, arg: *mut c_void) -> c_int;
251
252pub type freefunc = unsafe extern "C" fn(*mut c_void);
253pub type destructor = unsafe extern "C" fn(*mut PyObject);
254pub type getattrfunc = unsafe extern "C" fn(*mut PyObject, *mut c_char) -> *mut PyObject;
255pub type getattrofunc = unsafe extern "C" fn(*mut PyObject, *mut PyObject) -> *mut PyObject;
256pub type setattrfunc = unsafe extern "C" fn(*mut PyObject, *mut c_char, *mut PyObject) -> c_int;
257pub type setattrofunc = unsafe extern "C" fn(*mut PyObject, *mut PyObject, *mut PyObject) -> c_int;
258pub type reprfunc = unsafe extern "C" fn(*mut PyObject) -> *mut PyObject;
259pub type hashfunc = unsafe extern "C" fn(*mut PyObject) -> Py_hash_t;
260pub type richcmpfunc = unsafe extern "C" fn(*mut PyObject, *mut PyObject, c_int) -> *mut PyObject;
261pub type getiterfunc = unsafe extern "C" fn(*mut PyObject) -> *mut PyObject;
262pub type iternextfunc = unsafe extern "C" fn(*mut PyObject) -> *mut PyObject;
263pub type descrgetfunc =
264    unsafe extern "C" fn(*mut PyObject, *mut PyObject, *mut PyObject) -> *mut PyObject;
265pub type descrsetfunc = unsafe extern "C" fn(*mut PyObject, *mut PyObject, *mut PyObject) -> c_int;
266pub type initproc = unsafe extern "C" fn(*mut PyObject, *mut PyObject, *mut PyObject) -> c_int;
267pub type newfunc =
268    unsafe extern "C" fn(*mut PyTypeObject, *mut PyObject, *mut PyObject) -> *mut PyObject;
269pub type allocfunc = unsafe extern "C" fn(*mut PyTypeObject, Py_ssize_t) -> *mut PyObject;
270
271#[cfg(Py_3_8)]
272pub type vectorcallfunc = unsafe extern "C" fn(
273    callable: *mut PyObject,
274    args: *const *mut PyObject,
275    nargsf: libc::size_t,
276    kwnames: *mut PyObject,
277) -> *mut PyObject;
278
279#[repr(C)]
280#[derive(Copy, Clone)]
281pub struct PyType_Slot {
282    pub slot: c_int,
283    pub pfunc: *mut c_void,
284}
285
286impl Default for PyType_Slot {
287    fn default() -> PyType_Slot {
288        unsafe { mem::zeroed() }
289    }
290}
291
292#[repr(C)]
293#[derive(Copy, Clone)]
294pub struct PyType_Spec {
295    pub name: *const c_char,
296    pub basicsize: c_int,
297    pub itemsize: c_int,
298    pub flags: c_uint,
299    pub slots: *mut PyType_Slot,
300}
301
302impl Default for PyType_Spec {
303    fn default() -> PyType_Spec {
304        unsafe { mem::zeroed() }
305    }
306}
307
308extern "C" {
309    #[cfg_attr(PyPy, link_name = "PyPyType_FromSpec")]
310    pub fn PyType_FromSpec(arg1: *mut PyType_Spec) -> *mut PyObject;
311
312    #[cfg_attr(PyPy, link_name = "PyPyType_FromSpecWithBases")]
313    pub fn PyType_FromSpecWithBases(arg1: *mut PyType_Spec, arg2: *mut PyObject) -> *mut PyObject;
314
315    #[cfg_attr(PyPy, link_name = "PyPyType_GetSlot")]
316    pub fn PyType_GetSlot(arg1: *mut PyTypeObject, arg2: c_int) -> *mut c_void;
317
318    #[cfg(any(Py_3_10, all(Py_3_9, not(Py_LIMITED_API))))]
319    #[cfg_attr(PyPy, link_name = "PyPyType_FromModuleAndSpec")]
320    pub fn PyType_FromModuleAndSpec(
321        module: *mut PyObject,
322        spec: *mut PyType_Spec,
323        bases: *mut PyObject,
324    ) -> *mut PyObject;
325
326    #[cfg(any(Py_3_10, all(Py_3_9, not(Py_LIMITED_API))))]
327    #[cfg_attr(PyPy, link_name = "PyPyType_GetModule")]
328    pub fn PyType_GetModule(arg1: *mut PyTypeObject) -> *mut PyObject;
329
330    #[cfg(any(Py_3_10, all(Py_3_9, not(Py_LIMITED_API))))]
331    #[cfg_attr(PyPy, link_name = "PyPyType_GetModuleState")]
332    pub fn PyType_GetModuleState(arg1: *mut PyTypeObject) -> *mut c_void;
333
334    #[cfg(Py_3_11)]
335    #[cfg_attr(PyPy, link_name = "PyPyType_GetName")]
336    pub fn PyType_GetName(arg1: *mut PyTypeObject) -> *mut PyObject;
337
338    #[cfg(Py_3_11)]
339    #[cfg_attr(PyPy, link_name = "PyPyType_GetQualName")]
340    pub fn PyType_GetQualName(arg1: *mut PyTypeObject) -> *mut PyObject;
341
342    #[cfg(Py_3_13)]
343    #[cfg_attr(PyPy, link_name = "PyPyType_GetFullyQualifiedName")]
344    pub fn PyType_GetFullyQualifiedName(arg1: *mut PyTypeObject) -> *mut PyObject;
345
346    #[cfg(Py_3_13)]
347    #[cfg_attr(PyPy, link_name = "PyPyType_GetModuleName")]
348    pub fn PyType_GetModuleName(arg1: *mut PyTypeObject) -> *mut PyObject;
349
350    #[cfg(Py_3_12)]
351    #[cfg_attr(PyPy, link_name = "PyPyType_FromMetaclass")]
352    pub fn PyType_FromMetaclass(
353        metaclass: *mut PyTypeObject,
354        module: *mut PyObject,
355        spec: *mut PyType_Spec,
356        bases: *mut PyObject,
357    ) -> *mut PyObject;
358
359    #[cfg(Py_3_12)]
360    #[cfg_attr(PyPy, link_name = "PyPyObject_GetTypeData")]
361    pub fn PyObject_GetTypeData(obj: *mut PyObject, cls: *mut PyTypeObject) -> *mut c_void;
362
363    #[cfg(Py_3_12)]
364    #[cfg_attr(PyPy, link_name = "PyPyObject_GetTypeDataSize")]
365    pub fn PyObject_GetTypeDataSize(cls: *mut PyTypeObject) -> Py_ssize_t;
366
367    #[cfg_attr(PyPy, link_name = "PyPyType_IsSubtype")]
368    pub fn PyType_IsSubtype(a: *mut PyTypeObject, b: *mut PyTypeObject) -> c_int;
369}
370
371#[inline]
372pub unsafe fn PyObject_TypeCheck(ob: *mut PyObject, tp: *mut PyTypeObject) -> c_int {
373    (Py_IS_TYPE(ob, tp) != 0 || PyType_IsSubtype(Py_TYPE(ob), tp) != 0) as c_int
374}
375
376#[cfg_attr(windows, link(name = "pythonXY"))]
377extern "C" {
378    /// built-in 'type'
379    #[cfg_attr(PyPy, link_name = "PyPyType_Type")]
380    pub static mut PyType_Type: PyTypeObject;
381    /// built-in 'object'
382    #[cfg_attr(PyPy, link_name = "PyPyBaseObject_Type")]
383    pub static mut PyBaseObject_Type: PyTypeObject;
384    /// built-in 'super'
385    pub static mut PySuper_Type: PyTypeObject;
386}
387
388extern "C" {
389    pub fn PyType_GetFlags(arg1: *mut PyTypeObject) -> c_ulong;
390
391    #[cfg_attr(PyPy, link_name = "PyPyType_Ready")]
392    pub fn PyType_Ready(t: *mut PyTypeObject) -> c_int;
393    #[cfg_attr(PyPy, link_name = "PyPyType_GenericAlloc")]
394    pub fn PyType_GenericAlloc(t: *mut PyTypeObject, nitems: Py_ssize_t) -> *mut PyObject;
395    #[cfg_attr(PyPy, link_name = "PyPyType_GenericNew")]
396    pub fn PyType_GenericNew(
397        t: *mut PyTypeObject,
398        args: *mut PyObject,
399        kwds: *mut PyObject,
400    ) -> *mut PyObject;
401    pub fn PyType_ClearCache() -> c_uint;
402    #[cfg_attr(PyPy, link_name = "PyPyType_Modified")]
403    pub fn PyType_Modified(t: *mut PyTypeObject);
404
405    #[cfg_attr(PyPy, link_name = "PyPyObject_Repr")]
406    pub fn PyObject_Repr(o: *mut PyObject) -> *mut PyObject;
407    #[cfg_attr(PyPy, link_name = "PyPyObject_Str")]
408    pub fn PyObject_Str(o: *mut PyObject) -> *mut PyObject;
409    #[cfg_attr(PyPy, link_name = "PyPyObject_ASCII")]
410    pub fn PyObject_ASCII(arg1: *mut PyObject) -> *mut PyObject;
411    #[cfg_attr(PyPy, link_name = "PyPyObject_Bytes")]
412    pub fn PyObject_Bytes(arg1: *mut PyObject) -> *mut PyObject;
413    #[cfg_attr(PyPy, link_name = "PyPyObject_RichCompare")]
414    pub fn PyObject_RichCompare(
415        arg1: *mut PyObject,
416        arg2: *mut PyObject,
417        arg3: c_int,
418    ) -> *mut PyObject;
419    #[cfg_attr(PyPy, link_name = "PyPyObject_RichCompareBool")]
420    pub fn PyObject_RichCompareBool(arg1: *mut PyObject, arg2: *mut PyObject, arg3: c_int)
421        -> c_int;
422    #[cfg_attr(PyPy, link_name = "PyPyObject_GetAttrString")]
423    pub fn PyObject_GetAttrString(arg1: *mut PyObject, arg2: *const c_char) -> *mut PyObject;
424    #[cfg_attr(PyPy, link_name = "PyPyObject_SetAttrString")]
425    pub fn PyObject_SetAttrString(
426        arg1: *mut PyObject,
427        arg2: *const c_char,
428        arg3: *mut PyObject,
429    ) -> c_int;
430    #[cfg(any(Py_3_13, all(PyPy, not(Py_3_11))))] // CPython defined in 3.12 as an inline function in abstract.h
431    #[cfg_attr(PyPy, link_name = "PyPyObject_DelAttrString")]
432    pub fn PyObject_DelAttrString(arg1: *mut PyObject, arg2: *const c_char) -> c_int;
433    #[cfg_attr(PyPy, link_name = "PyPyObject_HasAttrString")]
434    pub fn PyObject_HasAttrString(arg1: *mut PyObject, arg2: *const c_char) -> c_int;
435    #[cfg_attr(PyPy, link_name = "PyPyObject_GetAttr")]
436    pub fn PyObject_GetAttr(arg1: *mut PyObject, arg2: *mut PyObject) -> *mut PyObject;
437    #[cfg(Py_3_13)]
438    #[cfg_attr(PyPy, link_name = "PyPyObject_GetOptionalAttr")]
439    pub fn PyObject_GetOptionalAttr(
440        arg1: *mut PyObject,
441        arg2: *mut PyObject,
442        arg3: *mut *mut PyObject,
443    ) -> c_int;
444    #[cfg(Py_3_13)]
445    #[cfg_attr(PyPy, link_name = "PyPyObject_GetOptionalAttrString")]
446    pub fn PyObject_GetOptionalAttrString(
447        arg1: *mut PyObject,
448        arg2: *const c_char,
449        arg3: *mut *mut PyObject,
450    ) -> c_int;
451    #[cfg_attr(PyPy, link_name = "PyPyObject_SetAttr")]
452    pub fn PyObject_SetAttr(arg1: *mut PyObject, arg2: *mut PyObject, arg3: *mut PyObject)
453        -> c_int;
454    #[cfg(any(Py_3_13, all(PyPy, not(Py_3_11))))] // CPython defined in 3.12 as an inline function in abstract.h
455    #[cfg_attr(PyPy, link_name = "PyPyObject_DelAttr")]
456    pub fn PyObject_DelAttr(arg1: *mut PyObject, arg2: *mut PyObject) -> c_int;
457    #[cfg_attr(PyPy, link_name = "PyPyObject_HasAttr")]
458    pub fn PyObject_HasAttr(arg1: *mut PyObject, arg2: *mut PyObject) -> c_int;
459    #[cfg(Py_3_13)]
460    #[cfg_attr(PyPy, link_name = "PyPyObject_HasAttrWithError")]
461    pub fn PyObject_HasAttrWithError(arg1: *mut PyObject, arg2: *mut PyObject) -> c_int;
462    #[cfg(Py_3_13)]
463    #[cfg_attr(PyPy, link_name = "PyPyObject_HasAttrStringWithError")]
464    pub fn PyObject_HasAttrStringWithError(arg1: *mut PyObject, arg2: *const c_char) -> c_int;
465    #[cfg_attr(PyPy, link_name = "PyPyObject_SelfIter")]
466    pub fn PyObject_SelfIter(arg1: *mut PyObject) -> *mut PyObject;
467    #[cfg_attr(PyPy, link_name = "PyPyObject_GenericGetAttr")]
468    pub fn PyObject_GenericGetAttr(arg1: *mut PyObject, arg2: *mut PyObject) -> *mut PyObject;
469    #[cfg_attr(PyPy, link_name = "PyPyObject_GenericSetAttr")]
470    pub fn PyObject_GenericSetAttr(
471        arg1: *mut PyObject,
472        arg2: *mut PyObject,
473        arg3: *mut PyObject,
474    ) -> c_int;
475    #[cfg(not(all(Py_LIMITED_API, not(Py_3_10))))]
476    #[cfg_attr(PyPy, link_name = "PyPyObject_GenericGetDict")]
477    pub fn PyObject_GenericGetDict(arg1: *mut PyObject, arg2: *mut c_void) -> *mut PyObject;
478    #[cfg_attr(PyPy, link_name = "PyPyObject_GenericSetDict")]
479    pub fn PyObject_GenericSetDict(
480        arg1: *mut PyObject,
481        arg2: *mut PyObject,
482        arg3: *mut c_void,
483    ) -> c_int;
484    #[cfg_attr(PyPy, link_name = "PyPyObject_Hash")]
485    pub fn PyObject_Hash(arg1: *mut PyObject) -> Py_hash_t;
486    #[cfg_attr(PyPy, link_name = "PyPyObject_HashNotImplemented")]
487    pub fn PyObject_HashNotImplemented(arg1: *mut PyObject) -> Py_hash_t;
488    #[cfg_attr(PyPy, link_name = "PyPyObject_IsTrue")]
489    pub fn PyObject_IsTrue(arg1: *mut PyObject) -> c_int;
490    #[cfg_attr(PyPy, link_name = "PyPyObject_Not")]
491    pub fn PyObject_Not(arg1: *mut PyObject) -> c_int;
492    #[cfg_attr(PyPy, link_name = "PyPyCallable_Check")]
493    pub fn PyCallable_Check(arg1: *mut PyObject) -> c_int;
494    #[cfg_attr(PyPy, link_name = "PyPyObject_ClearWeakRefs")]
495    pub fn PyObject_ClearWeakRefs(arg1: *mut PyObject);
496
497    #[cfg_attr(PyPy, link_name = "PyPyObject_Dir")]
498    pub fn PyObject_Dir(arg1: *mut PyObject) -> *mut PyObject;
499    pub fn Py_ReprEnter(arg1: *mut PyObject) -> c_int;
500    pub fn Py_ReprLeave(arg1: *mut PyObject);
501}
502
503// Flag bits for printing:
504pub const Py_PRINT_RAW: c_int = 1; // No string quotes etc.
505
506// skipped because is a private API
507// const _Py_TPFLAGS_STATIC_BUILTIN: c_ulong = 1 << 1;
508
509#[cfg(all(Py_3_12, not(Py_LIMITED_API)))]
510pub const Py_TPFLAGS_MANAGED_WEAKREF: c_ulong = 1 << 3;
511
512#[cfg(all(Py_3_11, not(Py_LIMITED_API)))]
513pub const Py_TPFLAGS_MANAGED_DICT: c_ulong = 1 << 4;
514
515#[cfg(all(Py_3_10, not(Py_LIMITED_API)))]
516pub const Py_TPFLAGS_SEQUENCE: c_ulong = 1 << 5;
517
518#[cfg(all(Py_3_10, not(Py_LIMITED_API)))]
519pub const Py_TPFLAGS_MAPPING: c_ulong = 1 << 6;
520
521#[cfg(Py_3_10)]
522pub const Py_TPFLAGS_DISALLOW_INSTANTIATION: c_ulong = 1 << 7;
523
524#[cfg(Py_3_10)]
525pub const Py_TPFLAGS_IMMUTABLETYPE: c_ulong = 1 << 8;
526
527/// Set if the type object is dynamically allocated
528pub const Py_TPFLAGS_HEAPTYPE: c_ulong = 1 << 9;
529
530/// Set if the type allows subclassing
531pub const Py_TPFLAGS_BASETYPE: c_ulong = 1 << 10;
532
533/// Set if the type implements the vectorcall protocol (PEP 590)
534#[cfg(any(Py_3_12, all(Py_3_8, not(Py_LIMITED_API))))]
535pub const Py_TPFLAGS_HAVE_VECTORCALL: c_ulong = 1 << 11;
536// skipped backwards-compatibility alias _Py_TPFLAGS_HAVE_VECTORCALL
537
538/// Set if the type is 'ready' -- fully initialized
539pub const Py_TPFLAGS_READY: c_ulong = 1 << 12;
540
541/// Set while the type is being 'readied', to prevent recursive ready calls
542pub const Py_TPFLAGS_READYING: c_ulong = 1 << 13;
543
544/// Objects support garbage collection (see objimp.h)
545pub const Py_TPFLAGS_HAVE_GC: c_ulong = 1 << 14;
546
547const Py_TPFLAGS_HAVE_STACKLESS_EXTENSION: c_ulong = 0;
548
549#[cfg(Py_3_8)]
550pub const Py_TPFLAGS_METHOD_DESCRIPTOR: c_ulong = 1 << 17;
551
552pub const Py_TPFLAGS_VALID_VERSION_TAG: c_ulong = 1 << 19;
553
554/* Type is abstract and cannot be instantiated */
555pub const Py_TPFLAGS_IS_ABSTRACT: c_ulong = 1 << 20;
556
557// skipped non-limited / 3.10 Py_TPFLAGS_HAVE_AM_SEND
558#[cfg(Py_3_12)]
559pub const Py_TPFLAGS_ITEMS_AT_END: c_ulong = 1 << 23;
560
561/* These flags are used to determine if a type is a subclass. */
562pub const Py_TPFLAGS_LONG_SUBCLASS: c_ulong = 1 << 24;
563pub const Py_TPFLAGS_LIST_SUBCLASS: c_ulong = 1 << 25;
564pub const Py_TPFLAGS_TUPLE_SUBCLASS: c_ulong = 1 << 26;
565pub const Py_TPFLAGS_BYTES_SUBCLASS: c_ulong = 1 << 27;
566pub const Py_TPFLAGS_UNICODE_SUBCLASS: c_ulong = 1 << 28;
567pub const Py_TPFLAGS_DICT_SUBCLASS: c_ulong = 1 << 29;
568pub const Py_TPFLAGS_BASE_EXC_SUBCLASS: c_ulong = 1 << 30;
569pub const Py_TPFLAGS_TYPE_SUBCLASS: c_ulong = 1 << 31;
570
571pub const Py_TPFLAGS_DEFAULT: c_ulong = if cfg!(Py_3_10) {
572    Py_TPFLAGS_HAVE_STACKLESS_EXTENSION
573} else {
574    Py_TPFLAGS_HAVE_STACKLESS_EXTENSION | Py_TPFLAGS_HAVE_VERSION_TAG
575};
576
577pub const Py_TPFLAGS_HAVE_FINALIZE: c_ulong = 1;
578pub const Py_TPFLAGS_HAVE_VERSION_TAG: c_ulong = 1 << 18;
579
580#[cfg(Py_3_13)]
581pub const Py_CONSTANT_NONE: c_uint = 0;
582#[cfg(Py_3_13)]
583pub const Py_CONSTANT_FALSE: c_uint = 1;
584#[cfg(Py_3_13)]
585pub const Py_CONSTANT_TRUE: c_uint = 2;
586#[cfg(Py_3_13)]
587pub const Py_CONSTANT_ELLIPSIS: c_uint = 3;
588#[cfg(Py_3_13)]
589pub const Py_CONSTANT_NOT_IMPLEMENTED: c_uint = 4;
590#[cfg(Py_3_13)]
591pub const Py_CONSTANT_ZERO: c_uint = 5;
592#[cfg(Py_3_13)]
593pub const Py_CONSTANT_ONE: c_uint = 6;
594#[cfg(Py_3_13)]
595pub const Py_CONSTANT_EMPTY_STR: c_uint = 7;
596#[cfg(Py_3_13)]
597pub const Py_CONSTANT_EMPTY_BYTES: c_uint = 8;
598#[cfg(Py_3_13)]
599pub const Py_CONSTANT_EMPTY_TUPLE: c_uint = 9;
600
601extern "C" {
602    #[cfg(Py_3_13)]
603    #[cfg_attr(PyPy, link_name = "PyPy_GetConstant")]
604    pub fn Py_GetConstant(constant_id: c_uint) -> *mut PyObject;
605    #[cfg(Py_3_13)]
606    #[cfg_attr(PyPy, link_name = "PyPy_GetConstantBorrowed")]
607    pub fn Py_GetConstantBorrowed(constant_id: c_uint) -> *mut PyObject;
608}
609
610#[cfg_attr(windows, link(name = "pythonXY"))]
611extern "C" {
612    #[cfg(all(not(GraalPy), not(all(Py_3_13, Py_LIMITED_API))))]
613    #[cfg_attr(PyPy, link_name = "_PyPy_NoneStruct")]
614    static mut _Py_NoneStruct: PyObject;
615
616    #[cfg(GraalPy)]
617    static mut _Py_NoneStructReference: *mut PyObject;
618}
619
620#[inline]
621pub unsafe fn Py_None() -> *mut PyObject {
622    #[cfg(all(not(GraalPy), all(Py_3_13, Py_LIMITED_API)))]
623    return Py_GetConstantBorrowed(Py_CONSTANT_NONE);
624
625    #[cfg(all(not(GraalPy), not(all(Py_3_13, Py_LIMITED_API))))]
626    return ptr::addr_of_mut!(_Py_NoneStruct);
627
628    #[cfg(GraalPy)]
629    return _Py_NoneStructReference;
630}
631
632#[inline]
633pub unsafe fn Py_IsNone(x: *mut PyObject) -> c_int {
634    Py_Is(x, Py_None())
635}
636
637// skipped Py_RETURN_NONE
638
639#[cfg_attr(windows, link(name = "pythonXY"))]
640extern "C" {
641    #[cfg(all(not(GraalPy), not(all(Py_3_13, Py_LIMITED_API))))]
642    #[cfg_attr(PyPy, link_name = "_PyPy_NotImplementedStruct")]
643    static mut _Py_NotImplementedStruct: PyObject;
644
645    #[cfg(GraalPy)]
646    static mut _Py_NotImplementedStructReference: *mut PyObject;
647}
648
649#[inline]
650pub unsafe fn Py_NotImplemented() -> *mut PyObject {
651    #[cfg(all(not(GraalPy), all(Py_3_13, Py_LIMITED_API)))]
652    return Py_GetConstantBorrowed(Py_CONSTANT_NOT_IMPLEMENTED);
653
654    #[cfg(all(not(GraalPy), not(all(Py_3_13, Py_LIMITED_API))))]
655    return ptr::addr_of_mut!(_Py_NotImplementedStruct);
656
657    #[cfg(GraalPy)]
658    return _Py_NotImplementedStructReference;
659}
660
661// skipped Py_RETURN_NOTIMPLEMENTED
662
663/* Rich comparison opcodes */
664pub const Py_LT: c_int = 0;
665pub const Py_LE: c_int = 1;
666pub const Py_EQ: c_int = 2;
667pub const Py_NE: c_int = 3;
668pub const Py_GT: c_int = 4;
669pub const Py_GE: c_int = 5;
670
671#[cfg(Py_3_10)]
672#[repr(C)]
673#[derive(Copy, Clone, Debug, PartialEq, Eq)]
674pub enum PySendResult {
675    PYGEN_RETURN = 0,
676    PYGEN_ERROR = -1,
677    PYGEN_NEXT = 1,
678}
679
680// skipped Py_RETURN_RICHCOMPARE
681
682#[inline]
683pub unsafe fn PyType_HasFeature(ty: *mut PyTypeObject, feature: c_ulong) -> c_int {
684    #[cfg(Py_LIMITED_API)]
685    let flags = PyType_GetFlags(ty);
686
687    #[cfg(all(not(Py_LIMITED_API), Py_GIL_DISABLED))]
688    let flags = (*ty).tp_flags.load(std::sync::atomic::Ordering::Relaxed);
689
690    #[cfg(all(not(Py_LIMITED_API), not(Py_GIL_DISABLED)))]
691    let flags = (*ty).tp_flags;
692
693    ((flags & feature) != 0) as c_int
694}
695
696#[inline]
697pub unsafe fn PyType_FastSubclass(t: *mut PyTypeObject, f: c_ulong) -> c_int {
698    PyType_HasFeature(t, f)
699}
700
701#[inline]
702pub unsafe fn PyType_Check(op: *mut PyObject) -> c_int {
703    PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_TYPE_SUBCLASS)
704}
705
706// skipped _PyType_CAST
707
708#[inline]
709pub unsafe fn PyType_CheckExact(op: *mut PyObject) -> c_int {
710    Py_IS_TYPE(op, ptr::addr_of_mut!(PyType_Type))
711}
712
713extern "C" {
714    #[cfg(any(Py_3_13, all(Py_3_11, not(Py_LIMITED_API))))]
715    #[cfg_attr(PyPy, link_name = "PyPyType_GetModuleByDef")]
716    pub fn PyType_GetModuleByDef(
717        arg1: *mut crate::PyTypeObject,
718        arg2: *mut crate::PyModuleDef,
719    ) -> *mut PyObject;
720
721    #[cfg(Py_3_14)]
722    pub fn PyType_Freeze(tp: *mut crate::PyTypeObject) -> c_int;
723}