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