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