this repo has no description
1/* Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com) */
2#ifndef CPYTHON_TYPES_H
3#define CPYTHON_TYPES_H
4
5#include <inttypes.h>
6#include <limits.h>
7#include <stdint.h>
8#include <stdio.h>
9#include <sys/types.h> /* for struct stat and pid_t. */
10#include <time.h>
11#include <wchar.h>
12
13#include "pyconfig.h"
14
15#ifdef __cplusplus
16extern "C" {
17#endif
18
19#define PY_UINT32_T uint32_t
20#define PY_UINT64_T uint64_t
21#define PY_INT32_T int32_t
22#define PY_INT64_T int64_t
23
24typedef ssize_t Py_ssize_t;
25typedef uintptr_t Py_uintptr_t;
26typedef intptr_t Py_intptr_t;
27typedef Py_ssize_t Py_hash_t;
28typedef size_t Py_uhash_t;
29
30#ifdef PY_SSIZE_T_CLEAN
31typedef Py_ssize_t Py_ssize_clean_t;
32#else
33typedef int Py_ssize_clean_t;
34#endif
35
36#define PY_LONG_LONG long long
37#define PY_LLONG_MIN LLONG_MIN
38#define PY_LLONG_MAX LLONG_MAX
39#define PY_ULLONG_MAX ULLONG_MAX
40#define PY_SIZE_MAX SIZE_MAX
41#define PY_SSIZE_T_MAX ((Py_ssize_t)(((size_t)-1) >> 1))
42#define PY_SSIZE_T_MIN (-PY_SSIZE_T_MAX - 1)
43
44#define PY_FORMAT_SIZE_T "z"
45
46#define Py_LL(x) x##LL
47#define Py_ULL(x) Py_LL(x##U)
48
49#define Py_HUGE_VAL HUGE_VAL
50#define Py_NAN __builtin_nan("")
51
52#define PY_BIG_ENDIAN (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
53#define PY_LITTLE_ENDIAN (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
54
55struct stat;
56#define _Py_stat_struct stat
57
58#define _PyObject_HEAD_EXTRA uintptr_t reference_;
59
60#define _PyObject_EXTRA_INIT 0,
61
62#define PyObject_HEAD PyObject ob_base;
63
64#define PyObject_VAR_HEAD PyVarObject ob_base;
65
66#define Py_MEMBER_SIZE(type, member) sizeof(((type*)0)->member)
67#define Py_ARRAY_LENGTH(array) (sizeof(array) / sizeof((array)[0]))
68
69/* clang-format off */
70#define PyObject_HEAD_INIT(type) \
71 { _PyObject_EXTRA_INIT 1},
72
73/* The modification of PyVarObject_HEAD_INIT is described in detail here:
74 * https://mail.python.org/pipermail/python-dev/2018-August/154946.html */
75#define PyVarObject_HEAD_INIT(type, size) \
76 { PyObject_HEAD_INIT(NULL) 0 },
77
78/* clang-format on */
79
80typedef struct _longobject PyLongObject;
81typedef struct _typeobject PyTypeObject;
82typedef struct _PyWeakReference PyWeakReference;
83typedef struct _structsequence PyStructSequence;
84
85struct PyMemberDef;
86
87#if defined(__GNUC__) && __GNUC__ < 10 && !defined(__clang__) && \
88 !defined(__cplusplus)
89/* GCC has a bug where applying `&*` to a pointer to an incomplete type is
90 * rejected incorrectly. See also:
91 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88827 */
92
93/* Our PyXXX_Type macros rely on this working. As a workaround for gcc we add
94 * an (incomplete) definition here so the type is no longer incomplete. */
95struct _typeobject {
96 _PyObject_HEAD_EXTRA
97};
98
99/* This definition is needed to compile the `&*` in _PyRuntime */
100struct _rs {};
101#endif
102
103typedef struct _object {
104 _PyObject_HEAD_EXTRA
105 Py_ssize_t ob_refcnt;
106} PyObject;
107
108typedef struct {
109 PyObject_HEAD
110 // no additional fields necessary, all data is in managed heap!
111} PyBaseExceptionObject;
112
113typedef struct {
114 PyObject_HEAD
115 // no additional fields necessary, all data is in managed heap!
116} PyHeapTypeObject;
117
118typedef struct {
119 PyObject ob_base;
120 Py_ssize_t ob_size; /* Number of items in variable part */
121} PyVarObject;
122
123typedef struct bufferinfo {
124 void* buf;
125 PyObject* obj; /* owned reference */
126 Py_ssize_t len;
127 Py_ssize_t itemsize; /* This is Py_ssize_t so it can be
128 pointed to by strides in simple case.*/
129 int readonly;
130 int ndim;
131 char* format;
132 Py_ssize_t* shape;
133 Py_ssize_t* strides;
134 Py_ssize_t* suboffsets;
135 void* internal;
136} Py_buffer;
137
138typedef struct _PyArg_Parser {
139 const char* format;
140 const char* const* keywords;
141 const char* fname;
142 const char* custom_msg;
143 int pos; // number of positional-only arguments
144 int min; // minimal number of arguments
145 int max; // maximal number of positional arguments
146 PyObject* kwtuple; // tuple of keyword parameter names
147 struct _PyArg_Parser* next;
148} _PyArg_Parser;
149
150typedef void (*freefunc)(void*);
151typedef void (*destructor)(PyObject*);
152typedef int (*printfunc)(PyObject*, FILE*, int);
153typedef PyObject* (*getattrfunc)(PyObject*, char*);
154typedef PyObject* (*getattrofunc)(PyObject*, PyObject*);
155typedef int (*setattrfunc)(PyObject*, char*, PyObject*);
156typedef int (*setattrofunc)(PyObject*, PyObject*, PyObject*);
157typedef PyObject* (*reprfunc)(PyObject*);
158typedef Py_hash_t (*hashfunc)(PyObject*);
159typedef PyObject* (*richcmpfunc)(PyObject*, PyObject*, int);
160typedef PyObject* (*getiterfunc)(PyObject*);
161typedef PyObject* (*iternextfunc)(PyObject*);
162typedef PyObject* (*descrgetfunc)(PyObject*, PyObject*, PyObject*);
163typedef int (*descrsetfunc)(PyObject*, PyObject*, PyObject*);
164typedef int (*initproc)(PyObject*, PyObject*, PyObject*);
165typedef PyObject* (*newfunc)(struct _typeobject*, PyObject*, PyObject*);
166typedef PyObject* (*allocfunc)(struct _typeobject*, Py_ssize_t);
167
168typedef PyObject* (*unaryfunc)(PyObject*);
169typedef PyObject* (*binaryfunc)(PyObject*, PyObject*);
170typedef PyObject* (*ternaryfunc)(PyObject*, PyObject*, PyObject*);
171typedef int (*inquiry)(PyObject*);
172typedef Py_ssize_t (*lenfunc)(PyObject*);
173typedef PyObject* (*ssizeargfunc)(PyObject*, Py_ssize_t);
174typedef PyObject* (*ssizessizeargfunc)(PyObject*, Py_ssize_t, Py_ssize_t);
175typedef int (*ssizeobjargproc)(PyObject*, Py_ssize_t, PyObject*);
176typedef int (*ssizessizeobjargproc)(PyObject*, Py_ssize_t, Py_ssize_t,
177 PyObject*);
178typedef int (*objobjargproc)(PyObject*, PyObject*, PyObject*);
179
180typedef int (*objobjproc)(PyObject*, PyObject*);
181typedef int (*visitproc)(PyObject*, void*);
182typedef int (*traverseproc)(PyObject*, visitproc, void*);
183
184typedef int (*getbufferproc)(PyObject*, Py_buffer*, int);
185typedef void (*releasebufferproc)(PyObject*, Py_buffer*);
186
187typedef PyObject* (*getter)(PyObject*, void*);
188typedef int (*setter)(PyObject*, PyObject*, void*);
189
190typedef PyObject* (*PyCFunction)(PyObject*, PyObject*);
191typedef PyObject* (*_PyCFunctionFast)(PyObject*, PyObject* const*, Py_ssize_t);
192typedef PyObject* (*_PyCFunctionFastWithKeywords)(PyObject*, PyObject* const*,
193 Py_ssize_t, PyObject*);
194typedef PyObject* (*PyCFunctionWithKeywords)(PyObject*, PyObject*, PyObject*);
195typedef PyObject* (*PyNoArgsFunction)(PyObject*);
196
197typedef struct {
198 /* Number implementations must check *both*
199 arguments for proper type and implement the necessary conversions
200 in the slot functions themselves. */
201
202 binaryfunc nb_add;
203 binaryfunc nb_subtract;
204 binaryfunc nb_multiply;
205 binaryfunc nb_remainder;
206 binaryfunc nb_divmod;
207 ternaryfunc nb_power;
208 unaryfunc nb_negative;
209 unaryfunc nb_positive;
210 unaryfunc nb_absolute;
211 inquiry nb_bool;
212 unaryfunc nb_invert;
213 binaryfunc nb_lshift;
214 binaryfunc nb_rshift;
215 binaryfunc nb_and;
216 binaryfunc nb_xor;
217 binaryfunc nb_or;
218 unaryfunc nb_int;
219 void* nb_reserved; /* the slot formerly known as nb_long */
220 unaryfunc nb_float;
221
222 binaryfunc nb_inplace_add;
223 binaryfunc nb_inplace_subtract;
224 binaryfunc nb_inplace_multiply;
225 binaryfunc nb_inplace_remainder;
226 ternaryfunc nb_inplace_power;
227 binaryfunc nb_inplace_lshift;
228 binaryfunc nb_inplace_rshift;
229 binaryfunc nb_inplace_and;
230 binaryfunc nb_inplace_xor;
231 binaryfunc nb_inplace_or;
232
233 binaryfunc nb_floor_divide;
234 binaryfunc nb_true_divide;
235 binaryfunc nb_inplace_floor_divide;
236 binaryfunc nb_inplace_true_divide;
237
238 unaryfunc nb_index;
239
240 binaryfunc nb_matrix_multiply;
241 binaryfunc nb_inplace_matrix_multiply;
242} PyNumberMethods;
243
244typedef struct {
245 lenfunc sq_length;
246 binaryfunc sq_concat;
247 ssizeargfunc sq_repeat;
248 ssizeargfunc sq_item;
249 void* was_sq_slice;
250 ssizeobjargproc sq_ass_item;
251 void* was_sq_ass_slice;
252 objobjproc sq_contains;
253
254 binaryfunc sq_inplace_concat;
255 ssizeargfunc sq_inplace_repeat;
256} PySequenceMethods;
257
258typedef struct {
259 lenfunc mp_length;
260 binaryfunc mp_subscript;
261 objobjargproc mp_ass_subscript;
262} PyMappingMethods;
263
264typedef struct {
265 getbufferproc bf_getbuffer;
266 releasebufferproc bf_releasebuffer;
267} PyBufferProcs;
268
269struct PyMethodDef {
270 const char* ml_name; /* The name of the built-in function/method */
271 PyCFunction ml_meth; /* The C function that implements it */
272 int ml_flags; /* Combination of METH_xxx flags, which mostly
273 describe the args expected by the C func */
274 const char* ml_doc; /* The __doc__ attribute, or NULL */
275};
276typedef struct PyMethodDef PyMethodDef;
277
278typedef struct PyModuleDef_Base {
279 PyObject_HEAD
280 PyObject* (*m_init)(void);
281 Py_ssize_t m_index;
282 PyObject* m_copy;
283} PyModuleDef_Base;
284
285#define PyModuleDef_HEAD_INIT \
286 { \
287 PyObject_HEAD_INIT(NULL) NULL, /* m_init */ \
288 0, /* m_index */ \
289 NULL, /* m_copy */ \
290 }
291
292typedef struct PyModuleDef_Slot {
293 int slot;
294 void* value;
295} PyModuleDef_Slot;
296
297#define Py_mod_create 1
298#define Py_mod_exec 2
299
300typedef struct PyModuleDef {
301 PyModuleDef_Base m_base;
302 const char* m_name;
303 const char* m_doc;
304 Py_ssize_t m_size;
305 PyMethodDef* m_methods;
306 struct PyModuleDef_Slot* m_slots;
307 traverseproc m_traverse;
308 inquiry m_clear;
309 freefunc m_free;
310} PyModuleDef;
311
312typedef struct PyGetSetDef {
313 const char* name;
314 getter get;
315 setter set;
316 const char* doc;
317 void* closure;
318} PyGetSetDef;
319
320typedef struct {
321 unaryfunc am_await;
322 unaryfunc am_aiter;
323 unaryfunc am_anext;
324} PyAsyncMethods;
325
326typedef struct {
327 int cf_flags; /* bitmask of CO_xxx flags relevant to future */
328 int cf_feature_version; /* minor Python version (PyCF_ONLY_AST) */
329} PyCompilerFlags;
330
331#define _PyCompilerFlags_INIT \
332 { 0, PY_MINOR_VERSION, }
333
334struct _inittab {
335 const char* name;
336 PyObject* (*initfunc)(void);
337};
338
339typedef struct {
340 double real;
341 double imag;
342} Py_complex;
343
344#define Py_UNICODE_SIZE __SIZEOF_WCHAR_T__
345#define PY_UNICODE_TYPE wchar_t
346
347typedef uint32_t Py_UCS4;
348typedef uint16_t Py_UCS2;
349typedef uint8_t Py_UCS1;
350typedef wchar_t Py_UNICODE;
351
352typedef struct {
353 int slot; /* slot id, see below */
354 void* pfunc; /* function pointer */
355} PyType_Slot;
356
357typedef struct {
358 const char* name;
359 int basicsize;
360 int itemsize;
361 unsigned int flags;
362 PyType_Slot* slots; /* terminated by slot==0. */
363} PyType_Spec;
364
365typedef struct PyStructSequence_Field {
366 const char* name;
367 const char* doc;
368} PyStructSequence_Field;
369
370typedef struct PyStructSequence_Desc {
371 const char* name;
372 const char* doc;
373 struct PyStructSequence_Field* fields;
374 int n_in_sequence;
375} PyStructSequence_Desc;
376
377enum PyUnicode_Kind {
378 PyUnicode_WCHAR_KIND = 0,
379 PyUnicode_1BYTE_KIND = 1,
380 PyUnicode_2BYTE_KIND = 2,
381 PyUnicode_4BYTE_KIND = 4
382};
383
384struct _Py_Identifier;
385struct _mod;
386struct _node;
387
388typedef struct {
389 unsigned char* heap_buffer; // byte*
390 unsigned char* ptr; // byte*
391 Py_ssize_t allocated; // word
392 Py_ssize_t min_size; // word
393 int overallocate; // bool
394 int use_bytearray; // bool
395 int use_heap_buffer; // bool
396 unsigned char stack_buffer[128]; // byte[128]
397} _PyBytesWriter;
398
399typedef struct {
400 PyObject* buffer;
401 void* data;
402 enum PyUnicode_Kind kind;
403 Py_UCS4 maxchar;
404 Py_ssize_t size;
405 Py_ssize_t pos;
406 Py_ssize_t min_length;
407 Py_UCS4 min_char;
408 unsigned char overallocate;
409 unsigned char readonly;
410} _PyUnicodeWriter;
411
412typedef struct {
413 int ff_features;
414 int ff_lineno;
415} PyFutureFeatures;
416
417typedef uint16_t _Py_CODEUNIT;
418
419// The following types are intentionally incomplete to make it impossible to
420// dereference the objects
421typedef struct _arena PyArena;
422typedef struct _frame PyFrameObject;
423typedef struct _code PyCodeObject;
424typedef struct _is PyInterpreterState;
425typedef struct _ts PyThreadState;
426typedef struct _rs _PyRuntimeState;
427
428typedef void (*PyOS_sighandler_t)(int);
429typedef void (*PyCapsule_Destructor)(PyObject*);
430typedef PyObject* (*Py_OpenCodeHookFunction)(PyObject*, void*);
431typedef int (*Py_tracefunc)(PyObject*, PyFrameObject*, int, PyObject*);
432
433struct timespec;
434struct timeval;
435typedef int64_t _PyTime_t;
436#define _PyTime_MIN INT64_MIN
437#define _PyTime_MAX INT64_MAX
438
439typedef enum {
440 _PyTime_ROUND_FLOOR = 0,
441 _PyTime_ROUND_CEILING = 1,
442 _PyTime_ROUND_HALF_EVEN = 2,
443 _PyTime_ROUND_UP = 3,
444 _PyTime_ROUND_TIMEOUT = _PyTime_ROUND_UP
445} _PyTime_round_t;
446
447typedef struct {
448 const char* implementation;
449 int monotonic;
450 int adjustable;
451 double resolution;
452} _Py_clock_info_t;
453
454typedef enum { PyGILState_LOCKED, PyGILState_UNLOCKED } PyGILState_STATE;
455
456typedef void* PyThread_type_lock;
457typedef void* PyThread_type_sema;
458
459typedef enum PyLockStatus {
460 PY_LOCK_FAILURE = 0,
461 PY_LOCK_ACQUIRED = 1,
462 PY_LOCK_INTR
463} PyLockStatus;
464
465#define PY_TIMEOUT_T long long
466#define PY_TIMEOUT_MAX LLONG_MAX
467
468typedef union {
469 /* ensure 24 bytes */
470 unsigned char uc[24];
471 /* two Py_hash_t for FNV */
472 struct {
473 Py_hash_t prefix;
474 Py_hash_t suffix;
475 } fnv;
476 /* two uint64 for SipHash24 */
477 struct {
478 uint64_t k0;
479 uint64_t k1;
480 } siphash;
481 /* a different (!) Py_hash_t for small string optimization */
482 struct {
483 unsigned char padding[16];
484 Py_hash_t suffix;
485 } djbx33a;
486 struct {
487 unsigned char padding[16];
488 Py_hash_t hashsalt;
489 } expat;
490} _Py_HashSecret_t;
491
492typedef enum {
493 _Py_ERROR_UNKNOWN = 0,
494 _Py_ERROR_STRICT,
495 _Py_ERROR_SURROGATEESCAPE,
496 _Py_ERROR_REPLACE,
497 _Py_ERROR_IGNORE,
498 _Py_ERROR_BACKSLASHREPLACE,
499 _Py_ERROR_SURROGATEPASS,
500 _Py_ERROR_XMLCHARREFREPLACE,
501 _Py_ERROR_OTHER,
502} _Py_error_handler;
503
504struct lconv;
505
506#ifdef __cplusplus
507}
508#endif
509
510#endif /* !CPYTHON_TYPES_H */