Skip to content

Commit f2a9d5c

Browse files
authored
bpo-35134: Create Include/cpython/pystate.h (GH-10733)
Move pystate.h code surrounded by "#ifndef Py_LIMITED_API" to a new Include/cpython/pystate.h header file.
1 parent ffedd9a commit f2a9d5c

File tree

2 files changed

+275
-281
lines changed

2 files changed

+275
-281
lines changed

Include/cpython/pystate.h

+272
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
#ifndef Py_CPYTHON_PYSTATE_H
2+
# error "this header file must not be included directly"
3+
#endif
4+
5+
#ifdef __cplusplus
6+
extern "C" {
7+
#endif
8+
9+
typedef PyObject* (*_PyFrameEvalFunction)(struct _frame *, int);
10+
11+
/* Placeholders while working on the new configuration API
12+
*
13+
* See PEP 432 for final anticipated contents
14+
*/
15+
typedef 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+
33+
typedef struct _is {
34+
35+
struct _is *next;
36+
struct _ts *tstate_head;
37+
38+
int64_t id;
39+
int64_t id_refcount;
40+
PyThread_type_lock id_mutex;
41+
42+
PyObject *modules;
43+
PyObject *modules_by_index;
44+
PyObject *sysdict;
45+
PyObject *builtins;
46+
PyObject *importlib;
47+
48+
/* Used in Python/sysmodule.c. */
49+
int check_interval;
50+
51+
/* Used in Modules/_threadmodule.c. */
52+
long num_threads;
53+
/* Support for runtime thread stack size tuning.
54+
A value of 0 means using the platform's default stack size
55+
or the size specified by the THREAD_STACK_SIZE macro. */
56+
/* Used in Python/thread.c. */
57+
size_t pythread_stacksize;
58+
59+
PyObject *codec_search_path;
60+
PyObject *codec_search_cache;
61+
PyObject *codec_error_registry;
62+
int codecs_initialized;
63+
int fscodec_initialized;
64+
65+
_PyCoreConfig core_config;
66+
_PyMainInterpreterConfig config;
67+
#ifdef HAVE_DLOPEN
68+
int dlopenflags;
69+
#endif
70+
71+
PyObject *builtins_copy;
72+
PyObject *import_func;
73+
/* Initialized to PyEval_EvalFrameDefault(). */
74+
_PyFrameEvalFunction eval_frame;
75+
76+
Py_ssize_t co_extra_user_count;
77+
freefunc co_extra_freefuncs[MAX_CO_EXTRA_USERS];
78+
79+
#ifdef HAVE_FORK
80+
PyObject *before_forkers;
81+
PyObject *after_forkers_parent;
82+
PyObject *after_forkers_child;
83+
#endif
84+
/* AtExit module */
85+
void (*pyexitfunc)(PyObject *);
86+
PyObject *pyexitmodule;
87+
88+
uint64_t tstate_next_unique_id;
89+
} PyInterpreterState;
90+
91+
/* State unique per thread */
92+
93+
/* Py_tracefunc return -1 when raising an exception, or 0 for success. */
94+
typedef int (*Py_tracefunc)(PyObject *, struct _frame *, int, PyObject *);
95+
96+
/* The following values are used for 'what' for tracefunc functions
97+
*
98+
* To add a new kind of trace event, also update "trace_init" in
99+
* Python/sysmodule.c to define the Python level event name
100+
*/
101+
#define PyTrace_CALL 0
102+
#define PyTrace_EXCEPTION 1
103+
#define PyTrace_LINE 2
104+
#define PyTrace_RETURN 3
105+
#define PyTrace_C_CALL 4
106+
#define PyTrace_C_EXCEPTION 5
107+
#define PyTrace_C_RETURN 6
108+
#define PyTrace_OPCODE 7
109+
110+
111+
typedef struct _err_stackitem {
112+
/* This struct represents an entry on the exception stack, which is a
113+
* per-coroutine state. (Coroutine in the computer science sense,
114+
* including the thread and generators).
115+
* This ensures that the exception state is not impacted by "yields"
116+
* from an except handler.
117+
*/
118+
PyObject *exc_type, *exc_value, *exc_traceback;
119+
120+
struct _err_stackitem *previous_item;
121+
122+
} _PyErr_StackItem;
123+
124+
125+
typedef struct _ts {
126+
/* See Python/ceval.c for comments explaining most fields */
127+
128+
struct _ts *prev;
129+
struct _ts *next;
130+
PyInterpreterState *interp;
131+
132+
struct _frame *frame;
133+
int recursion_depth;
134+
char overflowed; /* The stack has overflowed. Allow 50 more calls
135+
to handle the runtime error. */
136+
char recursion_critical; /* The current calls must not cause
137+
a stack overflow. */
138+
int stackcheck_counter;
139+
140+
/* 'tracing' keeps track of the execution depth when tracing/profiling.
141+
This is to prevent the actual trace/profile code from being recorded in
142+
the trace/profile. */
143+
int tracing;
144+
int use_tracing;
145+
146+
Py_tracefunc c_profilefunc;
147+
Py_tracefunc c_tracefunc;
148+
PyObject *c_profileobj;
149+
PyObject *c_traceobj;
150+
151+
/* The exception currently being raised */
152+
PyObject *curexc_type;
153+
PyObject *curexc_value;
154+
PyObject *curexc_traceback;
155+
156+
/* The exception currently being handled, if no coroutines/generators
157+
* are present. Always last element on the stack referred to be exc_info.
158+
*/
159+
_PyErr_StackItem exc_state;
160+
161+
/* Pointer to the top of the stack of the exceptions currently
162+
* being handled */
163+
_PyErr_StackItem *exc_info;
164+
165+
PyObject *dict; /* Stores per-thread state */
166+
167+
int gilstate_counter;
168+
169+
PyObject *async_exc; /* Asynchronous exception to raise */
170+
unsigned long thread_id; /* Thread id where this tstate was created */
171+
172+
int trash_delete_nesting;
173+
PyObject *trash_delete_later;
174+
175+
/* Called when a thread state is deleted normally, but not when it
176+
* is destroyed after fork().
177+
* Pain: to prevent rare but fatal shutdown errors (issue 18808),
178+
* Thread.join() must wait for the join'ed thread's tstate to be unlinked
179+
* from the tstate chain. That happens at the end of a thread's life,
180+
* in pystate.c.
181+
* The obvious way doesn't quite work: create a lock which the tstate
182+
* unlinking code releases, and have Thread.join() wait to acquire that
183+
* lock. The problem is that we _are_ at the end of the thread's life:
184+
* if the thread holds the last reference to the lock, decref'ing the
185+
* lock will delete the lock, and that may trigger arbitrary Python code
186+
* if there's a weakref, with a callback, to the lock. But by this time
187+
* _PyRuntime.gilstate.tstate_current is already NULL, so only the simplest
188+
* of C code can be allowed to run (in particular it must not be possible to
189+
* release the GIL).
190+
* So instead of holding the lock directly, the tstate holds a weakref to
191+
* the lock: that's the value of on_delete_data below. Decref'ing a
192+
* weakref is harmless.
193+
* on_delete points to _threadmodule.c's static release_sentinel() function.
194+
* After the tstate is unlinked, release_sentinel is called with the
195+
* weakref-to-lock (on_delete_data) argument, and release_sentinel releases
196+
* the indirectly held lock.
197+
*/
198+
void (*on_delete)(void *);
199+
void *on_delete_data;
200+
201+
int coroutine_origin_tracking_depth;
202+
203+
PyObject *coroutine_wrapper;
204+
int in_coroutine_wrapper;
205+
206+
PyObject *async_gen_firstiter;
207+
PyObject *async_gen_finalizer;
208+
209+
PyObject *context;
210+
uint64_t context_ver;
211+
212+
/* Unique thread state id. */
213+
uint64_t id;
214+
215+
/* XXX signal handlers should also be here */
216+
217+
} PyThreadState;
218+
219+
/* Get the current interpreter state.
220+
221+
Issue a fatal error if there no current Python thread state or no current
222+
interpreter. It cannot return NULL.
223+
224+
The caller must hold the GIL.*/
225+
PyAPI_FUNC(PyInterpreterState *) _PyInterpreterState_Get(void);
226+
227+
PyAPI_FUNC(int) _PyState_AddModule(PyObject*, struct PyModuleDef*);
228+
PyAPI_FUNC(void) _PyState_ClearModules(void);
229+
PyAPI_FUNC(PyThreadState *) _PyThreadState_Prealloc(PyInterpreterState *);
230+
PyAPI_FUNC(void) _PyThreadState_Init(PyThreadState *);
231+
PyAPI_FUNC(void) _PyThreadState_DeleteExcept(PyThreadState *tstate);
232+
PyAPI_FUNC(void) _PyGILState_Reinit(void);
233+
234+
/* Similar to PyThreadState_Get(), but don't issue a fatal error
235+
* if it is NULL. */
236+
PyAPI_FUNC(PyThreadState *) _PyThreadState_UncheckedGet(void);
237+
238+
/* PyGILState */
239+
240+
/* Helper/diagnostic function - return 1 if the current thread
241+
currently holds the GIL, 0 otherwise.
242+
243+
The function returns 1 if _PyGILState_check_enabled is non-zero. */
244+
PyAPI_FUNC(int) PyGILState_Check(void);
245+
246+
/* Get the single PyInterpreterState used by this process' GILState
247+
implementation.
248+
249+
This function doesn't check for error. Return NULL before _PyGILState_Init()
250+
is called and after _PyGILState_Fini() is called.
251+
252+
See also _PyInterpreterState_Get() and _PyInterpreterState_GET_UNSAFE(). */
253+
PyAPI_FUNC(PyInterpreterState *) _PyGILState_GetInterpreterStateUnsafe(void);
254+
255+
/* The implementation of sys._current_frames() Returns a dict mapping
256+
thread id to that thread's current frame.
257+
*/
258+
PyAPI_FUNC(PyObject *) _PyThread_CurrentFrames(void);
259+
260+
/* Routines for advanced debuggers, requested by David Beazley.
261+
Don't use unless you know what you are doing! */
262+
PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Main(void);
263+
PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Head(void);
264+
PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Next(PyInterpreterState *);
265+
PyAPI_FUNC(PyThreadState *) PyInterpreterState_ThreadHead(PyInterpreterState *);
266+
PyAPI_FUNC(PyThreadState *) PyThreadState_Next(PyThreadState *);
267+
268+
typedef struct _frame *(*PyThreadFrameGetter)(PyThreadState *self_);
269+
270+
#ifdef __cplusplus
271+
}
272+
#endif

0 commit comments

Comments
 (0)