Coverage Report

Created: 2025-08-29 07:13

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