#ifndef Py_INTERNAL_INTERP_H1#define Py_INTERNAL_INTERP_H2#ifdef __cplusplus3extern "C" {4#endif56#ifndef Py_BUILD_CORE7# error "this header requires Py_BUILD_CORE define"8#endif910#include <stdbool.h>1112#include "pycore_ast_state.h" // struct ast_state13#include "pycore_atexit.h" // struct atexit_state14#include "pycore_atomic.h" // _Py_atomic_address15#include "pycore_ceval_state.h" // struct _ceval_state16#include "pycore_code.h" // struct callable_cache17#include "pycore_context.h" // struct _Py_context_state18#include "pycore_dict_state.h" // struct _Py_dict_state19#include "pycore_dtoa.h" // struct _dtoa_state20#include "pycore_exceptions.h" // struct _Py_exc_state21#include "pycore_floatobject.h" // struct _Py_float_state22#include "pycore_function.h" // FUNC_MAX_WATCHERS23#include "pycore_genobject.h" // struct _Py_async_gen_state24#include "pycore_gc.h" // struct _gc_runtime_state25#include "pycore_global_objects.h" // struct _Py_interp_static_objects26#include "pycore_import.h" // struct _import_state27#include "pycore_instruments.h" // PY_MONITORING_EVENTS28#include "pycore_list.h" // struct _Py_list_state29#include "pycore_object_state.h" // struct _py_object_state30#include "pycore_obmalloc.h" // struct obmalloc_state31#include "pycore_tuple.h" // struct _Py_tuple_state32#include "pycore_typeobject.h" // struct type_cache33#include "pycore_unicodeobject.h" // struct _Py_unicode_state34#include "pycore_warnings.h" // struct _warnings_runtime_state353637struct _Py_long_state {38int max_str_digits;39};4041/* interpreter state */4243/* PyInterpreterState holds the global state for one of the runtime's44interpreters. Typically the initial (main) interpreter is the only one.4546The PyInterpreterState typedef is in Include/pytypedefs.h.47*/48struct _is {4950PyInterpreterState *next;5152int64_t id;53int64_t id_refcount;54int requires_idref;55PyThread_type_lock id_mutex;5657/* Has been initialized to a safe state.5859In order to be effective, this must be set to 0 during or right60after allocation. */61int _initialized;62int finalizing;6364uint64_t monitoring_version;65uint64_t last_restart_version;66struct pythreads {67uint64_t next_unique_id;68/* The linked list of threads, newest first. */69PyThreadState *head;70/* Used in Modules/_threadmodule.c. */71long count;72/* Support for runtime thread stack size tuning.73A value of 0 means using the platform's default stack size74or the size specified by the THREAD_STACK_SIZE macro. */75/* Used in Python/thread.c. */76size_t stacksize;77} threads;7879/* Reference to the _PyRuntime global variable. This field exists80to not have to pass runtime in addition to tstate to a function.81Get runtime from tstate: tstate->interp->runtime. */82struct pyruntimestate *runtime;8384/* Set by Py_EndInterpreter().8586Use _PyInterpreterState_GetFinalizing()87and _PyInterpreterState_SetFinalizing()88to access it, don't access it directly. */89_Py_atomic_address _finalizing;9091struct _gc_runtime_state gc;9293/* The following fields are here to avoid allocation during init.94The data is exposed through PyInterpreterState pointer fields.95These fields should not be accessed directly outside of init.9697All other PyInterpreterState pointer fields are populated when98needed and default to NULL.99100For now there are some exceptions to that rule, which require101allocation during init. These will be addressed on a case-by-case102basis. Also see _PyRuntimeState regarding the various mutex fields.103*/104105// Dictionary of the sys module106PyObject *sysdict;107108// Dictionary of the builtins module109PyObject *builtins;110111struct _ceval_state ceval;112113struct _import_state imports;114115/* The per-interpreter GIL, which might not be used. */116struct _gil_runtime_state _gil;117118/* ---------- IMPORTANT ---------------------------119The fields above this line are declared as early as120possible to facilitate out-of-process observability121tools. */122123PyObject *codec_search_path;124PyObject *codec_search_cache;125PyObject *codec_error_registry;126int codecs_initialized;127128PyConfig config;129unsigned long feature_flags;130131PyObject *dict; /* Stores per-interpreter state */132133PyObject *sysdict_copy;134PyObject *builtins_copy;135// Initialized to _PyEval_EvalFrameDefault().136_PyFrameEvalFunction eval_frame;137138PyFunction_WatchCallback func_watchers[FUNC_MAX_WATCHERS];139// One bit is set for each non-NULL entry in func_watchers140uint8_t active_func_watchers;141142Py_ssize_t co_extra_user_count;143freefunc co_extra_freefuncs[MAX_CO_EXTRA_USERS];144145#ifdef HAVE_FORK146PyObject *before_forkers;147PyObject *after_forkers_parent;148PyObject *after_forkers_child;149#endif150151struct _warnings_runtime_state warnings;152struct atexit_state atexit;153154struct _obmalloc_state obmalloc;155156PyObject *audit_hooks;157PyType_WatchCallback type_watchers[TYPE_MAX_WATCHERS];158PyCode_WatchCallback code_watchers[CODE_MAX_WATCHERS];159// One bit is set for each non-NULL entry in code_watchers160uint8_t active_code_watchers;161162struct _py_object_state object_state;163struct _Py_unicode_state unicode;164struct _Py_float_state float_state;165struct _Py_long_state long_state;166struct _dtoa_state dtoa;167struct _py_func_state func_state;168/* Using a cache is very effective since typically only a single slice is169created and then deleted again. */170PySliceObject *slice_cache;171172struct _Py_tuple_state tuple;173struct _Py_list_state list;174struct _Py_dict_state dict_state;175struct _Py_async_gen_state async_gen;176struct _Py_context_state context;177struct _Py_exc_state exc_state;178179struct ast_state ast;180struct types_state types;181struct callable_cache callable_cache;182_PyOptimizerObject *optimizer;183uint16_t optimizer_resume_threshold;184uint16_t optimizer_backedge_threshold;185186_Py_Monitors monitors;187bool f_opcode_trace_set;188bool sys_profile_initialized;189bool sys_trace_initialized;190Py_ssize_t sys_profiling_threads; /* Count of threads with c_profilefunc set */191Py_ssize_t sys_tracing_threads; /* Count of threads with c_tracefunc set */192PyObject *monitoring_callables[PY_MONITORING_TOOL_IDS][PY_MONITORING_EVENTS];193PyObject *monitoring_tool_names[PY_MONITORING_TOOL_IDS];194195struct _Py_interp_cached_objects cached_objects;196struct _Py_interp_static_objects static_objects;197198/* the initial PyInterpreterState.threads.head */199PyThreadState _initial_thread;200};201202203/* other API */204205extern void _PyInterpreterState_Clear(PyThreadState *tstate);206207208static inline PyThreadState*209_PyInterpreterState_GetFinalizing(PyInterpreterState *interp) {210return (PyThreadState*)_Py_atomic_load_relaxed(&interp->_finalizing);211}212213static inline void214_PyInterpreterState_SetFinalizing(PyInterpreterState *interp, PyThreadState *tstate) {215_Py_atomic_store_relaxed(&interp->_finalizing, (uintptr_t)tstate);216}217218219/* cross-interpreter data registry */220221/* For now we use a global registry of shareable classes. An222alternative would be to add a tp_* slot for a class's223crossinterpdatafunc. It would be simpler and more efficient. */224225struct _xidregitem;226227struct _xidregitem {228struct _xidregitem *prev;229struct _xidregitem *next;230PyObject *cls; // weakref to a PyTypeObject231crossinterpdatafunc getdata;232};233234PyAPI_FUNC(PyInterpreterState*) _PyInterpreterState_LookUpID(int64_t);235236PyAPI_FUNC(int) _PyInterpreterState_IDInitref(PyInterpreterState *);237PyAPI_FUNC(int) _PyInterpreterState_IDIncref(PyInterpreterState *);238PyAPI_FUNC(void) _PyInterpreterState_IDDecref(PyInterpreterState *);239240PyAPI_FUNC(PyObject*) _PyInterpreterState_GetMainModule(PyInterpreterState *);241242extern const PyConfig* _PyInterpreterState_GetConfig(PyInterpreterState *interp);243244/* Get a copy of the current interpreter configuration.245246Return 0 on success. Raise an exception and return -1 on error.247248The caller must initialize 'config', using PyConfig_InitPythonConfig()249for example.250251Python must be preinitialized to call this method.252The caller must hold the GIL.253254Once done with the configuration, PyConfig_Clear() must be called to clear255it. */256PyAPI_FUNC(int) _PyInterpreterState_GetConfigCopy(257struct PyConfig *config);258259/* Set the configuration of the current interpreter.260261This function should be called during or just after the Python262initialization.263264Update the sys module with the new configuration. If the sys module was265modified directly after the Python initialization, these changes are lost.266267Some configuration like faulthandler or warnoptions can be updated in the268configuration, but don't reconfigure Python (don't enable/disable269faulthandler and don't reconfigure warnings filters).270271Return 0 on success. Raise an exception and return -1 on error.272273The configuration should come from _PyInterpreterState_GetConfigCopy(). */274PyAPI_FUNC(int) _PyInterpreterState_SetConfig(275const struct PyConfig *config);276277#ifdef __cplusplus278}279#endif280#endif /* !Py_INTERNAL_INTERP_H */281282283