summaryrefslogtreecommitdiffstats
path: root/contrib/tools/python3/src/Include/cpython/code.h
diff options
context:
space:
mode:
authorAlexSm <[email protected]>2024-02-16 11:51:30 +0100
committerGitHub <[email protected]>2024-02-16 11:51:30 +0100
commit506ecaee93b52cc12c2e2f97c3d42e3ca2a7f59e (patch)
treed096fb9eb988fbb0ca1ba970041773207ce3aa70 /contrib/tools/python3/src/Include/cpython/code.h
parent4749b9e5d260714490997e6f5ee1ee8c1c8fc46c (diff)
parentf200f72c9d7a89c1018e3dc6b46c49fe2ecf84fb (diff)
Merge pull request #1940 from dcherednik/importlib
Library import 14
Diffstat (limited to 'contrib/tools/python3/src/Include/cpython/code.h')
-rw-r--r--contrib/tools/python3/src/Include/cpython/code.h213
1 files changed, 183 insertions, 30 deletions
diff --git a/contrib/tools/python3/src/Include/cpython/code.h b/contrib/tools/python3/src/Include/cpython/code.h
index 7006060cc76..03834b20c3e 100644
--- a/contrib/tools/python3/src/Include/cpython/code.h
+++ b/contrib/tools/python3/src/Include/cpython/code.h
@@ -3,10 +3,29 @@
#ifndef Py_LIMITED_API
#ifndef Py_CODE_H
#define Py_CODE_H
+
#ifdef __cplusplus
extern "C" {
#endif
+/* Count of all local monitoring events */
+#define _PY_MONITORING_LOCAL_EVENTS 10
+/* Count of all "real" monitoring events (not derived from other events) */
+#define _PY_MONITORING_UNGROUPED_EVENTS 15
+/* Count of all monitoring events */
+#define _PY_MONITORING_EVENTS 17
+
+/* Tables of which tools are active for each monitored event. */
+/* For 3.12 ABI compatibility this is over sized */
+typedef struct _Py_LocalMonitors {
+ /* Only _PY_MONITORING_LOCAL_EVENTS of these are used */
+ uint8_t tools[_PY_MONITORING_UNGROUPED_EVENTS];
+} _Py_LocalMonitors;
+
+typedef struct _Py_GlobalMonitors {
+ uint8_t tools[_PY_MONITORING_UNGROUPED_EVENTS];
+} _Py_GlobalMonitors;
+
/* Each instruction in a code object is a fixed-width value,
* currently 2 bytes: 1-byte opcode + 1-byte oparg. The EXTENDED_ARG
* opcode allows for larger values but the current limit is 3 uses
@@ -16,20 +35,74 @@ extern "C" {
* 2**32 - 1, rather than INT_MAX.
*/
-typedef uint16_t _Py_CODEUNIT;
+typedef union {
+ uint16_t cache;
+ struct {
+ uint8_t code;
+ uint8_t arg;
+ } op;
+} _Py_CODEUNIT;
-#ifdef WORDS_BIGENDIAN
-# define _Py_OPCODE(word) ((word) >> 8)
-# define _Py_OPARG(word) ((word) & 255)
-# define _Py_MAKECODEUNIT(opcode, oparg) (((opcode)<<8)|(oparg))
-#else
-# define _Py_OPCODE(word) ((word) & 255)
-# define _Py_OPARG(word) ((word) >> 8)
-# define _Py_MAKECODEUNIT(opcode, oparg) ((opcode)|((oparg)<<8))
-#endif
-// Use "unsigned char" instead of "uint8_t" here to avoid illegal aliasing:
-#define _Py_SET_OPCODE(word, opcode) (((unsigned char *)&(word))[0] = (opcode))
+/* These macros only remain defined for compatibility. */
+#define _Py_OPCODE(word) ((word).op.code)
+#define _Py_OPARG(word) ((word).op.arg)
+
+static inline _Py_CODEUNIT
+_py_make_codeunit(uint8_t opcode, uint8_t oparg)
+{
+ // No designated initialisers because of C++ compat
+ _Py_CODEUNIT word;
+ word.op.code = opcode;
+ word.op.arg = oparg;
+ return word;
+}
+
+static inline void
+_py_set_opcode(_Py_CODEUNIT *word, uint8_t opcode)
+{
+ word->op.code = opcode;
+}
+
+#define _Py_MAKE_CODEUNIT(opcode, oparg) _py_make_codeunit((opcode), (oparg))
+#define _Py_SET_OPCODE(word, opcode) _py_set_opcode(&(word), (opcode))
+
+
+typedef struct {
+ PyObject *_co_code;
+ PyObject *_co_varnames;
+ PyObject *_co_cellvars;
+ PyObject *_co_freevars;
+} _PyCoCached;
+
+/* Ancilliary data structure used for instrumentation.
+ Line instrumentation creates an array of
+ these. One entry per code unit.*/
+typedef struct {
+ uint8_t original_opcode;
+ int8_t line_delta;
+} _PyCoLineInstrumentationData;
+
+/* Main data structure used for instrumentation.
+ * This is allocated when needed for instrumentation
+ */
+typedef struct {
+ /* Monitoring specific to this code object */
+ _Py_LocalMonitors local_monitors;
+ /* Monitoring that is active on this code object */
+ _Py_LocalMonitors active_monitors;
+ /* The tools that are to be notified for events for the matching code unit */
+ uint8_t *tools;
+ /* Information to support line events */
+ _PyCoLineInstrumentationData *lines;
+ /* The tools that are to be notified for line events for the matching code unit */
+ uint8_t *line_tools;
+ /* Information to support instruction events */
+ /* The underlying instructions, which can themselves be instrumented */
+ uint8_t *per_instruction_opcodes;
+ /* The tools that are to be notified for instruction events for the matching code unit */
+ uint8_t *per_instruction_tools;
+} _PyCoMonitoringData;
// To avoid repeating ourselves in deepfreeze.py, all PyCodeObject members are
// defined in this macro:
@@ -62,8 +135,6 @@ typedef uint16_t _Py_CODEUNIT;
PyObject *co_exceptiontable; /* Byte string encoding exception handling \
table */ \
int co_flags; /* CO_..., see below */ \
- short co_warmup; /* Warmup counter for quickening */ \
- short _co_linearray_entry_size; /* Size of each entry in _co_linearray */ \
\
/* The rest are not so impactful on performance. */ \
int co_argcount; /* #arguments, except *args */ \
@@ -74,12 +145,12 @@ typedef uint16_t _Py_CODEUNIT;
\
/* redundant values (derived from co_localsplusnames and \
co_localspluskinds) */ \
- int co_nlocalsplus; /* number of local + cell + free variables \
- */ \
+ int co_nlocalsplus; /* number of local + cell + free variables */ \
+ int co_framesize; /* Size of frame in words */ \
int co_nlocals; /* number of local variables */ \
- int co_nplaincellvars; /* number of non-arg cell variables */ \
int co_ncellvars; /* total number of cell variables */ \
int co_nfreevars; /* number of free variables */ \
+ uint32_t co_version; /* version number */ \
\
PyObject *co_localsplusnames; /* tuple mapping offsets to names */ \
PyObject *co_localspluskinds; /* Bytes mapping to local kinds (one byte \
@@ -89,8 +160,9 @@ typedef uint16_t _Py_CODEUNIT;
PyObject *co_qualname; /* unicode (qualname, for reference) */ \
PyObject *co_linetable; /* bytes object that holds location info */ \
PyObject *co_weakreflist; /* to support weakrefs to code objects */ \
- PyObject *_co_code; /* cached co_code object/attribute */ \
- char *_co_linearray; /* array of line offsets */ \
+ _PyCoCached *_co_cached; /* cached co_* attributes */ \
+ uint64_t _co_instrumentation_version; /* current instrumentation version */ \
+ _PyCoMonitoringData *_co_monitoring; /* Monitoring data */ \
int _co_firsttraceable; /* index of first traceable instruction */ \
/* Scratch space for extra data relating to the code object. \
Type is a void* to keep the format private in codeobject.c to force \
@@ -139,24 +211,55 @@ struct PyCodeObject _PyCode_DEF(1);
PyAPI_DATA(PyTypeObject) PyCode_Type;
-#define PyCode_Check(op) Py_IS_TYPE(op, &PyCode_Type)
-#define PyCode_GetNumFree(op) ((op)->co_nfreevars)
-#define _PyCode_CODE(CO) ((_Py_CODEUNIT *)(CO)->co_code_adaptive)
+#define PyCode_Check(op) Py_IS_TYPE((op), &PyCode_Type)
+
+static inline Py_ssize_t PyCode_GetNumFree(PyCodeObject *op) {
+ assert(PyCode_Check(op));
+ return op->co_nfreevars;
+}
+
+static inline int PyCode_GetFirstFree(PyCodeObject *op) {
+ assert(PyCode_Check(op));
+ return op->co_nlocalsplus - op->co_nfreevars;
+}
+
+#define _PyCode_CODE(CO) _Py_RVALUE((_Py_CODEUNIT *)(CO)->co_code_adaptive)
#define _PyCode_NBYTES(CO) (Py_SIZE(CO) * (Py_ssize_t)sizeof(_Py_CODEUNIT))
-/* Public interface */
-PyAPI_FUNC(PyCodeObject *) PyCode_New(
+/* Unstable public interface */
+PyAPI_FUNC(PyCodeObject *) PyUnstable_Code_New(
int, int, int, int, int, PyObject *, PyObject *,
PyObject *, PyObject *, PyObject *, PyObject *,
PyObject *, PyObject *, PyObject *, int, PyObject *,
PyObject *);
-PyAPI_FUNC(PyCodeObject *) PyCode_NewWithPosOnlyArgs(
+PyAPI_FUNC(PyCodeObject *) PyUnstable_Code_NewWithPosOnlyArgs(
int, int, int, int, int, int, PyObject *, PyObject *,
PyObject *, PyObject *, PyObject *, PyObject *,
PyObject *, PyObject *, PyObject *, int, PyObject *,
PyObject *);
/* same as struct above */
+// Old names -- remove when this API changes:
+_Py_DEPRECATED_EXTERNALLY(3.12) static inline PyCodeObject *
+PyCode_New(
+ int a, int b, int c, int d, int e, PyObject *f, PyObject *g,
+ PyObject *h, PyObject *i, PyObject *j, PyObject *k,
+ PyObject *l, PyObject *m, PyObject *n, int o, PyObject *p,
+ PyObject *q)
+{
+ return PyUnstable_Code_New(
+ a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q);
+}
+_Py_DEPRECATED_EXTERNALLY(3.12) static inline PyCodeObject *
+PyCode_NewWithPosOnlyArgs(
+ int a, int poac, int b, int c, int d, int e, PyObject *f, PyObject *g,
+ PyObject *h, PyObject *i, PyObject *j, PyObject *k,
+ PyObject *l, PyObject *m, PyObject *n, int o, PyObject *p,
+ PyObject *q)
+{
+ return PyUnstable_Code_NewWithPosOnlyArgs(
+ a, poac, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q);
+}
/* Creates a new empty code object with the specified source location. */
PyAPI_FUNC(PyCodeObject *)
@@ -169,6 +272,46 @@ PyAPI_FUNC(int) PyCode_Addr2Line(PyCodeObject *, int);
PyAPI_FUNC(int) PyCode_Addr2Location(PyCodeObject *, int, int *, int *, int *, int *);
+#define PY_FOREACH_CODE_EVENT(V) \
+ V(CREATE) \
+ V(DESTROY)
+
+typedef enum {
+ #define PY_DEF_EVENT(op) PY_CODE_EVENT_##op,
+ PY_FOREACH_CODE_EVENT(PY_DEF_EVENT)
+ #undef PY_DEF_EVENT
+} PyCodeEvent;
+
+
+/*
+ * A callback that is invoked for different events in a code object's lifecycle.
+ *
+ * The callback is invoked with a borrowed reference to co, after it is
+ * created and before it is destroyed.
+ *
+ * If the callback sets an exception, it must return -1. Otherwise
+ * it should return 0.
+ */
+typedef int (*PyCode_WatchCallback)(
+ PyCodeEvent event,
+ PyCodeObject* co);
+
+/*
+ * Register a per-interpreter callback that will be invoked for code object
+ * lifecycle events.
+ *
+ * Returns a handle that may be passed to PyCode_ClearWatcher on success,
+ * or -1 and sets an error if no more handles are available.
+ */
+PyAPI_FUNC(int) PyCode_AddWatcher(PyCode_WatchCallback callback);
+
+/*
+ * Clear the watcher associated with the watcher_id handle.
+ *
+ * Returns 0 on success or -1 if no watcher exists for the provided id.
+ */
+PyAPI_FUNC(int) PyCode_ClearWatcher(int watcher_id);
+
/* for internal use only */
struct _opaque {
int computed_line;
@@ -200,11 +343,21 @@ PyAPI_FUNC(PyObject*) _PyCode_ConstantKey(PyObject *obj);
PyAPI_FUNC(PyObject*) PyCode_Optimize(PyObject *code, PyObject* consts,
PyObject *names, PyObject *lnotab);
-
-PyAPI_FUNC(int) _PyCode_GetExtra(PyObject *code, Py_ssize_t index,
- void **extra);
-PyAPI_FUNC(int) _PyCode_SetExtra(PyObject *code, Py_ssize_t index,
- void *extra);
+PyAPI_FUNC(int) PyUnstable_Code_GetExtra(
+ PyObject *code, Py_ssize_t index, void **extra);
+PyAPI_FUNC(int) PyUnstable_Code_SetExtra(
+ PyObject *code, Py_ssize_t index, void *extra);
+// Old names -- remove when this API changes:
+_Py_DEPRECATED_EXTERNALLY(3.12) static inline int
+_PyCode_GetExtra(PyObject *code, Py_ssize_t index, void **extra)
+{
+ return PyUnstable_Code_GetExtra(code, index, extra);
+}
+_Py_DEPRECATED_EXTERNALLY(3.12) static inline int
+_PyCode_SetExtra(PyObject *code, Py_ssize_t index, void *extra)
+{
+ return PyUnstable_Code_SetExtra(code, index, extra);
+}
/* Equivalent to getattr(code, 'co_code') in Python.
Returns a strong reference to a bytes object. */