blob: 5439d07e6af3ea769d8c0ee578de98c51688919a [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 Snowc11183c2019-03-15 16:35:46 -060033PyAPI_FUNC(int) _PyInterpreterState_RequiresIDRef(PyInterpreterState *);
34PyAPI_FUNC(void) _PyInterpreterState_RequireIDRef(PyInterpreterState *, int);
35
Eric Snowbe3b2952019-02-23 11:35:52 -070036PyAPI_FUNC(_PyCoreConfig *) _PyInterpreterState_GetCoreConfig(PyInterpreterState *);
37PyAPI_FUNC(_PyMainInterpreterConfig *) _PyInterpreterState_GetMainConfig(PyInterpreterState *);
Victor Stinnerf2a9d5c2018-11-27 00:20:00 +010038
Eric Snowc11183c2019-03-15 16:35:46 -060039PyAPI_FUNC(PyObject *) _PyInterpreterState_GetMainModule(PyInterpreterState *);
Victor Stinnerf2a9d5c2018-11-27 00:20:00 +010040
41/* State unique per thread */
42
43/* Py_tracefunc return -1 when raising an exception, or 0 for success. */
44typedef int (*Py_tracefunc)(PyObject *, struct _frame *, int, PyObject *);
45
46/* The following values are used for 'what' for tracefunc functions
47 *
48 * To add a new kind of trace event, also update "trace_init" in
49 * Python/sysmodule.c to define the Python level event name
50 */
51#define PyTrace_CALL 0
52#define PyTrace_EXCEPTION 1
53#define PyTrace_LINE 2
54#define PyTrace_RETURN 3
55#define PyTrace_C_CALL 4
56#define PyTrace_C_EXCEPTION 5
57#define PyTrace_C_RETURN 6
58#define PyTrace_OPCODE 7
59
60
61typedef struct _err_stackitem {
62 /* This struct represents an entry on the exception stack, which is a
63 * per-coroutine state. (Coroutine in the computer science sense,
64 * including the thread and generators).
65 * This ensures that the exception state is not impacted by "yields"
66 * from an except handler.
67 */
68 PyObject *exc_type, *exc_value, *exc_traceback;
69
70 struct _err_stackitem *previous_item;
71
72} _PyErr_StackItem;
73
74
Eric Snowbe3b2952019-02-23 11:35:52 -070075// The PyThreadState typedef is in Include/pystate.h.
76struct _ts {
Victor Stinnerf2a9d5c2018-11-27 00:20:00 +010077 /* See Python/ceval.c for comments explaining most fields */
78
79 struct _ts *prev;
80 struct _ts *next;
81 PyInterpreterState *interp;
82
83 struct _frame *frame;
84 int recursion_depth;
85 char overflowed; /* The stack has overflowed. Allow 50 more calls
86 to handle the runtime error. */
87 char recursion_critical; /* The current calls must not cause
88 a stack overflow. */
89 int stackcheck_counter;
90
91 /* 'tracing' keeps track of the execution depth when tracing/profiling.
92 This is to prevent the actual trace/profile code from being recorded in
93 the trace/profile. */
94 int tracing;
95 int use_tracing;
96
97 Py_tracefunc c_profilefunc;
98 Py_tracefunc c_tracefunc;
99 PyObject *c_profileobj;
100 PyObject *c_traceobj;
101
102 /* The exception currently being raised */
103 PyObject *curexc_type;
104 PyObject *curexc_value;
105 PyObject *curexc_traceback;
106
107 /* The exception currently being handled, if no coroutines/generators
108 * are present. Always last element on the stack referred to be exc_info.
109 */
110 _PyErr_StackItem exc_state;
111
112 /* Pointer to the top of the stack of the exceptions currently
113 * being handled */
114 _PyErr_StackItem *exc_info;
115
116 PyObject *dict; /* Stores per-thread state */
117
118 int gilstate_counter;
119
120 PyObject *async_exc; /* Asynchronous exception to raise */
121 unsigned long thread_id; /* Thread id where this tstate was created */
122
123 int trash_delete_nesting;
124 PyObject *trash_delete_later;
125
126 /* Called when a thread state is deleted normally, but not when it
127 * is destroyed after fork().
128 * Pain: to prevent rare but fatal shutdown errors (issue 18808),
129 * Thread.join() must wait for the join'ed thread's tstate to be unlinked
130 * from the tstate chain. That happens at the end of a thread's life,
131 * in pystate.c.
132 * The obvious way doesn't quite work: create a lock which the tstate
133 * unlinking code releases, and have Thread.join() wait to acquire that
134 * lock. The problem is that we _are_ at the end of the thread's life:
135 * if the thread holds the last reference to the lock, decref'ing the
136 * lock will delete the lock, and that may trigger arbitrary Python code
137 * if there's a weakref, with a callback, to the lock. But by this time
138 * _PyRuntime.gilstate.tstate_current is already NULL, so only the simplest
139 * of C code can be allowed to run (in particular it must not be possible to
140 * release the GIL).
141 * So instead of holding the lock directly, the tstate holds a weakref to
142 * the lock: that's the value of on_delete_data below. Decref'ing a
143 * weakref is harmless.
144 * on_delete points to _threadmodule.c's static release_sentinel() function.
145 * After the tstate is unlinked, release_sentinel is called with the
146 * weakref-to-lock (on_delete_data) argument, and release_sentinel releases
147 * the indirectly held lock.
148 */
149 void (*on_delete)(void *);
150 void *on_delete_data;
151
152 int coroutine_origin_tracking_depth;
153
154 PyObject *coroutine_wrapper;
155 int in_coroutine_wrapper;
156
157 PyObject *async_gen_firstiter;
158 PyObject *async_gen_finalizer;
159
160 PyObject *context;
161 uint64_t context_ver;
162
163 /* Unique thread state id. */
164 uint64_t id;
165
166 /* XXX signal handlers should also be here */
167
Eric Snowbe3b2952019-02-23 11:35:52 -0700168};
Victor Stinnerf2a9d5c2018-11-27 00:20:00 +0100169
170/* Get the current interpreter state.
171
172 Issue a fatal error if there no current Python thread state or no current
173 interpreter. It cannot return NULL.
174
175 The caller must hold the GIL.*/
176PyAPI_FUNC(PyInterpreterState *) _PyInterpreterState_Get(void);
177
178PyAPI_FUNC(int) _PyState_AddModule(PyObject*, struct PyModuleDef*);
179PyAPI_FUNC(void) _PyState_ClearModules(void);
180PyAPI_FUNC(PyThreadState *) _PyThreadState_Prealloc(PyInterpreterState *);
181PyAPI_FUNC(void) _PyThreadState_Init(PyThreadState *);
182PyAPI_FUNC(void) _PyThreadState_DeleteExcept(PyThreadState *tstate);
183PyAPI_FUNC(void) _PyGILState_Reinit(void);
184
185/* Similar to PyThreadState_Get(), but don't issue a fatal error
186 * if it is NULL. */
187PyAPI_FUNC(PyThreadState *) _PyThreadState_UncheckedGet(void);
188
189/* PyGILState */
190
191/* Helper/diagnostic function - return 1 if the current thread
192 currently holds the GIL, 0 otherwise.
193
194 The function returns 1 if _PyGILState_check_enabled is non-zero. */
195PyAPI_FUNC(int) PyGILState_Check(void);
196
197/* Get the single PyInterpreterState used by this process' GILState
198 implementation.
199
200 This function doesn't check for error. Return NULL before _PyGILState_Init()
201 is called and after _PyGILState_Fini() is called.
202
203 See also _PyInterpreterState_Get() and _PyInterpreterState_GET_UNSAFE(). */
204PyAPI_FUNC(PyInterpreterState *) _PyGILState_GetInterpreterStateUnsafe(void);
205
206/* The implementation of sys._current_frames() Returns a dict mapping
207 thread id to that thread's current frame.
208*/
209PyAPI_FUNC(PyObject *) _PyThread_CurrentFrames(void);
210
211/* Routines for advanced debuggers, requested by David Beazley.
212 Don't use unless you know what you are doing! */
213PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Main(void);
214PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Head(void);
215PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Next(PyInterpreterState *);
216PyAPI_FUNC(PyThreadState *) PyInterpreterState_ThreadHead(PyInterpreterState *);
217PyAPI_FUNC(PyThreadState *) PyThreadState_Next(PyThreadState *);
218
219typedef struct _frame *(*PyThreadFrameGetter)(PyThreadState *self_);
220
Eric Snowc11183c2019-03-15 16:35:46 -0600221/* cross-interpreter data */
222
223struct _xid;
224
225// _PyCrossInterpreterData is similar to Py_buffer as an effectively
226// opaque struct that holds data outside the object machinery. This
227// is necessary to pass safely between interpreters in the same process.
228typedef struct _xid {
229 // data is the cross-interpreter-safe derivation of a Python object
230 // (see _PyObject_GetCrossInterpreterData). It will be NULL if the
231 // new_object func (below) encodes the data.
232 void *data;
233 // obj is the Python object from which the data was derived. This
234 // is non-NULL only if the data remains bound to the object in some
235 // way, such that the object must be "released" (via a decref) when
236 // the data is released. In that case the code that sets the field,
237 // likely a registered "crossinterpdatafunc", is responsible for
238 // ensuring it owns the reference (i.e. incref).
239 PyObject *obj;
240 // interp is the ID of the owning interpreter of the original
241 // object. It corresponds to the active interpreter when
242 // _PyObject_GetCrossInterpreterData() was called. This should only
243 // be set by the cross-interpreter machinery.
244 //
245 // We use the ID rather than the PyInterpreterState to avoid issues
246 // with deleted interpreters. Note that IDs are never re-used, so
247 // each one will always correspond to a specific interpreter
248 // (whether still alive or not).
249 int64_t interp;
250 // new_object is a function that returns a new object in the current
251 // interpreter given the data. The resulting object (a new
252 // reference) will be equivalent to the original object. This field
253 // is required.
254 PyObject *(*new_object)(struct _xid *);
255 // free is called when the data is released. If it is NULL then
256 // nothing will be done to free the data. For some types this is
257 // okay (e.g. bytes) and for those types this field should be set
258 // to NULL. However, for most the data was allocated just for
259 // cross-interpreter use, so it must be freed when
260 // _PyCrossInterpreterData_Release is called or the memory will
261 // leak. In that case, at the very least this field should be set
262 // to PyMem_RawFree (the default if not explicitly set to NULL).
263 // The call will happen with the original interpreter activated.
264 void (*free)(void *);
265} _PyCrossInterpreterData;
266
267PyAPI_FUNC(int) _PyObject_GetCrossInterpreterData(PyObject *, _PyCrossInterpreterData *);
268PyAPI_FUNC(PyObject *) _PyCrossInterpreterData_NewObject(_PyCrossInterpreterData *);
269PyAPI_FUNC(void) _PyCrossInterpreterData_Release(_PyCrossInterpreterData *);
270
271PyAPI_FUNC(int) _PyObject_CheckCrossInterpreterData(PyObject *);
272
273/* cross-interpreter data registry */
274
275typedef int (*crossinterpdatafunc)(PyObject *, struct _xid *);
276
277PyAPI_FUNC(int) _PyCrossInterpreterData_RegisterClass(PyTypeObject *, crossinterpdatafunc);
278PyAPI_FUNC(crossinterpdatafunc) _PyCrossInterpreterData_Lookup(PyObject *);
279
Victor Stinnerf2a9d5c2018-11-27 00:20:00 +0100280#ifdef __cplusplus
281}
282#endif