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 "pycore_atomic.h"    /* _Py_atomic_address */
12 #include "pycore_gil.h"       /* struct _gil_runtime_state  */
13 #include "pycore_gc.h"        /* struct _gc_runtime_state */
14 #include "pycore_warnings.h"  /* struct _warnings_runtime_state */
15 
16 /* ceval state */
17 
18 struct _pending_calls {
19     PyThread_type_lock lock;
20     /* Request for running pending calls. */
21     _Py_atomic_int calls_to_do;
22     /* Request for looking at the `async_exc` field of the current
23        thread state.
24        Guarded by the GIL. */
25     int async_exc;
26 #define NPENDINGCALLS 32
27     struct {
28         int (*func)(void *);
29         void *arg;
30     } calls[NPENDINGCALLS];
31     int first;
32     int last;
33 };
34 
35 struct _ceval_state {
36     int recursion_limit;
37     /* Records whether tracing is on for any thread.  Counts the number
38        of threads for which tstate->c_tracefunc is non-NULL, so if the
39        value is 0, we know we don't have to check this thread's
40        c_tracefunc.  This speeds up the if statement in
41        _PyEval_EvalFrameDefault() after fast_next_opcode. */
42     int tracing_possible;
43     /* This single variable consolidates all requests to break out of
44        the fast path in the eval loop. */
45     _Py_atomic_int eval_breaker;
46     /* Request for dropping the GIL */
47     _Py_atomic_int gil_drop_request;
48     struct _pending_calls pending;
49 };
50 
51 /* fs_codec.encoding is initialized to NULL.
52    Later, it is set to a non-NULL string by _PyUnicode_InitEncodings(). */
53 struct _Py_unicode_fs_codec {
54     char *encoding;   // Filesystem encoding (encoded to UTF-8)
55     int utf8;         // encoding=="utf-8"?
56     char *errors;     // Filesystem errors (encoded to UTF-8)
57     _Py_error_handler error_handler;
58 };
59 
60 struct _Py_unicode_state {
61     struct _Py_unicode_fs_codec fs_codec;
62 };
63 
64 
65 /* interpreter state */
66 
67 #define _PY_NSMALLPOSINTS           257
68 #define _PY_NSMALLNEGINTS           5
69 
70 // The PyInterpreterState typedef is in Include/pystate.h.
71 struct _is {
72 
73     struct _is *next;
74     struct _ts *tstate_head;
75 
76     /* Reference to the _PyRuntime global variable. This field exists
77        to not have to pass runtime in addition to tstate to a function.
78        Get runtime from tstate: tstate->interp->runtime. */
79     struct pyruntimestate *runtime;
80 
81     int64_t id;
82     int64_t id_refcount;
83     int requires_idref;
84     PyThread_type_lock id_mutex;
85 
86     int finalizing;
87 
88     struct _ceval_state ceval;
89     struct _gc_runtime_state gc;
90 
91     PyObject *modules;
92     PyObject *modules_by_index;
93     PyObject *sysdict;
94     PyObject *builtins;
95     PyObject *importlib;
96 
97     /* Used in Modules/_threadmodule.c. */
98     long num_threads;
99     /* Support for runtime thread stack size tuning.
100        A value of 0 means using the platform's default stack size
101        or the size specified by the THREAD_STACK_SIZE macro. */
102     /* Used in Python/thread.c. */
103     size_t pythread_stacksize;
104 
105     PyObject *codec_search_path;
106     PyObject *codec_search_cache;
107     PyObject *codec_error_registry;
108     int codecs_initialized;
109 
110     struct _Py_unicode_state unicode;
111 
112     PyConfig config;
113 #ifdef HAVE_DLOPEN
114     int dlopenflags;
115 #endif
116 
117     PyObject *dict;  /* Stores per-interpreter state */
118 
119     PyObject *builtins_copy;
120     PyObject *import_func;
121     /* Initialized to PyEval_EvalFrameDefault(). */
122     _PyFrameEvalFunction eval_frame;
123 
124     Py_ssize_t co_extra_user_count;
125     freefunc co_extra_freefuncs[MAX_CO_EXTRA_USERS];
126 
127 #ifdef HAVE_FORK
128     PyObject *before_forkers;
129     PyObject *after_forkers_parent;
130     PyObject *after_forkers_child;
131 #endif
132     /* AtExit module */
133     void (*pyexitfunc)(PyObject *);
134     PyObject *pyexitmodule;
135 
136     uint64_t tstate_next_unique_id;
137 
138     struct _warnings_runtime_state warnings;
139 
140     PyObject *audit_hooks;
141 
142     struct {
143         struct {
144             int level;
145             int atbol;
146         } listnode;
147     } parser;
148 
149 #if _PY_NSMALLNEGINTS + _PY_NSMALLPOSINTS > 0
150     /* Small integers are preallocated in this array so that they
151        can be shared.
152        The integers that are preallocated are those in the range
153        -_PY_NSMALLNEGINTS (inclusive) to _PY_NSMALLPOSINTS (not inclusive).
154     */
155     PyLongObject* small_ints[_PY_NSMALLNEGINTS + _PY_NSMALLPOSINTS];
156 #endif
157 };
158 
159 /* Used by _PyImport_Cleanup() */
160 extern void _PyInterpreterState_ClearModules(PyInterpreterState *interp);
161 
162 extern PyStatus _PyInterpreterState_SetConfig(
163     PyInterpreterState *interp,
164     const PyConfig *config);
165 
166 
167 
168 /* cross-interpreter data registry */
169 
170 /* For now we use a global registry of shareable classes.  An
171    alternative would be to add a tp_* slot for a class's
172    crossinterpdatafunc. It would be simpler and more efficient. */
173 
174 struct _xidregitem;
175 
176 struct _xidregitem {
177     PyTypeObject *cls;
178     crossinterpdatafunc getdata;
179     struct _xidregitem *next;
180 };
181 
182 PyAPI_FUNC(struct _is*) _PyInterpreterState_LookUpID(int64_t);
183 
184 PyAPI_FUNC(int) _PyInterpreterState_IDInitref(struct _is *);
185 PyAPI_FUNC(void) _PyInterpreterState_IDIncref(struct _is *);
186 PyAPI_FUNC(void) _PyInterpreterState_IDDecref(struct _is *);
187 
188 #ifdef __cplusplus
189 }
190 #endif
191 #endif /* !Py_INTERNAL_INTERP_H */
192 
193