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