1#[cfg(Py_3_8)]
2use crate::vectorcallfunc;
3use crate::{object, PyGetSetDef, PyMemberDef, PyMethodDef, PyObject, Py_ssize_t};
4use std::mem;
5use std::os::raw::{c_char, c_int, c_uint, c_ulong, c_void};
6
7// skipped _Py_NewReference
8// skipped _Py_ForgetReference
9// skipped _Py_GetRefTotal
10
11// skipped _Py_Identifier
12
13// skipped _Py_static_string_init
14// skipped _Py_static_string
15// skipped _Py_IDENTIFIER
16
17#[cfg(not(Py_3_11))] // moved to src/buffer.rs from Python
18mod bufferinfo {
19 use crate::Py_ssize_t;
20 use std::os::raw::{c_char, c_int, c_void};
21 use std::ptr;
22
23 #[cfg(PyPy)]
24 const Py_MAX_NDIMS: usize = 36;
25
26 #[repr(C)]
27 #[derive(Copy, Clone)]
28 pub struct Py_buffer {
29 pub buf: *mut c_void,
30 /// Owned reference
31 pub obj: *mut crate::PyObject,
32 pub len: Py_ssize_t,
33 pub itemsize: Py_ssize_t,
34 pub readonly: c_int,
35 pub ndim: c_int,
36 pub format: *mut c_char,
37 pub shape: *mut Py_ssize_t,
38 pub strides: *mut Py_ssize_t,
39 pub suboffsets: *mut Py_ssize_t,
40 pub internal: *mut c_void,
41 #[cfg(PyPy)]
42 pub flags: c_int,
43 #[cfg(PyPy)]
44 pub _strides: [Py_ssize_t; Py_MAX_NDIMS],
45 #[cfg(PyPy)]
46 pub _shape: [Py_ssize_t; Py_MAX_NDIMS],
47 }
48
49 impl Py_buffer {
50 pub const fn new() -> Self {
51 Py_buffer {
52 buf: ptr::null_mut(),
53 obj: ptr::null_mut(),
54 len: 0,
55 itemsize: 0,
56 readonly: 0,
57 ndim: 0,
58 format: ptr::null_mut(),
59 shape: ptr::null_mut(),
60 strides: ptr::null_mut(),
61 suboffsets: ptr::null_mut(),
62 internal: ptr::null_mut(),
63 #[cfg(PyPy)]
64 flags: 0,
65 #[cfg(PyPy)]
66 _strides: [0; Py_MAX_NDIMS],
67 #[cfg(PyPy)]
68 _shape: [0; Py_MAX_NDIMS],
69 }
70 }
71 }
72
73 pub type getbufferproc = unsafe extern "C" fn(
74 arg1: *mut crate::PyObject,
75 arg2: *mut Py_buffer,
76 arg3: c_int,
77 ) -> c_int;
78 pub type releasebufferproc =
79 unsafe extern "C" fn(arg1: *mut crate::PyObject, arg2: *mut Py_buffer);
80
81 /// Maximum number of dimensions
82 pub const PyBUF_MAX_NDIM: c_int = 64;
83
84 /* Flags for getting buffers */
85 pub const PyBUF_SIMPLE: c_int = 0;
86 pub const PyBUF_WRITABLE: c_int = 0x0001;
87 /* we used to include an E, backwards compatible alias */
88 pub const PyBUF_WRITEABLE: c_int = PyBUF_WRITABLE;
89 pub const PyBUF_FORMAT: c_int = 0x0004;
90 pub const PyBUF_ND: c_int = 0x0008;
91 pub const PyBUF_STRIDES: c_int = 0x0010 | PyBUF_ND;
92 pub const PyBUF_C_CONTIGUOUS: c_int = 0x0020 | PyBUF_STRIDES;
93 pub const PyBUF_F_CONTIGUOUS: c_int = 0x0040 | PyBUF_STRIDES;
94 pub const PyBUF_ANY_CONTIGUOUS: c_int = 0x0080 | PyBUF_STRIDES;
95 pub const PyBUF_INDIRECT: c_int = 0x0100 | PyBUF_STRIDES;
96
97 pub const PyBUF_CONTIG: c_int = PyBUF_ND | PyBUF_WRITABLE;
98 pub const PyBUF_CONTIG_RO: c_int = PyBUF_ND;
99
100 pub const PyBUF_STRIDED: c_int = PyBUF_STRIDES | PyBUF_WRITABLE;
101 pub const PyBUF_STRIDED_RO: c_int = PyBUF_STRIDES;
102
103 pub const PyBUF_RECORDS: c_int = PyBUF_STRIDES | PyBUF_WRITABLE | PyBUF_FORMAT;
104 pub const PyBUF_RECORDS_RO: c_int = PyBUF_STRIDES | PyBUF_FORMAT;
105
106 pub const PyBUF_FULL: c_int = PyBUF_INDIRECT | PyBUF_WRITABLE | PyBUF_FORMAT;
107 pub const PyBUF_FULL_RO: c_int = PyBUF_INDIRECT | PyBUF_FORMAT;
108
109 pub const PyBUF_READ: c_int = 0x100;
110 pub const PyBUF_WRITE: c_int = 0x200;
111}
112
113#[cfg(not(Py_3_11))]
114pub use self::bufferinfo::*;
115
116#[repr(C)]
117#[derive(Copy, Clone)]
118pub struct PyNumberMethods {
119 pub nb_add: Option<object::binaryfunc>,
120 pub nb_subtract: Option<object::binaryfunc>,
121 pub nb_multiply: Option<object::binaryfunc>,
122 pub nb_remainder: Option<object::binaryfunc>,
123 pub nb_divmod: Option<object::binaryfunc>,
124 pub nb_power: Option<object::ternaryfunc>,
125 pub nb_negative: Option<object::unaryfunc>,
126 pub nb_positive: Option<object::unaryfunc>,
127 pub nb_absolute: Option<object::unaryfunc>,
128 pub nb_bool: Option<object::inquiry>,
129 pub nb_invert: Option<object::unaryfunc>,
130 pub nb_lshift: Option<object::binaryfunc>,
131 pub nb_rshift: Option<object::binaryfunc>,
132 pub nb_and: Option<object::binaryfunc>,
133 pub nb_xor: Option<object::binaryfunc>,
134 pub nb_or: Option<object::binaryfunc>,
135 pub nb_int: Option<object::unaryfunc>,
136 pub nb_reserved: *mut c_void,
137 pub nb_float: Option<object::unaryfunc>,
138 pub nb_inplace_add: Option<object::binaryfunc>,
139 pub nb_inplace_subtract: Option<object::binaryfunc>,
140 pub nb_inplace_multiply: Option<object::binaryfunc>,
141 pub nb_inplace_remainder: Option<object::binaryfunc>,
142 pub nb_inplace_power: Option<object::ternaryfunc>,
143 pub nb_inplace_lshift: Option<object::binaryfunc>,
144 pub nb_inplace_rshift: Option<object::binaryfunc>,
145 pub nb_inplace_and: Option<object::binaryfunc>,
146 pub nb_inplace_xor: Option<object::binaryfunc>,
147 pub nb_inplace_or: Option<object::binaryfunc>,
148 pub nb_floor_divide: Option<object::binaryfunc>,
149 pub nb_true_divide: Option<object::binaryfunc>,
150 pub nb_inplace_floor_divide: Option<object::binaryfunc>,
151 pub nb_inplace_true_divide: Option<object::binaryfunc>,
152 pub nb_index: Option<object::unaryfunc>,
153 pub nb_matrix_multiply: Option<object::binaryfunc>,
154 pub nb_inplace_matrix_multiply: Option<object::binaryfunc>,
155}
156
157#[repr(C)]
158#[derive(Clone)]
159pub struct PySequenceMethods {
160 pub sq_length: Option<object::lenfunc>,
161 pub sq_concat: Option<object::binaryfunc>,
162 pub sq_repeat: Option<object::ssizeargfunc>,
163 pub sq_item: Option<object::ssizeargfunc>,
164 pub was_sq_slice: *mut c_void,
165 pub sq_ass_item: Option<object::ssizeobjargproc>,
166 pub was_sq_ass_slice: *mut c_void,
167 pub sq_contains: Option<object::objobjproc>,
168 pub sq_inplace_concat: Option<object::binaryfunc>,
169 pub sq_inplace_repeat: Option<object::ssizeargfunc>,
170}
171
172#[repr(C)]
173#[derive(Clone, Default)]
174pub struct PyMappingMethods {
175 pub mp_length: Option<object::lenfunc>,
176 pub mp_subscript: Option<object::binaryfunc>,
177 pub mp_ass_subscript: Option<object::objobjargproc>,
178}
179
180#[cfg(Py_3_10)]
181pub type sendfunc = unsafe extern "C" fn(
182 iter: *mut PyObject,
183 value: *mut PyObject,
184 result: *mut *mut PyObject,
185) -> object::PySendResult;
186
187#[repr(C)]
188#[derive(Clone, Default)]
189pub struct PyAsyncMethods {
190 pub am_await: Option<object::unaryfunc>,
191 pub am_aiter: Option<object::unaryfunc>,
192 pub am_anext: Option<object::unaryfunc>,
193 #[cfg(Py_3_10)]
194 pub am_send: Option<sendfunc>,
195}
196
197#[repr(C)]
198#[derive(Clone, Default)]
199pub struct PyBufferProcs {
200 pub bf_getbuffer: Option<crate::getbufferproc>,
201 pub bf_releasebuffer: Option<crate::releasebufferproc>,
202}
203
204pub type printfunc =
205 unsafe extern "C" fn(arg1: *mut PyObject, arg2: *mut ::libc::FILE, arg3: c_int) -> c_int;
206
207#[repr(C)]
208#[derive(Debug, Copy, Clone)]
209pub struct PyTypeObject {
210 #[cfg(all(PyPy, not(Py_3_9)))]
211 pub ob_refcnt: Py_ssize_t,
212 #[cfg(all(PyPy, not(Py_3_9)))]
213 pub ob_pypy_link: Py_ssize_t,
214 #[cfg(all(PyPy, not(Py_3_9)))]
215 pub ob_type: *mut PyTypeObject,
216 #[cfg(all(PyPy, not(Py_3_9)))]
217 pub ob_size: Py_ssize_t,
218 #[cfg(not(all(PyPy, not(Py_3_9))))]
219 pub ob_base: object::PyVarObject,
220 pub tp_name: *const c_char,
221 pub tp_basicsize: Py_ssize_t,
222 pub tp_itemsize: Py_ssize_t,
223 pub tp_dealloc: Option<object::destructor>,
224 #[cfg(not(Py_3_8))]
225 pub tp_print: Option<printfunc>,
226 #[cfg(Py_3_8)]
227 pub tp_vectorcall_offset: Py_ssize_t,
228 pub tp_getattr: Option<object::getattrfunc>,
229 pub tp_setattr: Option<object::setattrfunc>,
230 pub tp_as_async: *mut PyAsyncMethods,
231 pub tp_repr: Option<object::reprfunc>,
232 pub tp_as_number: *mut PyNumberMethods,
233 pub tp_as_sequence: *mut PySequenceMethods,
234 pub tp_as_mapping: *mut PyMappingMethods,
235 pub tp_hash: Option<object::hashfunc>,
236 pub tp_call: Option<object::ternaryfunc>,
237 pub tp_str: Option<object::reprfunc>,
238 pub tp_getattro: Option<object::getattrofunc>,
239 pub tp_setattro: Option<object::setattrofunc>,
240 pub tp_as_buffer: *mut PyBufferProcs,
241 pub tp_flags: c_ulong,
242 pub tp_doc: *const c_char,
243 pub tp_traverse: Option<object::traverseproc>,
244 pub tp_clear: Option<object::inquiry>,
245 pub tp_richcompare: Option<object::richcmpfunc>,
246 pub tp_weaklistoffset: Py_ssize_t,
247 pub tp_iter: Option<object::getiterfunc>,
248 pub tp_iternext: Option<object::iternextfunc>,
249 pub tp_methods: *mut PyMethodDef,
250 pub tp_members: *mut PyMemberDef,
251 pub tp_getset: *mut PyGetSetDef,
252 pub tp_base: *mut PyTypeObject,
253 pub tp_dict: *mut object::PyObject,
254 pub tp_descr_get: Option<object::descrgetfunc>,
255 pub tp_descr_set: Option<object::descrsetfunc>,
256 pub tp_dictoffset: Py_ssize_t,
257 pub tp_init: Option<object::initproc>,
258 pub tp_alloc: Option<object::allocfunc>,
259 pub tp_new: Option<object::newfunc>,
260 pub tp_free: Option<object::freefunc>,
261 pub tp_is_gc: Option<object::inquiry>,
262 pub tp_bases: *mut object::PyObject,
263 pub tp_mro: *mut object::PyObject,
264 pub tp_cache: *mut object::PyObject,
265 pub tp_subclasses: *mut object::PyObject,
266 pub tp_weaklist: *mut object::PyObject,
267 pub tp_del: Option<object::destructor>,
268 pub tp_version_tag: c_uint,
269 pub tp_finalize: Option<object::destructor>,
270 #[cfg(Py_3_8)]
271 pub tp_vectorcall: Option<vectorcallfunc>,
272 #[cfg(Py_3_12)]
273 pub tp_watched: c_char,
274 #[cfg(any(all(PyPy, Py_3_8, not(Py_3_10)), all(not(PyPy), Py_3_8, not(Py_3_9))))]
275 pub tp_print: Option<printfunc>,
276 #[cfg(all(PyPy, not(Py_3_10)))]
277 pub tp_pypy_flags: std::os::raw::c_long,
278 #[cfg(py_sys_config = "COUNT_ALLOCS")]
279 pub tp_allocs: Py_ssize_t,
280 #[cfg(py_sys_config = "COUNT_ALLOCS")]
281 pub tp_frees: Py_ssize_t,
282 #[cfg(py_sys_config = "COUNT_ALLOCS")]
283 pub tp_maxalloc: Py_ssize_t,
284 #[cfg(py_sys_config = "COUNT_ALLOCS")]
285 pub tp_prev: *mut PyTypeObject,
286 #[cfg(py_sys_config = "COUNT_ALLOCS")]
287 pub tp_next: *mut PyTypeObject,
288}
289
290#[cfg(Py_3_11)]
291#[repr(C)]
292#[derive(Clone)]
293pub struct _specialization_cache {
294 pub getitem: *mut PyObject,
295 #[cfg(Py_3_12)]
296 pub getitem_version: u32,
297}
298
299#[repr(C)]
300#[derive(Clone)]
301pub struct PyHeapTypeObject {
302 pub ht_type: PyTypeObject,
303 pub as_async: PyAsyncMethods,
304 pub as_number: PyNumberMethods,
305 pub as_mapping: PyMappingMethods,
306 pub as_sequence: PySequenceMethods,
307 pub as_buffer: PyBufferProcs,
308 pub ht_name: *mut object::PyObject,
309 pub ht_slots: *mut object::PyObject,
310 pub ht_qualname: *mut object::PyObject,
311 #[cfg(not(PyPy))]
312 pub ht_cached_keys: *mut c_void,
313 #[cfg(Py_3_9)]
314 pub ht_module: *mut object::PyObject,
315 #[cfg(Py_3_11)]
316 pub _ht_tpname: *mut c_char,
317 #[cfg(Py_3_11)]
318 pub _spec_cache: _specialization_cache,
319}
320
321impl Default for PyHeapTypeObject {
322 #[inline]
323 fn default() -> Self {
324 unsafe { mem::zeroed() }
325 }
326}
327
328#[inline]
329pub unsafe fn PyHeapType_GET_MEMBERS(etype: *mut PyHeapTypeObject) -> *mut PyMemberDef {
330 let py_type: *mut PyTypeObject = object::Py_TYPE(ob:etype as *mut object::PyObject);
331 let ptr: *mut PyHeapTypeObject = etype.offset((*py_type).tp_basicsize);
332 ptr as *mut PyMemberDef
333}
334
335// skipped _PyType_Name
336// skipped _PyType_Lookup
337// skipped _PyType_LookupId
338// skipped _PyObject_LookupSpecial
339// skipped _PyType_CalculateMetaclass
340// skipped _PyType_GetDocFromInternalDoc
341// skipped _PyType_GetTextSignatureFromInternalDoc
342// skipped _PyType_GetModuleByDef
343
344extern "C" {
345 #[cfg(Py_3_12)]
346 pub fn PyType_GetDict(o: *mut PyTypeObject) -> *mut PyObject;
347
348 #[cfg_attr(PyPy, link_name = "PyPyObject_Print")]
349 pub fn PyObject_Print(o: *mut PyObject, fp: *mut ::libc::FILE, flags: c_int) -> c_int;
350
351 // skipped _Py_BreakPoint
352 // skipped _PyObject_Dump
353 // skipped _PyObject_IsFreed
354 // skipped _PyObject_IsAbstract
355 // skipped _PyObject_GetAttrId
356 // skipped _PyObject_SetAttrId
357 // skipped _PyObject_LookupAttr
358 // skipped _PyObject_LookupAttrId
359 // skipped _PyObject_GetMethod
360
361 #[cfg(not(PyPy))]
362 pub fn _PyObject_GetDictPtr(obj: *mut PyObject) -> *mut *mut PyObject;
363 #[cfg(not(PyPy))]
364 pub fn _PyObject_NextNotImplemented(arg1: *mut PyObject) -> *mut PyObject;
365 pub fn PyObject_CallFinalizer(arg1: *mut PyObject);
366 #[cfg_attr(PyPy, link_name = "PyPyObject_CallFinalizerFromDealloc")]
367 pub fn PyObject_CallFinalizerFromDealloc(arg1: *mut PyObject) -> c_int;
368
369 // skipped _PyObject_GenericGetAttrWithDict
370 // skipped _PyObject_GenericSetAttrWithDict
371 // skipped _PyObject_FunctionStr
372}
373
374// skipped Py_SETREF
375// skipped Py_XSETREF
376
377#[cfg_attr(windows, link(name = "pythonXY"))]
378extern "C" {
379 pub static mut _PyNone_Type: PyTypeObject;
380 pub static mut _PyNotImplemented_Type: PyTypeObject;
381}
382
383// skipped _Py_SwappedOp
384
385// skipped _PyDebugAllocatorStats
386// skipped _PyObject_DebugTypeStats
387// skipped _PyObject_ASSERT_FROM
388// skipped _PyObject_ASSERT_WITH_MSG
389// skipped _PyObject_ASSERT
390// skipped _PyObject_ASSERT_FAILED_MSG
391// skipped _PyObject_AssertFailed
392// skipped _PyObject_CheckConsistency
393
394// skipped _PyTrash_thread_deposit_object
395// skipped _PyTrash_thread_destroy_chain
396// skipped _PyTrash_begin
397// skipped _PyTrash_end
398// skipped _PyTrash_cond
399// skipped PyTrash_UNWIND_LEVEL
400// skipped Py_TRASHCAN_BEGIN_CONDITION
401// skipped Py_TRASHCAN_END
402// skipped Py_TRASHCAN_BEGIN
403// skipped Py_TRASHCAN_SAFE_BEGIN
404// skipped Py_TRASHCAN_SAFE_END
405