Victor Stinner | f2a9d5c | 2018-11-27 00:20:00 +0100 | [diff] [blame] | 1 | #ifndef Py_CPYTHON_PYSTATE_H |
| 2 | # error "this header file must not be included directly" |
| 3 | #endif |
| 4 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 5 | #include "cpython/initconfig.h" |
Victor Stinner | f684d83 | 2019-03-01 03:44:13 +0100 | [diff] [blame] | 6 | |
Eric Snow | c11183c | 2019-03-15 16:35:46 -0600 | [diff] [blame] | 7 | PyAPI_FUNC(int) _PyInterpreterState_RequiresIDRef(PyInterpreterState *); |
| 8 | PyAPI_FUNC(void) _PyInterpreterState_RequireIDRef(PyInterpreterState *, int); |
| 9 | |
Eric Snow | c11183c | 2019-03-15 16:35:46 -0600 | [diff] [blame] | 10 | PyAPI_FUNC(PyObject *) _PyInterpreterState_GetMainModule(PyInterpreterState *); |
Victor Stinner | f2a9d5c | 2018-11-27 00:20:00 +0100 | [diff] [blame] | 11 | |
| 12 | /* State unique per thread */ |
| 13 | |
| 14 | /* Py_tracefunc return -1 when raising an exception, or 0 for success. */ |
Victor Stinner | 7c59d7c | 2020-04-28 16:32:48 +0200 | [diff] [blame] | 15 | typedef int (*Py_tracefunc)(PyObject *, PyFrameObject *, int, PyObject *); |
Victor Stinner | f2a9d5c | 2018-11-27 00:20:00 +0100 | [diff] [blame] | 16 | |
| 17 | /* The following values are used for 'what' for tracefunc functions |
| 18 | * |
| 19 | * To add a new kind of trace event, also update "trace_init" in |
| 20 | * Python/sysmodule.c to define the Python level event name |
| 21 | */ |
| 22 | #define PyTrace_CALL 0 |
| 23 | #define PyTrace_EXCEPTION 1 |
| 24 | #define PyTrace_LINE 2 |
| 25 | #define PyTrace_RETURN 3 |
| 26 | #define PyTrace_C_CALL 4 |
| 27 | #define PyTrace_C_EXCEPTION 5 |
| 28 | #define PyTrace_C_RETURN 6 |
| 29 | #define PyTrace_OPCODE 7 |
| 30 | |
| 31 | |
Mark Shannon | 9e7b207 | 2021-04-13 11:08:14 +0100 | [diff] [blame^] | 32 | typedef struct _cframe { |
| 33 | /* This struct will be threaded through the C stack |
| 34 | * allowing fast access to per-thread state that needs |
| 35 | * to be accessed quickly by the interpreter, but can |
| 36 | * be modified outside of the interpreter. |
| 37 | * |
| 38 | * WARNING: This makes data on the C stack accessible from |
| 39 | * heap objects. Care must be taken to maintain stack |
| 40 | * discipline and make sure that instances of this struct cannot |
| 41 | * accessed outside of their lifetime. |
| 42 | */ |
| 43 | int use_tracing; |
| 44 | struct _cframe *previous; |
| 45 | } CFrame; |
| 46 | |
Victor Stinner | f2a9d5c | 2018-11-27 00:20:00 +0100 | [diff] [blame] | 47 | typedef struct _err_stackitem { |
| 48 | /* This struct represents an entry on the exception stack, which is a |
| 49 | * per-coroutine state. (Coroutine in the computer science sense, |
| 50 | * including the thread and generators). |
| 51 | * This ensures that the exception state is not impacted by "yields" |
| 52 | * from an except handler. |
| 53 | */ |
| 54 | PyObject *exc_type, *exc_value, *exc_traceback; |
| 55 | |
| 56 | struct _err_stackitem *previous_item; |
| 57 | |
| 58 | } _PyErr_StackItem; |
| 59 | |
| 60 | |
Eric Snow | be3b295 | 2019-02-23 11:35:52 -0700 | [diff] [blame] | 61 | // The PyThreadState typedef is in Include/pystate.h. |
| 62 | struct _ts { |
Victor Stinner | f2a9d5c | 2018-11-27 00:20:00 +0100 | [diff] [blame] | 63 | /* See Python/ceval.c for comments explaining most fields */ |
| 64 | |
| 65 | struct _ts *prev; |
| 66 | struct _ts *next; |
| 67 | PyInterpreterState *interp; |
| 68 | |
Victor Stinner | 5804f87 | 2020-03-24 16:32:26 +0100 | [diff] [blame] | 69 | /* Borrowed reference to the current frame (it can be NULL) */ |
Victor Stinner | 7c59d7c | 2020-04-28 16:32:48 +0200 | [diff] [blame] | 70 | PyFrameObject *frame; |
Victor Stinner | f2a9d5c | 2018-11-27 00:20:00 +0100 | [diff] [blame] | 71 | int recursion_depth; |
Mark Shannon | 4e7a69b | 2020-12-02 13:30:55 +0000 | [diff] [blame] | 72 | int recursion_headroom; /* Allow 50 more calls to handle any errors. */ |
Victor Stinner | f2a9d5c | 2018-11-27 00:20:00 +0100 | [diff] [blame] | 73 | int stackcheck_counter; |
| 74 | |
| 75 | /* 'tracing' keeps track of the execution depth when tracing/profiling. |
| 76 | This is to prevent the actual trace/profile code from being recorded in |
| 77 | the trace/profile. */ |
| 78 | int tracing; |
Mark Shannon | 9e7b207 | 2021-04-13 11:08:14 +0100 | [diff] [blame^] | 79 | |
| 80 | /* Pointer to current CFrame in the C stack frame of the currently, |
| 81 | * or most recently, executing _PyEval_EvalFrameDefault. */ |
| 82 | CFrame *cframe; |
Victor Stinner | f2a9d5c | 2018-11-27 00:20:00 +0100 | [diff] [blame] | 83 | |
| 84 | Py_tracefunc c_profilefunc; |
| 85 | Py_tracefunc c_tracefunc; |
| 86 | PyObject *c_profileobj; |
| 87 | PyObject *c_traceobj; |
| 88 | |
| 89 | /* The exception currently being raised */ |
| 90 | PyObject *curexc_type; |
| 91 | PyObject *curexc_value; |
| 92 | PyObject *curexc_traceback; |
| 93 | |
| 94 | /* The exception currently being handled, if no coroutines/generators |
| 95 | * are present. Always last element on the stack referred to be exc_info. |
| 96 | */ |
| 97 | _PyErr_StackItem exc_state; |
| 98 | |
| 99 | /* Pointer to the top of the stack of the exceptions currently |
| 100 | * being handled */ |
| 101 | _PyErr_StackItem *exc_info; |
| 102 | |
| 103 | PyObject *dict; /* Stores per-thread state */ |
| 104 | |
| 105 | int gilstate_counter; |
| 106 | |
| 107 | PyObject *async_exc; /* Asynchronous exception to raise */ |
| 108 | unsigned long thread_id; /* Thread id where this tstate was created */ |
| 109 | |
| 110 | int trash_delete_nesting; |
| 111 | PyObject *trash_delete_later; |
| 112 | |
| 113 | /* Called when a thread state is deleted normally, but not when it |
| 114 | * is destroyed after fork(). |
| 115 | * Pain: to prevent rare but fatal shutdown errors (issue 18808), |
| 116 | * Thread.join() must wait for the join'ed thread's tstate to be unlinked |
| 117 | * from the tstate chain. That happens at the end of a thread's life, |
| 118 | * in pystate.c. |
| 119 | * The obvious way doesn't quite work: create a lock which the tstate |
| 120 | * unlinking code releases, and have Thread.join() wait to acquire that |
| 121 | * lock. The problem is that we _are_ at the end of the thread's life: |
| 122 | * if the thread holds the last reference to the lock, decref'ing the |
| 123 | * lock will delete the lock, and that may trigger arbitrary Python code |
| 124 | * if there's a weakref, with a callback, to the lock. But by this time |
Victor Stinner | 0fd2c30 | 2019-06-04 03:15:09 +0200 | [diff] [blame] | 125 | * _PyRuntime.gilstate.tstate_current is already NULL, so only the simplest |
| 126 | * of C code can be allowed to run (in particular it must not be possible to |
| 127 | * release the GIL). |
Victor Stinner | f2a9d5c | 2018-11-27 00:20:00 +0100 | [diff] [blame] | 128 | * So instead of holding the lock directly, the tstate holds a weakref to |
| 129 | * the lock: that's the value of on_delete_data below. Decref'ing a |
| 130 | * weakref is harmless. |
| 131 | * on_delete points to _threadmodule.c's static release_sentinel() function. |
| 132 | * After the tstate is unlinked, release_sentinel is called with the |
| 133 | * weakref-to-lock (on_delete_data) argument, and release_sentinel releases |
| 134 | * the indirectly held lock. |
| 135 | */ |
| 136 | void (*on_delete)(void *); |
| 137 | void *on_delete_data; |
| 138 | |
| 139 | int coroutine_origin_tracking_depth; |
| 140 | |
Victor Stinner | f2a9d5c | 2018-11-27 00:20:00 +0100 | [diff] [blame] | 141 | PyObject *async_gen_firstiter; |
| 142 | PyObject *async_gen_finalizer; |
| 143 | |
| 144 | PyObject *context; |
| 145 | uint64_t context_ver; |
| 146 | |
| 147 | /* Unique thread state id. */ |
| 148 | uint64_t id; |
| 149 | |
Mark Shannon | 9e7b207 | 2021-04-13 11:08:14 +0100 | [diff] [blame^] | 150 | CFrame root_cframe; |
| 151 | |
Victor Stinner | f2a9d5c | 2018-11-27 00:20:00 +0100 | [diff] [blame] | 152 | /* XXX signal handlers should also be here */ |
| 153 | |
Eric Snow | be3b295 | 2019-02-23 11:35:52 -0700 | [diff] [blame] | 154 | }; |
Victor Stinner | f2a9d5c | 2018-11-27 00:20:00 +0100 | [diff] [blame] | 155 | |
Victor Stinner | be79373 | 2020-03-13 18:15:33 +0100 | [diff] [blame] | 156 | // Alias for backward compatibility with Python 3.8 |
| 157 | #define _PyInterpreterState_Get PyInterpreterState_Get |
Victor Stinner | f2a9d5c | 2018-11-27 00:20:00 +0100 | [diff] [blame] | 158 | |
Victor Stinner | f2a9d5c | 2018-11-27 00:20:00 +0100 | [diff] [blame] | 159 | PyAPI_FUNC(PyThreadState *) _PyThreadState_Prealloc(PyInterpreterState *); |
Victor Stinner | f2a9d5c | 2018-11-27 00:20:00 +0100 | [diff] [blame] | 160 | |
| 161 | /* Similar to PyThreadState_Get(), but don't issue a fatal error |
| 162 | * if it is NULL. */ |
| 163 | PyAPI_FUNC(PyThreadState *) _PyThreadState_UncheckedGet(void); |
| 164 | |
Victor Stinner | 0e427c6 | 2020-03-25 21:22:55 +0100 | [diff] [blame] | 165 | PyAPI_FUNC(PyObject *) _PyThreadState_GetDict(PyThreadState *tstate); |
| 166 | |
Victor Stinner | f2a9d5c | 2018-11-27 00:20:00 +0100 | [diff] [blame] | 167 | /* PyGILState */ |
| 168 | |
| 169 | /* Helper/diagnostic function - return 1 if the current thread |
| 170 | currently holds the GIL, 0 otherwise. |
| 171 | |
| 172 | The function returns 1 if _PyGILState_check_enabled is non-zero. */ |
| 173 | PyAPI_FUNC(int) PyGILState_Check(void); |
| 174 | |
| 175 | /* Get the single PyInterpreterState used by this process' GILState |
| 176 | implementation. |
| 177 | |
| 178 | This function doesn't check for error. Return NULL before _PyGILState_Init() |
| 179 | is called and after _PyGILState_Fini() is called. |
| 180 | |
Victor Stinner | 81a7be3 | 2020-04-14 15:14:01 +0200 | [diff] [blame] | 181 | See also _PyInterpreterState_Get() and _PyInterpreterState_GET(). */ |
Victor Stinner | f2a9d5c | 2018-11-27 00:20:00 +0100 | [diff] [blame] | 182 | PyAPI_FUNC(PyInterpreterState *) _PyGILState_GetInterpreterStateUnsafe(void); |
| 183 | |
| 184 | /* The implementation of sys._current_frames() Returns a dict mapping |
| 185 | thread id to that thread's current frame. |
| 186 | */ |
| 187 | PyAPI_FUNC(PyObject *) _PyThread_CurrentFrames(void); |
| 188 | |
Julien Danjou | 64366fa | 2020-11-02 15:16:25 +0100 | [diff] [blame] | 189 | /* The implementation of sys._current_exceptions() Returns a dict mapping |
| 190 | thread id to that thread's current exception. |
| 191 | */ |
| 192 | PyAPI_FUNC(PyObject *) _PyThread_CurrentExceptions(void); |
| 193 | |
Victor Stinner | f2a9d5c | 2018-11-27 00:20:00 +0100 | [diff] [blame] | 194 | /* Routines for advanced debuggers, requested by David Beazley. |
| 195 | Don't use unless you know what you are doing! */ |
| 196 | PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Main(void); |
| 197 | PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Head(void); |
| 198 | PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Next(PyInterpreterState *); |
| 199 | PyAPI_FUNC(PyThreadState *) PyInterpreterState_ThreadHead(PyInterpreterState *); |
| 200 | PyAPI_FUNC(PyThreadState *) PyThreadState_Next(PyThreadState *); |
Joannah Nanjekye | 8855e47 | 2019-10-04 08:35:42 -0300 | [diff] [blame] | 201 | PyAPI_FUNC(void) PyThreadState_DeleteCurrent(void); |
Victor Stinner | f2a9d5c | 2018-11-27 00:20:00 +0100 | [diff] [blame] | 202 | |
Victor Stinner | 0b72b23 | 2020-03-12 23:18:39 +0100 | [diff] [blame] | 203 | /* Frame evaluation API */ |
| 204 | |
Victor Stinner | 7c59d7c | 2020-04-28 16:32:48 +0200 | [diff] [blame] | 205 | typedef PyObject* (*_PyFrameEvalFunction)(PyThreadState *tstate, PyFrameObject *, int); |
Victor Stinner | 0b72b23 | 2020-03-12 23:18:39 +0100 | [diff] [blame] | 206 | |
| 207 | PyAPI_FUNC(_PyFrameEvalFunction) _PyInterpreterState_GetEvalFrameFunc( |
| 208 | PyInterpreterState *interp); |
| 209 | PyAPI_FUNC(void) _PyInterpreterState_SetEvalFrameFunc( |
| 210 | PyInterpreterState *interp, |
| 211 | _PyFrameEvalFunction eval_frame); |
| 212 | |
Victor Stinner | da7933e | 2020-04-13 03:04:28 +0200 | [diff] [blame] | 213 | PyAPI_FUNC(const PyConfig*) _PyInterpreterState_GetConfig(PyInterpreterState *interp); |
| 214 | |
Victor Stinner | 048a356 | 2020-11-05 00:45:56 +0100 | [diff] [blame] | 215 | /* Get a copy of the current interpreter configuration. |
| 216 | |
| 217 | Return 0 on success. Raise an exception and return -1 on error. |
| 218 | |
| 219 | The caller must initialize 'config', using PyConfig_InitPythonConfig() |
| 220 | for example. |
| 221 | |
| 222 | Python must be preinitialized to call this method. |
| 223 | The caller must hold the GIL. */ |
| 224 | PyAPI_FUNC(int) _PyInterpreterState_GetConfigCopy( |
| 225 | struct PyConfig *config); |
| 226 | |
| 227 | /* Set the configuration of the current interpreter. |
| 228 | |
| 229 | This function should be called during or just after the Python |
| 230 | initialization. |
| 231 | |
| 232 | Update the sys module with the new configuration. If the sys module was |
| 233 | modified directly after the Python initialization, these changes are lost. |
| 234 | |
| 235 | Some configuration like faulthandler or warnoptions can be updated in the |
| 236 | configuration, but don't reconfigure Python (don't enable/disable |
| 237 | faulthandler and don't reconfigure warnings filters). |
| 238 | |
| 239 | Return 0 on success. Raise an exception and return -1 on error. |
| 240 | |
| 241 | The configuration should come from _PyInterpreterState_GetConfigCopy(). */ |
| 242 | PyAPI_FUNC(int) _PyInterpreterState_SetConfig( |
| 243 | const struct PyConfig *config); |
| 244 | |
Victor Stinner | da7933e | 2020-04-13 03:04:28 +0200 | [diff] [blame] | 245 | // Get the configuration of the currrent interpreter. |
| 246 | // The caller must hold the GIL. |
| 247 | PyAPI_FUNC(const PyConfig*) _Py_GetConfig(void); |
| 248 | |
| 249 | |
Eric Snow | c11183c | 2019-03-15 16:35:46 -0600 | [diff] [blame] | 250 | /* cross-interpreter data */ |
| 251 | |
| 252 | struct _xid; |
| 253 | |
| 254 | // _PyCrossInterpreterData is similar to Py_buffer as an effectively |
| 255 | // opaque struct that holds data outside the object machinery. This |
| 256 | // is necessary to pass safely between interpreters in the same process. |
| 257 | typedef struct _xid { |
| 258 | // data is the cross-interpreter-safe derivation of a Python object |
| 259 | // (see _PyObject_GetCrossInterpreterData). It will be NULL if the |
| 260 | // new_object func (below) encodes the data. |
| 261 | void *data; |
| 262 | // obj is the Python object from which the data was derived. This |
| 263 | // is non-NULL only if the data remains bound to the object in some |
| 264 | // way, such that the object must be "released" (via a decref) when |
| 265 | // the data is released. In that case the code that sets the field, |
| 266 | // likely a registered "crossinterpdatafunc", is responsible for |
| 267 | // ensuring it owns the reference (i.e. incref). |
| 268 | PyObject *obj; |
| 269 | // interp is the ID of the owning interpreter of the original |
| 270 | // object. It corresponds to the active interpreter when |
| 271 | // _PyObject_GetCrossInterpreterData() was called. This should only |
| 272 | // be set by the cross-interpreter machinery. |
| 273 | // |
| 274 | // We use the ID rather than the PyInterpreterState to avoid issues |
| 275 | // with deleted interpreters. Note that IDs are never re-used, so |
| 276 | // each one will always correspond to a specific interpreter |
| 277 | // (whether still alive or not). |
| 278 | int64_t interp; |
| 279 | // new_object is a function that returns a new object in the current |
| 280 | // interpreter given the data. The resulting object (a new |
| 281 | // reference) will be equivalent to the original object. This field |
| 282 | // is required. |
| 283 | PyObject *(*new_object)(struct _xid *); |
| 284 | // free is called when the data is released. If it is NULL then |
| 285 | // nothing will be done to free the data. For some types this is |
| 286 | // okay (e.g. bytes) and for those types this field should be set |
| 287 | // to NULL. However, for most the data was allocated just for |
| 288 | // cross-interpreter use, so it must be freed when |
| 289 | // _PyCrossInterpreterData_Release is called or the memory will |
| 290 | // leak. In that case, at the very least this field should be set |
| 291 | // to PyMem_RawFree (the default if not explicitly set to NULL). |
| 292 | // The call will happen with the original interpreter activated. |
| 293 | void (*free)(void *); |
| 294 | } _PyCrossInterpreterData; |
| 295 | |
| 296 | PyAPI_FUNC(int) _PyObject_GetCrossInterpreterData(PyObject *, _PyCrossInterpreterData *); |
| 297 | PyAPI_FUNC(PyObject *) _PyCrossInterpreterData_NewObject(_PyCrossInterpreterData *); |
| 298 | PyAPI_FUNC(void) _PyCrossInterpreterData_Release(_PyCrossInterpreterData *); |
| 299 | |
| 300 | PyAPI_FUNC(int) _PyObject_CheckCrossInterpreterData(PyObject *); |
| 301 | |
| 302 | /* cross-interpreter data registry */ |
| 303 | |
| 304 | typedef int (*crossinterpdatafunc)(PyObject *, struct _xid *); |
| 305 | |
| 306 | PyAPI_FUNC(int) _PyCrossInterpreterData_RegisterClass(PyTypeObject *, crossinterpdatafunc); |
| 307 | PyAPI_FUNC(crossinterpdatafunc) _PyCrossInterpreterData_Lookup(PyObject *); |