Skip to main content

pyo3_ffi/
object.rs

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