blob: 3fca78f9ddf2384212ac6abebb69bea827db820f [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
5#ifdef __cplusplus
6extern "C" {
7#endif
8
Victor Stinnerf684d832019-03-01 03:44:13 +01009#include "cpython/coreconfig.h"
10
Victor Stinnerf2a9d5c2018-11-27 00:20:00 +010011/* Placeholders while working on the new configuration API
12 *
13 * See PEP 432 for final anticipated contents
14 */
15typedef struct {
16 int install_signal_handlers; /* Install signal handlers? -1 means unset */
17 PyObject *argv; /* sys.argv list, can be NULL */
18 PyObject *executable; /* sys.executable str */
19 PyObject *prefix; /* sys.prefix str */
20 PyObject *base_prefix; /* sys.base_prefix str, can be NULL */
21 PyObject *exec_prefix; /* sys.exec_prefix str */
22 PyObject *base_exec_prefix; /* sys.base_exec_prefix str, can be NULL */
23 PyObject *warnoptions; /* sys.warnoptions list, can be NULL */
24 PyObject *xoptions; /* sys._xoptions dict, can be NULL */
25 PyObject *module_search_path; /* sys.path list */
26 PyObject *pycache_prefix; /* sys.pycache_prefix str, can be NULL */
27} _PyMainInterpreterConfig;
28
29#define _PyMainInterpreterConfig_INIT \
30 (_PyMainInterpreterConfig){.install_signal_handlers = -1}
31/* Note: _PyMainInterpreterConfig_INIT sets other fields to 0/NULL */
32
Eric Snowbe3b2952019-02-23 11:35:52 -070033PyAPI_FUNC(_PyCoreConfig *) _PyInterpreterState_GetCoreConfig(PyInterpreterState *);
34PyAPI_FUNC(_PyMainInterpreterConfig *) _PyInterpreterState_GetMainConfig(PyInterpreterState *);
Victor Stinnerf2a9d5c2018-11-27 00:20:00 +010035
Victor Stinnerf2a9d5c2018-11-27 00:20:00 +010036
37/* State unique per thread */
38
39/* Py_tracefunc return -1 when raising an exception, or 0 for success. */
40typedef int (*Py_tracefunc)(PyObject *, struct _frame *, int, PyObject *);
41
42/* The following values are used for 'what' for tracefunc functions
43 *
44 * To add a new kind of trace event, also update "trace_init" in
45 * Python/sysmodule.c to define the Python level event name
46 */
47#define PyTrace_CALL 0
48#define PyTrace_EXCEPTION 1
49#define PyTrace_LINE 2
50#define PyTrace_RETURN 3
51#define PyTrace_C_CALL 4
52#define PyTrace_C_EXCEPTION 5
53#define PyTrace_C_RETURN 6
54#define PyTrace_OPCODE 7
55
56
57typedef struct _err_stackitem {
58 /* This struct represents an entry on the exception stack, which is a
59 * per-coroutine state. (Coroutine in the computer science sense,
60 * including the thread and generators).
61 * This ensures that the exception state is not impacted by "yields"
62 * from an except handler.
63 */
64 PyObject *exc_type, *exc_value, *exc_traceback;
65
66 struct _err_stackitem *previous_item;
67
68} _PyErr_StackItem;
69
70
Eric Snowbe3b2952019-02-23 11:35:52 -070071// The PyThreadState typedef is in Include/pystate.h.
72struct _ts {
Victor Stinnerf2a9d5c2018-11-27 00:20:00 +010073 /* See Python/ceval.c for comments explaining most fields */
74
75 struct _ts *prev;
76 struct _ts *next;
77 PyInterpreterState *interp;
78
79 struct _frame *frame;
80 int recursion_depth;
81 char overflowed; /* The stack has overflowed. Allow 50 more calls
82 to handle the runtime error. */
83 char recursion_critical; /* The current calls must not cause
84 a stack overflow. */
85 int stackcheck_counter;
86
87 /* 'tracing' keeps track of the execution depth when tracing/profiling.
88 This is to prevent the actual trace/profile code from being recorded in
89 the trace/profile. */
90 int tracing;
91 int use_tracing;
92
93 Py_tracefunc c_profilefunc;
94 Py_tracefunc c_tracefunc;
95 PyObject *c_profileobj;
96 PyObject *c_traceobj;
97
98 /* The exception currently being raised */
99 PyObject *curexc_type;
100 PyObject *curexc_value;
101 PyObject *curexc_traceback;
102
103 /* The exception currently being handled, if no coroutines/generators
104 * are present. Always last element on the stack referred to be exc_info.
105 */
106 _PyErr_StackItem exc_state;
107
108 /* Pointer to the top of the stack of the exceptions currently
109 * being handled */
110 _PyErr_StackItem *exc_info;
111
112 PyObject *dict; /* Stores per-thread state */
113
114 int gilstate_counter;
115
116 PyObject *async_exc; /* Asynchronous exception to raise */
117 unsigned long thread_id; /* Thread id where this tstate was created */
118
119 int trash_delete_nesting;
120 PyObject *trash_delete_later;
121
122 /* Called when a thread state is deleted normally, but not when it
123 * is destroyed after fork().
124 * Pain: to prevent rare but fatal shutdown errors (issue 18808),
125 * Thread.join() must wait for the join'ed thread's tstate to be unlinked
126 * from the tstate chain. That happens at the end of a thread's life,
127 * in pystate.c.
128 * The obvious way doesn't quite work: create a lock which the tstate
129 * unlinking code releases, and have Thread.join() wait to acquire that
130 * lock. The problem is that we _are_ at the end of the thread's life:
131 * if the thread holds the last reference to the lock, decref'ing the
132 * lock will delete the lock, and that may trigger arbitrary Python code
133 * if there's a weakref, with a callback, to the lock. But by this time
134 * _PyRuntime.gilstate.tstate_current is already NULL, so only the simplest
135 * of C code can be allowed to run (in particular it must not be possible to
136 * release the GIL).
137 * So instead of holding the lock directly, the tstate holds a weakref to
138 * the lock: that's the value of on_delete_data below. Decref'ing a
139 * weakref is harmless.
140 * on_delete points to _threadmodule.c's static release_sentinel() function.
141 * After the tstate is unlinked, release_sentinel is called with the
142 * weakref-to-lock (on_delete_data) argument, and release_sentinel releases
143 * the indirectly held lock.
144 */
145 void (*on_delete)(void *);
146 void *on_delete_data;
147
148 int coroutine_origin_tracking_depth;
149
150 PyObject *coroutine_wrapper;
151 int in_coroutine_wrapper;
152
153 PyObject *async_gen_firstiter;
154 PyObject *async_gen_finalizer;
155
156 PyObject *context;
157 uint64_t context_ver;
158
159 /* Unique thread state id. */
160 uint64_t id;
161
162 /* XXX signal handlers should also be here */
163
Eric Snowbe3b2952019-02-23 11:35:52 -0700164};
Victor Stinnerf2a9d5c2018-11-27 00:20:00 +0100165
166/* Get the current interpreter state.
167
168 Issue a fatal error if there no current Python thread state or no current
169 interpreter. It cannot return NULL.
170
171 The caller must hold the GIL.*/
172PyAPI_FUNC(PyInterpreterState *) _PyInterpreterState_Get(void);
173
174PyAPI_FUNC(int) _PyState_AddModule(PyObject*, struct PyModuleDef*);
175PyAPI_FUNC(void) _PyState_ClearModules(void);
176PyAPI_FUNC(PyThreadState *) _PyThreadState_Prealloc(PyInterpreterState *);
177PyAPI_FUNC(void) _PyThreadState_Init(PyThreadState *);
178PyAPI_FUNC(void) _PyThreadState_DeleteExcept(PyThreadState *tstate);
179PyAPI_FUNC(void) _PyGILState_Reinit(void);
180
181/* Similar to PyThreadState_Get(), but don't issue a fatal error
182 * if it is NULL. */
183PyAPI_FUNC(PyThreadState *) _PyThreadState_UncheckedGet(void);
184
185/* PyGILState */
186
187/* Helper/diagnostic function - return 1 if the current thread
188 currently holds the GIL, 0 otherwise.
189
190 The function returns 1 if _PyGILState_check_enabled is non-zero. */
191PyAPI_FUNC(int) PyGILState_Check(void);
192
193/* Get the single PyInterpreterState used by this process' GILState
194 implementation.
195
196 This function doesn't check for error. Return NULL before _PyGILState_Init()
197 is called and after _PyGILState_Fini() is called.
198
199 See also _PyInterpreterState_Get() and _PyInterpreterState_GET_UNSAFE(). */
200PyAPI_FUNC(PyInterpreterState *) _PyGILState_GetInterpreterStateUnsafe(void);
201
202/* The implementation of sys._current_frames() Returns a dict mapping
203 thread id to that thread's current frame.
204*/
205PyAPI_FUNC(PyObject *) _PyThread_CurrentFrames(void);
206
207/* Routines for advanced debuggers, requested by David Beazley.
208 Don't use unless you know what you are doing! */
209PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Main(void);
210PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Head(void);
211PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Next(PyInterpreterState *);
212PyAPI_FUNC(PyThreadState *) PyInterpreterState_ThreadHead(PyInterpreterState *);
213PyAPI_FUNC(PyThreadState *) PyThreadState_Next(PyThreadState *);
214
215typedef struct _frame *(*PyThreadFrameGetter)(PyThreadState *self_);
216
Victor Stinnerf2a9d5c2018-11-27 00:20:00 +0100217#ifdef __cplusplus
218}
219#endif