File indexing completed on 2025-01-30 10:18:03
0001
0002
0003 #ifndef Py_LIMITED_API
0004 #ifndef Py_CODE_H
0005 #define Py_CODE_H
0006
0007 #ifdef __cplusplus
0008 extern "C" {
0009 #endif
0010
0011
0012 #define _PY_MONITORING_LOCAL_EVENTS 10
0013
0014 #define _PY_MONITORING_UNGROUPED_EVENTS 15
0015
0016 #define _PY_MONITORING_EVENTS 17
0017
0018
0019
0020 typedef struct _Py_LocalMonitors {
0021
0022 uint8_t tools[_PY_MONITORING_UNGROUPED_EVENTS];
0023 } _Py_LocalMonitors;
0024
0025 typedef struct _Py_GlobalMonitors {
0026 uint8_t tools[_PY_MONITORING_UNGROUPED_EVENTS];
0027 } _Py_GlobalMonitors;
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038 typedef union {
0039 uint16_t cache;
0040 struct {
0041 uint8_t code;
0042 uint8_t arg;
0043 } op;
0044 } _Py_CODEUNIT;
0045
0046
0047
0048 #define _Py_OPCODE(word) ((word).op.code)
0049 #define _Py_OPARG(word) ((word).op.arg)
0050
0051 static inline _Py_CODEUNIT
0052 _py_make_codeunit(uint8_t opcode, uint8_t oparg)
0053 {
0054
0055 _Py_CODEUNIT word;
0056 word.op.code = opcode;
0057 word.op.arg = oparg;
0058 return word;
0059 }
0060
0061 static inline void
0062 _py_set_opcode(_Py_CODEUNIT *word, uint8_t opcode)
0063 {
0064 word->op.code = opcode;
0065 }
0066
0067 #define _Py_MAKE_CODEUNIT(opcode, oparg) _py_make_codeunit((opcode), (oparg))
0068 #define _Py_SET_OPCODE(word, opcode) _py_set_opcode(&(word), (opcode))
0069
0070
0071 typedef struct {
0072 PyObject *_co_code;
0073 PyObject *_co_varnames;
0074 PyObject *_co_cellvars;
0075 PyObject *_co_freevars;
0076 } _PyCoCached;
0077
0078
0079
0080
0081 typedef struct {
0082 uint8_t original_opcode;
0083 int8_t line_delta;
0084 } _PyCoLineInstrumentationData;
0085
0086
0087
0088
0089 typedef struct {
0090
0091 _Py_LocalMonitors local_monitors;
0092
0093 _Py_LocalMonitors active_monitors;
0094
0095 uint8_t *tools;
0096
0097 _PyCoLineInstrumentationData *lines;
0098
0099 uint8_t *line_tools;
0100
0101
0102 uint8_t *per_instruction_opcodes;
0103
0104 uint8_t *per_instruction_tools;
0105 } _PyCoMonitoringData;
0106
0107
0108
0109 #define _PyCode_DEF(SIZE) { \
0110 PyObject_VAR_HEAD \
0111 \
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128 \
0129 \
0130 \
0131 \
0132 \
0133 PyObject *co_consts; \
0134 PyObject *co_names; \
0135 PyObject *co_exceptiontable;
0136 \
0137 int co_flags; \
0138 \
0139 \
0140 int co_argcount; \
0141 int co_posonlyargcount; \
0142 int co_kwonlyargcount; \
0143 int co_stacksize; \
0144 int co_firstlineno; \
0145 \
0146
0147 \
0148 int co_nlocalsplus; \
0149 int co_framesize; \
0150 int co_nlocals; \
0151 int co_ncellvars; \
0152 int co_nfreevars; \
0153 uint32_t co_version; \
0154 \
0155 PyObject *co_localsplusnames; \
0156 PyObject *co_localspluskinds;
0157 \
0158 PyObject *co_filename; \
0159 PyObject *co_name; \
0160 PyObject *co_qualname; \
0161 PyObject *co_linetable; \
0162 PyObject *co_weakreflist; \
0163 _PyCoCached *_co_cached; \
0164 uint64_t _co_instrumentation_version; \
0165 _PyCoMonitoringData *_co_monitoring; \
0166 int _co_firsttraceable; \
0167
0168
0169 \
0170 void *co_extra; \
0171 char co_code_adaptive[(SIZE)]; \
0172 }
0173
0174
0175 struct PyCodeObject _PyCode_DEF(1);
0176
0177
0178 #define CO_OPTIMIZED 0x0001
0179 #define CO_NEWLOCALS 0x0002
0180 #define CO_VARARGS 0x0004
0181 #define CO_VARKEYWORDS 0x0008
0182 #define CO_NESTED 0x0010
0183 #define CO_GENERATOR 0x0020
0184
0185
0186
0187 #define CO_COROUTINE 0x0080
0188 #define CO_ITERABLE_COROUTINE 0x0100
0189 #define CO_ASYNC_GENERATOR 0x0200
0190
0191
0192
0193
0194
0195 #define CO_FUTURE_DIVISION 0x20000
0196 #define CO_FUTURE_ABSOLUTE_IMPORT 0x40000
0197 #define CO_FUTURE_WITH_STATEMENT 0x80000
0198 #define CO_FUTURE_PRINT_FUNCTION 0x100000
0199 #define CO_FUTURE_UNICODE_LITERALS 0x200000
0200
0201 #define CO_FUTURE_BARRY_AS_BDFL 0x400000
0202 #define CO_FUTURE_GENERATOR_STOP 0x800000
0203 #define CO_FUTURE_ANNOTATIONS 0x1000000
0204
0205
0206
0207
0208 #define PY_PARSER_REQUIRES_FUTURE_KEYWORD
0209
0210 #define CO_MAXBLOCKS 21
0211
0212 PyAPI_DATA(PyTypeObject) PyCode_Type;
0213
0214 #define PyCode_Check(op) Py_IS_TYPE((op), &PyCode_Type)
0215
0216 static inline Py_ssize_t PyCode_GetNumFree(PyCodeObject *op) {
0217 assert(PyCode_Check(op));
0218 return op->co_nfreevars;
0219 }
0220
0221 static inline int PyCode_GetFirstFree(PyCodeObject *op) {
0222 assert(PyCode_Check(op));
0223 return op->co_nlocalsplus - op->co_nfreevars;
0224 }
0225
0226 #define _PyCode_CODE(CO) _Py_RVALUE((_Py_CODEUNIT *)(CO)->co_code_adaptive)
0227 #define _PyCode_NBYTES(CO) (Py_SIZE(CO) * (Py_ssize_t)sizeof(_Py_CODEUNIT))
0228
0229
0230 PyAPI_FUNC(PyCodeObject *) PyUnstable_Code_New(
0231 int, int, int, int, int, PyObject *, PyObject *,
0232 PyObject *, PyObject *, PyObject *, PyObject *,
0233 PyObject *, PyObject *, PyObject *, int, PyObject *,
0234 PyObject *);
0235
0236 PyAPI_FUNC(PyCodeObject *) PyUnstable_Code_NewWithPosOnlyArgs(
0237 int, int, int, int, int, int, PyObject *, PyObject *,
0238 PyObject *, PyObject *, PyObject *, PyObject *,
0239 PyObject *, PyObject *, PyObject *, int, PyObject *,
0240 PyObject *);
0241
0242
0243 _Py_DEPRECATED_EXTERNALLY(3.12) static inline PyCodeObject *
0244 PyCode_New(
0245 int a, int b, int c, int d, int e, PyObject *f, PyObject *g,
0246 PyObject *h, PyObject *i, PyObject *j, PyObject *k,
0247 PyObject *l, PyObject *m, PyObject *n, int o, PyObject *p,
0248 PyObject *q)
0249 {
0250 return PyUnstable_Code_New(
0251 a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q);
0252 }
0253 _Py_DEPRECATED_EXTERNALLY(3.12) static inline PyCodeObject *
0254 PyCode_NewWithPosOnlyArgs(
0255 int a, int poac, int b, int c, int d, int e, PyObject *f, PyObject *g,
0256 PyObject *h, PyObject *i, PyObject *j, PyObject *k,
0257 PyObject *l, PyObject *m, PyObject *n, int o, PyObject *p,
0258 PyObject *q)
0259 {
0260 return PyUnstable_Code_NewWithPosOnlyArgs(
0261 a, poac, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q);
0262 }
0263
0264
0265 PyAPI_FUNC(PyCodeObject *)
0266 PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno);
0267
0268
0269
0270
0271 PyAPI_FUNC(int) PyCode_Addr2Line(PyCodeObject *, int);
0272
0273 PyAPI_FUNC(int) PyCode_Addr2Location(PyCodeObject *, int, int *, int *, int *, int *);
0274
0275 #define PY_FOREACH_CODE_EVENT(V) \
0276 V(CREATE) \
0277 V(DESTROY)
0278
0279 typedef enum {
0280 #define PY_DEF_EVENT(op) PY_CODE_EVENT_##op,
0281 PY_FOREACH_CODE_EVENT(PY_DEF_EVENT)
0282 #undef PY_DEF_EVENT
0283 } PyCodeEvent;
0284
0285
0286
0287
0288
0289
0290
0291
0292
0293
0294
0295 typedef int (*PyCode_WatchCallback)(
0296 PyCodeEvent event,
0297 PyCodeObject* co);
0298
0299
0300
0301
0302
0303
0304
0305
0306 PyAPI_FUNC(int) PyCode_AddWatcher(PyCode_WatchCallback callback);
0307
0308
0309
0310
0311
0312
0313 PyAPI_FUNC(int) PyCode_ClearWatcher(int watcher_id);
0314
0315
0316 struct _opaque {
0317 int computed_line;
0318 const uint8_t *lo_next;
0319 const uint8_t *limit;
0320 };
0321
0322 typedef struct _line_offsets {
0323 int ar_start;
0324 int ar_end;
0325 int ar_line;
0326 struct _opaque opaque;
0327 } PyCodeAddressRange;
0328
0329
0330
0331
0332 PyAPI_FUNC(int) _PyCode_CheckLineNumber(int lasti, PyCodeAddressRange *bounds);
0333
0334
0335
0336
0337
0338
0339
0340
0341 PyAPI_FUNC(PyObject*) _PyCode_ConstantKey(PyObject *obj);
0342
0343 PyAPI_FUNC(PyObject*) PyCode_Optimize(PyObject *code, PyObject* consts,
0344 PyObject *names, PyObject *lnotab);
0345
0346 PyAPI_FUNC(int) PyUnstable_Code_GetExtra(
0347 PyObject *code, Py_ssize_t index, void **extra);
0348 PyAPI_FUNC(int) PyUnstable_Code_SetExtra(
0349 PyObject *code, Py_ssize_t index, void *extra);
0350
0351 _Py_DEPRECATED_EXTERNALLY(3.12) static inline int
0352 _PyCode_GetExtra(PyObject *code, Py_ssize_t index, void **extra)
0353 {
0354 return PyUnstable_Code_GetExtra(code, index, extra);
0355 }
0356 _Py_DEPRECATED_EXTERNALLY(3.12) static inline int
0357 _PyCode_SetExtra(PyObject *code, Py_ssize_t index, void *extra)
0358 {
0359 return PyUnstable_Code_SetExtra(code, index, extra);
0360 }
0361
0362
0363
0364 PyAPI_FUNC(PyObject *) PyCode_GetCode(PyCodeObject *code);
0365
0366 PyAPI_FUNC(PyObject *) PyCode_GetVarnames(PyCodeObject *code);
0367
0368 PyAPI_FUNC(PyObject *) PyCode_GetCellvars(PyCodeObject *code);
0369
0370 PyAPI_FUNC(PyObject *) PyCode_GetFreevars(PyCodeObject *code);
0371
0372 typedef enum _PyCodeLocationInfoKind {
0373
0374 PY_CODE_LOCATION_INFO_SHORT0 = 0,
0375
0376 PY_CODE_LOCATION_INFO_ONE_LINE0 = 10,
0377 PY_CODE_LOCATION_INFO_ONE_LINE1 = 11,
0378 PY_CODE_LOCATION_INFO_ONE_LINE2 = 12,
0379
0380 PY_CODE_LOCATION_INFO_NO_COLUMNS = 13,
0381 PY_CODE_LOCATION_INFO_LONG = 14,
0382 PY_CODE_LOCATION_INFO_NONE = 15
0383 } _PyCodeLocationInfoKind;
0384
0385 #ifdef __cplusplus
0386 }
0387 #endif
0388 #endif
0389 #endif