Coverage Report

Created: 2025-07-18 06:09

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