Coverage Report

Created: 2025-10-10 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/workspace/out/libfuzzer-coverage-x86_64/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
        PY_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;
144
#endif
145
        _Py_ALIGNED_DEF(_PyObject_MIN_ALIGNMENT, char) _aligner;
146
    };
147
148
    PyTypeObject *ob_type;
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
59.1M
#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 */
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
#if defined(Py_GIL_DISABLED) && !defined(Py_LIMITED_API)
190
PyAPI_FUNC(uintptr_t) _Py_GetThreadLocal_Addr(void);
191
192
static inline uintptr_t
193
_Py_ThreadId(void)
194
{
195
    uintptr_t tid;
196
#if defined(_MSC_VER) && defined(_M_X64)
197
    tid = __readgsqword(48);
198
#elif defined(_MSC_VER) && defined(_M_IX86)
199
    tid = __readfsdword(24);
200
#elif defined(_MSC_VER) && defined(_M_ARM64)
201
    tid = __getReg(18);
202
#elif defined(__MINGW32__) && defined(_M_X64)
203
    tid = __readgsqword(48);
204
#elif defined(__MINGW32__) && defined(_M_IX86)
205
    tid = __readfsdword(24);
206
#elif defined(__MINGW32__) && defined(_M_ARM64)
207
    tid = __getReg(18);
208
#elif defined(__i386__)
209
    __asm__("movl %%gs:0, %0" : "=r" (tid));  // 32-bit always uses GS
210
#elif defined(__MACH__) && defined(__x86_64__)
211
    __asm__("movq %%gs:0, %0" : "=r" (tid));  // x86_64 macOSX uses GS
212
#elif defined(__x86_64__)
213
   __asm__("movq %%fs:0, %0" : "=r" (tid));  // x86_64 Linux, BSD uses FS
214
#elif defined(__arm__) && __ARM_ARCH >= 7
215
    __asm__ ("mrc p15, 0, %0, c13, c0, 3\nbic %0, %0, #3" : "=r" (tid));
216
#elif defined(__aarch64__) && defined(__APPLE__)
217
    __asm__ ("mrs %0, tpidrro_el0" : "=r" (tid));
218
#elif defined(__aarch64__)
219
    __asm__ ("mrs %0, tpidr_el0" : "=r" (tid));
220
#elif defined(__powerpc64__)
221
    #if defined(__clang__) && _Py__has_builtin(__builtin_thread_pointer)
222
    tid = (uintptr_t)__builtin_thread_pointer();
223
    #else
224
    // r13 is reserved for use as system thread ID by the Power 64-bit ABI.
225
    register uintptr_t tp __asm__ ("r13");
226
    __asm__("" : "=r" (tp));
227
    tid = tp;
228
    #endif
229
#elif defined(__powerpc__)
230
    #if defined(__clang__) && _Py__has_builtin(__builtin_thread_pointer)
231
    tid = (uintptr_t)__builtin_thread_pointer();
232
    #else
233
    // r2 is reserved for use as system thread ID by the Power 32-bit ABI.
234
    register uintptr_t tp __asm__ ("r2");
235
    __asm__ ("" : "=r" (tp));
236
    tid = tp;
237
    #endif
238
#elif defined(__s390__) && defined(__GNUC__)
239
    // Both GCC and Clang have supported __builtin_thread_pointer
240
    // for s390 from long time ago.
241
    tid = (uintptr_t)__builtin_thread_pointer();
242
#elif defined(__riscv)
243
    #if defined(__clang__) && _Py__has_builtin(__builtin_thread_pointer)
244
    tid = (uintptr_t)__builtin_thread_pointer();
245
    #else
246
    // tp is Thread Pointer provided by the RISC-V ABI.
247
    __asm__ ("mv %0, tp" : "=r" (tid));
248
    #endif
249
#else
250
    // Fallback to a portable implementation if we do not have a faster
251
    // platform-specific implementation.
252
    tid = _Py_GetThreadLocal_Addr();
253
#endif
254
  return tid;
255
}
256
257
static inline Py_ALWAYS_INLINE int
258
_Py_IsOwnedByCurrentThread(PyObject *ob)
259
{
260
#ifdef _Py_THREAD_SANITIZER
261
    return _Py_atomic_load_uintptr_relaxed(&ob->ob_tid) == _Py_ThreadId();
262
#else
263
    return ob->ob_tid == _Py_ThreadId();
264
#endif
265
}
266
#endif
267
268
// Py_TYPE() implementation for the stable ABI
269
PyAPI_FUNC(PyTypeObject*) Py_TYPE(PyObject *ob);
270
271
#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030e0000
272
    // Stable ABI implements Py_TYPE() as a function call
273
    // on limited C API version 3.14 and newer.
274
#else
275
    static inline PyTypeObject* _Py_TYPE(PyObject *ob)
276
0
    {
277
0
        return ob->ob_type;
278
0
    }
279
    #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
280
    #   define Py_TYPE(ob) _Py_TYPE(_PyObject_CAST(ob))
281
    #else
282
    #   define Py_TYPE(ob) _Py_TYPE(ob)
283
    #endif
284
#endif
285
286
PyAPI_DATA(PyTypeObject) PyLong_Type;
287
PyAPI_DATA(PyTypeObject) PyBool_Type;
288
289
#ifndef _Py_OPAQUE_PYOBJECT
290
// bpo-39573: The Py_SET_SIZE() function must be used to set an object size.
291
0
static inline Py_ssize_t Py_SIZE(PyObject *ob) {
292
0
    assert(Py_TYPE(ob) != &PyLong_Type);
293
0
    assert(Py_TYPE(ob) != &PyBool_Type);
294
0
    return  _PyVarObject_CAST(ob)->ob_size;
295
0
}
296
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
297
#  define Py_SIZE(ob) Py_SIZE(_PyObject_CAST(ob))
298
#endif
299
#endif // !defined(_Py_OPAQUE_PYOBJECT)
300
301
0
static inline int Py_IS_TYPE(PyObject *ob, PyTypeObject *type) {
302
0
    return Py_TYPE(ob) == type;
303
0
}
304
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
305
#  define Py_IS_TYPE(ob, type) Py_IS_TYPE(_PyObject_CAST(ob), (type))
306
#endif
307
308
309
#ifndef _Py_OPAQUE_PYOBJECT
310
0
static inline void Py_SET_TYPE(PyObject *ob, PyTypeObject *type) {
311
0
    ob->ob_type = type;
312
0
}
313
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
314
#  define Py_SET_TYPE(ob, type) Py_SET_TYPE(_PyObject_CAST(ob), type)
315
#endif
316
317
0
static inline void Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size) {
318
0
    assert(Py_TYPE(_PyObject_CAST(ob)) != &PyLong_Type);
319
0
    assert(Py_TYPE(_PyObject_CAST(ob)) != &PyBool_Type);
320
0
#ifdef Py_GIL_DISABLED
321
0
    _Py_atomic_store_ssize_relaxed(&ob->ob_size, size);
322
0
#else
323
0
    ob->ob_size = size;
324
0
#endif
325
0
}
326
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
327
#  define Py_SET_SIZE(ob, size) Py_SET_SIZE(_PyVarObject_CAST(ob), (size))
328
#endif
329
#endif // !defined(_Py_OPAQUE_PYOBJECT)
330
331
332
/*
333
Type objects contain a string containing the type name (to help somewhat
334
in debugging), the allocation parameters (see PyObject_New() and
335
PyObject_NewVar()),
336
and methods for accessing objects of the type.  Methods are optional, a
337
nil pointer meaning that particular kind of access is not available for
338
this type.  The Py_DECREF() macro uses the tp_dealloc method without
339
checking for a nil pointer; it should always be implemented except if
340
the implementation can guarantee that the reference count will never
341
reach zero (e.g., for statically allocated type objects).
342
343
NB: the methods for certain type groups are now contained in separate
344
method blocks.
345
*/
346
347
typedef PyObject * (*unaryfunc)(PyObject *);
348
typedef PyObject * (*binaryfunc)(PyObject *, PyObject *);
349
typedef PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *);
350
typedef int (*inquiry)(PyObject *);
351
typedef Py_ssize_t (*lenfunc)(PyObject *);
352
typedef PyObject *(*ssizeargfunc)(PyObject *, Py_ssize_t);
353
typedef PyObject *(*ssizessizeargfunc)(PyObject *, Py_ssize_t, Py_ssize_t);
354
typedef int(*ssizeobjargproc)(PyObject *, Py_ssize_t, PyObject *);
355
typedef int(*ssizessizeobjargproc)(PyObject *, Py_ssize_t, Py_ssize_t, PyObject *);
356
typedef int(*objobjargproc)(PyObject *, PyObject *, PyObject *);
357
358
typedef int (*objobjproc)(PyObject *, PyObject *);
359
typedef int (*visitproc)(PyObject *, void *);
360
typedef int (*traverseproc)(PyObject *, visitproc, void *);
361
362
363
typedef void (*freefunc)(void *);
364
typedef void (*destructor)(PyObject *);
365
typedef PyObject *(*getattrfunc)(PyObject *, char *);
366
typedef PyObject *(*getattrofunc)(PyObject *, PyObject *);
367
typedef int (*setattrfunc)(PyObject *, char *, PyObject *);
368
typedef int (*setattrofunc)(PyObject *, PyObject *, PyObject *);
369
typedef PyObject *(*reprfunc)(PyObject *);
370
typedef Py_hash_t (*hashfunc)(PyObject *);
371
typedef PyObject *(*richcmpfunc) (PyObject *, PyObject *, int);
372
typedef PyObject *(*getiterfunc) (PyObject *);
373
typedef PyObject *(*iternextfunc) (PyObject *);
374
typedef PyObject *(*descrgetfunc) (PyObject *, PyObject *, PyObject *);
375
typedef int (*descrsetfunc) (PyObject *, PyObject *, PyObject *);
376
typedef int (*initproc)(PyObject *, PyObject *, PyObject *);
377
typedef PyObject *(*newfunc)(PyTypeObject *, PyObject *, PyObject *);
378
typedef PyObject *(*allocfunc)(PyTypeObject *, Py_ssize_t);
379
380
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030c0000 // 3.12
381
typedef PyObject *(*vectorcallfunc)(PyObject *callable, PyObject *const *args,
382
                                    size_t nargsf, PyObject *kwnames);
383
#endif
384
385
typedef struct{
386
    int slot;    /* slot id, see below */
387
    void *pfunc; /* function pointer */
388
} PyType_Slot;
389
390
typedef struct{
391
    const char* name;
392
    int basicsize;
393
    int itemsize;
394
    unsigned int flags;
395
    PyType_Slot *slots; /* terminated by slot==0. */
396
} PyType_Spec;
397
398
PyAPI_FUNC(PyObject*) PyType_FromSpec(PyType_Spec*);
399
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
400
PyAPI_FUNC(PyObject*) PyType_FromSpecWithBases(PyType_Spec*, PyObject*);
401
#endif
402
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03040000
403
PyAPI_FUNC(void*) PyType_GetSlot(PyTypeObject*, int);
404
#endif
405
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000
406
PyAPI_FUNC(PyObject*) PyType_FromModuleAndSpec(PyObject *, PyType_Spec *, PyObject *);
407
PyAPI_FUNC(PyObject *) PyType_GetModule(PyTypeObject *);
408
PyAPI_FUNC(void *) PyType_GetModuleState(PyTypeObject *);
409
#endif
410
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030B0000
411
PyAPI_FUNC(PyObject *) PyType_GetName(PyTypeObject *);
412
PyAPI_FUNC(PyObject *) PyType_GetQualName(PyTypeObject *);
413
#endif
414
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030D0000
415
PyAPI_FUNC(PyObject *) PyType_GetFullyQualifiedName(PyTypeObject *type);
416
PyAPI_FUNC(PyObject *) PyType_GetModuleName(PyTypeObject *type);
417
#endif
418
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030C0000
419
PyAPI_FUNC(PyObject *) PyType_FromMetaclass(PyTypeObject*, PyObject*, PyType_Spec*, PyObject*);
420
PyAPI_FUNC(void *) PyObject_GetTypeData(PyObject *obj, PyTypeObject *cls);
421
PyAPI_FUNC(Py_ssize_t) PyType_GetTypeDataSize(PyTypeObject *cls);
422
#endif
423
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030E0000
424
PyAPI_FUNC(int) PyType_GetBaseByToken(PyTypeObject *, void *, PyTypeObject **);
425
#define Py_TP_USE_SPEC NULL
426
#endif
427
428
/* Generic type check */
429
PyAPI_FUNC(int) PyType_IsSubtype(PyTypeObject *, PyTypeObject *);
430
431
0
static inline int PyObject_TypeCheck(PyObject *ob, PyTypeObject *type) {
432
0
    return Py_IS_TYPE(ob, type) || PyType_IsSubtype(Py_TYPE(ob), type);
433
0
}
434
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
435
#  define PyObject_TypeCheck(ob, type) PyObject_TypeCheck(_PyObject_CAST(ob), (type))
436
#endif
437
438
PyAPI_DATA(PyTypeObject) PyType_Type; /* built-in 'type' */
439
PyAPI_DATA(PyTypeObject) PyBaseObject_Type; /* built-in 'object' */
440
PyAPI_DATA(PyTypeObject) PySuper_Type; /* built-in 'super' */
441
442
PyAPI_FUNC(unsigned long) PyType_GetFlags(PyTypeObject*);
443
444
PyAPI_FUNC(int) PyType_Ready(PyTypeObject *);
445
PyAPI_FUNC(PyObject *) PyType_GenericAlloc(PyTypeObject *, Py_ssize_t);
446
PyAPI_FUNC(PyObject *) PyType_GenericNew(PyTypeObject *,
447
                                               PyObject *, PyObject *);
448
PyAPI_FUNC(unsigned int) PyType_ClearCache(void);
449
PyAPI_FUNC(void) PyType_Modified(PyTypeObject *);
450
451
/* Generic operations on objects */
452
PyAPI_FUNC(PyObject *) PyObject_Repr(PyObject *);
453
PyAPI_FUNC(PyObject *) PyObject_Str(PyObject *);
454
PyAPI_FUNC(PyObject *) PyObject_ASCII(PyObject *);
455
PyAPI_FUNC(PyObject *) PyObject_Bytes(PyObject *);
456
PyAPI_FUNC(PyObject *) PyObject_RichCompare(PyObject *, PyObject *, int);
457
PyAPI_FUNC(int) PyObject_RichCompareBool(PyObject *, PyObject *, int);
458
PyAPI_FUNC(PyObject *) PyObject_GetAttrString(PyObject *, const char *);
459
PyAPI_FUNC(int) PyObject_SetAttrString(PyObject *, const char *, PyObject *);
460
PyAPI_FUNC(int) PyObject_DelAttrString(PyObject *v, const char *name);
461
PyAPI_FUNC(int) PyObject_HasAttrString(PyObject *, const char *);
462
PyAPI_FUNC(PyObject *) PyObject_GetAttr(PyObject *, PyObject *);
463
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000
464
PyAPI_FUNC(int) PyObject_GetOptionalAttr(PyObject *, PyObject *, PyObject **);
465
PyAPI_FUNC(int) PyObject_GetOptionalAttrString(PyObject *, const char *, PyObject **);
466
#endif
467
PyAPI_FUNC(int) PyObject_SetAttr(PyObject *, PyObject *, PyObject *);
468
PyAPI_FUNC(int) PyObject_DelAttr(PyObject *v, PyObject *name);
469
PyAPI_FUNC(int) PyObject_HasAttr(PyObject *, PyObject *);
470
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000
471
PyAPI_FUNC(int) PyObject_HasAttrWithError(PyObject *, PyObject *);
472
PyAPI_FUNC(int) PyObject_HasAttrStringWithError(PyObject *, const char *);
473
#endif
474
PyAPI_FUNC(PyObject *) PyObject_SelfIter(PyObject *);
475
PyAPI_FUNC(PyObject *) PyObject_GenericGetAttr(PyObject *, PyObject *);
476
PyAPI_FUNC(int) PyObject_GenericSetAttr(PyObject *, PyObject *, PyObject *);
477
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
478
PyAPI_FUNC(int) PyObject_GenericSetDict(PyObject *, PyObject *, void *);
479
#endif
480
PyAPI_FUNC(Py_hash_t) PyObject_Hash(PyObject *);
481
PyAPI_FUNC(Py_hash_t) PyObject_HashNotImplemented(PyObject *);
482
PyAPI_FUNC(int) PyObject_IsTrue(PyObject *);
483
PyAPI_FUNC(int) PyObject_Not(PyObject *);
484
PyAPI_FUNC(int) PyCallable_Check(PyObject *);
485
PyAPI_FUNC(void) PyObject_ClearWeakRefs(PyObject *);
486
487
/* PyObject_Dir(obj) acts like Python builtins.dir(obj), returning a
488
   list of strings.  PyObject_Dir(NULL) is like builtins.dir(),
489
   returning the names of the current locals.  In this case, if there are
490
   no current locals, NULL is returned, and PyErr_Occurred() is false.
491
*/
492
PyAPI_FUNC(PyObject *) PyObject_Dir(PyObject *);
493
494
/* Helpers for printing recursive container types */
495
PyAPI_FUNC(int) Py_ReprEnter(PyObject *);
496
PyAPI_FUNC(void) Py_ReprLeave(PyObject *);
497
498
/* Flag bits for printing: */
499
#define Py_PRINT_RAW    1       /* No string quotes etc. */
500
501
/*
502
Type flags (tp_flags)
503
504
These flags are used to change expected features and behavior for a
505
particular type.
506
507
Arbitration of the flag bit positions will need to be coordinated among
508
all extension writers who publicly release their extensions (this will
509
be fewer than you might expect!).
510
511
Most flags were removed as of Python 3.0 to make room for new flags.  (Some
512
flags are not for backwards compatibility but to indicate the presence of an
513
optional feature; these flags remain of course.)
514
515
Type definitions should use Py_TPFLAGS_DEFAULT for their tp_flags value.
516
517
Code can use PyType_HasFeature(type_ob, flag_value) to test whether the
518
given type object has a specified feature.
519
*/
520
521
#ifndef Py_LIMITED_API
522
523
/* Track types initialized using _PyStaticType_InitBuiltin(). */
524
#define _Py_TPFLAGS_STATIC_BUILTIN (1 << 1)
525
526
/* The values array is placed inline directly after the rest of
527
 * the object. Implies Py_TPFLAGS_HAVE_GC.
528
 */
529
#define Py_TPFLAGS_INLINE_VALUES (1 << 2)
530
531
/* Placement of weakref pointers are managed by the VM, not by the type.
532
 * The VM will automatically set tp_weaklistoffset.
533
 */
534
#define Py_TPFLAGS_MANAGED_WEAKREF (1 << 3)
535
536
/* Placement of dict (and values) pointers are managed by the VM, not by the type.
537
 * The VM will automatically set tp_dictoffset. Implies Py_TPFLAGS_HAVE_GC.
538
 */
539
#define Py_TPFLAGS_MANAGED_DICT (1 << 4)
540
541
/* Type has dictionary or weakref pointers that are managed by VM and has
542
 * to allocate space to store these.
543
 */
544
#define Py_TPFLAGS_PREHEADER (Py_TPFLAGS_MANAGED_WEAKREF | Py_TPFLAGS_MANAGED_DICT)
545
546
/* Set if instances of the type object are treated as sequences for pattern matching */
547
#define Py_TPFLAGS_SEQUENCE (1 << 5)
548
/* Set if instances of the type object are treated as mappings for pattern matching */
549
#define Py_TPFLAGS_MAPPING (1 << 6)
550
#endif
551
552
/* Disallow creating instances of the type: set tp_new to NULL and don't create
553
 * the "__new__" key in the type dictionary. */
554
#define Py_TPFLAGS_DISALLOW_INSTANTIATION (1UL << 7)
555
556
/* Set if the type object is immutable: type attributes cannot be set nor deleted */
557
#define Py_TPFLAGS_IMMUTABLETYPE (1UL << 8)
558
559
/* Set if the type object is dynamically allocated */
560
#define Py_TPFLAGS_HEAPTYPE (1UL << 9)
561
562
/* Set if the type allows subclassing */
563
#define Py_TPFLAGS_BASETYPE (1UL << 10)
564
565
/* Set if the type implements the vectorcall protocol (PEP 590) */
566
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030C0000
567
#define Py_TPFLAGS_HAVE_VECTORCALL (1UL << 11)
568
#ifndef Py_LIMITED_API
569
// Backwards compatibility alias for API that was provisional in Python 3.8
570
#define _Py_TPFLAGS_HAVE_VECTORCALL Py_TPFLAGS_HAVE_VECTORCALL
571
#endif
572
#endif
573
574
/* Set if the type is 'ready' -- fully initialized */
575
#define Py_TPFLAGS_READY (1UL << 12)
576
577
/* Set while the type is being 'readied', to prevent recursive ready calls */
578
#define Py_TPFLAGS_READYING (1UL << 13)
579
580
/* Objects support garbage collection (see objimpl.h) */
581
#define Py_TPFLAGS_HAVE_GC (1UL << 14)
582
583
/* These two bits are preserved for Stackless Python, next after this is 17 */
584
#ifdef STACKLESS
585
#define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION (3UL << 15)
586
#else
587
#define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION 0
588
#endif
589
590
/* Objects behave like an unbound method */
591
#define Py_TPFLAGS_METHOD_DESCRIPTOR (1UL << 17)
592
593
/* Unused. Legacy flag */
594
#define Py_TPFLAGS_VALID_VERSION_TAG  (1UL << 19)
595
596
/* Type is abstract and cannot be instantiated */
597
#define Py_TPFLAGS_IS_ABSTRACT (1UL << 20)
598
599
// This undocumented flag gives certain built-ins their unique pattern-matching
600
// behavior, which allows a single positional subpattern to match against the
601
// subject itself (rather than a mapped attribute on it):
602
#define _Py_TPFLAGS_MATCH_SELF (1UL << 22)
603
604
/* Items (ob_size*tp_itemsize) are found at the end of an instance's memory */
605
#define Py_TPFLAGS_ITEMS_AT_END (1UL << 23)
606
607
/* These flags are used to determine if a type is a subclass. */
608
#define Py_TPFLAGS_LONG_SUBCLASS        (1UL << 24)
609
#define Py_TPFLAGS_LIST_SUBCLASS        (1UL << 25)
610
#define Py_TPFLAGS_TUPLE_SUBCLASS       (1UL << 26)
611
#define Py_TPFLAGS_BYTES_SUBCLASS       (1UL << 27)
612
#define Py_TPFLAGS_UNICODE_SUBCLASS     (1UL << 28)
613
#define Py_TPFLAGS_DICT_SUBCLASS        (1UL << 29)
614
#define Py_TPFLAGS_BASE_EXC_SUBCLASS    (1UL << 30)
615
#define Py_TPFLAGS_TYPE_SUBCLASS        (1UL << 31)
616
617
#define Py_TPFLAGS_DEFAULT  ( \
618
                 Py_TPFLAGS_HAVE_STACKLESS_EXTENSION | \
619
                0)
620
621
/* NOTE: Some of the following flags reuse lower bits (removed as part of the
622
 * Python 3.0 transition). */
623
624
/* The following flags are kept for compatibility; in previous
625
 * versions they indicated presence of newer tp_* fields on the
626
 * type struct.
627
 * Starting with 3.8, binary compatibility of C extensions across
628
 * feature releases of Python is not supported anymore (except when
629
 * using the stable ABI, in which all classes are created dynamically,
630
 * using the interpreter's memory layout.)
631
 * Note that older extensions using the stable ABI set these flags,
632
 * so the bits must not be repurposed.
633
 */
634
#define Py_TPFLAGS_HAVE_FINALIZE (1UL << 0)
635
#define Py_TPFLAGS_HAVE_VERSION_TAG   (1UL << 18)
636
637
// Flag values for ob_flags (16 bits available, if SIZEOF_VOID_P > 4).
638
#define _Py_IMMORTAL_FLAGS (1 << 0)
639
#define _Py_LEGACY_ABI_CHECK_FLAG (1 << 1) /* see PyModuleDef_Init() */
640
#define _Py_STATICALLY_ALLOCATED_FLAG (1 << 2)
641
#if defined(Py_GIL_DISABLED) && defined(Py_DEBUG)
642
#define _Py_TYPE_REVEALED_FLAG (1 << 3)
643
#endif
644
645
#define Py_CONSTANT_NONE 0
646
#define Py_CONSTANT_FALSE 1
647
#define Py_CONSTANT_TRUE 2
648
#define Py_CONSTANT_ELLIPSIS 3
649
#define Py_CONSTANT_NOT_IMPLEMENTED 4
650
#define Py_CONSTANT_ZERO 5
651
#define Py_CONSTANT_ONE 6
652
#define Py_CONSTANT_EMPTY_STR 7
653
#define Py_CONSTANT_EMPTY_BYTES 8
654
#define Py_CONSTANT_EMPTY_TUPLE 9
655
656
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000
657
PyAPI_FUNC(PyObject*) Py_GetConstant(unsigned int constant_id);
658
PyAPI_FUNC(PyObject*) Py_GetConstantBorrowed(unsigned int constant_id);
659
#endif
660
661
662
/*
663
_Py_NoneStruct is an object of undefined type which can be used in contexts
664
where NULL (nil) is not suitable (since NULL often means 'error').
665
*/
666
PyAPI_DATA(PyObject) _Py_NoneStruct; /* Don't use this directly */
667
668
#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030D0000
669
#  define Py_None Py_GetConstantBorrowed(Py_CONSTANT_NONE)
670
#else
671
#  define Py_None (&_Py_NoneStruct)
672
#endif
673
674
// Test if an object is the None singleton, the same as "x is None" in Python.
675
PyAPI_FUNC(int) Py_IsNone(PyObject *x);
676
#define Py_IsNone(x) Py_Is((x), Py_None)
677
678
/* Macro for returning Py_None from a function.
679
 * Only treat Py_None as immortal in the limited C API 3.12 and newer. */
680
#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 < 0x030c0000
681
#  define Py_RETURN_NONE return Py_NewRef(Py_None)
682
#else
683
#  define Py_RETURN_NONE return Py_None
684
#endif
685
686
/*
687
Py_NotImplemented is a singleton used to signal that an operation is
688
not implemented for a given type combination.
689
*/
690
PyAPI_DATA(PyObject) _Py_NotImplementedStruct; /* Don't use this directly */
691
692
#if defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030D0000
693
#  define Py_NotImplemented Py_GetConstantBorrowed(Py_CONSTANT_NOT_IMPLEMENTED)
694
#else
695
#  define Py_NotImplemented (&_Py_NotImplementedStruct)
696
#endif
697
698
/* Macro for returning Py_NotImplemented from a function */
699
#define Py_RETURN_NOTIMPLEMENTED return Py_NotImplemented
700
701
/* Rich comparison opcodes */
702
#define Py_LT 0
703
#define Py_LE 1
704
#define Py_EQ 2
705
#define Py_NE 3
706
#define Py_GT 4
707
#define Py_GE 5
708
709
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030A0000
710
/* Result of calling PyIter_Send */
711
typedef enum {
712
    PYGEN_RETURN = 0,
713
    PYGEN_ERROR = -1,
714
    PYGEN_NEXT = 1
715
} PySendResult;
716
#endif
717
718
/*
719
 * Macro for implementing rich comparisons
720
 *
721
 * Needs to be a macro because any C-comparable type can be used.
722
 */
723
#define Py_RETURN_RICHCOMPARE(val1, val2, op)                               \
724
    do {                                                                    \
725
        switch (op) {                                                       \
726
        case Py_EQ: if ((val1) == (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE;  \
727
        case Py_NE: if ((val1) != (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE;  \
728
        case Py_LT: if ((val1) < (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE;   \
729
        case Py_GT: if ((val1) > (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE;   \
730
        case Py_LE: if ((val1) <= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE;  \
731
        case Py_GE: if ((val1) >= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE;  \
732
        default:                                                            \
733
            Py_UNREACHABLE();                                               \
734
        }                                                                   \
735
    } while (0)
736
737
738
/*
739
More conventions
740
================
741
742
Argument Checking
743
-----------------
744
745
Functions that take objects as arguments normally don't check for nil
746
arguments, but they do check the type of the argument, and return an
747
error if the function doesn't apply to the type.
748
749
Failure Modes
750
-------------
751
752
Functions may fail for a variety of reasons, including running out of
753
memory.  This is communicated to the caller in two ways: an error string
754
is set (see errors.h), and the function result differs: functions that
755
normally return a pointer return NULL for failure, functions returning
756
an integer return -1 (which could be a legal return value too!), and
757
other functions return 0 for success and -1 for failure.
758
Callers should always check for errors before using the result.  If
759
an error was set, the caller must either explicitly clear it, or pass
760
the error on to its caller.
761
762
Reference Counts
763
----------------
764
765
It takes a while to get used to the proper usage of reference counts.
766
767
Functions that create an object set the reference count to 1; such new
768
objects must be stored somewhere or destroyed again with Py_DECREF().
769
Some functions that 'store' objects, such as PyTuple_SetItem() and
770
PyList_SetItem(),
771
don't increment the reference count of the object, since the most
772
frequent use is to store a fresh object.  Functions that 'retrieve'
773
objects, such as PyTuple_GetItem() and PyDict_GetItemString(), also
774
don't increment
775
the reference count, since most frequently the object is only looked at
776
quickly.  Thus, to retrieve an object and store it again, the caller
777
must call Py_INCREF() explicitly.
778
779
NOTE: functions that 'consume' a reference count, like
780
PyList_SetItem(), consume the reference even if the object wasn't
781
successfully stored, to simplify error handling.
782
783
It seems attractive to make other functions that take an object as
784
argument consume a reference count; however, this may quickly get
785
confusing (even the current practice is already confusing).  Consider
786
it carefully, it may save lots of calls to Py_INCREF() and Py_DECREF() at
787
times.
788
*/
789
790
#ifndef Py_LIMITED_API
791
#  define Py_CPYTHON_OBJECT_H
792
#  include "cpython/object.h"
793
#  undef Py_CPYTHON_OBJECT_H
794
#endif
795
796
797
static inline int
798
PyType_HasFeature(PyTypeObject *type, unsigned long feature)
799
0
{
800
0
    unsigned long flags;
801
0
#ifdef Py_LIMITED_API
802
0
    // PyTypeObject is opaque in the limited C API
803
0
    flags = PyType_GetFlags(type);
804
0
#else
805
0
    flags = type->tp_flags;
806
0
#endif
807
0
    return ((flags & feature) != 0);
808
0
}
809
810
#define PyType_FastSubclass(type, flag) PyType_HasFeature((type), (flag))
811
812
0
static inline int PyType_Check(PyObject *op) {
813
0
    return PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_TYPE_SUBCLASS);
814
0
}
815
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
816
#  define PyType_Check(op) PyType_Check(_PyObject_CAST(op))
817
#endif
818
819
#define _PyType_CAST(op) \
820
    (assert(PyType_Check(op)), _Py_CAST(PyTypeObject*, (op)))
821
822
0
static inline int PyType_CheckExact(PyObject *op) {
823
0
    return Py_IS_TYPE(op, &PyType_Type);
824
0
}
825
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
826
#  define PyType_CheckExact(op) PyType_CheckExact(_PyObject_CAST(op))
827
#endif
828
829
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000
830
PyAPI_FUNC(PyObject *) PyType_GetModuleByDef(PyTypeObject *, PyModuleDef *);
831
#endif
832
833
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030e0000
834
PyAPI_FUNC(int) PyType_Freeze(PyTypeObject *type);
835
#endif
836
837
#ifdef __cplusplus
838
}
839
#endif
840
#endif   // !Py_OBJECT_H