/src/cpython-install/include/python3.15/object.h
Line | Count | Source |
1 | | #ifndef Py_OBJECT_H |
2 | | #define Py_OBJECT_H |
3 | | #ifdef __cplusplus |
4 | | extern "C" { |
5 | | #endif |
6 | | |
7 | | |
8 | | /* Object and type object interface */ |
9 | | |
10 | | /* |
11 | | Objects are structures allocated on the heap. Special rules apply to |
12 | | the use of objects to ensure they are properly garbage-collected. |
13 | | Objects are never allocated statically or on the stack; they must be |
14 | | accessed through special macros and functions only. (Type objects are |
15 | | exceptions to the first rule; the standard types are represented by |
16 | | statically initialized type objects, although work on type/class unification |
17 | | for Python 2.2 made it possible to have heap-allocated type objects too). |
18 | | |
19 | | An object has a 'reference count' that is increased or decreased when a |
20 | | pointer to the object is copied or deleted; when the reference count |
21 | | reaches zero there are no references to the object left and it can be |
22 | | removed from the heap. |
23 | | |
24 | | An object has a 'type' that determines what it represents and what kind |
25 | | of data it contains. An object's type is fixed when it is created. |
26 | | Types themselves are represented as objects; an object contains a |
27 | | pointer to the corresponding type object. The type itself has a type |
28 | | pointer pointing to the object representing the type 'type', which |
29 | | contains a pointer to itself!. |
30 | | |
31 | | Objects do not float around in memory; once allocated an object keeps |
32 | | the same size and address. Objects that must hold variable-size data |
33 | | can contain pointers to variable-size parts of the object. Not all |
34 | | objects of the same type have the same size; but the size cannot change |
35 | | after allocation. (These restrictions are made so a reference to an |
36 | | object can be simply a pointer -- moving an object would require |
37 | | updating all the pointers, and changing an object's size would require |
38 | | moving it if there was another object right next to it.) |
39 | | |
40 | | Objects are always accessed through pointers of the type 'PyObject *'. |
41 | | The type 'PyObject' is a structure that only contains the reference count |
42 | | and the type pointer. The actual memory allocated for an object |
43 | | contains other data that can only be accessed after casting the pointer |
44 | | to a pointer to a longer structure type. This longer type must start |
45 | | with the reference count and type fields; the macro PyObject_HEAD should be |
46 | | used for this (to accommodate for future changes). The implementation |
47 | | of a particular object type can cast the object pointer to the proper |
48 | | type and back. |
49 | | |
50 | | A standard interface exists for objects that contain an array of items |
51 | | whose size is determined when the object is allocated. |
52 | | */ |
53 | | |
54 | | /* Py_DEBUG implies Py_REF_DEBUG. */ |
55 | | #if defined(Py_DEBUG) && !defined(Py_REF_DEBUG) |
56 | | # define Py_REF_DEBUG |
57 | | #endif |
58 | | |
59 | | #if defined(_Py_OPAQUE_PYOBJECT) && !defined(Py_LIMITED_API) |
60 | | # error "_Py_OPAQUE_PYOBJECT only makes sense with Py_LIMITED_API" |
61 | | #endif |
62 | | |
63 | | #ifndef _Py_OPAQUE_PYOBJECT |
64 | | /* PyObject_HEAD defines the initial segment of every PyObject. */ |
65 | | #define PyObject_HEAD PyObject ob_base; |
66 | | |
67 | | // Kept for backward compatibility. It was needed by Py_TRACE_REFS build. |
68 | | #define _PyObject_EXTRA_INIT |
69 | | |
70 | | /* Make all uses of PyObject_HEAD_INIT immortal. |
71 | | * |
72 | | * Statically allocated objects might be shared between |
73 | | * interpreters, so must be marked as immortal. |
74 | | * |
75 | | * Before changing this, see the check in PyModuleDef_Init(). |
76 | | */ |
77 | | #if defined(Py_GIL_DISABLED) |
78 | | #define PyObject_HEAD_INIT(type) \ |
79 | | { \ |
80 | | 0, \ |
81 | | _Py_STATICALLY_ALLOCATED_FLAG, \ |
82 | | { 0 }, \ |
83 | | 0, \ |
84 | | _Py_IMMORTAL_REFCNT_LOCAL, \ |
85 | | 0, \ |
86 | | (type), \ |
87 | | }, |
88 | | #else |
89 | | #define PyObject_HEAD_INIT(type) \ |
90 | | { \ |
91 | | { _Py_STATIC_IMMORTAL_INITIAL_REFCNT }, \ |
92 | | (type) \ |
93 | | }, |
94 | | #endif |
95 | | |
96 | | #define PyVarObject_HEAD_INIT(type, size) \ |
97 | | { \ |
98 | | PyObject_HEAD_INIT(type) \ |
99 | | (size) \ |
100 | | }, |
101 | | |
102 | | /* PyObject_VAR_HEAD defines the initial segment of all variable-size |
103 | | * container objects. These end with a declaration of an array with 1 |
104 | | * element, but enough space is malloc'ed so that the array actually |
105 | | * has room for ob_size elements. Note that ob_size is an element count, |
106 | | * not necessarily a byte count. |
107 | | */ |
108 | | #define PyObject_VAR_HEAD PyVarObject ob_base; |
109 | | #endif // !defined(_Py_OPAQUE_PYOBJECT) |
110 | | |
111 | | #define Py_INVALID_SIZE (Py_ssize_t)-1 |
112 | | |
113 | | /* PyObjects are given a minimum alignment so that the least significant bits |
114 | | * of an object pointer become available for other purposes. |
115 | | * This must be an integer literal with the value (1 << _PyGC_PREV_SHIFT), number of bytes. |
116 | | */ |
117 | | #define _PyObject_MIN_ALIGNMENT 4 |
118 | | |
119 | | /* Nothing is actually declared to be a PyObject, but every pointer to |
120 | | * a Python object can be cast to a PyObject*. This is inheritance built |
121 | | * by hand. Similarly every pointer to a variable-size Python object can, |
122 | | * in addition, be cast to PyVarObject*. |
123 | | */ |
124 | | #ifdef _Py_OPAQUE_PYOBJECT |
125 | | /* PyObject is opaque */ |
126 | | #elif !defined(Py_GIL_DISABLED) |
127 | | struct _object { |
128 | | _Py_ANONYMOUS union { |
129 | | #if SIZEOF_VOID_P > 4 |
130 | | int64_t ob_refcnt_full; /* This field is needed for efficient initialization with Clang on ARM */ |
131 | | struct { |
132 | | # if PY_BIG_ENDIAN |
133 | | uint16_t ob_flags; |
134 | | uint16_t ob_overflow; |
135 | | uint32_t ob_refcnt; |
136 | | # else |
137 | | uint32_t ob_refcnt; |
138 | | uint16_t ob_overflow; |
139 | | uint16_t ob_flags; |
140 | | # endif |
141 | | }; |
142 | | #else |
143 | | Py_ssize_t ob_refcnt; // part of stable ABI; do not change |
144 | | #endif |
145 | | _Py_ALIGNED_DEF(_PyObject_MIN_ALIGNMENT, char) _aligner; |
146 | | }; |
147 | | |
148 | | PyTypeObject *ob_type; // part of stable ABI; do not change |
149 | | }; |
150 | | #else |
151 | | // Objects that are not owned by any thread use a thread id (tid) of zero. |
152 | | // This includes both immortal objects and objects whose reference count |
153 | | // fields have been merged. |
154 | | #define _Py_UNOWNED_TID 0 |
155 | | |
156 | | struct _object { |
157 | | // ob_tid stores the thread id (or zero). It is also used by the GC and the |
158 | | // trashcan mechanism as a linked list pointer and by the GC to store the |
159 | | // computed "gc_refs" refcount. |
160 | | _Py_ALIGNED_DEF(_PyObject_MIN_ALIGNMENT, uintptr_t) ob_tid; |
161 | | uint16_t ob_flags; |
162 | | PyMutex ob_mutex; // per-object lock |
163 | | uint8_t ob_gc_bits; // gc-related state |
164 | | uint32_t ob_ref_local; // local reference count |
165 | | Py_ssize_t ob_ref_shared; // shared (atomic) reference count |
166 | | PyTypeObject *ob_type; |
167 | | }; |
168 | | #endif // !defined(_Py_OPAQUE_PYOBJECT) |
169 | | |
170 | | /* Cast argument to PyObject* type. */ |
171 | 568k | #define _PyObject_CAST(op) _Py_CAST(PyObject*, (op)) |
172 | | |
173 | | #ifndef _Py_OPAQUE_PYOBJECT |
174 | | struct PyVarObject { |
175 | | PyObject ob_base; |
176 | | Py_ssize_t ob_size; // Number of items in variable part. Part of stable ABI |
177 | | }; |
178 | | #endif |
179 | | typedef struct PyVarObject PyVarObject; |
180 | | |
181 | | /* Cast argument to PyVarObject* type. */ |
182 | | #define _PyVarObject_CAST(op) _Py_CAST(PyVarObject*, (op)) |
183 | | |
184 | | |
185 | | // Test if the 'x' object is the 'y' object, the same as "x is y" in Python. |
186 | | PyAPI_FUNC(int) Py_Is(PyObject *x, PyObject *y); |
187 | | #define Py_Is(x, y) ((x) == (y)) |
188 | | |
189 | | PyAPI_DATA(PyTypeObject) PyLong_Type; |
190 | | PyAPI_DATA(PyTypeObject) PyBool_Type; |
191 | | |
192 | | /* Definitions for the stable ABI */ |
193 | | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 14) |
194 | | PyAPI_FUNC(PyTypeObject*) Py_TYPE(PyObject *ob); |
195 | | #endif |
196 | | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 15) |
197 | | PyAPI_FUNC(Py_ssize_t) Py_SIZE(PyObject *ob); |
198 | | PyAPI_FUNC(int) Py_IS_TYPE(PyObject *ob, PyTypeObject *type); |
199 | | PyAPI_FUNC(void) Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size); |
200 | | #endif |
201 | | |
202 | | #ifndef _Py_OPAQUE_PYOBJECT |
203 | | |
204 | | static inline void |
205 | | Py_SET_TYPE(PyObject *ob, PyTypeObject *type) |
206 | 0 | { |
207 | 0 | ob->ob_type = type; |
208 | 0 | } |
209 | | |
210 | | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 11) |
211 | | // Non-limited API & limited API 3.11 & below: use static inline functions and |
212 | | // use _PyObject_CAST so that users don't need their own casts |
213 | | # define Py_TYPE(ob) _Py_TYPE_impl(_PyObject_CAST(ob)) |
214 | | # define Py_SIZE(ob) _Py_SIZE_impl(_PyObject_CAST(ob)) |
215 | | # define Py_IS_TYPE(ob, type) _Py_IS_TYPE_impl(_PyObject_CAST(ob), (type)) |
216 | | # define Py_SET_SIZE(ob, size) _Py_SET_SIZE_impl(_PyVarObject_CAST(ob), (size)) |
217 | | # define Py_SET_TYPE(ob, type) Py_SET_TYPE(_PyObject_CAST(ob), type) |
218 | | #elif Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 15) |
219 | | // Limited API 3.11-3.14: use static inline functions, without casts |
220 | | # define Py_SIZE(ob) _Py_SIZE_impl(ob) |
221 | | # define Py_IS_TYPE(ob, type) _Py_IS_TYPE_impl((ob), (type)) |
222 | | # define Py_SET_SIZE(ob, size) _Py_SET_SIZE_impl((ob), (size)) |
223 | | # if Py_LIMITED_API+0 < _Py_PACK_VERSION(3, 14) |
224 | | // Py_TYPE() is static inline only on Limited API 3.13 and below |
225 | | # define Py_TYPE(ob) _Py_TYPE_impl(ob) |
226 | | # endif |
227 | | #else |
228 | | // Limited API 3.15+: use function calls |
229 | | #endif |
230 | | |
231 | | static inline |
232 | | PyTypeObject* _Py_TYPE_impl(PyObject *ob) |
233 | 142k | { |
234 | 142k | return ob->ob_type; |
235 | 142k | } |
236 | | |
237 | | // bpo-39573: The Py_SET_SIZE() function must be used to set an object size. |
238 | | static inline Py_ssize_t |
239 | | _Py_SIZE_impl(PyObject *ob) |
240 | 0 | { |
241 | 0 | assert(Py_TYPE(ob) != &PyLong_Type); |
242 | 0 | assert(Py_TYPE(ob) != &PyBool_Type); |
243 | 0 | return _PyVarObject_CAST(ob)->ob_size; |
244 | 0 | } |
245 | | |
246 | | static inline int |
247 | | _Py_IS_TYPE_impl(PyObject *ob, PyTypeObject *type) |
248 | 0 | { |
249 | 0 | return Py_TYPE(ob) == type; |
250 | 0 | } |
251 | | |
252 | | static inline void |
253 | | _Py_SET_SIZE_impl(PyVarObject *ob, Py_ssize_t size) |
254 | 0 | { |
255 | 0 | assert(Py_TYPE(_PyObject_CAST(ob)) != &PyLong_Type); |
256 | 0 | assert(Py_TYPE(_PyObject_CAST(ob)) != &PyBool_Type); |
257 | 0 | #ifdef Py_GIL_DISABLED |
258 | 0 | _Py_atomic_store_ssize_relaxed(&ob->ob_size, size); |
259 | 0 | #else |
260 | 0 | ob->ob_size = size; |
261 | 0 | #endif |
262 | 0 | } |
263 | | |
264 | | #endif // !defined(_Py_OPAQUE_PYOBJECT) |
265 | | |
266 | | |
267 | | /* |
268 | | Type objects contain a string containing the type name (to help somewhat |
269 | | in debugging), the allocation parameters (see PyObject_New() and |
270 | | PyObject_NewVar()), |
271 | | and methods for accessing objects of the type. Methods are optional, a |
272 | | nil pointer meaning that particular kind of access is not available for |
273 | | this type. The Py_DECREF() macro uses the tp_dealloc method without |
274 | | checking for a nil pointer; it should always be implemented except if |
275 | | the implementation can guarantee that the reference count will never |
276 | | reach zero (e.g., for statically allocated type objects). |
277 | | |
278 | | NB: the methods for certain type groups are now contained in separate |
279 | | method blocks. |
280 | | */ |
281 | | |
282 | | typedef PyObject * (*unaryfunc)(PyObject *); |
283 | | typedef PyObject * (*binaryfunc)(PyObject *, PyObject *); |
284 | | typedef PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *); |
285 | | typedef int (*inquiry)(PyObject *); |
286 | | typedef Py_ssize_t (*lenfunc)(PyObject *); |
287 | | typedef PyObject *(*ssizeargfunc)(PyObject *, Py_ssize_t); |
288 | | typedef PyObject *(*ssizessizeargfunc)(PyObject *, Py_ssize_t, Py_ssize_t); |
289 | | typedef int(*ssizeobjargproc)(PyObject *, Py_ssize_t, PyObject *); |
290 | | typedef int(*ssizessizeobjargproc)(PyObject *, Py_ssize_t, Py_ssize_t, PyObject *); |
291 | | typedef int(*objobjargproc)(PyObject *, PyObject *, PyObject *); |
292 | | |
293 | | typedef int (*objobjproc)(PyObject *, PyObject *); |
294 | | typedef int (*visitproc)(PyObject *, void *); |
295 | | typedef int (*traverseproc)(PyObject *, visitproc, void *); |
296 | | |
297 | | |
298 | | typedef void (*freefunc)(void *); |
299 | | typedef void (*destructor)(PyObject *); |
300 | | typedef PyObject *(*getattrfunc)(PyObject *, char *); |
301 | | typedef PyObject *(*getattrofunc)(PyObject *, PyObject *); |
302 | | typedef int (*setattrfunc)(PyObject *, char *, PyObject *); |
303 | | typedef int (*setattrofunc)(PyObject *, PyObject *, PyObject *); |
304 | | typedef PyObject *(*reprfunc)(PyObject *); |
305 | | typedef Py_hash_t (*hashfunc)(PyObject *); |
306 | | typedef PyObject *(*richcmpfunc) (PyObject *, PyObject *, int); |
307 | | typedef PyObject *(*getiterfunc) (PyObject *); |
308 | | typedef PyObject *(*iternextfunc) (PyObject *); |
309 | | typedef PyObject *(*descrgetfunc) (PyObject *, PyObject *, PyObject *); |
310 | | typedef int (*descrsetfunc) (PyObject *, PyObject *, PyObject *); |
311 | | typedef int (*initproc)(PyObject *, PyObject *, PyObject *); |
312 | | typedef PyObject *(*newfunc)(PyTypeObject *, PyObject *, PyObject *); |
313 | | typedef PyObject *(*allocfunc)(PyTypeObject *, Py_ssize_t); |
314 | | |
315 | | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030c0000 // 3.12 |
316 | | typedef PyObject *(*vectorcallfunc)(PyObject *callable, PyObject *const *args, |
317 | | size_t nargsf, PyObject *kwnames); |
318 | | #endif |
319 | | |
320 | | typedef struct{ |
321 | | int slot; /* slot id, see below */ |
322 | | void *pfunc; /* function pointer */ |
323 | | } PyType_Slot; |
324 | | |
325 | | typedef struct{ |
326 | | const char* name; |
327 | | int basicsize; |
328 | | int itemsize; |
329 | | unsigned int flags; |
330 | | PyType_Slot *slots; /* terminated by slot==0. */ |
331 | | } PyType_Spec; |
332 | | |
333 | | PyAPI_FUNC(PyObject*) PyType_FromSpec(PyType_Spec*); |
334 | | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000 |
335 | | PyAPI_FUNC(PyObject*) PyType_FromSpecWithBases(PyType_Spec*, PyObject*); |
336 | | #endif |
337 | | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03040000 |
338 | | PyAPI_FUNC(void*) PyType_GetSlot(PyTypeObject*, int); |
339 | | #endif |
340 | | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000 |
341 | | PyAPI_FUNC(PyObject*) PyType_FromModuleAndSpec(PyObject *, PyType_Spec *, PyObject *); |
342 | | PyAPI_FUNC(PyObject *) PyType_GetModule(PyTypeObject *); |
343 | | PyAPI_FUNC(void *) PyType_GetModuleState(PyTypeObject *); |
344 | | #endif |
345 | | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030B0000 |
346 | | PyAPI_FUNC(PyObject *) PyType_GetName(PyTypeObject *); |
347 | | PyAPI_FUNC(PyObject *) PyType_GetQualName(PyTypeObject *); |
348 | | #endif |
349 | | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030D0000 |
350 | | PyAPI_FUNC(PyObject *) PyType_GetFullyQualifiedName(PyTypeObject *type); |
351 | | PyAPI_FUNC(PyObject *) PyType_GetModuleName(PyTypeObject *type); |
352 | | #endif |
353 | | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030C0000 |
354 | | PyAPI_FUNC(PyObject *) PyType_FromMetaclass(PyTypeObject*, PyObject*, PyType_Spec*, PyObject*); |
355 | | PyAPI_FUNC(void *) PyObject_GetTypeData(PyObject *obj, PyTypeObject *cls); |
356 | | PyAPI_FUNC(Py_ssize_t) PyType_GetTypeDataSize(PyTypeObject *cls); |
357 | | #endif |
358 | | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030E0000 |
359 | | PyAPI_FUNC(int) PyType_GetBaseByToken(PyTypeObject *, void *, PyTypeObject **); |
360 | | #define Py_TP_USE_SPEC NULL |
361 | | #endif |
362 | | |
363 | | /* Generic type check */ |
364 | | PyAPI_FUNC(int) PyType_IsSubtype(PyTypeObject *, PyTypeObject *); |
365 | | |
366 | 0 | static inline int PyObject_TypeCheck(PyObject *ob, PyTypeObject *type) { |
367 | 0 | return Py_IS_TYPE(ob, type) || PyType_IsSubtype(Py_TYPE(ob), type); |
368 | 0 | } |
369 | | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 |
370 | | # define PyObject_TypeCheck(ob, type) PyObject_TypeCheck(_PyObject_CAST(ob), (type)) |
371 | | #endif |
372 | | |
373 | | PyAPI_DATA(PyTypeObject) PyType_Type; /* built-in 'type' */ |
374 | | PyAPI_DATA(PyTypeObject) PyBaseObject_Type; /* built-in 'object' */ |
375 | | PyAPI_DATA(PyTypeObject) PySuper_Type; /* built-in 'super' */ |
376 | | |
377 | | PyAPI_FUNC(unsigned long) PyType_GetFlags(PyTypeObject*); |
378 | | |
379 | | PyAPI_FUNC(int) PyType_Ready(PyTypeObject *); |
380 | | PyAPI_FUNC(PyObject *) PyType_GenericAlloc(PyTypeObject *, Py_ssize_t); |
381 | | PyAPI_FUNC(PyObject *) PyType_GenericNew(PyTypeObject *, |
382 | | PyObject *, PyObject *); |
383 | | PyAPI_FUNC(unsigned int) PyType_ClearCache(void); |
384 | | PyAPI_FUNC(void) PyType_Modified(PyTypeObject *); |
385 | | |
386 | | /* Generic operations on objects */ |
387 | | PyAPI_FUNC(PyObject *) PyObject_Repr(PyObject *); |
388 | | PyAPI_FUNC(PyObject *) PyObject_Str(PyObject *); |
389 | | PyAPI_FUNC(PyObject *) PyObject_ASCII(PyObject *); |
390 | | PyAPI_FUNC(PyObject *) PyObject_Bytes(PyObject *); |
391 | | PyAPI_FUNC(PyObject *) PyObject_RichCompare(PyObject *, PyObject *, int); |
392 | | PyAPI_FUNC(int) PyObject_RichCompareBool(PyObject *, PyObject *, int); |
393 | | PyAPI_FUNC(PyObject *) PyObject_GetAttrString(PyObject *, const char *); |
394 | | PyAPI_FUNC(int) PyObject_SetAttrString(PyObject *, const char *, PyObject *); |
395 | | PyAPI_FUNC(int) PyObject_DelAttrString(PyObject *v, const char *name); |
396 | | PyAPI_FUNC(int) PyObject_HasAttrString(PyObject *, const char *); |
397 | | PyAPI_FUNC(PyObject *) PyObject_GetAttr(PyObject *, PyObject *); |
398 | | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000 |
399 | | PyAPI_FUNC(int) PyObject_GetOptionalAttr(PyObject *, PyObject *, PyObject **); |
400 | | PyAPI_FUNC(int) PyObject_GetOptionalAttrString(PyObject *, const char *, PyObject **); |
401 | | #endif |
402 | | PyAPI_FUNC(int) PyObject_SetAttr(PyObject *, PyObject *, PyObject *); |
403 | | PyAPI_FUNC(int) PyObject_DelAttr(PyObject *v, PyObject *name); |
404 | | PyAPI_FUNC(int) PyObject_HasAttr(PyObject *, PyObject *); |
405 | | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000 |
406 | | PyAPI_FUNC(int) PyObject_HasAttrWithError(PyObject *, PyObject *); |
407 | | PyAPI_FUNC(int) PyObject_HasAttrStringWithError(PyObject *, const char *); |
408 | | #endif |
409 | | PyAPI_FUNC(PyObject *) PyObject_SelfIter(PyObject *); |
410 | | PyAPI_FUNC(PyObject *) PyObject_GenericGetAttr(PyObject *, PyObject *); |
411 | | PyAPI_FUNC(int) PyObject_GenericSetAttr(PyObject *, PyObject *, PyObject *); |
412 | | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000 |
413 | | PyAPI_FUNC(int) PyObject_GenericSetDict(PyObject *, PyObject *, void *); |
414 | | #endif |
415 | | PyAPI_FUNC(Py_hash_t) PyObject_Hash(PyObject *); |
416 | | PyAPI_FUNC(Py_hash_t) PyObject_HashNotImplemented(PyObject *); |
417 | | PyAPI_FUNC(int) PyObject_IsTrue(PyObject *); |
418 | | PyAPI_FUNC(int) PyObject_Not(PyObject *); |
419 | | PyAPI_FUNC(int) PyCallable_Check(PyObject *); |
420 | | PyAPI_FUNC(void) PyObject_ClearWeakRefs(PyObject *); |
421 | | |
422 | | /* PyObject_Dir(obj) acts like Python builtins.dir(obj), returning a |
423 | | list of strings. PyObject_Dir(NULL) is like builtins.dir(), |
424 | | returning the names of the current locals. In this case, if there are |
425 | | no current locals, NULL is returned, and PyErr_Occurred() is false. |
426 | | */ |
427 | | PyAPI_FUNC(PyObject *) PyObject_Dir(PyObject *); |
428 | | |
429 | | /* Helpers for printing recursive container types */ |
430 | | PyAPI_FUNC(int) Py_ReprEnter(PyObject *); |
431 | | PyAPI_FUNC(void) Py_ReprLeave(PyObject *); |
432 | | |
433 | | /* Flag bits for printing: */ |
434 | | #define Py_PRINT_RAW 1 /* No string quotes etc. */ |
435 | | |
436 | | /* |
437 | | Type flags (tp_flags) |
438 | | |
439 | | These flags are used to change expected features and behavior for a |
440 | | particular type. |
441 | | |
442 | | Arbitration of the flag bit positions will need to be coordinated among |
443 | | all extension writers who publicly release their extensions (this will |
444 | | be fewer than you might expect!). |
445 | | |
446 | | Most flags were removed as of Python 3.0 to make room for new flags. (Some |
447 | | flags are not for backwards compatibility but to indicate the presence of an |
448 | | optional feature; these flags remain of course.) |
449 | | |
450 | | Type definitions should use Py_TPFLAGS_DEFAULT for their tp_flags value. |
451 | | |
452 | | Code can use PyType_HasFeature(type_ob, flag_value) to test whether the |
453 | | given type object has a specified feature. |
454 | | */ |
455 | | |
456 | | #ifndef Py_LIMITED_API |
457 | | |
458 | | /* Track types initialized using _PyStaticType_InitBuiltin(). */ |
459 | | #define _Py_TPFLAGS_STATIC_BUILTIN (1 << 1) |
460 | | |
461 | | /* The values array is placed inline directly after the rest of |
462 | | * the object. Implies Py_TPFLAGS_HAVE_GC. |
463 | | */ |
464 | | #define Py_TPFLAGS_INLINE_VALUES (1 << 2) |
465 | | |
466 | | /* Placement of weakref pointers are managed by the VM, not by the type. |
467 | | * The VM will automatically set tp_weaklistoffset. Implies Py_TPFLAGS_HAVE_GC. |
468 | | */ |
469 | | #define Py_TPFLAGS_MANAGED_WEAKREF (1 << 3) |
470 | | |
471 | | /* Placement of dict (and values) pointers are managed by the VM, not by the type. |
472 | | * The VM will automatically set tp_dictoffset. Implies Py_TPFLAGS_HAVE_GC. |
473 | | */ |
474 | | #define Py_TPFLAGS_MANAGED_DICT (1 << 4) |
475 | | |
476 | | /* Type has dictionary or weakref pointers that are managed by VM and has |
477 | | * to allocate space to store these. |
478 | | */ |
479 | | #define Py_TPFLAGS_PREHEADER (Py_TPFLAGS_MANAGED_WEAKREF | Py_TPFLAGS_MANAGED_DICT) |
480 | | |
481 | | /* Set if instances of the type object are treated as sequences for pattern matching */ |
482 | | #define Py_TPFLAGS_SEQUENCE (1 << 5) |
483 | | /* Set if instances of the type object are treated as mappings for pattern matching */ |
484 | | #define Py_TPFLAGS_MAPPING (1 << 6) |
485 | | #endif |
486 | | |
487 | | /* Disallow creating instances of the type: set tp_new to NULL and don't create |
488 | | * the "__new__" key in the type dictionary. */ |
489 | | #define Py_TPFLAGS_DISALLOW_INSTANTIATION (1UL << 7) |
490 | | |
491 | | /* Set if the type object is immutable: type attributes cannot be set nor deleted */ |
492 | | #define Py_TPFLAGS_IMMUTABLETYPE (1UL << 8) |
493 | | |
494 | | /* Set if the type object is dynamically allocated */ |
495 | | #define Py_TPFLAGS_HEAPTYPE (1UL << 9) |
496 | | |
497 | | /* Set if the type allows subclassing */ |
498 | | #define Py_TPFLAGS_BASETYPE (1UL << 10) |
499 | | |
500 | | /* Set if the type implements the vectorcall protocol (PEP 590) */ |
501 | | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030C0000 |
502 | | #define Py_TPFLAGS_HAVE_VECTORCALL (1UL << 11) |
503 | | #ifndef Py_LIMITED_API |
504 | | // Backwards compatibility alias for API that was provisional in Python 3.8 |
505 | | #define _Py_TPFLAGS_HAVE_VECTORCALL Py_TPFLAGS_HAVE_VECTORCALL |
506 | | #endif |
507 | | #endif |
508 | | |
509 | | /* Set if the type is 'ready' -- fully initialized */ |
510 | | #define Py_TPFLAGS_READY (1UL << 12) |
511 | | |
512 | | /* Set while the type is being 'readied', to prevent recursive ready calls */ |
513 | | #define Py_TPFLAGS_READYING (1UL << 13) |
514 | | |
515 | | /* Objects support garbage collection (see objimpl.h) */ |
516 | | #define Py_TPFLAGS_HAVE_GC (1UL << 14) |
517 | | |
518 | | /* These two bits are preserved for Stackless Python, next after this is 17 */ |
519 | | #ifdef STACKLESS |
520 | | #define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION (3UL << 15) |
521 | | #else |
522 | | #define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION 0 |
523 | | #endif |
524 | | |
525 | | /* Objects behave like an unbound method */ |
526 | | #define Py_TPFLAGS_METHOD_DESCRIPTOR (1UL << 17) |
527 | | |
528 | | /* Unused. Legacy flag */ |
529 | | #define Py_TPFLAGS_VALID_VERSION_TAG (1UL << 19) |
530 | | |
531 | | /* Type is abstract and cannot be instantiated */ |
532 | | #define Py_TPFLAGS_IS_ABSTRACT (1UL << 20) |
533 | | |
534 | | // This undocumented flag gives certain built-ins their unique pattern-matching |
535 | | // behavior, which allows a single positional subpattern to match against the |
536 | | // subject itself (rather than a mapped attribute on it): |
537 | | #define _Py_TPFLAGS_MATCH_SELF (1UL << 22) |
538 | | |
539 | | /* Items (ob_size*tp_itemsize) are found at the end of an instance's memory */ |
540 | | #define Py_TPFLAGS_ITEMS_AT_END (1UL << 23) |
541 | | |
542 | | /* These flags are used to determine if a type is a subclass. */ |
543 | | #define Py_TPFLAGS_LONG_SUBCLASS (1UL << 24) |
544 | | #define Py_TPFLAGS_LIST_SUBCLASS (1UL << 25) |
545 | | #define Py_TPFLAGS_TUPLE_SUBCLASS (1UL << 26) |
546 | | #define Py_TPFLAGS_BYTES_SUBCLASS (1UL << 27) |
547 | | #define Py_TPFLAGS_UNICODE_SUBCLASS (1UL << 28) |
548 | | #define Py_TPFLAGS_DICT_SUBCLASS (1UL << 29) |
549 | | #define Py_TPFLAGS_BASE_EXC_SUBCLASS (1UL << 30) |
550 | | #define Py_TPFLAGS_TYPE_SUBCLASS (1UL << 31) |
551 | | |
552 | | #define Py_TPFLAGS_DEFAULT ( \ |
553 | | Py_TPFLAGS_HAVE_STACKLESS_EXTENSION | \ |
554 | | 0) |
555 | | |
556 | | /* NOTE: Some of the following flags reuse lower bits (removed as part of the |
557 | | * Python 3.0 transition). */ |
558 | | |
559 | | /* The following flags are kept for compatibility; in previous |
560 | | * versions they indicated presence of newer tp_* fields on the |
561 | | * type struct. |
562 | | * Starting with 3.8, binary compatibility of C extensions across |
563 | | * feature releases of Python is not supported anymore (except when |
564 | | * using the stable ABI, in which all classes are created dynamically, |
565 | | * using the interpreter's memory layout.) |
566 | | * Note that older extensions using the stable ABI set these flags, |
567 | | * so the bits must not be repurposed. |
568 | | */ |
569 | | #define Py_TPFLAGS_HAVE_FINALIZE (1UL << 0) |
570 | | #define Py_TPFLAGS_HAVE_VERSION_TAG (1UL << 18) |
571 | | |
572 | | // Flag values for ob_flags (16 bits available, if SIZEOF_VOID_P > 4). |
573 | | #define _Py_IMMORTAL_FLAGS (1 << 0) |
574 | | #define _Py_LEGACY_ABI_CHECK_FLAG (1 << 1) /* see PyModuleDef_Init() */ |
575 | | #define _Py_STATICALLY_ALLOCATED_FLAG (1 << 2) |
576 | | #if !defined(Py_LIMITED_API) |
577 | | # if defined(Py_GIL_DISABLED) && defined(Py_DEBUG) |
578 | | # define _Py_TYPE_REVEALED_FLAG (1 << 3) |
579 | | # endif |
580 | | #endif |
581 | | |
582 | | #define Py_CONSTANT_NONE 0 |
583 | | #define Py_CONSTANT_FALSE 1 |
584 | | #define Py_CONSTANT_TRUE 2 |
585 | | #define Py_CONSTANT_ELLIPSIS 3 |
586 | | #define Py_CONSTANT_NOT_IMPLEMENTED 4 |
587 | | #define Py_CONSTANT_ZERO 5 |
588 | | #define Py_CONSTANT_ONE 6 |
589 | | #define Py_CONSTANT_EMPTY_STR 7 |
590 | | #define Py_CONSTANT_EMPTY_BYTES 8 |
591 | | #define Py_CONSTANT_EMPTY_TUPLE 9 |
592 | | |
593 | | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000 |
594 | | PyAPI_FUNC(PyObject*) Py_GetConstant(unsigned int constant_id); |
595 | | PyAPI_FUNC(PyObject*) Py_GetConstantBorrowed(unsigned int constant_id); |
596 | | #endif |
597 | | |
598 | | |
599 | | /* |
600 | | _Py_NoneStruct is an object of undefined type which can be used in contexts |
601 | | where NULL (nil) is not suitable (since NULL often means 'error'). |
602 | | */ |
603 | | PyAPI_DATA(PyObject) _Py_NoneStruct; /* Don't use this directly */ |
604 | | |
605 | | #if defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030D0000 |
606 | | # define Py_None Py_GetConstantBorrowed(Py_CONSTANT_NONE) |
607 | | #else |
608 | | # define Py_None (&_Py_NoneStruct) |
609 | | #endif |
610 | | |
611 | | // Test if an object is the None singleton, the same as "x is None" in Python. |
612 | | PyAPI_FUNC(int) Py_IsNone(PyObject *x); |
613 | | #define Py_IsNone(x) Py_Is((x), Py_None) |
614 | | |
615 | | /* Macro for returning Py_None from a function. |
616 | | * Only treat Py_None as immortal in the limited C API 3.12 and newer. */ |
617 | | #if defined(Py_LIMITED_API) && Py_LIMITED_API+0 < 0x030c0000 |
618 | | # define Py_RETURN_NONE return Py_NewRef(Py_None) |
619 | | #else |
620 | | # define Py_RETURN_NONE return Py_None |
621 | | #endif |
622 | | |
623 | | /* |
624 | | Py_NotImplemented is a singleton used to signal that an operation is |
625 | | not implemented for a given type combination. |
626 | | */ |
627 | | PyAPI_DATA(PyObject) _Py_NotImplementedStruct; /* Don't use this directly */ |
628 | | |
629 | | #if defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030D0000 |
630 | | # define Py_NotImplemented Py_GetConstantBorrowed(Py_CONSTANT_NOT_IMPLEMENTED) |
631 | | #else |
632 | | # define Py_NotImplemented (&_Py_NotImplementedStruct) |
633 | | #endif |
634 | | |
635 | | /* Macro for returning Py_NotImplemented from a function. Only treat |
636 | | * Py_NotImplemented as immortal in the limited C API 3.12 and newer. */ |
637 | | #if defined(Py_LIMITED_API) && Py_LIMITED_API+0 < 0x030c0000 |
638 | | # define Py_RETURN_NOTIMPLEMENTED return Py_NewRef(Py_NotImplemented) |
639 | | #else |
640 | | # define Py_RETURN_NOTIMPLEMENTED return Py_NotImplemented |
641 | | #endif |
642 | | |
643 | | /* Rich comparison opcodes */ |
644 | | #define Py_LT 0 |
645 | | #define Py_LE 1 |
646 | | #define Py_EQ 2 |
647 | | #define Py_NE 3 |
648 | | #define Py_GT 4 |
649 | | #define Py_GE 5 |
650 | | |
651 | | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030A0000 |
652 | | /* Result of calling PyIter_Send */ |
653 | | typedef enum { |
654 | | PYGEN_RETURN = 0, |
655 | | PYGEN_ERROR = -1, |
656 | | PYGEN_NEXT = 1 |
657 | | } PySendResult; |
658 | | #endif |
659 | | |
660 | | /* |
661 | | * Macro for implementing rich comparisons |
662 | | * |
663 | | * Needs to be a macro because any C-comparable type can be used. |
664 | | */ |
665 | | #define Py_RETURN_RICHCOMPARE(val1, val2, op) \ |
666 | | do { \ |
667 | | switch (op) { \ |
668 | | case Py_EQ: if ((val1) == (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \ |
669 | | case Py_NE: if ((val1) != (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \ |
670 | | case Py_LT: if ((val1) < (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \ |
671 | | case Py_GT: if ((val1) > (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \ |
672 | | case Py_LE: if ((val1) <= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \ |
673 | | case Py_GE: if ((val1) >= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \ |
674 | | default: \ |
675 | | Py_UNREACHABLE(); \ |
676 | | } \ |
677 | | } while (0) |
678 | | |
679 | | |
680 | | /* |
681 | | More conventions |
682 | | ================ |
683 | | |
684 | | Argument Checking |
685 | | ----------------- |
686 | | |
687 | | Functions that take objects as arguments normally don't check for nil |
688 | | arguments, but they do check the type of the argument, and return an |
689 | | error if the function doesn't apply to the type. |
690 | | |
691 | | Failure Modes |
692 | | ------------- |
693 | | |
694 | | Functions may fail for a variety of reasons, including running out of |
695 | | memory. This is communicated to the caller in two ways: an error string |
696 | | is set (see errors.h), and the function result differs: functions that |
697 | | normally return a pointer return NULL for failure, functions returning |
698 | | an integer return -1 (which could be a legal return value too!), and |
699 | | other functions return 0 for success and -1 for failure. |
700 | | Callers should always check for errors before using the result. If |
701 | | an error was set, the caller must either explicitly clear it, or pass |
702 | | the error on to its caller. |
703 | | |
704 | | Reference Counts |
705 | | ---------------- |
706 | | |
707 | | It takes a while to get used to the proper usage of reference counts. |
708 | | |
709 | | Functions that create an object set the reference count to 1; such new |
710 | | objects must be stored somewhere or destroyed again with Py_DECREF(). |
711 | | Some functions that 'store' objects, such as PyTuple_SetItem() and |
712 | | PyList_SetItem(), |
713 | | don't increment the reference count of the object, since the most |
714 | | frequent use is to store a fresh object. Functions that 'retrieve' |
715 | | objects, such as PyTuple_GetItem() and PyDict_GetItemString(), also |
716 | | don't increment |
717 | | the reference count, since most frequently the object is only looked at |
718 | | quickly. Thus, to retrieve an object and store it again, the caller |
719 | | must call Py_INCREF() explicitly. |
720 | | |
721 | | NOTE: functions that 'consume' a reference count, like |
722 | | PyList_SetItem(), consume the reference even if the object wasn't |
723 | | successfully stored, to simplify error handling. |
724 | | |
725 | | It seems attractive to make other functions that take an object as |
726 | | argument consume a reference count; however, this may quickly get |
727 | | confusing (even the current practice is already confusing). Consider |
728 | | it carefully, it may save lots of calls to Py_INCREF() and Py_DECREF() at |
729 | | times. |
730 | | */ |
731 | | |
732 | | #ifndef Py_LIMITED_API |
733 | | # define Py_CPYTHON_OBJECT_H |
734 | | # include "cpython/object.h" |
735 | | # undef Py_CPYTHON_OBJECT_H |
736 | | #endif |
737 | | |
738 | | |
739 | | static inline int |
740 | | PyType_HasFeature(PyTypeObject *type, unsigned long feature) |
741 | 142k | { |
742 | 142k | unsigned long flags; |
743 | | #ifdef Py_LIMITED_API |
744 | | // PyTypeObject is opaque in the limited C API |
745 | | flags = PyType_GetFlags(type); |
746 | | #else |
747 | 142k | flags = type->tp_flags; |
748 | 142k | #endif |
749 | 142k | return ((flags & feature) != 0); |
750 | 142k | } |
751 | | |
752 | 142k | #define PyType_FastSubclass(type, flag) PyType_HasFeature((type), (flag)) |
753 | | |
754 | 0 | static inline int PyType_Check(PyObject *op) { |
755 | 0 | return PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_TYPE_SUBCLASS); |
756 | 0 | } |
757 | | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 |
758 | | # define PyType_Check(op) PyType_Check(_PyObject_CAST(op)) |
759 | | #endif |
760 | | |
761 | | #define _PyType_CAST(op) \ |
762 | | (assert(PyType_Check(op)), _Py_CAST(PyTypeObject*, (op))) |
763 | | |
764 | 0 | static inline int PyType_CheckExact(PyObject *op) { |
765 | 0 | return Py_IS_TYPE(op, &PyType_Type); |
766 | 0 | } |
767 | | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 |
768 | | # define PyType_CheckExact(op) PyType_CheckExact(_PyObject_CAST(op)) |
769 | | #endif |
770 | | |
771 | | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000 |
772 | | PyAPI_FUNC(PyObject *) PyType_GetModuleByDef(PyTypeObject *, PyModuleDef *); |
773 | | #endif |
774 | | |
775 | | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030e0000 |
776 | | PyAPI_FUNC(int) PyType_Freeze(PyTypeObject *type); |
777 | | #endif |
778 | | |
779 | | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 15) |
780 | | PyAPI_FUNC(PyObject *) PyType_GetModuleByToken(PyTypeObject *type, |
781 | | const void *token); |
782 | | PyAPI_FUNC(void *) PyObject_GetTypeData_DuringGC(PyObject *obj, |
783 | | PyTypeObject *cls); |
784 | | PyAPI_FUNC(void *) PyType_GetModuleState_DuringGC(PyTypeObject *); |
785 | | PyAPI_FUNC(int) PyType_GetBaseByToken_DuringGC(PyTypeObject *, |
786 | | void *, PyTypeObject **); |
787 | | PyAPI_FUNC(PyObject *) PyType_GetModule_DuringGC(PyTypeObject *); |
788 | | PyAPI_FUNC(PyObject *) PyType_GetModuleByToken_DuringGC(PyTypeObject *type, |
789 | | const void *token); |
790 | | #endif |
791 | | |
792 | | #ifdef __cplusplus |
793 | | } |
794 | | #endif |
795 | | #endif // !Py_OBJECT_H |