1#ifndef Py_CPYTHON_PYSTATE_H
2# error "this header file must not be included directly"
3#endif
4
5
6/*
7Runtime Feature Flags
8
9Each flag indicate whether or not a specific runtime feature
10is available in a given context. For example, forking the process
11might not be allowed in the current interpreter (i.e. os.fork() would fail).
12*/
13
14/* Set if the interpreter share obmalloc runtime state
15 with the main interpreter. */
16#define Py_RTFLAGS_USE_MAIN_OBMALLOC (1UL << 5)
17
18/* Set if import should check a module for subinterpreter support. */
19#define Py_RTFLAGS_MULTI_INTERP_EXTENSIONS (1UL << 8)
20
21/* Set if threads are allowed. */
22#define Py_RTFLAGS_THREADS (1UL << 10)
23
24/* Set if daemon threads are allowed. */
25#define Py_RTFLAGS_DAEMON_THREADS (1UL << 11)
26
27/* Set if os.fork() is allowed. */
28#define Py_RTFLAGS_FORK (1UL << 15)
29
30/* Set if os.exec*() is allowed. */
31#define Py_RTFLAGS_EXEC (1UL << 16)
32
33
34PyAPI_FUNC(int) _PyInterpreterState_HasFeature(PyInterpreterState *interp,
35 unsigned long feature);
36
37
38/* private interpreter helpers */
39
40PyAPI_FUNC(int) _PyInterpreterState_RequiresIDRef(PyInterpreterState *);
41PyAPI_FUNC(void) _PyInterpreterState_RequireIDRef(PyInterpreterState *, int);
42
43PyAPI_FUNC(PyObject *) _PyInterpreterState_GetMainModule(PyInterpreterState *);
44
45
46/* State unique per thread */
47
48/* Py_tracefunc return -1 when raising an exception, or 0 for success. */
49typedef int (*Py_tracefunc)(PyObject *, PyFrameObject *, int, PyObject *);
50
51/* The following values are used for 'what' for tracefunc functions
52 *
53 * To add a new kind of trace event, also update "trace_init" in
54 * Python/sysmodule.c to define the Python level event name
55 */
56#define PyTrace_CALL 0
57#define PyTrace_EXCEPTION 1
58#define PyTrace_LINE 2
59#define PyTrace_RETURN 3
60#define PyTrace_C_CALL 4
61#define PyTrace_C_EXCEPTION 5
62#define PyTrace_C_RETURN 6
63#define PyTrace_OPCODE 7
64
65// Internal structure: you should not use it directly, but use public functions
66// like PyThreadState_EnterTracing() and PyThreadState_LeaveTracing().
67typedef struct _PyCFrame {
68 /* This struct will be threaded through the C stack
69 * allowing fast access to per-thread state that needs
70 * to be accessed quickly by the interpreter, but can
71 * be modified outside of the interpreter.
72 *
73 * WARNING: This makes data on the C stack accessible from
74 * heap objects. Care must be taken to maintain stack
75 * discipline and make sure that instances of this struct cannot
76 * accessed outside of their lifetime.
77 */
78 /* Pointer to the currently executing frame (it can be NULL) */
79 struct _PyInterpreterFrame *current_frame;
80 struct _PyCFrame *previous;
81} _PyCFrame;
82
83typedef struct _err_stackitem {
84 /* This struct represents a single execution context where we might
85 * be currently handling an exception. It is a per-coroutine state
86 * (coroutine in the computer science sense, including the thread
87 * and generators).
88 *
89 * This is used as an entry on the exception stack, where each
90 * entry indicates if it is currently handling an exception.
91 * This ensures that the exception state is not impacted
92 * by "yields" from an except handler. The thread
93 * always has an entry (the bottom-most one).
94 */
95
96 /* The exception currently being handled in this context, if any. */
97 PyObject *exc_value;
98
99 struct _err_stackitem *previous_item;
100
101} _PyErr_StackItem;
102
103typedef struct _stack_chunk {
104 struct _stack_chunk *previous;
105 size_t size;
106 size_t top;
107 PyObject * data[1]; /* Variable sized */
108} _PyStackChunk;
109
110struct _py_trashcan {
111 int delete_nesting;
112 PyObject *delete_later;
113};
114
115struct _ts {
116 /* See Python/ceval.c for comments explaining most fields */
117
118 PyThreadState *prev;
119 PyThreadState *next;
120 PyInterpreterState *interp;
121
122 struct {
123 /* Has been initialized to a safe state.
124
125 In order to be effective, this must be set to 0 during or right
126 after allocation. */
127 unsigned int initialized:1;
128
129 /* Has been bound to an OS thread. */
130 unsigned int bound:1;
131 /* Has been unbound from its OS thread. */
132 unsigned int unbound:1;
133 /* Has been bound aa current for the GILState API. */
134 unsigned int bound_gilstate:1;
135 /* Currently in use (maybe holds the GIL). */
136 unsigned int active:1;
137
138 /* various stages of finalization */
139 unsigned int finalizing:1;
140 unsigned int cleared:1;
141 unsigned int finalized:1;
142
143 /* padding to align to 4 bytes */
144 unsigned int :24;
145 } _status;
146
147 int py_recursion_remaining;
148 int py_recursion_limit;
149
150 int c_recursion_remaining;
151 int recursion_headroom; /* Allow 50 more calls to handle any errors. */
152
153 /* 'tracing' keeps track of the execution depth when tracing/profiling.
154 This is to prevent the actual trace/profile code from being recorded in
155 the trace/profile. */
156 int tracing;
157 int what_event; /* The event currently being monitored, if any. */
158
159 /* Pointer to current _PyCFrame in the C stack frame of the currently,
160 * or most recently, executing _PyEval_EvalFrameDefault. */
161 _PyCFrame *cframe;
162
163 Py_tracefunc c_profilefunc;
164 Py_tracefunc c_tracefunc;
165 PyObject *c_profileobj;
166 PyObject *c_traceobj;
167
168 /* The exception currently being raised */
169 PyObject *current_exception;
170
171 /* Pointer to the top of the exception stack for the exceptions
172 * we may be currently handling. (See _PyErr_StackItem above.)
173 * This is never NULL. */
174 _PyErr_StackItem *exc_info;
175
176 PyObject *dict; /* Stores per-thread state */
177
178 int gilstate_counter;
179
180 PyObject *async_exc; /* Asynchronous exception to raise */
181 unsigned long thread_id; /* Thread id where this tstate was created */
182
183 /* Native thread id where this tstate was created. This will be 0 except on
184 * those platforms that have the notion of native thread id, for which the
185 * macro PY_HAVE_THREAD_NATIVE_ID is then defined.
186 */
187 unsigned long native_thread_id;
188
189 struct _py_trashcan trash;
190
191 /* Called when a thread state is deleted normally, but not when it
192 * is destroyed after fork().
193 * Pain: to prevent rare but fatal shutdown errors (issue 18808),
194 * Thread.join() must wait for the join'ed thread's tstate to be unlinked
195 * from the tstate chain. That happens at the end of a thread's life,
196 * in pystate.c.
197 * The obvious way doesn't quite work: create a lock which the tstate
198 * unlinking code releases, and have Thread.join() wait to acquire that
199 * lock. The problem is that we _are_ at the end of the thread's life:
200 * if the thread holds the last reference to the lock, decref'ing the
201 * lock will delete the lock, and that may trigger arbitrary Python code
202 * if there's a weakref, with a callback, to the lock. But by this time
203 * _PyRuntime.gilstate.tstate_current is already NULL, so only the simplest
204 * of C code can be allowed to run (in particular it must not be possible to
205 * release the GIL).
206 * So instead of holding the lock directly, the tstate holds a weakref to
207 * the lock: that's the value of on_delete_data below. Decref'ing a
208 * weakref is harmless.
209 * on_delete points to _threadmodule.c's static release_sentinel() function.
210 * After the tstate is unlinked, release_sentinel is called with the
211 * weakref-to-lock (on_delete_data) argument, and release_sentinel releases
212 * the indirectly held lock.
213 */
214 void (*on_delete)(void *);
215 void *on_delete_data;
216
217 int coroutine_origin_tracking_depth;
218
219 PyObject *async_gen_firstiter;
220 PyObject *async_gen_finalizer;
221
222 PyObject *context;
223 uint64_t context_ver;
224
225 /* Unique thread state id. */
226 uint64_t id;
227
228 _PyStackChunk *datastack_chunk;
229 PyObject **datastack_top;
230 PyObject **datastack_limit;
231 /* XXX signal handlers should also be here */
232
233 /* The following fields are here to avoid allocation during init.
234 The data is exposed through PyThreadState pointer fields.
235 These fields should not be accessed directly outside of init.
236 This is indicated by an underscore prefix on the field names.
237
238 All other PyInterpreterState pointer fields are populated when
239 needed and default to NULL.
240 */
241 // Note some fields do not have a leading underscore for backward
242 // compatibility. See https://bugs.python.org/issue45953#msg412046.
243
244 /* The thread's exception stack entry. (Always the last entry.) */
245 _PyErr_StackItem exc_state;
246
247 /* The bottom-most frame on the stack. */
248 _PyCFrame root_cframe;
249};
250
251/* WASI has limited call stack. Python's recursion limit depends on code
252 layout, optimization, and WASI runtime. Wasmtime can handle about 700
253 recursions, sometimes less. 500 is a more conservative limit. */
254#ifdef Py_DEBUG
255# if defined(__wasi__)
256# define C_RECURSION_LIMIT 150
257# else
258# define C_RECURSION_LIMIT 500
259# endif
260#else
261# if defined(__wasi__)
262# define C_RECURSION_LIMIT 500
263# elif defined(__s390x__)
264# define C_RECURSION_LIMIT 800
265# elif defined(_WIN32)
266# define C_RECURSION_LIMIT 3000
267# elif defined(_Py_ADDRESS_SANITIZER)
268# define C_RECURSION_LIMIT 4000
269# else
270 // This value is duplicated in Lib/test/support/__init__.py
271# define C_RECURSION_LIMIT 10000
272# endif
273#endif
274
275/* other API */
276
277// Alias for backward compatibility with Python 3.8
278#define _PyInterpreterState_Get PyInterpreterState_Get
279
280/* An alias for the internal _PyThreadState_New(),
281 kept for stable ABI compatibility. */
282PyAPI_FUNC(PyThreadState *) _PyThreadState_Prealloc(PyInterpreterState *);
283
284/* Similar to PyThreadState_Get(), but don't issue a fatal error
285 * if it is NULL. */
286PyAPI_FUNC(PyThreadState *) _PyThreadState_UncheckedGet(void);
287
288PyAPI_FUNC(PyObject *) _PyThreadState_GetDict(PyThreadState *tstate);
289
290// Disable tracing and profiling.
291PyAPI_FUNC(void) PyThreadState_EnterTracing(PyThreadState *tstate);
292
293// Reset tracing and profiling: enable them if a trace function or a profile
294// function is set, otherwise disable them.
295PyAPI_FUNC(void) PyThreadState_LeaveTracing(PyThreadState *tstate);
296
297/* PyGILState */
298
299/* Helper/diagnostic function - return 1 if the current thread
300 currently holds the GIL, 0 otherwise.
301
302 The function returns 1 if _PyGILState_check_enabled is non-zero. */
303PyAPI_FUNC(int) PyGILState_Check(void);
304
305/* Get the single PyInterpreterState used by this process' GILState
306 implementation.
307
308 This function doesn't check for error. Return NULL before _PyGILState_Init()
309 is called and after _PyGILState_Fini() is called.
310
311 See also _PyInterpreterState_Get() and _PyInterpreterState_GET(). */
312PyAPI_FUNC(PyInterpreterState *) _PyGILState_GetInterpreterStateUnsafe(void);
313
314/* The implementation of sys._current_frames() Returns a dict mapping
315 thread id to that thread's current frame.
316*/
317PyAPI_FUNC(PyObject *) _PyThread_CurrentFrames(void);
318
319/* The implementation of sys._current_exceptions() Returns a dict mapping
320 thread id to that thread's current exception.
321*/
322PyAPI_FUNC(PyObject *) _PyThread_CurrentExceptions(void);
323
324/* Routines for advanced debuggers, requested by David Beazley.
325 Don't use unless you know what you are doing! */
326PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Main(void);
327PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Head(void);
328PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Next(PyInterpreterState *);
329PyAPI_FUNC(PyThreadState *) PyInterpreterState_ThreadHead(PyInterpreterState *);
330PyAPI_FUNC(PyThreadState *) PyThreadState_Next(PyThreadState *);
331PyAPI_FUNC(void) PyThreadState_DeleteCurrent(void);
332
333/* Frame evaluation API */
334
335typedef PyObject* (*_PyFrameEvalFunction)(PyThreadState *tstate, struct _PyInterpreterFrame *, int);
336
337PyAPI_FUNC(_PyFrameEvalFunction) _PyInterpreterState_GetEvalFrameFunc(
338 PyInterpreterState *interp);
339PyAPI_FUNC(void) _PyInterpreterState_SetEvalFrameFunc(
340 PyInterpreterState *interp,
341 _PyFrameEvalFunction eval_frame);
342
343PyAPI_FUNC(const PyConfig*) _PyInterpreterState_GetConfig(PyInterpreterState *interp);
344
345/* Get a copy of the current interpreter configuration.
346
347 Return 0 on success. Raise an exception and return -1 on error.
348
349 The caller must initialize 'config', using PyConfig_InitPythonConfig()
350 for example.
351
352 Python must be preinitialized to call this method.
353 The caller must hold the GIL.
354
355 Once done with the configuration, PyConfig_Clear() must be called to clear
356 it. */
357PyAPI_FUNC(int) _PyInterpreterState_GetConfigCopy(
358 struct PyConfig *config);
359
360/* Set the configuration of the current interpreter.
361
362 This function should be called during or just after the Python
363 initialization.
364
365 Update the sys module with the new configuration. If the sys module was
366 modified directly after the Python initialization, these changes are lost.
367
368 Some configuration like faulthandler or warnoptions can be updated in the
369 configuration, but don't reconfigure Python (don't enable/disable
370 faulthandler and don't reconfigure warnings filters).
371
372 Return 0 on success. Raise an exception and return -1 on error.
373
374 The configuration should come from _PyInterpreterState_GetConfigCopy(). */
375PyAPI_FUNC(int) _PyInterpreterState_SetConfig(
376 const struct PyConfig *config);
377
378// Get the configuration of the current interpreter.
379// The caller must hold the GIL.
380PyAPI_FUNC(const PyConfig*) _Py_GetConfig(void);
381
382
383/* cross-interpreter data */
384
385// _PyCrossInterpreterData is similar to Py_buffer as an effectively
386// opaque struct that holds data outside the object machinery. This
387// is necessary to pass safely between interpreters in the same process.
388typedef struct _xid _PyCrossInterpreterData;
389
390typedef PyObject *(*xid_newobjectfunc)(_PyCrossInterpreterData *);
391typedef void (*xid_freefunc)(void *);
392
393struct _xid {
394 // data is the cross-interpreter-safe derivation of a Python object
395 // (see _PyObject_GetCrossInterpreterData). It will be NULL if the
396 // new_object func (below) encodes the data.
397 void *data;
398 // obj is the Python object from which the data was derived. This
399 // is non-NULL only if the data remains bound to the object in some
400 // way, such that the object must be "released" (via a decref) when
401 // the data is released. In that case the code that sets the field,
402 // likely a registered "crossinterpdatafunc", is responsible for
403 // ensuring it owns the reference (i.e. incref).
404 PyObject *obj;
405 // interp is the ID of the owning interpreter of the original
406 // object. It corresponds to the active interpreter when
407 // _PyObject_GetCrossInterpreterData() was called. This should only
408 // be set by the cross-interpreter machinery.
409 //
410 // We use the ID rather than the PyInterpreterState to avoid issues
411 // with deleted interpreters. Note that IDs are never re-used, so
412 // each one will always correspond to a specific interpreter
413 // (whether still alive or not).
414 int64_t interp;
415 // new_object is a function that returns a new object in the current
416 // interpreter given the data. The resulting object (a new
417 // reference) will be equivalent to the original object. This field
418 // is required.
419 xid_newobjectfunc new_object;
420 // free is called when the data is released. If it is NULL then
421 // nothing will be done to free the data. For some types this is
422 // okay (e.g. bytes) and for those types this field should be set
423 // to NULL. However, for most the data was allocated just for
424 // cross-interpreter use, so it must be freed when
425 // _PyCrossInterpreterData_Release is called or the memory will
426 // leak. In that case, at the very least this field should be set
427 // to PyMem_RawFree (the default if not explicitly set to NULL).
428 // The call will happen with the original interpreter activated.
429 xid_freefunc free;
430};
431
432PyAPI_FUNC(void) _PyCrossInterpreterData_Init(
433 _PyCrossInterpreterData *data,
434 PyInterpreterState *interp, void *shared, PyObject *obj,
435 xid_newobjectfunc new_object);
436PyAPI_FUNC(int) _PyCrossInterpreterData_InitWithSize(
437 _PyCrossInterpreterData *,
438 PyInterpreterState *interp, const size_t, PyObject *,
439 xid_newobjectfunc);
440PyAPI_FUNC(void) _PyCrossInterpreterData_Clear(
441 PyInterpreterState *, _PyCrossInterpreterData *);
442
443PyAPI_FUNC(int) _PyObject_GetCrossInterpreterData(PyObject *, _PyCrossInterpreterData *);
444PyAPI_FUNC(PyObject *) _PyCrossInterpreterData_NewObject(_PyCrossInterpreterData *);
445PyAPI_FUNC(int) _PyCrossInterpreterData_Release(_PyCrossInterpreterData *);
446
447PyAPI_FUNC(int) _PyObject_CheckCrossInterpreterData(PyObject *);
448
449/* cross-interpreter data registry */
450
451typedef int (*crossinterpdatafunc)(PyThreadState *tstate, PyObject *,
452 _PyCrossInterpreterData *);
453
454PyAPI_FUNC(int) _PyCrossInterpreterData_RegisterClass(PyTypeObject *, crossinterpdatafunc);
455PyAPI_FUNC(int) _PyCrossInterpreterData_UnregisterClass(PyTypeObject *);
456PyAPI_FUNC(crossinterpdatafunc) _PyCrossInterpreterData_Lookup(PyObject *);
457

source code of include/python3.12/cpython/pystate.h