Coverage Report

Created: 2026-12-01 06:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython-install/include/python3.16/cpython/code.h
Line
Count
Source
1
/* Definitions for bytecode */
2
3
#ifndef Py_LIMITED_API
4
#ifndef Py_CODE_H
5
#define Py_CODE_H
6
7
#ifdef __cplusplus
8
extern "C" {
9
#endif
10
11
typedef struct {
12
    PyObject *_co_code;
13
    PyObject *_co_varnames;
14
    PyObject *_co_cellvars;
15
    PyObject *_co_freevars;
16
} _PyCoCached;
17
18
typedef struct {
19
    int size;
20
    int capacity;
21
    struct _PyExecutorObject *executors[1];
22
} _PyExecutorArray;
23
24
25
#ifdef Py_GIL_DISABLED
26
27
/* Each thread specializes a thread-local copy of the bytecode in free-threaded
28
 * builds. These copies are stored on the code object in a `_PyCodeArray`. The
29
 * first entry in the array always points to the "main" copy of the bytecode
30
 * that is stored at the end of the code object.
31
 */
32
typedef struct {
33
    Py_ssize_t size;
34
    char *entries[1];
35
} _PyCodeArray;
36
37
#define _PyCode_DEF_UNIQUE_ID() \
38
    Py_ssize_t _co_unique_id;     /* ID used for per-thread refcounting */
39
40
#define _PyCode_DEF_THREAD_LOCAL_BYTECODE() \
41
    _PyCodeArray *co_tlbc;
42
#else
43
#define _PyCode_DEF_UNIQUE_ID()
44
#define _PyCode_DEF_THREAD_LOCAL_BYTECODE()
45
#endif
46
47
// To avoid repeating ourselves in deepfreeze.py, all PyCodeObject members are
48
// defined in this macro:
49
#define _PyCode_DEF(SIZE) {                                                    \
50
    PyObject_VAR_HEAD                                                          \
51
                                                                               \
52
    /* Note only the following fields are used in hash and/or comparisons      \
53
     *                                                                         \
54
     * - co_name                                                               \
55
     * - co_argcount                                                           \
56
     * - co_posonlyargcount                                                    \
57
     * - co_kwonlyargcount                                                     \
58
     * - co_nlocals                                                            \
59
     * - co_stacksize                                                          \
60
     * - co_flags                                                              \
61
     * - co_firstlineno                                                        \
62
     * - co_consts                                                             \
63
     * - co_names                                                              \
64
     * - co_localsplusnames                                                    \
65
     * This is done to preserve the name and line number for tracebacks        \
66
     * and debuggers; otherwise, constant de-duplication would collapse        \
67
     * identical functions/lambdas defined on different lines.                 \
68
     */                                                                        \
69
                                                                               \
70
    /* These fields are set with provided values on new code objects. */       \
71
                                                                               \
72
    /* The hottest fields (in the eval loop) are grouped here at the top. */   \
73
    PyObject *co_consts;           /* list (constants used) */                 \
74
    PyObject *co_names;            /* list of strings (names used) */          \
75
    PyObject *co_exceptiontable;   /* Byte string encoding exception handling  \
76
                                      table */                                 \
77
    int co_flags;                  /* CO_..., see below */                     \
78
                                                                               \
79
    /* The rest are not so impactful on performance. */                        \
80
    int co_argcount;              /* #arguments, except *args */               \
81
    int co_posonlyargcount;       /* #positional only arguments */             \
82
    int co_kwonlyargcount;        /* #keyword only arguments */                \
83
    int co_stacksize;             /* #entries needed for evaluation stack */   \
84
    int co_firstlineno;           /* first source line number */               \
85
                                                                               \
86
    /* redundant values (derived from co_localsplusnames and                   \
87
       co_localspluskinds) */                                                  \
88
    int co_nlocalsplus;           /* number of spaces for holding local, cell, \
89
                                     and free variables */                     \
90
    int co_framesize;             /* Size of frame in words */                 \
91
    int co_nlocals;               /* number of local variables */              \
92
    int co_ncellvars;             /* total number of cell variables */         \
93
    int co_nfreevars;             /* number of free variables */               \
94
    uint32_t co_version;          /* version number */                         \
95
                                                                               \
96
    PyObject *co_localsplusnames; /* tuple mapping offsets to names */         \
97
    PyObject *co_localspluskinds; /* Bytes mapping to local kinds (one byte    \
98
                                     per variable) */                          \
99
    PyObject *co_filename;        /* unicode (where it was loaded from) */     \
100
    PyObject *co_name;            /* unicode (name, for reference) */          \
101
    PyObject *co_qualname;        /* unicode (qualname, for reference) */      \
102
    PyObject *co_linetable;       /* bytes object that holds location info */  \
103
    PyObject *co_weakreflist;     /* to support weakrefs to code objects */    \
104
    _PyExecutorArray *co_executors;      /* executors from optimizer */        \
105
    _PyCoCached *_co_cached;      /* cached co_* attributes */                 \
106
    uintptr_t _co_instrumentation_version; /* current instrumentation version */ \
107
    struct _PyCoMonitoringData *_co_monitoring; /* Monitoring data */          \
108
    _PyCode_DEF_UNIQUE_ID()                                                    \
109
    int _co_firsttraceable;       /* index of first traceable instruction */   \
110
    /* Scratch space for extra data relating to the code object.               \
111
       Type is a void* to keep the format private in codeobject.c to force     \
112
       people to go through the proper APIs. */                                \
113
    void *co_extra;                                                            \
114
    _PyCode_DEF_THREAD_LOCAL_BYTECODE()                                        \
115
    char co_code_adaptive[(SIZE)];                                             \
116
}
117
118
/* Bytecode object */
119
struct PyCodeObject _PyCode_DEF(1);
120
121
/* Masks for co_flags above */
122
#define CO_OPTIMIZED    0x0001
123
#define CO_NEWLOCALS    0x0002
124
#define CO_VARARGS      0x0004
125
#define CO_VARKEYWORDS  0x0008
126
#define CO_NESTED       0x0010
127
#define CO_GENERATOR    0x0020
128
129
/* The CO_COROUTINE flag is set for coroutine functions (defined with
130
   ``async def`` keywords) */
131
#define CO_COROUTINE            0x0080
132
#define CO_ITERABLE_COROUTINE   0x0100
133
#define CO_ASYNC_GENERATOR      0x0200
134
135
/* bpo-39562: These constant values are changed in Python 3.9
136
   to prevent collision with compiler flags. CO_FUTURE_ and PyCF_
137
   constants must be kept unique. PyCF_ constants can use bits from
138
   0x0100 to 0x10000. CO_FUTURE_ constants use bits starting at 0x20000. */
139
#define CO_FUTURE_DIVISION      0x20000
140
#define CO_FUTURE_ABSOLUTE_IMPORT 0x40000 /* do absolute imports by default */
141
#define CO_FUTURE_WITH_STATEMENT  0x80000
142
#define CO_FUTURE_PRINT_FUNCTION  0x100000
143
#define CO_FUTURE_UNICODE_LITERALS 0x200000
144
145
#define CO_FUTURE_BARRY_AS_BDFL  0x400000
146
#define CO_FUTURE_GENERATOR_STOP  0x800000
147
#define CO_FUTURE_ANNOTATIONS    0x1000000
148
149
#define CO_NO_MONITORING_EVENTS 0x2000000
150
151
/* Whether the code object has a docstring,
152
   If so, it will be the first item in co_consts
153
*/
154
#define CO_HAS_DOCSTRING 0x4000000
155
156
/* A function defined in class scope */
157
#define CO_METHOD  0x8000000
158
159
/* This should be defined if a future statement modifies the syntax.
160
   For example, when a keyword is added.
161
*/
162
#define PY_PARSER_REQUIRES_FUTURE_KEYWORD
163
164
#define CO_MAXBLOCKS 21 /* Max static block nesting within a function */
165
166
PyAPI_DATA(PyTypeObject) PyCode_Type;
167
168
#define PyCode_Check(op) Py_IS_TYPE((op), &PyCode_Type)
169
170
0
static inline Py_ssize_t PyCode_GetNumFree(PyCodeObject *op) {
171
0
    assert(PyCode_Check(op));
172
0
    return op->co_nfreevars;
173
0
}
174
175
0
static inline int PyUnstable_Code_GetFirstFree(PyCodeObject *op) {
176
0
    assert(PyCode_Check(op));
177
0
    return op->co_nlocalsplus - op->co_nfreevars;
178
0
}
179
180
0
static inline PyObject *PyUnstable_Code_GetLocalPlusNames(PyCodeObject *op) {
181
0
    assert(PyCode_Check(op));
182
0
    return Py_NewRef(op->co_localsplusnames);
183
0
}
184
185
0
Py_DEPRECATED(3.13) static inline int PyCode_GetFirstFree(PyCodeObject *op) {
186
0
    return PyUnstable_Code_GetFirstFree(op);
187
0
}
188
189
/* Unstable public interface */
190
PyAPI_FUNC(PyCodeObject *) PyUnstable_Code_New(
191
        int, int, int, int, int, PyObject *, PyObject *,
192
        PyObject *, PyObject *, PyObject *, PyObject *,
193
        PyObject *, PyObject *, PyObject *, int, PyObject *,
194
        PyObject *);
195
196
PyAPI_FUNC(PyCodeObject *) PyUnstable_Code_NewWithPosOnlyArgs(
197
        int, int, int, int, int, int, PyObject *, PyObject *,
198
        PyObject *, PyObject *, PyObject *, PyObject *,
199
        PyObject *, PyObject *, PyObject *, int, PyObject *,
200
        PyObject *);
201
        /* same as struct above */
202
// Old names -- remove when this API changes:
203
_Py_DEPRECATED_EXTERNALLY(3.12) static inline PyCodeObject *
204
PyCode_New(
205
        int a, int b, int c, int d, int e, PyObject *f, PyObject *g,
206
        PyObject *h, PyObject *i, PyObject *j, PyObject *k,
207
        PyObject *l, PyObject *m, PyObject *n, int o, PyObject *p,
208
        PyObject *q)
209
0
{
210
0
    return PyUnstable_Code_New(
211
0
        a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q);
212
0
}
213
_Py_DEPRECATED_EXTERNALLY(3.12) static inline PyCodeObject *
214
PyCode_NewWithPosOnlyArgs(
215
        int a, int poac, int b, int c, int d, int e, PyObject *f, PyObject *g,
216
        PyObject *h, PyObject *i, PyObject *j, PyObject *k,
217
        PyObject *l, PyObject *m, PyObject *n, int o, PyObject *p,
218
        PyObject *q)
219
0
{
220
0
    return PyUnstable_Code_NewWithPosOnlyArgs(
221
0
        a, poac, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q);
222
0
}
223
224
/* Creates a new empty code object with the specified source location. */
225
PyAPI_FUNC(PyCodeObject *)
226
PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno);
227
228
/* Return the line number associated with the specified bytecode index
229
   in this code object.  If you just need the line number of a frame,
230
   use PyFrame_GetLineNumber() instead. */
231
PyAPI_FUNC(int) PyCode_Addr2Line(PyCodeObject *, int);
232
233
PyAPI_FUNC(int) PyCode_Addr2Location(PyCodeObject *, int, int *, int *, int *, int *);
234
235
#define PY_FOREACH_CODE_EVENT(V) \
236
    V(CREATE)                 \
237
    V(DESTROY)
238
239
typedef enum {
240
    #define _Py_DEF_EVENT(op) PY_CODE_EVENT_##op,
241
    PY_FOREACH_CODE_EVENT(_Py_DEF_EVENT)
242
    #undef _Py_DEF_EVENT
243
} PyCodeEvent;
244
245
246
/*
247
 * A callback that is invoked for different events in a code object's lifecycle.
248
 *
249
 * The callback is invoked with a borrowed reference to co, after it is
250
 * created and before it is destroyed.
251
 *
252
 * If the callback sets an exception, it must return -1. Otherwise
253
 * it should return 0.
254
 */
255
typedef int (*PyCode_WatchCallback)(
256
  PyCodeEvent event,
257
  PyCodeObject* co);
258
259
/*
260
 * Register a per-interpreter callback that will be invoked for code object
261
 * lifecycle events.
262
 *
263
 * Returns a handle that may be passed to PyCode_ClearWatcher on success,
264
 * or -1 and sets an error if no more handles are available.
265
 */
266
PyAPI_FUNC(int) PyCode_AddWatcher(PyCode_WatchCallback callback);
267
268
/*
269
 * Clear the watcher associated with the watcher_id handle.
270
 *
271
 * Returns 0 on success or -1 if no watcher exists for the provided id.
272
 */
273
PyAPI_FUNC(int) PyCode_ClearWatcher(int watcher_id);
274
275
/* for internal use only */
276
struct _opaque {
277
    int computed_line;
278
    const uint8_t *lo_next;
279
    const uint8_t *limit;
280
};
281
282
typedef struct _line_offsets {
283
    int ar_start;
284
    int ar_end;
285
    int ar_line;
286
    struct _opaque opaque;
287
} PyCodeAddressRange;
288
289
/* Update *bounds to describe the first and one-past-the-last instructions in the
290
   same line as lasti.  Return the number of that line.
291
*/
292
PyAPI_FUNC(int) _PyCode_CheckLineNumber(int lasti, PyCodeAddressRange *bounds);
293
294
PyAPI_FUNC(PyObject*) PyCode_Optimize(PyObject *code, PyObject* consts,
295
                                      PyObject *names, PyObject *lnotab);
296
297
PyAPI_FUNC(int) PyUnstable_Code_GetExtra(
298
    PyObject *code, Py_ssize_t index, void **extra);
299
PyAPI_FUNC(int) PyUnstable_Code_SetExtra(
300
    PyObject *code, Py_ssize_t index, void *extra);
301
// Old names -- remove when this API changes:
302
_Py_DEPRECATED_EXTERNALLY(3.12) static inline int
303
_PyCode_GetExtra(PyObject *code, Py_ssize_t index, void **extra)
304
0
{
305
0
    return PyUnstable_Code_GetExtra(code, index, extra);
306
0
}
307
_Py_DEPRECATED_EXTERNALLY(3.12) static inline int
308
_PyCode_SetExtra(PyObject *code, Py_ssize_t index, void *extra)
309
0
{
310
0
    return PyUnstable_Code_SetExtra(code, index, extra);
311
0
}
312
313
/* Equivalent to getattr(code, 'co_code') in Python.
314
   Returns a strong reference to a bytes object. */
315
PyAPI_FUNC(PyObject *) PyCode_GetCode(PyCodeObject *code);
316
/* Equivalent to getattr(code, 'co_varnames') in Python. */
317
PyAPI_FUNC(PyObject *) PyCode_GetVarnames(PyCodeObject *code);
318
/* Equivalent to getattr(code, 'co_cellvars') in Python. */
319
PyAPI_FUNC(PyObject *) PyCode_GetCellvars(PyCodeObject *code);
320
/* Equivalent to getattr(code, 'co_freevars') in Python. */
321
PyAPI_FUNC(PyObject *) PyCode_GetFreevars(PyCodeObject *code);
322
323
typedef enum _PyCodeLocationInfoKind {
324
    /* short forms are 0 to 9 */
325
    PY_CODE_LOCATION_INFO_SHORT0 = 0,
326
    /* one lineforms are 10 to 12 */
327
    PY_CODE_LOCATION_INFO_ONE_LINE0 = 10,
328
    PY_CODE_LOCATION_INFO_ONE_LINE1 = 11,
329
    PY_CODE_LOCATION_INFO_ONE_LINE2 = 12,
330
331
    PY_CODE_LOCATION_INFO_NO_COLUMNS = 13,
332
    PY_CODE_LOCATION_INFO_LONG = 14,
333
    PY_CODE_LOCATION_INFO_NONE = 15
334
} _PyCodeLocationInfoKind;
335
336
#ifdef __cplusplus
337
}
338
#endif
339
#endif  // !Py_CODE_H
340
#endif  // !Py_LIMITED_API