python3_sys/
object.rs

1use core::ptr;
2use libc::{c_char, c_int, c_uint, c_ulong, c_void};
3
4use crate::pyport::{Py_hash_t, Py_ssize_t};
5
6#[repr(C)]
7#[derive(Copy, Clone)]
8pub struct PyObject {
9    #[cfg(py_sys_config = "Py_TRACE_REFS")]
10    _ob_next: *mut PyObject,
11    #[cfg(py_sys_config = "Py_TRACE_REFS")]
12    _ob_prev: *mut PyObject,
13    pub ob_refcnt: Py_ssize_t,
14    pub ob_type: *mut PyTypeObject,
15}
16
17#[cfg(py_sys_config = "Py_TRACE_REFS")]
18pub const PyObject_HEAD_INIT: PyObject = PyObject {
19    _ob_next: 0 as *mut PyObject,
20    _ob_prev: 0 as *mut PyObject,
21    ob_refcnt: 1,
22    ob_type: 0 as *mut PyTypeObject,
23};
24
25#[cfg(not(py_sys_config = "Py_TRACE_REFS"))]
26pub const PyObject_HEAD_INIT: PyObject = PyObject {
27    ob_refcnt: 1,
28    ob_type: 0 as *mut PyTypeObject,
29};
30
31#[repr(C)]
32#[derive(Copy, Clone)]
33pub struct PyVarObject {
34    pub ob_base: PyObject,
35    pub ob_size: Py_ssize_t,
36}
37
38#[inline(always)]
39pub unsafe fn Py_REFCNT(ob: *mut PyObject) -> Py_ssize_t {
40    (*ob).ob_refcnt
41}
42
43#[inline(always)]
44pub unsafe fn Py_TYPE(ob: *mut PyObject) -> *mut PyTypeObject {
45    (*ob).ob_type
46}
47
48#[inline(always)]
49pub unsafe fn Py_SIZE(ob: *mut PyObject) -> Py_ssize_t {
50    (*(ob as *mut PyVarObject)).ob_size
51}
52
53pub type unaryfunc = unsafe extern "C" fn(arg1: *mut PyObject) -> *mut PyObject;
54pub type binaryfunc =
55    unsafe extern "C" fn(arg1: *mut PyObject, arg2: *mut PyObject) -> *mut PyObject;
56pub type ternaryfunc = unsafe extern "C" fn(
57    arg1: *mut PyObject,
58    arg2: *mut PyObject,
59    arg3: *mut PyObject,
60) -> *mut PyObject;
61pub type inquiry = unsafe extern "C" fn(arg1: *mut PyObject) -> c_int;
62pub type lenfunc = unsafe extern "C" fn(arg1: *mut PyObject) -> Py_ssize_t;
63pub type ssizeargfunc =
64    unsafe extern "C" fn(arg1: *mut PyObject, arg2: Py_ssize_t) -> *mut PyObject;
65pub type ssizessizeargfunc =
66    unsafe extern "C" fn(arg1: *mut PyObject, arg2: Py_ssize_t, arg3: Py_ssize_t) -> *mut PyObject;
67pub type ssizeobjargproc =
68    unsafe extern "C" fn(arg1: *mut PyObject, arg2: Py_ssize_t, arg3: *mut PyObject) -> c_int;
69pub type ssizessizeobjargproc = unsafe extern "C" fn(
70    arg1: *mut PyObject,
71    arg2: Py_ssize_t,
72    arg3: Py_ssize_t,
73    arg4: *mut PyObject,
74) -> c_int;
75pub type objobjargproc =
76    unsafe extern "C" fn(arg1: *mut PyObject, arg2: *mut PyObject, arg3: *mut PyObject) -> c_int;
77
78#[cfg(not(Py_LIMITED_API))]
79pub type getbufferproc = extern "C" fn(
80        arg1: *mut crate::object::PyObject,
81        arg2: *mut crate::pybuffer::Py_buffer,
82        arg3: c_int,
83    ) -> c_int;
84#[cfg(not(Py_LIMITED_API))]
85pub type releasebufferproc =
86    extern "C" fn(arg1: *mut crate::object::PyObject, arg2: *mut crate::pybuffer::Py_buffer) -> ();
87
88pub type objobjproc = unsafe extern "C" fn(arg1: *mut PyObject, arg2: *mut PyObject) -> c_int;
89pub type visitproc = unsafe extern "C" fn(object: *mut PyObject, arg: *mut c_void) -> c_int;
90pub type traverseproc =
91    unsafe extern "C" fn(slf: *mut PyObject, visit: visitproc, arg: *mut c_void) -> c_int;
92
93pub type freefunc = unsafe extern "C" fn(arg1: *mut c_void);
94pub type destructor = unsafe extern "C" fn(arg1: *mut PyObject);
95#[cfg(not(Py_LIMITED_API))]
96pub type printfunc =
97    unsafe extern "C" fn(arg1: *mut PyObject, arg2: *mut libc::FILE, arg3: c_int) -> c_int;
98pub type getattrfunc =
99    unsafe extern "C" fn(arg1: *mut PyObject, arg2: *mut c_char) -> *mut PyObject;
100pub type getattrofunc =
101    unsafe extern "C" fn(arg1: *mut PyObject, arg2: *mut PyObject) -> *mut PyObject;
102pub type setattrfunc =
103    unsafe extern "C" fn(arg1: *mut PyObject, arg2: *mut c_char, arg3: *mut PyObject) -> c_int;
104pub type setattrofunc =
105    unsafe extern "C" fn(arg1: *mut PyObject, arg2: *mut PyObject, arg3: *mut PyObject) -> c_int;
106pub type reprfunc = unsafe extern "C" fn(arg1: *mut PyObject) -> *mut PyObject;
107pub type hashfunc = unsafe extern "C" fn(arg1: *mut PyObject) -> Py_hash_t;
108pub type richcmpfunc =
109    unsafe extern "C" fn(arg1: *mut PyObject, arg2: *mut PyObject, arg3: c_int) -> *mut PyObject;
110pub type getiterfunc = unsafe extern "C" fn(arg1: *mut PyObject) -> *mut PyObject;
111pub type iternextfunc = unsafe extern "C" fn(arg1: *mut PyObject) -> *mut PyObject;
112pub type descrgetfunc = unsafe extern "C" fn(
113    arg1: *mut PyObject,
114    arg2: *mut PyObject,
115    arg3: *mut PyObject,
116) -> *mut PyObject;
117pub type descrsetfunc =
118    unsafe extern "C" fn(arg1: *mut PyObject, arg2: *mut PyObject, arg3: *mut PyObject) -> c_int;
119pub type initproc =
120    unsafe extern "C" fn(arg1: *mut PyObject, arg2: *mut PyObject, arg3: *mut PyObject) -> c_int;
121pub type newfunc = unsafe extern "C" fn(
122    arg1: *mut PyTypeObject,
123    arg2: *mut PyObject,
124    arg3: *mut PyObject,
125) -> *mut PyObject;
126pub type allocfunc =
127    unsafe extern "C" fn(arg1: *mut PyTypeObject, arg2: Py_ssize_t) -> *mut PyObject;
128
129#[cfg(Py_3_8)]
130pub type vectorcallfunc = unsafe extern "C" fn(
131    callable: *mut crate::object::PyObject,
132    args: *const *mut crate::object::PyObject,
133    nargsf: libc::size_t,
134    kwnames: *mut crate::object::PyObject,
135) -> *mut crate::object::PyObject;
136
137#[cfg(Py_LIMITED_API)]
138#[repr(C)]
139pub struct PyTypeObject {
140    _private: [u8; 0],
141}
142
143#[cfg(not(Py_LIMITED_API))]
144mod typeobject {
145    use libc::{c_char, c_uint, c_ulong, c_void};
146
147    use crate::pyport::Py_ssize_t;
148    #[cfg(Py_3_10)]
149    use super::PyObject;
150
151    #[repr(C)]
152    #[derive(Copy)]
153    pub struct PyNumberMethods {
154        pub nb_add: Option<crate::object::binaryfunc>,
155        pub nb_subtract: Option<crate::object::binaryfunc>,
156        pub nb_multiply: Option<crate::object::binaryfunc>,
157        pub nb_remainder: Option<crate::object::binaryfunc>,
158        pub nb_divmod: Option<crate::object::binaryfunc>,
159        pub nb_power: Option<crate::object::ternaryfunc>,
160        pub nb_negative: Option<crate::object::unaryfunc>,
161        pub nb_positive: Option<crate::object::unaryfunc>,
162        pub nb_absolute: Option<crate::object::unaryfunc>,
163        pub nb_bool: Option<crate::object::inquiry>,
164        pub nb_invert: Option<crate::object::unaryfunc>,
165        pub nb_lshift: Option<crate::object::binaryfunc>,
166        pub nb_rshift: Option<crate::object::binaryfunc>,
167        pub nb_and: Option<crate::object::binaryfunc>,
168        pub nb_xor: Option<crate::object::binaryfunc>,
169        pub nb_or: Option<crate::object::binaryfunc>,
170        pub nb_int: Option<crate::object::unaryfunc>,
171        pub nb_reserved: *mut c_void,
172        pub nb_float: Option<crate::object::unaryfunc>,
173        pub nb_inplace_add: Option<crate::object::binaryfunc>,
174        pub nb_inplace_subtract: Option<crate::object::binaryfunc>,
175        pub nb_inplace_multiply: Option<crate::object::binaryfunc>,
176        pub nb_inplace_remainder: Option<crate::object::binaryfunc>,
177        pub nb_inplace_power: Option<crate::object::ternaryfunc>,
178        pub nb_inplace_lshift: Option<crate::object::binaryfunc>,
179        pub nb_inplace_rshift: Option<crate::object::binaryfunc>,
180        pub nb_inplace_and: Option<crate::object::binaryfunc>,
181        pub nb_inplace_xor: Option<crate::object::binaryfunc>,
182        pub nb_inplace_or: Option<crate::object::binaryfunc>,
183        pub nb_floor_divide: Option<crate::object::binaryfunc>,
184        pub nb_true_divide: Option<crate::object::binaryfunc>,
185        pub nb_inplace_floor_divide: Option<crate::object::binaryfunc>,
186        pub nb_inplace_true_divide: Option<crate::object::binaryfunc>,
187        pub nb_index: Option<crate::object::unaryfunc>,
188        #[cfg(Py_3_5)]
189        pub nb_matrix_multiply: Option<crate::object::binaryfunc>,
190        #[cfg(Py_3_5)]
191        pub nb_inplace_matrix_multiply: Option<crate::object::binaryfunc>,
192    }
193    impl Clone for PyNumberMethods {
194        #[inline]
195        fn clone(&self) -> Self {
196            *self
197        }
198    }
199    impl Default for PyNumberMethods {
200        #[inline]
201        fn default() -> Self {
202            unsafe { core::mem::zeroed() }
203        }
204    }
205    macro_rules! as_expr {
206        ($e:expr) => {
207            $e
208        };
209    }
210
211    macro_rules! py_number_methods_init {
212        ($($tail:tt)*) => {
213            as_expr! {
214                PyNumberMethods {
215                    nb_add: None,
216                    nb_subtract: None,
217                    nb_multiply: None,
218                    nb_remainder: None,
219                    nb_divmod: None,
220                    nb_power: None,
221                    nb_negative: None,
222                    nb_positive: None,
223                    nb_absolute: None,
224                    nb_bool: None,
225                    nb_invert: None,
226                    nb_lshift: None,
227                    nb_rshift: None,
228                    nb_and: None,
229                    nb_xor: None,
230                    nb_or: None,
231                    nb_int: None,
232                    nb_reserved: 0 as *mut c_void,
233                    nb_float: None,
234                    nb_inplace_add: None,
235                    nb_inplace_subtract: None,
236                    nb_inplace_multiply: None,
237                    nb_inplace_remainder: None,
238                    nb_inplace_power: None,
239                    nb_inplace_lshift: None,
240                    nb_inplace_rshift: None,
241                    nb_inplace_and: None,
242                    nb_inplace_xor: None,
243                    nb_inplace_or: None,
244                    nb_floor_divide: None,
245                    nb_true_divide: None,
246                    nb_inplace_floor_divide: None,
247                    nb_inplace_true_divide: None,
248                    nb_index: None,
249                    $($tail)*
250                }
251            }
252        }
253    }
254
255    #[cfg(not(Py_3_5))]
256    pub const PyNumberMethods_INIT: PyNumberMethods = py_number_methods_init!();
257
258    #[cfg(Py_3_5)]
259    pub const PyNumberMethods_INIT: PyNumberMethods = py_number_methods_init! {
260        nb_matrix_multiply: None,
261        nb_inplace_matrix_multiply: None,
262    };
263
264    #[repr(C)]
265    #[derive(Copy)]
266    pub struct PySequenceMethods {
267        pub sq_length: Option<crate::object::lenfunc>,
268        pub sq_concat: Option<crate::object::binaryfunc>,
269        pub sq_repeat: Option<crate::object::ssizeargfunc>,
270        pub sq_item: Option<crate::object::ssizeargfunc>,
271        pub was_sq_slice: *mut c_void,
272        pub sq_ass_item: Option<crate::object::ssizeobjargproc>,
273        pub was_sq_ass_slice: *mut c_void,
274        pub sq_contains: Option<crate::object::objobjproc>,
275        pub sq_inplace_concat: Option<crate::object::binaryfunc>,
276        pub sq_inplace_repeat: Option<crate::object::ssizeargfunc>,
277    }
278    impl Clone for PySequenceMethods {
279        #[inline]
280        fn clone(&self) -> Self {
281            *self
282        }
283    }
284    impl Default for PySequenceMethods {
285        #[inline]
286        fn default() -> Self {
287            unsafe { core::mem::zeroed() }
288        }
289    }
290    pub const PySequenceMethods_INIT: PySequenceMethods = PySequenceMethods {
291        sq_length: None,
292        sq_concat: None,
293        sq_repeat: None,
294        sq_item: None,
295        was_sq_slice: 0 as *mut _,
296        sq_ass_item: None,
297        was_sq_ass_slice: 0 as *mut _,
298        sq_contains: None,
299        sq_inplace_concat: None,
300        sq_inplace_repeat: None,
301    };
302    #[repr(C)]
303    #[derive(Copy)]
304    pub struct PyMappingMethods {
305        pub mp_length: Option<crate::object::lenfunc>,
306        pub mp_subscript: Option<crate::object::binaryfunc>,
307        pub mp_ass_subscript: Option<crate::object::objobjargproc>,
308    }
309    impl Clone for PyMappingMethods {
310        #[inline]
311        fn clone(&self) -> Self {
312            *self
313        }
314    }
315    impl Default for PyMappingMethods {
316        #[inline]
317        fn default() -> Self {
318            unsafe { core::mem::zeroed() }
319        }
320    }
321    pub const PyMappingMethods_INIT: PyMappingMethods = PyMappingMethods {
322        mp_length: None,
323        mp_subscript: None,
324        mp_ass_subscript: None,
325    };
326    #[cfg(Py_3_10)]
327    pub type sendfunc = unsafe extern "C" fn(iter: *mut PyObject, value: *mut PyObject, result: *mut *mut PyObject) -> super::PySendResult;
328    #[repr(C)]
329    #[derive(Copy)]
330    #[cfg(Py_3_5)]
331    pub struct PyAsyncMethods {
332        pub am_await: Option<crate::object::unaryfunc>,
333        pub am_aiter: Option<crate::object::unaryfunc>,
334        pub am_anext: Option<crate::object::unaryfunc>,
335        #[cfg(Py_3_10)]
336        pub am_send: Option<sendfunc>,
337    }
338    #[cfg(Py_3_5)]
339    impl Clone for PyAsyncMethods {
340        #[inline]
341        fn clone(&self) -> Self {
342            *self
343        }
344    }
345    #[cfg(Py_3_5)]
346    impl Default for PyAsyncMethods {
347        #[inline]
348        fn default() -> Self {
349            unsafe { core::mem::zeroed() }
350        }
351    }
352    #[cfg(Py_3_5)]
353    pub const PyAsyncMethods_INIT: PyAsyncMethods = PyAsyncMethods {
354        am_await: None,
355        am_aiter: None,
356        am_anext: None,
357        #[cfg(Py_3_10)]
358        am_send: None
359    };
360    #[repr(C)]
361    #[derive(Copy)]
362    pub struct PyBufferProcs {
363        pub bf_getbuffer: Option<crate::object::getbufferproc>,
364        pub bf_releasebuffer: Option<crate::object::releasebufferproc>,
365    }
366    impl Clone for PyBufferProcs {
367        #[inline]
368        fn clone(&self) -> Self {
369            *self
370        }
371    }
372    impl Default for PyBufferProcs {
373        #[inline]
374        fn default() -> Self {
375            unsafe { core::mem::zeroed() }
376        }
377    }
378
379    #[repr(C)]
380    #[derive(Copy)]
381    pub struct PyTypeObject {
382        pub ob_base: crate::object::PyVarObject,
383        pub tp_name: *const c_char,
384        pub tp_basicsize: Py_ssize_t,
385        pub tp_itemsize: Py_ssize_t,
386        pub tp_dealloc: Option<crate::object::destructor>,
387        #[cfg(not(Py_3_8))]
388        pub tp_print: Option<crate::object::printfunc>,
389        #[cfg(Py_3_8)]
390        pub tp_vectorcall_offset: Py_ssize_t,
391        pub tp_getattr: Option<crate::object::getattrfunc>,
392        pub tp_setattr: Option<crate::object::setattrfunc>,
393        #[cfg(Py_3_5)]
394        pub tp_as_async: *mut PyAsyncMethods,
395        #[cfg(not(Py_3_5))]
396        pub tp_reserved: *mut c_void,
397        pub tp_repr: Option<crate::object::reprfunc>,
398        pub tp_as_number: *mut PyNumberMethods,
399        pub tp_as_sequence: *mut PySequenceMethods,
400        pub tp_as_mapping: *mut PyMappingMethods,
401        pub tp_hash: Option<crate::object::hashfunc>,
402        pub tp_call: Option<crate::object::ternaryfunc>,
403        pub tp_str: Option<crate::object::reprfunc>,
404        pub tp_getattro: Option<crate::object::getattrofunc>,
405        pub tp_setattro: Option<crate::object::setattrofunc>,
406        pub tp_as_buffer: *mut PyBufferProcs,
407        pub tp_flags: c_ulong,
408        pub tp_doc: *const c_char,
409        pub tp_traverse: Option<crate::object::traverseproc>,
410        pub tp_clear: Option<crate::object::inquiry>,
411        pub tp_richcompare: Option<crate::object::richcmpfunc>,
412        pub tp_weaklistoffset: Py_ssize_t,
413        pub tp_iter: Option<crate::object::getiterfunc>,
414        pub tp_iternext: Option<crate::object::iternextfunc>,
415        pub tp_methods: *mut crate::methodobject::PyMethodDef,
416        pub tp_members: *mut crate::structmember::PyMemberDef,
417        pub tp_getset: *mut crate::descrobject::PyGetSetDef,
418        pub tp_base: *mut PyTypeObject,
419        pub tp_dict: *mut crate::object::PyObject,
420        pub tp_descr_get: Option<crate::object::descrgetfunc>,
421        pub tp_descr_set: Option<crate::object::descrsetfunc>,
422        pub tp_dictoffset: Py_ssize_t,
423        pub tp_init: Option<crate::object::initproc>,
424        pub tp_alloc: Option<crate::object::allocfunc>,
425        pub tp_new: Option<crate::object::newfunc>,
426        pub tp_free: Option<crate::object::freefunc>,
427        pub tp_is_gc: Option<crate::object::inquiry>,
428        pub tp_bases: *mut crate::object::PyObject,
429        pub tp_mro: *mut crate::object::PyObject,
430        pub tp_cache: *mut crate::object::PyObject,
431        pub tp_subclasses: *mut crate::object::PyObject,
432        pub tp_weaklist: *mut crate::object::PyObject,
433        pub tp_del: Option<crate::object::destructor>,
434        pub tp_version_tag: c_uint,
435        #[cfg(Py_3_4)]
436        pub tp_finalize: Option<crate::object::destructor>,
437        #[cfg(Py_3_8)]
438        pub tp_vectorcall: Option<crate::object::vectorcallfunc>,
439        #[cfg(Py_3_12)]
440        pub tp_watched: c_char,
441        #[cfg(all(Py_3_8, not(Py_3_9)))]
442        pub tp_print: Option<crate::object::printfunc>,
443        #[cfg(all(py_sys_config = "COUNT_ALLOCS", not(Py_3_9)))]
444        pub tp_allocs: Py_ssize_t,
445        #[cfg(all(py_sys_config = "COUNT_ALLOCS", not(Py_3_9)))]
446        pub tp_frees: Py_ssize_t,
447        #[cfg(all(py_sys_config = "COUNT_ALLOCS", not(Py_3_9)))]
448        pub tp_maxalloc: Py_ssize_t,
449        #[cfg(all(py_sys_config = "COUNT_ALLOCS", not(Py_3_9)))]
450        pub tp_prev: *mut PyTypeObject,
451        #[cfg(all(py_sys_config = "COUNT_ALLOCS", not(Py_3_9)))]
452        pub tp_next: *mut PyTypeObject,
453    }
454    impl Clone for PyTypeObject {
455        #[inline]
456        fn clone(&self) -> Self {
457            *self
458        }
459    }
460
461    macro_rules! py_type_object_init {
462        ($($tail:tt)*) => {
463            as_expr! {
464                PyTypeObject {
465                    ob_base: crate::object::PyVarObject {
466                        ob_base: crate::object::PyObject_HEAD_INIT,
467                        ob_size: 0
468                    },
469                    tp_name: 0 as *const c_char,
470                    tp_basicsize: 0,
471                    tp_itemsize: 0,
472                    tp_dealloc: None,
473                    tp_getattr: None,
474                    tp_setattr: None,
475                    tp_repr: None,
476                    tp_as_number: 0 as *mut PyNumberMethods,
477                    tp_as_sequence: 0 as *mut PySequenceMethods,
478                    tp_as_mapping: 0 as *mut PyMappingMethods,
479                    tp_hash: None,
480                    tp_call: None,
481                    tp_str: None,
482                    tp_getattro: None,
483                    tp_setattro: None,
484                    tp_as_buffer: 0 as *mut PyBufferProcs,
485                    tp_flags: crate::object::Py_TPFLAGS_DEFAULT,
486                    tp_doc: 0 as *const c_char,
487                    tp_traverse: None,
488                    tp_clear: None,
489                    tp_richcompare: None,
490                    tp_weaklistoffset: 0,
491                    tp_iter: None,
492                    tp_iternext: None,
493                    tp_methods: 0 as *mut crate::methodobject::PyMethodDef,
494                    tp_members: 0 as *mut crate::structmember::PyMemberDef,
495                    tp_getset: 0 as *mut crate::descrobject::PyGetSetDef,
496                    tp_base: 0 as *mut PyTypeObject,
497                    tp_dict: 0 as *mut crate::object::PyObject,
498                    tp_descr_get: None,
499                    tp_descr_set: None,
500                    tp_dictoffset: 0,
501                    tp_init: None,
502                    tp_alloc: None,
503                    tp_new: None,
504                    tp_free: None,
505                    tp_is_gc: None,
506                    tp_bases: 0 as *mut crate::object::PyObject,
507                    tp_mro: 0 as *mut crate::object::PyObject,
508                    tp_cache: 0 as *mut crate::object::PyObject,
509                    tp_subclasses: 0 as *mut crate::object::PyObject,
510                    tp_weaklist: 0 as *mut crate::object::PyObject,
511                    tp_del: None,
512                    tp_version_tag: 0,
513                    $($tail)*
514                }
515            }
516        }
517    }
518
519    #[cfg(py_sys_config = "COUNT_ALLOCS")]
520    macro_rules! py_type_object_init_with_count_allocs {
521        ($($tail:tt)*) => {
522            py_type_object_init!(
523                $($tail)*
524                tp_allocs: 0,
525                tp_frees: 0,
526                tp_maxalloc: 0,
527                tp_prev: 0 as *mut PyTypeObject,
528                tp_next: 0 as *mut PyTypeObject,
529            )
530        }
531    }
532
533    #[cfg(not(py_sys_config = "COUNT_ALLOCS"))]
534    macro_rules! py_type_object_init_with_count_allocs {
535        ($($tail:tt)*) => {
536            py_type_object_init!($($tail)*)
537        }
538    }
539
540    #[cfg(Py_3_12)]
541    pub const PyTypeObject_INIT: PyTypeObject = py_type_object_init_with_count_allocs!(
542        tp_as_async: 0 as *mut PyAsyncMethods,
543        tp_vectorcall_offset: 0,
544        tp_vectorcall: None,
545        tp_finalize: None,
546        tp_watched: 0,
547    );
548
549    #[cfg(all(Py_3_9, not(Py_3_12)))]
550    pub const PyTypeObject_INIT: PyTypeObject = py_type_object_init_with_count_allocs!(
551        tp_as_async: 0 as *mut PyAsyncMethods,
552        tp_vectorcall_offset: 0,
553        tp_vectorcall: None,
554        tp_finalize: None,
555    );
556
557    #[cfg(all(Py_3_8, not(Py_3_9)))]
558    pub const PyTypeObject_INIT: PyTypeObject = py_type_object_init_with_count_allocs!(
559        tp_print: None,
560        tp_as_async: 0 as *mut PyAsyncMethods,
561        tp_vectorcall_offset: 0,
562        tp_vectorcall: None,
563        tp_finalize: None,
564    );
565
566    #[cfg(all(Py_3_5, not(Py_3_8)))]
567    pub const PyTypeObject_INIT: PyTypeObject = py_type_object_init_with_count_allocs!(
568        tp_print: None,
569        tp_as_async: 0 as *mut PyAsyncMethods,
570        tp_finalize: None,
571    );
572
573    #[cfg(all(Py_3_4, not(Py_3_5)))]
574    pub const PyTypeObject_INIT: PyTypeObject = py_type_object_init_with_count_allocs!(
575        tp_print: None,
576        tp_reserved: 0 as *mut c_void,
577        tp_finalize: None,
578    );
579
580    #[cfg(not(Py_3_4))]
581    pub const PyTypeObject_INIT: PyTypeObject = py_type_object_init_with_count_allocs!(
582        tp_print: None,
583        tp_reserved: 0 as *mut c_void,
584    );
585
586    impl PyTypeObject {
587        #[inline]
588        pub fn init_ob_type(&mut self, type_object: *mut PyTypeObject) {
589            self.ob_base.ob_base.ob_type = type_object;
590        }
591    }
592
593    #[repr(C)]
594    #[derive(Copy)]
595    pub struct PyHeapTypeObject {
596        pub ht_type: PyTypeObject,
597        #[cfg(Py_3_5)]
598        pub as_async: PyAsyncMethods,
599        pub as_number: PyNumberMethods,
600        pub as_mapping: PyMappingMethods,
601        pub as_sequence: PySequenceMethods,
602        pub as_buffer: PyBufferProcs,
603        pub ht_name: *mut crate::object::PyObject,
604        pub ht_slots: *mut crate::object::PyObject,
605        pub ht_qualname: *mut crate::object::PyObject,
606        pub ht_cached_keys: *mut c_void,
607        #[cfg(Py_3_9)]
608        pub ht_module: *mut crate::object::PyObject,
609        #[cfg(Py_3_11)]
610        _ht_tpname: *mut c_char,
611        #[cfg(Py_3_11)]
612        _spec_cache: *mut c_void,
613    }
614    impl Clone for PyHeapTypeObject {
615        #[inline]
616        fn clone(&self) -> Self {
617            *self
618        }
619    }
620    impl Default for PyHeapTypeObject {
621        #[inline]
622        fn default() -> Self {
623            unsafe { core::mem::zeroed() }
624        }
625    }
626
627    #[inline]
628    #[cfg(not(Py_3_11))]
629    pub unsafe fn PyHeapType_GET_MEMBERS(
630        etype: *mut PyHeapTypeObject,
631    ) -> *mut crate::structmember::PyMemberDef {
632        (etype as *mut c_char).offset(
633            (*crate::object::Py_TYPE(etype as *mut crate::object::PyObject)).tp_basicsize as isize,
634        ) as *mut crate::structmember::PyMemberDef
635    }
636}
637#[cfg(not(Py_LIMITED_API))]
638pub use self::typeobject::*;
639
640#[repr(C)]
641#[derive(Copy)]
642pub struct PyType_Slot {
643    pub slot: c_int,
644    pub pfunc: *mut c_void,
645}
646impl Clone for PyType_Slot {
647    fn clone(&self) -> PyType_Slot {
648        *self
649    }
650}
651impl Default for PyType_Slot {
652    fn default() -> PyType_Slot {
653        unsafe { core::mem::zeroed() }
654    }
655}
656
657#[repr(C)]
658#[derive(Copy)]
659pub struct PyType_Spec {
660    pub name: *const c_char,
661    pub basicsize: c_int,
662    pub itemsize: c_int,
663    pub flags: c_uint,
664    pub slots: *mut PyType_Slot,
665}
666impl Clone for PyType_Spec {
667    fn clone(&self) -> PyType_Spec {
668        *self
669    }
670}
671impl Default for PyType_Spec {
672    fn default() -> PyType_Spec {
673        unsafe { core::mem::zeroed() }
674    }
675}
676
677#[cfg_attr(windows, link(name = "pythonXY"))]
678extern "C" {
679    pub fn PyType_FromSpec(arg1: *mut PyType_Spec) -> *mut PyObject;
680
681    //#[cfg(Py_3_3)]
682    pub fn PyType_FromSpecWithBases(arg1: *mut PyType_Spec, arg2: *mut PyObject) -> *mut PyObject;
683
684    #[cfg(Py_3_4)]
685    pub fn PyType_GetSlot(arg1: *mut PyTypeObject, arg2: c_int) -> *mut c_void;
686
687    #[cfg(Py_3_9)]
688    pub fn PyType_FromModuleAndSpec(
689        arg1: *mut PyObject,
690        arg2: *mut PyType_Spec,
691        arg3: *mut PyObject,
692    ) -> *mut PyObject;
693
694    #[cfg(Py_3_9)]
695    pub fn PyType_GetModule(arg1: *mut PyTypeObject) -> *mut PyObject;
696
697    #[cfg(Py_3_9)]
698    pub fn PyType_GetModuleState(arg1: *mut PyTypeObject) -> *mut c_void;
699
700    #[cfg(Py_3_11)]
701    pub fn PyType_GetName(arg1: *mut PyTypeObject) -> *mut PyObject;
702
703    #[cfg(Py_3_11)]
704    pub fn PyType_GetQualName(arg1: *mut PyTypeObject) -> *mut PyObject;
705}
706
707#[cfg_attr(windows, link(name = "pythonXY"))]
708extern "C" {
709    pub fn PyType_IsSubtype(a: *mut PyTypeObject, b: *mut PyTypeObject) -> c_int;
710}
711
712#[inline(always)]
713pub unsafe fn PyObject_TypeCheck(ob: *mut PyObject, tp: *mut PyTypeObject) -> c_int {
714    (Py_TYPE(ob) == tp || PyType_IsSubtype(Py_TYPE(ob), tp) != 0) as c_int
715}
716
717#[cfg_attr(windows, link(name = "pythonXY"))]
718extern "C" {
719    /// built-in 'type'
720    pub static mut PyType_Type: PyTypeObject;
721    /// built-in 'object'
722    pub static mut PyBaseObject_Type: PyTypeObject;
723    /// built-in 'super'
724    pub static mut PySuper_Type: PyTypeObject;
725
726    pub fn PyType_GetFlags(arg1: *mut PyTypeObject) -> c_ulong;
727}
728
729#[inline(always)]
730pub unsafe fn PyType_Check(op: *mut PyObject) -> c_int {
731    PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_TYPE_SUBCLASS)
732}
733
734#[inline(always)]
735pub unsafe fn PyType_CheckExact(op: *mut PyObject) -> c_int {
736    (Py_TYPE(op) == &mut PyType_Type) as c_int
737}
738
739#[cfg_attr(windows, link(name = "pythonXY"))]
740extern "C" {
741    pub fn PyType_Ready(t: *mut PyTypeObject) -> c_int;
742    pub fn PyType_GenericAlloc(t: *mut PyTypeObject, nitems: Py_ssize_t) -> *mut PyObject;
743    pub fn PyType_GenericNew(
744        t: *mut PyTypeObject,
745        args: *mut PyObject,
746        kwds: *mut PyObject,
747    ) -> *mut PyObject;
748    pub fn PyType_ClearCache() -> c_uint;
749    pub fn PyType_Modified(t: *mut PyTypeObject);
750
751    #[cfg(not(Py_LIMITED_API))]
752    pub fn PyObject_Print(o: *mut PyObject, fp: *mut libc::FILE, flags: c_int) -> c_int;
753    pub fn PyObject_Repr(o: *mut PyObject) -> *mut PyObject;
754    pub fn PyObject_Str(o: *mut PyObject) -> *mut PyObject;
755    pub fn PyObject_ASCII(arg1: *mut PyObject) -> *mut PyObject;
756    pub fn PyObject_Bytes(arg1: *mut PyObject) -> *mut PyObject;
757    pub fn PyObject_RichCompare(
758        arg1: *mut PyObject,
759        arg2: *mut PyObject,
760        arg3: c_int,
761    ) -> *mut PyObject;
762    pub fn PyObject_RichCompareBool(arg1: *mut PyObject, arg2: *mut PyObject, arg3: c_int)
763        -> c_int;
764    pub fn PyObject_GetAttrString(arg1: *mut PyObject, arg2: *const c_char) -> *mut PyObject;
765    pub fn PyObject_SetAttrString(
766        arg1: *mut PyObject,
767        arg2: *const c_char,
768        arg3: *mut PyObject,
769    ) -> c_int;
770    pub fn PyObject_HasAttrString(arg1: *mut PyObject, arg2: *const c_char) -> c_int;
771    pub fn PyObject_GetAttr(arg1: *mut PyObject, arg2: *mut PyObject) -> *mut PyObject;
772    pub fn PyObject_SetAttr(arg1: *mut PyObject, arg2: *mut PyObject, arg3: *mut PyObject)
773        -> c_int;
774    pub fn PyObject_HasAttr(arg1: *mut PyObject, arg2: *mut PyObject) -> c_int;
775    pub fn PyObject_SelfIter(arg1: *mut PyObject) -> *mut PyObject;
776
777    #[cfg(not(Py_LIMITED_API))]
778    pub fn _PyObject_NextNotImplemented(arg1: *mut PyObject) -> *mut PyObject;
779
780    pub fn PyObject_GenericGetAttr(arg1: *mut PyObject, arg2: *mut PyObject) -> *mut PyObject;
781    pub fn PyObject_GenericSetAttr(
782        arg1: *mut PyObject,
783        arg2: *mut PyObject,
784        arg3: *mut PyObject,
785    ) -> c_int;
786    pub fn PyObject_GenericSetDict(
787        arg1: *mut PyObject,
788        arg2: *mut PyObject,
789        arg3: *mut c_void,
790    ) -> c_int;
791    pub fn PyObject_Hash(arg1: *mut PyObject) -> Py_hash_t;
792    pub fn PyObject_HashNotImplemented(arg1: *mut PyObject) -> Py_hash_t;
793    pub fn PyObject_IsTrue(arg1: *mut PyObject) -> c_int;
794    pub fn PyObject_Not(arg1: *mut PyObject) -> c_int;
795    pub fn PyCallable_Check(arg1: *mut PyObject) -> c_int;
796    pub fn PyObject_ClearWeakRefs(arg1: *mut PyObject) -> ();
797    #[cfg(Py_3_4)]
798    #[cfg(not(Py_LIMITED_API))]
799    pub fn PyObject_CallFinalizer(arg1: *mut PyObject) -> ();
800    #[cfg(Py_3_4)]
801    #[cfg(not(Py_LIMITED_API))]
802    pub fn PyObject_CallFinalizerFromDealloc(arg1: *mut PyObject) -> c_int;
803
804    pub fn PyObject_Dir(arg1: *mut PyObject) -> *mut PyObject;
805    pub fn Py_ReprEnter(arg1: *mut PyObject) -> c_int;
806    pub fn Py_ReprLeave(arg1: *mut PyObject) -> ();
807}
808
809// Flag bits for printing:
810pub const Py_PRINT_RAW: c_int = 1; // No string quotes etc.
811
812/// Disallow creating instances of the type: set tp_new to NULL and don't create
813/// the "__new__" key in the type dictionary.
814#[cfg(Py_3_10)]
815pub const Py_TPFLAGS_DISALLOW_INSTANTIATION: c_ulong = (1 << 7);
816
817/// Set if the type object is immutable: type attributes cannot be set nor deleted
818#[cfg(Py_3_10)]
819pub const Py_TPFLAGS_IMMUTABLETYPE: c_ulong = (1 << 8);
820
821/// Set if the type object is dynamically allocated
822pub const Py_TPFLAGS_HEAPTYPE: c_ulong = (1 << 9);
823
824/// Set if the type allows subclassing
825pub const Py_TPFLAGS_BASETYPE: c_ulong = (1 << 10);
826
827#[cfg(all(Py_3_8, not(Py_LIMITED_API)))]
828pub const Py_TPFLAGS_HAVE_VECTORCALL: c_ulong = (1 << 11);
829
830/// Set if the type is 'ready' -- fully initialized
831pub const Py_TPFLAGS_READY: c_ulong = (1 << 12);
832
833/// Set while the type is being 'readied', to prevent recursive ready calls
834pub const Py_TPFLAGS_READYING: c_ulong = (1 << 13);
835
836/// Objects support garbage collection (see objimp.h)
837pub const Py_TPFLAGS_HAVE_GC: c_ulong = (1 << 14);
838
839const Py_TPFLAGS_HAVE_STACKLESS_EXTENSION: c_ulong = 0;
840
841#[cfg(Py_3_8)]
842pub const Py_TPFLAGS_METHOD_DESCRIPTOR: c_ulong = (1 << 17);
843
844/// Object has up-to-date type attribute cache
845pub const Py_TPFLAGS_VALID_VERSION_TAG: c_ulong = (1 << 19);
846
847/* Type is abstract and cannot be instantiated */
848pub const Py_TPFLAGS_IS_ABSTRACT: c_ulong = (1 << 20);
849
850/* These flags are used to determine if a type is a subclass. */
851pub const Py_TPFLAGS_LONG_SUBCLASS: c_ulong = (1 << 24);
852pub const Py_TPFLAGS_LIST_SUBCLASS: c_ulong = (1 << 25);
853pub const Py_TPFLAGS_TUPLE_SUBCLASS: c_ulong = (1 << 26);
854pub const Py_TPFLAGS_BYTES_SUBCLASS: c_ulong = (1 << 27);
855pub const Py_TPFLAGS_UNICODE_SUBCLASS: c_ulong = (1 << 28);
856pub const Py_TPFLAGS_DICT_SUBCLASS: c_ulong = (1 << 29);
857pub const Py_TPFLAGS_BASE_EXC_SUBCLASS: c_ulong = (1 << 30);
858pub const Py_TPFLAGS_TYPE_SUBCLASS: c_ulong = (1 << 31);
859
860#[cfg(not(Py_3_10))]
861pub const Py_TPFLAGS_DEFAULT: c_ulong =
862    (Py_TPFLAGS_HAVE_STACKLESS_EXTENSION | Py_TPFLAGS_HAVE_VERSION_TAG | 0);
863
864#[cfg(Py_3_10)]
865pub const Py_TPFLAGS_DEFAULT: c_ulong =
866    (Py_TPFLAGS_HAVE_STACKLESS_EXTENSION | 0);
867
868
869pub const Py_TPFLAGS_HAVE_FINALIZE: c_ulong = (1 << 0);
870pub const Py_TPFLAGS_HAVE_VERSION_TAG: c_ulong = (1 << 18);
871
872#[inline(always)]
873#[cfg(Py_LIMITED_API)]
874pub unsafe fn PyType_HasFeature(t: *mut PyTypeObject, f: c_ulong) -> c_int {
875    ((PyType_GetFlags(t) & f) != 0) as c_int
876}
877
878#[inline(always)]
879#[cfg(not(Py_LIMITED_API))]
880pub unsafe fn PyType_HasFeature(t: *mut PyTypeObject, f: c_ulong) -> c_int {
881    (((*t).tp_flags & f) != 0) as c_int
882}
883
884#[inline(always)]
885pub unsafe fn PyType_FastSubclass(t: *mut PyTypeObject, f: c_ulong) -> c_int {
886    PyType_HasFeature(t, f)
887}
888
889#[cfg_attr(windows, link(name = "pythonXY"))]
890extern "C" {
891    pub fn _Py_Dealloc(arg1: *mut PyObject) -> ();
892}
893
894// Reference counting macros.
895#[inline(always)]
896pub unsafe fn Py_INCREF(op: *mut PyObject) {
897    if cfg!(py_sys_config = "Py_REF_DEBUG") {
898        Py_IncRef(op)
899    } else {
900        (*op).ob_refcnt += 1
901    }
902}
903
904#[inline(always)]
905pub unsafe fn Py_DECREF(op: *mut PyObject) {
906    if cfg!(py_sys_config = "Py_REF_DEBUG") {
907        Py_DecRef(op)
908    } else {
909        (*op).ob_refcnt -= 1;
910        if (*op).ob_refcnt == 0 {
911            _Py_Dealloc(op)
912        }
913    }
914}
915
916#[inline(always)]
917pub unsafe fn Py_CLEAR(op: &mut *mut PyObject) {
918    let tmp = *op;
919    if !tmp.is_null() {
920        *op = ptr::null_mut();
921        Py_DECREF(tmp);
922    }
923}
924
925#[inline(always)]
926pub unsafe fn Py_XINCREF(op: *mut PyObject) {
927    if !op.is_null() {
928        Py_INCREF(op)
929    }
930}
931
932#[inline(always)]
933pub unsafe fn Py_XDECREF(op: *mut PyObject) {
934    if !op.is_null() {
935        Py_DECREF(op)
936    }
937}
938
939#[cfg_attr(windows, link(name = "pythonXY"))]
940extern "C" {
941    pub fn Py_IncRef(o: *mut PyObject);
942    pub fn Py_DecRef(o: *mut PyObject);
943
944    #[cfg(Py_3_10)]
945    pub fn Py_NewRef(o: *mut PyObject) -> *mut PyObject;
946    #[cfg(Py_3_10)]
947    pub fn Py_XNewRef(o: *mut PyObject) -> *mut PyObject;
948
949    static mut _Py_NoneStruct: PyObject;
950    static mut _Py_NotImplementedStruct: PyObject;
951}
952
953#[inline(always)]
954pub unsafe fn Py_None() -> *mut PyObject {
955    &mut _Py_NoneStruct
956}
957
958#[inline(always)]
959pub unsafe fn Py_IsNone(x: *mut PyObject) -> c_int {
960    (x == Py_None()) as c_int
961}
962
963#[inline(always)]
964pub unsafe fn Py_NotImplemented() -> *mut PyObject {
965    &mut _Py_NotImplementedStruct
966}
967
968/* Rich comparison opcodes */
969pub const Py_LT: c_int = 0;
970pub const Py_LE: c_int = 1;
971pub const Py_EQ: c_int = 2;
972pub const Py_NE: c_int = 3;
973pub const Py_GT: c_int = 4;
974pub const Py_GE: c_int = 5;
975
976/* Result of calling PyIter_Send */
977#[cfg(Py_3_10)]
978#[repr(C)]
979pub enum PySendResult {
980    PYGEN_RETURN = 0,
981    PYGEN_ERROR = -1,
982    PYGEN_NEXT = 1,
983}