Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
allendowney
GitHub Repository: allendowney/cpython
Path: blob/main/Include/internal/pycore_interp.h
12 views
1
#ifndef Py_INTERNAL_INTERP_H
2
#define Py_INTERNAL_INTERP_H
3
#ifdef __cplusplus
4
extern "C" {
5
#endif
6
7
#ifndef Py_BUILD_CORE
8
# error "this header requires Py_BUILD_CORE define"
9
#endif
10
11
#include <stdbool.h>
12
13
#include "pycore_ast_state.h" // struct ast_state
14
#include "pycore_atexit.h" // struct atexit_state
15
#include "pycore_atomic.h" // _Py_atomic_address
16
#include "pycore_ceval_state.h" // struct _ceval_state
17
#include "pycore_code.h" // struct callable_cache
18
#include "pycore_context.h" // struct _Py_context_state
19
#include "pycore_dict_state.h" // struct _Py_dict_state
20
#include "pycore_dtoa.h" // struct _dtoa_state
21
#include "pycore_exceptions.h" // struct _Py_exc_state
22
#include "pycore_floatobject.h" // struct _Py_float_state
23
#include "pycore_function.h" // FUNC_MAX_WATCHERS
24
#include "pycore_genobject.h" // struct _Py_async_gen_state
25
#include "pycore_gc.h" // struct _gc_runtime_state
26
#include "pycore_global_objects.h" // struct _Py_interp_static_objects
27
#include "pycore_import.h" // struct _import_state
28
#include "pycore_instruments.h" // PY_MONITORING_EVENTS
29
#include "pycore_list.h" // struct _Py_list_state
30
#include "pycore_object_state.h" // struct _py_object_state
31
#include "pycore_obmalloc.h" // struct obmalloc_state
32
#include "pycore_tuple.h" // struct _Py_tuple_state
33
#include "pycore_typeobject.h" // struct type_cache
34
#include "pycore_unicodeobject.h" // struct _Py_unicode_state
35
#include "pycore_warnings.h" // struct _warnings_runtime_state
36
37
38
struct _Py_long_state {
39
int max_str_digits;
40
};
41
42
/* interpreter state */
43
44
/* PyInterpreterState holds the global state for one of the runtime's
45
interpreters. Typically the initial (main) interpreter is the only one.
46
47
The PyInterpreterState typedef is in Include/pytypedefs.h.
48
*/
49
struct _is {
50
51
PyInterpreterState *next;
52
53
int64_t id;
54
int64_t id_refcount;
55
int requires_idref;
56
PyThread_type_lock id_mutex;
57
58
/* Has been initialized to a safe state.
59
60
In order to be effective, this must be set to 0 during or right
61
after allocation. */
62
int _initialized;
63
int finalizing;
64
65
uint64_t monitoring_version;
66
uint64_t last_restart_version;
67
struct pythreads {
68
uint64_t next_unique_id;
69
/* The linked list of threads, newest first. */
70
PyThreadState *head;
71
/* Used in Modules/_threadmodule.c. */
72
long count;
73
/* Support for runtime thread stack size tuning.
74
A value of 0 means using the platform's default stack size
75
or the size specified by the THREAD_STACK_SIZE macro. */
76
/* Used in Python/thread.c. */
77
size_t stacksize;
78
} threads;
79
80
/* Reference to the _PyRuntime global variable. This field exists
81
to not have to pass runtime in addition to tstate to a function.
82
Get runtime from tstate: tstate->interp->runtime. */
83
struct pyruntimestate *runtime;
84
85
/* Set by Py_EndInterpreter().
86
87
Use _PyInterpreterState_GetFinalizing()
88
and _PyInterpreterState_SetFinalizing()
89
to access it, don't access it directly. */
90
_Py_atomic_address _finalizing;
91
92
struct _gc_runtime_state gc;
93
94
/* The following fields are here to avoid allocation during init.
95
The data is exposed through PyInterpreterState pointer fields.
96
These fields should not be accessed directly outside of init.
97
98
All other PyInterpreterState pointer fields are populated when
99
needed and default to NULL.
100
101
For now there are some exceptions to that rule, which require
102
allocation during init. These will be addressed on a case-by-case
103
basis. Also see _PyRuntimeState regarding the various mutex fields.
104
*/
105
106
// Dictionary of the sys module
107
PyObject *sysdict;
108
109
// Dictionary of the builtins module
110
PyObject *builtins;
111
112
struct _ceval_state ceval;
113
114
struct _import_state imports;
115
116
/* The per-interpreter GIL, which might not be used. */
117
struct _gil_runtime_state _gil;
118
119
/* ---------- IMPORTANT ---------------------------
120
The fields above this line are declared as early as
121
possible to facilitate out-of-process observability
122
tools. */
123
124
PyObject *codec_search_path;
125
PyObject *codec_search_cache;
126
PyObject *codec_error_registry;
127
int codecs_initialized;
128
129
PyConfig config;
130
unsigned long feature_flags;
131
132
PyObject *dict; /* Stores per-interpreter state */
133
134
PyObject *sysdict_copy;
135
PyObject *builtins_copy;
136
// Initialized to _PyEval_EvalFrameDefault().
137
_PyFrameEvalFunction eval_frame;
138
139
PyFunction_WatchCallback func_watchers[FUNC_MAX_WATCHERS];
140
// One bit is set for each non-NULL entry in func_watchers
141
uint8_t active_func_watchers;
142
143
Py_ssize_t co_extra_user_count;
144
freefunc co_extra_freefuncs[MAX_CO_EXTRA_USERS];
145
146
#ifdef HAVE_FORK
147
PyObject *before_forkers;
148
PyObject *after_forkers_parent;
149
PyObject *after_forkers_child;
150
#endif
151
152
struct _warnings_runtime_state warnings;
153
struct atexit_state atexit;
154
155
struct _obmalloc_state obmalloc;
156
157
PyObject *audit_hooks;
158
PyType_WatchCallback type_watchers[TYPE_MAX_WATCHERS];
159
PyCode_WatchCallback code_watchers[CODE_MAX_WATCHERS];
160
// One bit is set for each non-NULL entry in code_watchers
161
uint8_t active_code_watchers;
162
163
struct _py_object_state object_state;
164
struct _Py_unicode_state unicode;
165
struct _Py_float_state float_state;
166
struct _Py_long_state long_state;
167
struct _dtoa_state dtoa;
168
struct _py_func_state func_state;
169
/* Using a cache is very effective since typically only a single slice is
170
created and then deleted again. */
171
PySliceObject *slice_cache;
172
173
struct _Py_tuple_state tuple;
174
struct _Py_list_state list;
175
struct _Py_dict_state dict_state;
176
struct _Py_async_gen_state async_gen;
177
struct _Py_context_state context;
178
struct _Py_exc_state exc_state;
179
180
struct ast_state ast;
181
struct types_state types;
182
struct callable_cache callable_cache;
183
_PyOptimizerObject *optimizer;
184
uint16_t optimizer_resume_threshold;
185
uint16_t optimizer_backedge_threshold;
186
187
_Py_Monitors monitors;
188
bool f_opcode_trace_set;
189
bool sys_profile_initialized;
190
bool sys_trace_initialized;
191
Py_ssize_t sys_profiling_threads; /* Count of threads with c_profilefunc set */
192
Py_ssize_t sys_tracing_threads; /* Count of threads with c_tracefunc set */
193
PyObject *monitoring_callables[PY_MONITORING_TOOL_IDS][PY_MONITORING_EVENTS];
194
PyObject *monitoring_tool_names[PY_MONITORING_TOOL_IDS];
195
196
struct _Py_interp_cached_objects cached_objects;
197
struct _Py_interp_static_objects static_objects;
198
199
/* the initial PyInterpreterState.threads.head */
200
PyThreadState _initial_thread;
201
};
202
203
204
/* other API */
205
206
extern void _PyInterpreterState_Clear(PyThreadState *tstate);
207
208
209
static inline PyThreadState*
210
_PyInterpreterState_GetFinalizing(PyInterpreterState *interp) {
211
return (PyThreadState*)_Py_atomic_load_relaxed(&interp->_finalizing);
212
}
213
214
static inline void
215
_PyInterpreterState_SetFinalizing(PyInterpreterState *interp, PyThreadState *tstate) {
216
_Py_atomic_store_relaxed(&interp->_finalizing, (uintptr_t)tstate);
217
}
218
219
220
/* cross-interpreter data registry */
221
222
/* For now we use a global registry of shareable classes. An
223
alternative would be to add a tp_* slot for a class's
224
crossinterpdatafunc. It would be simpler and more efficient. */
225
226
struct _xidregitem;
227
228
struct _xidregitem {
229
struct _xidregitem *prev;
230
struct _xidregitem *next;
231
PyObject *cls; // weakref to a PyTypeObject
232
crossinterpdatafunc getdata;
233
};
234
235
PyAPI_FUNC(PyInterpreterState*) _PyInterpreterState_LookUpID(int64_t);
236
237
PyAPI_FUNC(int) _PyInterpreterState_IDInitref(PyInterpreterState *);
238
PyAPI_FUNC(int) _PyInterpreterState_IDIncref(PyInterpreterState *);
239
PyAPI_FUNC(void) _PyInterpreterState_IDDecref(PyInterpreterState *);
240
241
PyAPI_FUNC(PyObject*) _PyInterpreterState_GetMainModule(PyInterpreterState *);
242
243
extern const PyConfig* _PyInterpreterState_GetConfig(PyInterpreterState *interp);
244
245
/* Get a copy of the current interpreter configuration.
246
247
Return 0 on success. Raise an exception and return -1 on error.
248
249
The caller must initialize 'config', using PyConfig_InitPythonConfig()
250
for example.
251
252
Python must be preinitialized to call this method.
253
The caller must hold the GIL.
254
255
Once done with the configuration, PyConfig_Clear() must be called to clear
256
it. */
257
PyAPI_FUNC(int) _PyInterpreterState_GetConfigCopy(
258
struct PyConfig *config);
259
260
/* Set the configuration of the current interpreter.
261
262
This function should be called during or just after the Python
263
initialization.
264
265
Update the sys module with the new configuration. If the sys module was
266
modified directly after the Python initialization, these changes are lost.
267
268
Some configuration like faulthandler or warnoptions can be updated in the
269
configuration, but don't reconfigure Python (don't enable/disable
270
faulthandler and don't reconfigure warnings filters).
271
272
Return 0 on success. Raise an exception and return -1 on error.
273
274
The configuration should come from _PyInterpreterState_GetConfigCopy(). */
275
PyAPI_FUNC(int) _PyInterpreterState_SetConfig(
276
const struct PyConfig *config);
277
278
#ifdef __cplusplus
279
}
280
#endif
281
#endif /* !Py_INTERNAL_INTERP_H */
282
283