blob: 81bcf68219a0e251f8ffa93ed2061e3fbeb3513c [file] [log] [blame]
Guido van Rossuma027efa1997-05-05 20:56:21 +00001
2/* Thread and interpreter state structures and their interfaces */
3
4#include "Python.h"
Victor Stinner09532fe2019-05-10 23:39:09 +02005#include "pycore_ceval.h"
Victor Stinner331a6a52019-05-27 16:39:22 +02006#include "pycore_initconfig.h"
Victor Stinner41010182020-12-26 01:45:43 +01007#include "pycore_object.h" // _PyType_InitCache()
Victor Stinner0e427c62020-03-25 21:22:55 +01008#include "pycore_pyerrors.h"
9#include "pycore_pylifecycle.h"
Victor Stinnerd9ea5ca2020-04-15 02:57:50 +020010#include "pycore_pymem.h" // _PyMem_SetDefaultAllocator()
Victor Stinnere5014be2020-04-14 17:52:15 +020011#include "pycore_pystate.h" // _PyThreadState_GET()
Victor Stinner71a35222020-03-26 22:46:14 +010012#include "pycore_sysmodule.h"
Guido van Rossuma027efa1997-05-05 20:56:21 +000013
Tim Peters84705582004-10-10 02:47:33 +000014/* --------------------------------------------------------------------------
15CAUTION
16
Victor Stinner1a7425f2013-07-07 16:25:15 +020017Always use PyMem_RawMalloc() and PyMem_RawFree() directly in this file. A
18number of these functions are advertised as safe to call when the GIL isn't
19held, and in a debug build Python redirects (e.g.) PyMem_NEW (etc) to Python's
20debugging obmalloc functions. Those aren't thread-safe (they rely on the GIL
21to avoid the expense of doing their own locking).
Tim Peters84705582004-10-10 02:47:33 +000022-------------------------------------------------------------------------- */
23
Martin v. Löwisf0473d52001-07-18 16:17:16 +000024#ifdef HAVE_DLOPEN
25#ifdef HAVE_DLFCN_H
26#include <dlfcn.h>
27#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030028#if !HAVE_DECL_RTLD_LAZY
Martin v. Löwisf0473d52001-07-18 16:17:16 +000029#define RTLD_LAZY 1
30#endif
31#endif
32
Benjamin Peterson43162b82012-04-13 11:58:27 -040033#ifdef __cplusplus
34extern "C" {
35#endif
Martin v. Löwisf0473d52001-07-18 16:17:16 +000036
Victor Stinner10c8e6a2019-04-26 01:53:18 +020037#define _PyRuntimeGILState_GetThreadState(gilstate) \
38 ((PyThreadState*)_Py_atomic_load_relaxed(&(gilstate)->tstate_current))
39#define _PyRuntimeGILState_SetThreadState(gilstate, value) \
40 _Py_atomic_store_relaxed(&(gilstate)->tstate_current, \
41 (uintptr_t)(value))
42
43/* Forward declarations */
44static PyThreadState *_PyGILState_GetThisThreadState(struct _gilstate_runtime_state *gilstate);
Victor Stinner9da74302019-11-20 11:17:17 +010045static void _PyThreadState_Delete(PyThreadState *tstate, int check_current);
Victor Stinner10c8e6a2019-04-26 01:53:18 +020046
47
Victor Stinner331a6a52019-05-27 16:39:22 +020048static PyStatus
Victor Stinner5d39e042017-11-29 17:20:38 +010049_PyRuntimeState_Init_impl(_PyRuntimeState *runtime)
Eric Snow2ebc5ce2017-09-07 23:51:28 -060050{
Steve Dowerb82e17e2019-05-23 08:45:22 -070051 /* We preserve the hook across init, because there is
52 currently no public API to set it between runtime
53 initialization and interpreter initialization. */
54 void *open_code_hook = runtime->open_code_hook;
55 void *open_code_userdata = runtime->open_code_userdata;
56 _Py_AuditHookEntry *audit_hook_head = runtime->audit_hook_head;
Victor Stinner44bf57a2021-01-12 10:29:45 +010057 // bpo-42882: Preserve next_index value if Py_Initialize()/Py_Finalize()
58 // is called multiple times.
Ken Jin196d4de2021-02-05 06:08:03 +080059 Py_ssize_t unicode_next_index = runtime->unicode_ids.next_index;
Steve Dowerb82e17e2019-05-23 08:45:22 -070060
Eric Snow2ebc5ce2017-09-07 23:51:28 -060061 memset(runtime, 0, sizeof(*runtime));
Victor Stinner8a1be612016-03-14 22:07:55 +010062
Steve Dowerb82e17e2019-05-23 08:45:22 -070063 runtime->open_code_hook = open_code_hook;
64 runtime->open_code_userdata = open_code_userdata;
65 runtime->audit_hook_head = audit_hook_head;
66
Victor Stinnerdab84232020-03-17 18:56:44 +010067 _PyEval_InitRuntimeState(&runtime->ceval);
Victor Stinner441b10c2019-09-28 04:28:35 +020068
Victor Stinner3c30a762019-10-01 10:56:37 +020069 PyPreConfig_InitPythonConfig(&runtime->preconfig);
Michael W. Hudson188d4362005-06-20 16:52:57 +000070
Eric Snow2ebc5ce2017-09-07 23:51:28 -060071 runtime->gilstate.check_enabled = 1;
Victor Stinnerf7e5b562017-11-15 15:48:08 -080072
Masayuki Yamamoto731e1892017-10-06 19:41:34 +090073 /* A TSS key must be initialized with Py_tss_NEEDS_INIT
74 in accordance with the specification. */
Victor Stinnerf7e5b562017-11-15 15:48:08 -080075 Py_tss_t initial = Py_tss_NEEDS_INIT;
76 runtime->gilstate.autoTSSkey = initial;
Guido van Rossum1d5ad901999-06-18 14:22:24 +000077
Eric Snow2ebc5ce2017-09-07 23:51:28 -060078 runtime->interpreters.mutex = PyThread_allocate_lock();
Victor Stinnerf7e5b562017-11-15 15:48:08 -080079 if (runtime->interpreters.mutex == NULL) {
Victor Stinnerba3d67c2020-12-26 00:41:46 +010080 return _PyStatus_NO_MEMORY();
Victor Stinnerf7e5b562017-11-15 15:48:08 -080081 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -060082 runtime->interpreters.next_id = -1;
Eric Snow7f8bfc92018-01-29 18:23:44 -070083
84 runtime->xidregistry.mutex = PyThread_allocate_lock();
85 if (runtime->xidregistry.mutex == NULL) {
Victor Stinnerba3d67c2020-12-26 00:41:46 +010086 return _PyStatus_NO_MEMORY();
Eric Snow7f8bfc92018-01-29 18:23:44 -070087 }
88
Eric Snow8479a342019-03-08 23:44:33 -070089 // Set it to the ID of the main thread of the main interpreter.
90 runtime->main_thread = PyThread_get_thread_ident();
Eric Snow5be45a62019-03-08 22:47:07 -070091
Victor Stinnerba3d67c2020-12-26 00:41:46 +010092 runtime->unicode_ids.lock = PyThread_allocate_lock();
93 if (runtime->unicode_ids.lock == NULL) {
94 return _PyStatus_NO_MEMORY();
95 }
Victor Stinner44bf57a2021-01-12 10:29:45 +010096 runtime->unicode_ids.next_index = unicode_next_index;
Victor Stinnerba3d67c2020-12-26 00:41:46 +010097
Victor Stinner331a6a52019-05-27 16:39:22 +020098 return _PyStatus_OK();
Eric Snow2ebc5ce2017-09-07 23:51:28 -060099}
Eric Snow05351c12017-09-05 21:43:08 -0700100
Victor Stinner331a6a52019-05-27 16:39:22 +0200101PyStatus
Victor Stinner5d39e042017-11-29 17:20:38 +0100102_PyRuntimeState_Init(_PyRuntimeState *runtime)
103{
104 /* Force default allocator, since _PyRuntimeState_Fini() must
105 use the same allocator than this function. */
106 PyMemAllocatorEx old_alloc;
107 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
108
Victor Stinner331a6a52019-05-27 16:39:22 +0200109 PyStatus status = _PyRuntimeState_Init_impl(runtime);
Victor Stinner5d39e042017-11-29 17:20:38 +0100110
111 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinner331a6a52019-05-27 16:39:22 +0200112 return status;
Victor Stinner5d39e042017-11-29 17:20:38 +0100113}
114
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600115void
116_PyRuntimeState_Fini(_PyRuntimeState *runtime)
117{
Victor Stinner5d39e042017-11-29 17:20:38 +0100118 /* Force the allocator used by _PyRuntimeState_Init(). */
119 PyMemAllocatorEx old_alloc;
120 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerba3d67c2020-12-26 00:41:46 +0100121#define FREE_LOCK(LOCK) \
122 if (LOCK != NULL) { \
123 PyThread_free_lock(LOCK); \
124 LOCK = NULL; \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600125 }
Victor Stinnerccb04422017-11-16 03:20:31 -0800126
Victor Stinnerba3d67c2020-12-26 00:41:46 +0100127 FREE_LOCK(runtime->interpreters.mutex);
128 FREE_LOCK(runtime->xidregistry.mutex);
129 FREE_LOCK(runtime->unicode_ids.lock);
Stéphane Wirtel943395f2019-03-19 11:51:32 +0100130
Victor Stinnerba3d67c2020-12-26 00:41:46 +0100131#undef FREE_LOCK
Victor Stinnerccb04422017-11-16 03:20:31 -0800132 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600133}
134
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900135#ifdef HAVE_FORK
Eric Snow8479a342019-03-08 23:44:33 -0700136/* This function is called from PyOS_AfterFork_Child to ensure that
Victor Stinner26881c82020-06-02 15:51:37 +0200137 newly created child processes do not share locks with the parent. */
138PyStatus
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200139_PyRuntimeState_ReInitThreads(_PyRuntimeState *runtime)
Eric Snow8479a342019-03-08 23:44:33 -0700140{
141 // This was initially set in _PyRuntimeState_Init().
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200142 runtime->main_thread = PyThread_get_thread_ident();
Eric Snow8479a342019-03-08 23:44:33 -0700143
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200144 /* Force default allocator, since _PyRuntimeState_Fini() must
145 use the same allocator than this function. */
146 PyMemAllocatorEx old_alloc;
147 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
148
Victor Stinner26881c82020-06-02 15:51:37 +0200149 int reinit_interp = _PyThread_at_fork_reinit(&runtime->interpreters.mutex);
150 int reinit_main_id = _PyThread_at_fork_reinit(&runtime->interpreters.main->id_mutex);
151 int reinit_xidregistry = _PyThread_at_fork_reinit(&runtime->xidregistry.mutex);
Victor Stinnerba3d67c2020-12-26 00:41:46 +0100152 int reinit_unicode_ids = _PyThread_at_fork_reinit(&runtime->unicode_ids.lock);
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200153
154 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
155
Victor Stinner26881c82020-06-02 15:51:37 +0200156 if (reinit_interp < 0
157 || reinit_main_id < 0
Victor Stinnerba3d67c2020-12-26 00:41:46 +0100158 || reinit_xidregistry < 0
159 || reinit_unicode_ids < 0)
Victor Stinner26881c82020-06-02 15:51:37 +0200160 {
161 return _PyStatus_ERR("Failed to reinitialize runtime locks");
Eric Snow8479a342019-03-08 23:44:33 -0700162
Eric Snow8479a342019-03-08 23:44:33 -0700163 }
Victor Stinner26881c82020-06-02 15:51:37 +0200164 return _PyStatus_OK();
Eric Snow8479a342019-03-08 23:44:33 -0700165}
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900166#endif
Eric Snow8479a342019-03-08 23:44:33 -0700167
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200168#define HEAD_LOCK(runtime) \
169 PyThread_acquire_lock((runtime)->interpreters.mutex, WAIT_LOCK)
170#define HEAD_UNLOCK(runtime) \
171 PyThread_release_lock((runtime)->interpreters.mutex)
Eric Snow05351c12017-09-05 21:43:08 -0700172
Victor Stinner8bb32302019-04-24 16:47:40 +0200173/* Forward declaration */
174static void _PyGILState_NoteThreadState(
175 struct _gilstate_runtime_state *gilstate, PyThreadState* tstate);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000176
Victor Stinner331a6a52019-05-27 16:39:22 +0200177PyStatus
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600178_PyInterpreterState_Enable(_PyRuntimeState *runtime)
Eric Snowe3774162017-05-22 19:46:40 -0700179{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200180 struct pyinterpreters *interpreters = &runtime->interpreters;
181 interpreters->next_id = 0;
Victor Stinner5d926472018-03-06 14:31:37 +0100182
183 /* Py_Finalize() calls _PyRuntimeState_Fini() which clears the mutex.
184 Create a new mutex if needed. */
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200185 if (interpreters->mutex == NULL) {
Victor Stinner5d926472018-03-06 14:31:37 +0100186 /* Force default allocator, since _PyRuntimeState_Fini() must
187 use the same allocator than this function. */
188 PyMemAllocatorEx old_alloc;
189 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
190
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200191 interpreters->mutex = PyThread_allocate_lock();
Victor Stinner5d926472018-03-06 14:31:37 +0100192
193 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
194
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200195 if (interpreters->mutex == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200196 return _PyStatus_ERR("Can't initialize threads for interpreter");
Victor Stinnera7368ac2017-11-15 18:11:45 -0800197 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600198 }
Victor Stinner5d926472018-03-06 14:31:37 +0100199
Victor Stinner331a6a52019-05-27 16:39:22 +0200200 return _PyStatus_OK();
Eric Snowe3774162017-05-22 19:46:40 -0700201}
Guido van Rossuma027efa1997-05-05 20:56:21 +0000202
203PyInterpreterState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000204PyInterpreterState_New(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000205{
Victor Stinner71a35222020-03-26 22:46:14 +0100206 PyThreadState *tstate = _PyThreadState_GET();
207 /* tstate is NULL when Py_InitializeFromConfig() calls
208 PyInterpreterState_New() to create the main interpreter. */
209 if (_PySys_Audit(tstate, "cpython.PyInterpreterState_New", NULL) < 0) {
Steve Dowerb82e17e2019-05-23 08:45:22 -0700210 return NULL;
211 }
212
Andy Lester7668a8b2020-03-24 23:26:44 -0500213 PyInterpreterState *interp = PyMem_RawCalloc(1, sizeof(PyInterpreterState));
Victor Stinnerd4341102017-11-23 00:12:09 +0100214 if (interp == NULL) {
215 return NULL;
216 }
217
Eric Snow4c6955e2018-02-16 18:53:40 -0700218 interp->id_refcount = -1;
Victor Stinner022be022019-05-22 23:58:50 +0200219
Victor Stinner71a35222020-03-26 22:46:14 +0100220 /* Don't get runtime from tstate since tstate can be NULL */
Victor Stinner01b1cc12019-11-20 02:27:56 +0100221 _PyRuntimeState *runtime = &_PyRuntime;
222 interp->runtime = runtime;
223
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200224 if (_PyEval_InitState(&interp->ceval) < 0) {
225 goto out_of_memory;
226 }
227
Victor Stinner72474072019-11-20 12:25:50 +0100228 _PyGC_InitState(&interp->gc);
Victor Stinner8462a492019-10-01 12:06:16 +0200229 PyConfig_InitPythonConfig(&interp->config);
Victor Stinner41010182020-12-26 01:45:43 +0100230 _PyType_InitCache(interp);
Victor Stinner022be022019-05-22 23:58:50 +0200231
Victor Stinnerd4341102017-11-23 00:12:09 +0100232 interp->eval_frame = _PyEval_EvalFrameDefault;
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000233#ifdef HAVE_DLOPEN
Serhiy Storchakac2f7d872016-05-04 09:44:44 +0300234#if HAVE_DECL_RTLD_NOW
Victor Stinnerd4341102017-11-23 00:12:09 +0100235 interp->dlopenflags = RTLD_NOW;
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000236#else
Victor Stinnerd4341102017-11-23 00:12:09 +0100237 interp->dlopenflags = RTLD_LAZY;
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000238#endif
239#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +0000240
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200241 struct pyinterpreters *interpreters = &runtime->interpreters;
242
243 HEAD_LOCK(runtime);
244 if (interpreters->next_id < 0) {
Victor Stinnerd4341102017-11-23 00:12:09 +0100245 /* overflow or Py_Initialize() not called! */
Victor Stinner71a35222020-03-26 22:46:14 +0100246 if (tstate != NULL) {
247 _PyErr_SetString(tstate, PyExc_RuntimeError,
248 "failed to get an interpreter ID");
249 }
Pablo Galindo95d630e2018-08-31 22:49:29 +0100250 PyMem_RawFree(interp);
Victor Stinnerd4341102017-11-23 00:12:09 +0100251 interp = NULL;
Victor Stinnerd4341102017-11-23 00:12:09 +0100252 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200253 else {
254 interp->id = interpreters->next_id;
255 interpreters->next_id += 1;
256 interp->next = interpreters->head;
257 if (interpreters->main == NULL) {
258 interpreters->main = interp;
259 }
260 interpreters->head = interp;
261 }
262 HEAD_UNLOCK(runtime);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000263
Pablo Galindo95d630e2018-08-31 22:49:29 +0100264 if (interp == NULL) {
265 return NULL;
266 }
267
Yury Selivanovf23746a2018-01-22 19:11:18 -0500268 interp->tstate_next_unique_id = 0;
269
Steve Dowerb82e17e2019-05-23 08:45:22 -0700270 interp->audit_hooks = NULL;
271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 return interp;
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200273
274out_of_memory:
275 if (tstate != NULL) {
276 _PyErr_NoMemory(tstate);
277 }
278
279 PyMem_RawFree(interp);
280 return NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000281}
282
283
Victor Stinnereba5bf22020-10-30 22:51:02 +0100284static void
285interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000286{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100287 _PyRuntimeState *runtime = interp->runtime;
288
Victor Stinner71a35222020-03-26 22:46:14 +0100289 if (_PySys_Audit(tstate, "cpython.PyInterpreterState_Clear", NULL) < 0) {
290 _PyErr_Clear(tstate);
Steve Dowerb82e17e2019-05-23 08:45:22 -0700291 }
292
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200293 HEAD_LOCK(runtime);
294 for (PyThreadState *p = interp->tstate_head; p != NULL; p = p->next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 PyThreadState_Clear(p);
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200296 }
297 HEAD_UNLOCK(runtime);
Steve Dowerb82e17e2019-05-23 08:45:22 -0700298
299 Py_CLEAR(interp->audit_hooks);
300
Victor Stinner331a6a52019-05-27 16:39:22 +0200301 PyConfig_Clear(&interp->config);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 Py_CLEAR(interp->codec_search_path);
303 Py_CLEAR(interp->codec_search_cache);
304 Py_CLEAR(interp->codec_error_registry);
Eric Snow93c92f72017-09-13 23:46:04 -0700305 Py_CLEAR(interp->modules);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 Py_CLEAR(interp->modules_by_index);
Serhiy Storchaka87a5c512014-02-10 18:21:34 +0200307 Py_CLEAR(interp->builtins_copy);
Brett Cannonfd074152012-04-14 14:10:13 -0400308 Py_CLEAR(interp->importlib);
Serhiy Storchaka133138a2016-08-02 22:51:21 +0300309 Py_CLEAR(interp->import_func);
Eric Snowd2fdd1f2019-03-15 17:47:43 -0600310 Py_CLEAR(interp->dict);
Brandt Bucher145bf262021-02-26 14:51:55 -0800311 Py_CLEAR(interp->map_abc);
312 Py_CLEAR(interp->seq_abc);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200313#ifdef HAVE_FORK
314 Py_CLEAR(interp->before_forkers);
315 Py_CLEAR(interp->after_forkers_parent);
316 Py_CLEAR(interp->after_forkers_child);
317#endif
Victor Stinnerfd957c12020-11-03 18:07:15 +0100318
319 _PyAST_Fini(interp);
320 _PyWarnings_Fini(interp);
Victor Stinnerb8fa1352020-12-15 14:34:19 +0100321 _PyAtExit_Fini(interp);
Victor Stinnerfd957c12020-11-03 18:07:15 +0100322
323 // All Python types must be destroyed before the last GC collection. Python
324 // types create a reference cycle to themselves in their in their
325 // PyTypeObject.tp_mro member (the tuple contains the type).
Victor Stinnereba5bf22020-10-30 22:51:02 +0100326
327 /* Last garbage collection on this interpreter */
328 _PyGC_CollectNoFail(tstate);
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100329 _PyGC_Fini(interp);
Victor Stinnereba5bf22020-10-30 22:51:02 +0100330
Hai Shi8ecc0c42020-08-13 05:23:30 +0800331 /* We don't clear sysdict and builtins until the end of this function.
332 Because clearing other attributes can execute arbitrary Python code
333 which requires sysdict and builtins. */
334 PyDict_Clear(interp->sysdict);
335 PyDict_Clear(interp->builtins);
336 Py_CLEAR(interp->sysdict);
337 Py_CLEAR(interp->builtins);
338
Eric Snow5be45a62019-03-08 22:47:07 -0700339 // XXX Once we have one allocator per interpreter (i.e.
340 // per-interpreter GC) we must ensure that all of the interpreter's
341 // objects have been cleaned up at the point.
Guido van Rossum25ce5661997-08-02 03:10:38 +0000342}
343
344
Victor Stinnereba5bf22020-10-30 22:51:02 +0100345void
346PyInterpreterState_Clear(PyInterpreterState *interp)
347{
348 // Use the current Python thread state to call audit hooks and to collect
349 // garbage. It can be different than the current Python thread state
350 // of 'interp'.
351 PyThreadState *current_tstate = _PyThreadState_GET();
352
353 interpreter_clear(interp, current_tstate);
354}
355
356
357void
358_PyInterpreterState_Clear(PyThreadState *tstate)
359{
360 interpreter_clear(tstate->interp, tstate);
361}
362
363
Guido van Rossum25ce5661997-08-02 03:10:38 +0000364static void
Victor Stinner9da74302019-11-20 11:17:17 +0100365zapthreads(PyInterpreterState *interp, int check_current)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000366{
Victor Stinner9da74302019-11-20 11:17:17 +0100367 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 /* No need to lock the mutex here because this should only happen
369 when the threads are all really dead (XXX famous last words). */
Victor Stinner9da74302019-11-20 11:17:17 +0100370 while ((tstate = interp->tstate_head) != NULL) {
371 _PyThreadState_Delete(tstate, check_current);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000373}
374
375
Victor Stinner01b1cc12019-11-20 02:27:56 +0100376void
377PyInterpreterState_Delete(PyInterpreterState *interp)
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200378{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100379 _PyRuntimeState *runtime = interp->runtime;
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200380 struct pyinterpreters *interpreters = &runtime->interpreters;
Victor Stinner9da74302019-11-20 11:17:17 +0100381 zapthreads(interp, 0);
382
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200383 _PyEval_FiniState(&interp->ceval);
384
Victor Stinner9da74302019-11-20 11:17:17 +0100385 /* Delete current thread. After this, many C API calls become crashy. */
386 _PyThreadState_Swap(&runtime->gilstate, NULL);
387
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200388 HEAD_LOCK(runtime);
389 PyInterpreterState **p;
390 for (p = &interpreters->head; ; p = &(*p)->next) {
391 if (*p == NULL) {
Victor Stinner87d3b9d2020-03-25 19:27:36 +0100392 Py_FatalError("NULL interpreter");
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200393 }
394 if (*p == interp) {
395 break;
396 }
397 }
398 if (interp->tstate_head != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100399 Py_FatalError("remaining threads");
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200400 }
401 *p = interp->next;
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200402
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200403 if (interpreters->main == interp) {
404 interpreters->main = NULL;
405 if (interpreters->head != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100406 Py_FatalError("remaining subinterpreters");
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200407 }
408 }
409 HEAD_UNLOCK(runtime);
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200410
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200411 if (interp->id_mutex != NULL) {
412 PyThread_free_lock(interp->id_mutex);
413 }
414 PyMem_RawFree(interp);
415}
416
417
Victor Stinner26881c82020-06-02 15:51:37 +0200418#ifdef HAVE_FORK
Eric Snow59032962018-09-14 14:17:20 -0700419/*
420 * Delete all interpreter states except the main interpreter. If there
421 * is a current interpreter state, it *must* be the main interpreter.
422 */
Victor Stinner26881c82020-06-02 15:51:37 +0200423PyStatus
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200424_PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime)
Eric Snow59032962018-09-14 14:17:20 -0700425{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200426 struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200427 struct pyinterpreters *interpreters = &runtime->interpreters;
428
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200429 PyThreadState *tstate = _PyThreadState_Swap(gilstate, NULL);
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200430 if (tstate != NULL && tstate->interp != interpreters->main) {
Victor Stinner26881c82020-06-02 15:51:37 +0200431 return _PyStatus_ERR("not main interpreter");
Eric Snow59032962018-09-14 14:17:20 -0700432 }
433
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200434 HEAD_LOCK(runtime);
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200435 PyInterpreterState *interp = interpreters->head;
436 interpreters->head = NULL;
Stéphane Wirtelb5409da2019-02-20 15:27:22 +0100437 while (interp != NULL) {
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200438 if (interp == interpreters->main) {
439 interpreters->main->next = NULL;
440 interpreters->head = interp;
Stéphane Wirtelb5409da2019-02-20 15:27:22 +0100441 interp = interp->next;
Eric Snow59032962018-09-14 14:17:20 -0700442 continue;
443 }
444
Victor Stinner01b1cc12019-11-20 02:27:56 +0100445 PyInterpreterState_Clear(interp); // XXX must activate?
Victor Stinner9da74302019-11-20 11:17:17 +0100446 zapthreads(interp, 1);
Eric Snow59032962018-09-14 14:17:20 -0700447 if (interp->id_mutex != NULL) {
448 PyThread_free_lock(interp->id_mutex);
449 }
Stéphane Wirtelb5409da2019-02-20 15:27:22 +0100450 PyInterpreterState *prev_interp = interp;
451 interp = interp->next;
452 PyMem_RawFree(prev_interp);
Eric Snow59032962018-09-14 14:17:20 -0700453 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200454 HEAD_UNLOCK(runtime);
Eric Snow59032962018-09-14 14:17:20 -0700455
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200456 if (interpreters->head == NULL) {
Victor Stinner26881c82020-06-02 15:51:37 +0200457 return _PyStatus_ERR("missing main interpreter");
Eric Snow59032962018-09-14 14:17:20 -0700458 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200459 _PyThreadState_Swap(gilstate, tstate);
Victor Stinner26881c82020-06-02 15:51:37 +0200460 return _PyStatus_OK();
Eric Snow59032962018-09-14 14:17:20 -0700461}
Victor Stinner26881c82020-06-02 15:51:37 +0200462#endif
Eric Snow59032962018-09-14 14:17:20 -0700463
464
Victor Stinnercaba55b2018-08-03 15:33:52 +0200465PyInterpreterState *
Victor Stinnerbe793732020-03-13 18:15:33 +0100466PyInterpreterState_Get(void)
Victor Stinnercaba55b2018-08-03 15:33:52 +0200467{
Victor Stinner50b48572018-11-01 01:51:40 +0100468 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner3026cad2020-06-01 16:02:40 +0200469 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnercaba55b2018-08-03 15:33:52 +0200470 PyInterpreterState *interp = tstate->interp;
471 if (interp == NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100472 Py_FatalError("no current interpreter");
Victor Stinnercaba55b2018-08-03 15:33:52 +0200473 }
474 return interp;
475}
476
477
Eric Snowe3774162017-05-22 19:46:40 -0700478int64_t
479PyInterpreterState_GetID(PyInterpreterState *interp)
480{
481 if (interp == NULL) {
482 PyErr_SetString(PyExc_RuntimeError, "no interpreter provided");
483 return -1;
484 }
485 return interp->id;
486}
487
488
Eric Snow5be45a62019-03-08 22:47:07 -0700489static PyInterpreterState *
Victor Stinner1a1bd2e2020-04-17 19:13:06 +0200490interp_look_up_id(_PyRuntimeState *runtime, int64_t requested_id)
Eric Snowb05b7112019-03-01 12:35:10 -0700491{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200492 PyInterpreterState *interp = runtime->interpreters.head;
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100493 while (interp != NULL) {
Victor Stinner1a1bd2e2020-04-17 19:13:06 +0200494 int64_t id = PyInterpreterState_GetID(interp);
Eric Snow5be45a62019-03-08 22:47:07 -0700495 if (id < 0) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100496 return NULL;
Eric Snow5be45a62019-03-08 22:47:07 -0700497 }
498 if (requested_id == id) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100499 return interp;
Eric Snow5be45a62019-03-08 22:47:07 -0700500 }
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100501 interp = PyInterpreterState_Next(interp);
Eric Snowb05b7112019-03-01 12:35:10 -0700502 }
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100503 return NULL;
Eric Snowb05b7112019-03-01 12:35:10 -0700504}
505
Eric Snow5be45a62019-03-08 22:47:07 -0700506PyInterpreterState *
Victor Stinner1a1bd2e2020-04-17 19:13:06 +0200507_PyInterpreterState_LookUpID(int64_t requested_id)
Eric Snow5be45a62019-03-08 22:47:07 -0700508{
509 PyInterpreterState *interp = NULL;
510 if (requested_id >= 0) {
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200511 _PyRuntimeState *runtime = &_PyRuntime;
512 HEAD_LOCK(runtime);
513 interp = interp_look_up_id(runtime, requested_id);
514 HEAD_UNLOCK(runtime);
Eric Snow5be45a62019-03-08 22:47:07 -0700515 }
516 if (interp == NULL && !PyErr_Occurred()) {
517 PyErr_Format(PyExc_RuntimeError,
518 "unrecognized interpreter ID %lld", requested_id);
519 }
520 return interp;
521}
522
Eric Snow4c6955e2018-02-16 18:53:40 -0700523
524int
525_PyInterpreterState_IDInitref(PyInterpreterState *interp)
526{
527 if (interp->id_mutex != NULL) {
528 return 0;
529 }
530 interp->id_mutex = PyThread_allocate_lock();
531 if (interp->id_mutex == NULL) {
532 PyErr_SetString(PyExc_RuntimeError,
533 "failed to create init interpreter ID mutex");
534 return -1;
535 }
536 interp->id_refcount = 0;
537 return 0;
538}
539
540
Victor Stinner32c5a172021-04-28 13:40:44 +0200541int
Eric Snow4c6955e2018-02-16 18:53:40 -0700542_PyInterpreterState_IDIncref(PyInterpreterState *interp)
543{
Victor Stinner32c5a172021-04-28 13:40:44 +0200544 if (_PyInterpreterState_IDInitref(interp) < 0) {
545 return -1;
Eric Snow4c6955e2018-02-16 18:53:40 -0700546 }
Victor Stinner32c5a172021-04-28 13:40:44 +0200547
Eric Snow4c6955e2018-02-16 18:53:40 -0700548 PyThread_acquire_lock(interp->id_mutex, WAIT_LOCK);
549 interp->id_refcount += 1;
550 PyThread_release_lock(interp->id_mutex);
Victor Stinner32c5a172021-04-28 13:40:44 +0200551 return 0;
Eric Snow4c6955e2018-02-16 18:53:40 -0700552}
553
554
555void
556_PyInterpreterState_IDDecref(PyInterpreterState *interp)
557{
Victor Stinner32c5a172021-04-28 13:40:44 +0200558 assert(interp->id_mutex != NULL);
559
Victor Stinner0fd2c302019-06-04 03:15:09 +0200560 struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
Eric Snow4c6955e2018-02-16 18:53:40 -0700561 PyThread_acquire_lock(interp->id_mutex, WAIT_LOCK);
562 assert(interp->id_refcount != 0);
563 interp->id_refcount -= 1;
564 int64_t refcount = interp->id_refcount;
565 PyThread_release_lock(interp->id_mutex);
566
Eric Snowc11183c2019-03-15 16:35:46 -0600567 if (refcount == 0 && interp->requires_idref) {
Eric Snowf53d9f22018-02-20 16:30:17 -0700568 // XXX Using the "head" thread isn't strictly correct.
569 PyThreadState *tstate = PyInterpreterState_ThreadHead(interp);
570 // XXX Possible GILState issues?
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200571 PyThreadState *save_tstate = _PyThreadState_Swap(gilstate, tstate);
Eric Snow4c6955e2018-02-16 18:53:40 -0700572 Py_EndInterpreter(tstate);
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200573 _PyThreadState_Swap(gilstate, save_tstate);
Eric Snow4c6955e2018-02-16 18:53:40 -0700574 }
575}
576
Eric Snowc11183c2019-03-15 16:35:46 -0600577int
578_PyInterpreterState_RequiresIDRef(PyInterpreterState *interp)
579{
580 return interp->requires_idref;
581}
582
583void
584_PyInterpreterState_RequireIDRef(PyInterpreterState *interp, int required)
585{
586 interp->requires_idref = required ? 1 : 0;
587}
588
Eric Snowc11183c2019-03-15 16:35:46 -0600589PyObject *
590_PyInterpreterState_GetMainModule(PyInterpreterState *interp)
591{
592 if (interp->modules == NULL) {
593 PyErr_SetString(PyExc_RuntimeError, "interpreter not initialized");
594 return NULL;
595 }
596 return PyMapping_GetItemString(interp->modules, "__main__");
597}
598
Eric Snowd2fdd1f2019-03-15 17:47:43 -0600599PyObject *
600PyInterpreterState_GetDict(PyInterpreterState *interp)
601{
602 if (interp->dict == NULL) {
603 interp->dict = PyDict_New();
604 if (interp->dict == NULL) {
605 PyErr_Clear();
606 }
607 }
608 /* Returning NULL means no per-interpreter dict is available. */
609 return interp->dict;
610}
611
Victor Stinner45b9be52010-03-03 23:28:07 +0000612static PyThreadState *
613new_threadstate(PyInterpreterState *interp, int init)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000614{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100615 _PyRuntimeState *runtime = interp->runtime;
Victor Stinner1a7425f2013-07-07 16:25:15 +0200616 PyThreadState *tstate = (PyThreadState *)PyMem_RawMalloc(sizeof(PyThreadState));
Victor Stinner8bb32302019-04-24 16:47:40 +0200617 if (tstate == NULL) {
618 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000620
Victor Stinner8bb32302019-04-24 16:47:40 +0200621 tstate->interp = interp;
622
623 tstate->frame = NULL;
624 tstate->recursion_depth = 0;
Mark Shannon4e7a69b2020-12-02 13:30:55 +0000625 tstate->recursion_headroom = 0;
Victor Stinner8bb32302019-04-24 16:47:40 +0200626 tstate->stackcheck_counter = 0;
627 tstate->tracing = 0;
Mark Shannon9e7b2072021-04-13 11:08:14 +0100628 tstate->root_cframe.use_tracing = 0;
629 tstate->cframe = &tstate->root_cframe;
Victor Stinner8bb32302019-04-24 16:47:40 +0200630 tstate->gilstate_counter = 0;
631 tstate->async_exc = NULL;
632 tstate->thread_id = PyThread_get_thread_ident();
633
634 tstate->dict = NULL;
635
636 tstate->curexc_type = NULL;
637 tstate->curexc_value = NULL;
638 tstate->curexc_traceback = NULL;
639
640 tstate->exc_state.exc_type = NULL;
641 tstate->exc_state.exc_value = NULL;
642 tstate->exc_state.exc_traceback = NULL;
643 tstate->exc_state.previous_item = NULL;
644 tstate->exc_info = &tstate->exc_state;
645
646 tstate->c_profilefunc = NULL;
647 tstate->c_tracefunc = NULL;
648 tstate->c_profileobj = NULL;
649 tstate->c_traceobj = NULL;
650
651 tstate->trash_delete_nesting = 0;
652 tstate->trash_delete_later = NULL;
653 tstate->on_delete = NULL;
654 tstate->on_delete_data = NULL;
655
656 tstate->coroutine_origin_tracking_depth = 0;
657
Victor Stinner8bb32302019-04-24 16:47:40 +0200658 tstate->async_gen_firstiter = NULL;
659 tstate->async_gen_finalizer = NULL;
660
661 tstate->context = NULL;
662 tstate->context_ver = 1;
663
Victor Stinner8bb32302019-04-24 16:47:40 +0200664 if (init) {
Victor Stinner01b1cc12019-11-20 02:27:56 +0100665 _PyThreadState_Init(tstate);
Victor Stinner8bb32302019-04-24 16:47:40 +0200666 }
667
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200668 HEAD_LOCK(runtime);
Stefan Krahb3b9ade2020-03-02 21:22:36 +0100669 tstate->id = ++interp->tstate_next_unique_id;
Victor Stinner8bb32302019-04-24 16:47:40 +0200670 tstate->prev = NULL;
671 tstate->next = interp->tstate_head;
672 if (tstate->next)
673 tstate->next->prev = tstate;
674 interp->tstate_head = tstate;
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200675 HEAD_UNLOCK(runtime);
Victor Stinner8bb32302019-04-24 16:47:40 +0200676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 return tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000678}
679
Victor Stinner45b9be52010-03-03 23:28:07 +0000680PyThreadState *
681PyThreadState_New(PyInterpreterState *interp)
682{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 return new_threadstate(interp, 1);
Victor Stinner45b9be52010-03-03 23:28:07 +0000684}
685
686PyThreadState *
687_PyThreadState_Prealloc(PyInterpreterState *interp)
688{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 return new_threadstate(interp, 0);
Victor Stinner45b9be52010-03-03 23:28:07 +0000690}
691
692void
Victor Stinner01b1cc12019-11-20 02:27:56 +0100693_PyThreadState_Init(PyThreadState *tstate)
Victor Stinner45b9be52010-03-03 23:28:07 +0000694{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100695 _PyGILState_NoteThreadState(&tstate->interp->runtime->gilstate, tstate);
Victor Stinner45b9be52010-03-03 23:28:07 +0000696}
697
Martin v. Löwis1a214512008-06-11 05:26:20 +0000698PyObject*
Martin v. Löwis7800f752012-06-22 12:20:55 +0200699PyState_FindModule(struct PyModuleDef* module)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000700{
Martin v. Löwis7800f752012-06-22 12:20:55 +0200701 Py_ssize_t index = module->m_base.m_index;
Victor Stinner81a7be32020-04-14 15:14:01 +0200702 PyInterpreterState *state = _PyInterpreterState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 PyObject *res;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000704 if (module->m_slots) {
705 return NULL;
706 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 if (index == 0)
708 return NULL;
709 if (state->modules_by_index == NULL)
710 return NULL;
Antoine Pitrou75506e82012-08-20 19:30:46 +0200711 if (index >= PyList_GET_SIZE(state->modules_by_index))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 return NULL;
713 res = PyList_GET_ITEM(state->modules_by_index, index);
714 return res==Py_None ? NULL : res;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000715}
716
717int
Victor Stinner82c83bd2019-11-22 18:52:27 +0100718_PyState_AddModule(PyThreadState *tstate, PyObject* module, struct PyModuleDef* def)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000719{
Berker Peksag4b7b5652016-08-22 18:05:56 +0300720 if (!def) {
Victor Stinner71a35222020-03-26 22:46:14 +0100721 assert(_PyErr_Occurred(tstate));
Berker Peksag4b7b5652016-08-22 18:05:56 +0300722 return -1;
723 }
Nick Coghland5cacbb2015-05-23 22:24:10 +1000724 if (def->m_slots) {
Victor Stinner71a35222020-03-26 22:46:14 +0100725 _PyErr_SetString(tstate,
726 PyExc_SystemError,
727 "PyState_AddModule called on module with slots");
Nick Coghland5cacbb2015-05-23 22:24:10 +1000728 return -1;
729 }
Victor Stinner82c83bd2019-11-22 18:52:27 +0100730
731 PyInterpreterState *interp = tstate->interp;
732 if (!interp->modules_by_index) {
733 interp->modules_by_index = PyList_New(0);
734 if (!interp->modules_by_index) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 return -1;
Victor Stinner82c83bd2019-11-22 18:52:27 +0100736 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 }
Victor Stinner82c83bd2019-11-22 18:52:27 +0100738
739 while (PyList_GET_SIZE(interp->modules_by_index) <= def->m_base.m_index) {
740 if (PyList_Append(interp->modules_by_index, Py_None) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 return -1;
Victor Stinner82c83bd2019-11-22 18:52:27 +0100742 }
743 }
744
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 Py_INCREF(module);
Victor Stinner82c83bd2019-11-22 18:52:27 +0100746 return PyList_SetItem(interp->modules_by_index,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 def->m_base.m_index, module);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000748}
Guido van Rossuma027efa1997-05-05 20:56:21 +0000749
Martin v. Löwis7800f752012-06-22 12:20:55 +0200750int
751PyState_AddModule(PyObject* module, struct PyModuleDef* def)
752{
Martin v. Löwis7800f752012-06-22 12:20:55 +0200753 if (!def) {
Victor Stinner87d3b9d2020-03-25 19:27:36 +0100754 Py_FatalError("module definition is NULL");
Martin v. Löwis7800f752012-06-22 12:20:55 +0200755 return -1;
756 }
Victor Stinner82c83bd2019-11-22 18:52:27 +0100757
758 PyThreadState *tstate = _PyThreadState_GET();
759 PyInterpreterState *interp = tstate->interp;
760 Py_ssize_t index = def->m_base.m_index;
761 if (interp->modules_by_index &&
762 index < PyList_GET_SIZE(interp->modules_by_index) &&
763 module == PyList_GET_ITEM(interp->modules_by_index, index))
764 {
Victor Stinner87d3b9d2020-03-25 19:27:36 +0100765 _Py_FatalErrorFormat(__func__, "module %p already added", module);
Benjamin Peterson39de95b2019-09-12 00:43:22 +0100766 return -1;
Martin v. Löwis7800f752012-06-22 12:20:55 +0200767 }
Victor Stinner82c83bd2019-11-22 18:52:27 +0100768 return _PyState_AddModule(tstate, module, def);
Martin v. Löwis7800f752012-06-22 12:20:55 +0200769}
770
771int
772PyState_RemoveModule(struct PyModuleDef* def)
773{
Victor Stinner71a35222020-03-26 22:46:14 +0100774 PyThreadState *tstate = _PyThreadState_GET();
775 PyInterpreterState *interp = tstate->interp;
776
Nick Coghland5cacbb2015-05-23 22:24:10 +1000777 if (def->m_slots) {
Victor Stinner71a35222020-03-26 22:46:14 +0100778 _PyErr_SetString(tstate,
779 PyExc_SystemError,
780 "PyState_RemoveModule called on module with slots");
Nick Coghland5cacbb2015-05-23 22:24:10 +1000781 return -1;
782 }
Victor Stinner71a35222020-03-26 22:46:14 +0100783
784 Py_ssize_t index = def->m_base.m_index;
Martin v. Löwis7800f752012-06-22 12:20:55 +0200785 if (index == 0) {
Victor Stinner87d3b9d2020-03-25 19:27:36 +0100786 Py_FatalError("invalid module index");
Martin v. Löwis7800f752012-06-22 12:20:55 +0200787 }
Victor Stinner71a35222020-03-26 22:46:14 +0100788 if (interp->modules_by_index == NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100789 Py_FatalError("Interpreters module-list not accessible.");
Martin v. Löwis7800f752012-06-22 12:20:55 +0200790 }
Victor Stinner71a35222020-03-26 22:46:14 +0100791 if (index > PyList_GET_SIZE(interp->modules_by_index)) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100792 Py_FatalError("Module index out of bounds.");
Martin v. Löwis7800f752012-06-22 12:20:55 +0200793 }
Victor Stinner71a35222020-03-26 22:46:14 +0100794
Zackery Spytz2a893432018-12-05 00:14:00 -0700795 Py_INCREF(Py_None);
Victor Stinner71a35222020-03-26 22:46:14 +0100796 return PyList_SetItem(interp->modules_by_index, index, Py_None);
Martin v. Löwis7800f752012-06-22 12:20:55 +0200797}
798
Victor Stinner048a3562020-11-05 00:45:56 +0100799// Used by finalize_modules()
Antoine Pitrou40322e62013-08-11 00:30:09 +0200800void
Victor Stinner0a28f8d2019-06-19 02:54:39 +0200801_PyInterpreterState_ClearModules(PyInterpreterState *interp)
Antoine Pitrou40322e62013-08-11 00:30:09 +0200802{
Victor Stinner0a28f8d2019-06-19 02:54:39 +0200803 if (!interp->modules_by_index) {
804 return;
805 }
806
807 Py_ssize_t i;
808 for (i = 0; i < PyList_GET_SIZE(interp->modules_by_index); i++) {
809 PyObject *m = PyList_GET_ITEM(interp->modules_by_index, i);
810 if (PyModule_Check(m)) {
811 /* cleanup the saved copy of module dicts */
812 PyModuleDef *md = PyModule_GetDef(m);
813 if (md) {
814 Py_CLEAR(md->m_base.m_copy);
Antoine Pitrou40322e62013-08-11 00:30:09 +0200815 }
816 }
Victor Stinner0a28f8d2019-06-19 02:54:39 +0200817 }
818
819 /* Setting modules_by_index to NULL could be dangerous, so we
820 clear the list instead. */
821 if (PyList_SetSlice(interp->modules_by_index,
822 0, PyList_GET_SIZE(interp->modules_by_index),
823 NULL)) {
824 PyErr_WriteUnraisable(interp->modules_by_index);
Antoine Pitrou40322e62013-08-11 00:30:09 +0200825 }
826}
827
Guido van Rossuma027efa1997-05-05 20:56:21 +0000828void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000829PyThreadState_Clear(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000830{
Victor Stinnerda7933e2020-04-13 03:04:28 +0200831 int verbose = _PyInterpreterState_GetConfig(tstate->interp)->verbose;
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200832
Victor Stinner5804f872020-03-24 16:32:26 +0100833 if (verbose && tstate->frame != NULL) {
834 /* bpo-20526: After the main thread calls
835 _PyRuntimeState_SetFinalizing() in Py_FinalizeEx(), threads must
836 exit when trying to take the GIL. If a thread exit in the middle of
837 _PyEval_EvalFrameDefault(), tstate->frame is not reset to its
838 previous value. It is more likely with daemon threads, but it can
839 happen with regular threads if threading._shutdown() fails
840 (ex: interrupted by CTRL+C). */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 fprintf(stderr,
842 "PyThreadState_Clear: warning: thread still has a frame\n");
Victor Stinner5804f872020-03-24 16:32:26 +0100843 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000844
Victor Stinner5804f872020-03-24 16:32:26 +0100845 /* Don't clear tstate->frame: it is a borrowed reference */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 Py_CLEAR(tstate->dict);
848 Py_CLEAR(tstate->async_exc);
Guido van Rossumede04391998-04-10 20:18:25 +0000849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 Py_CLEAR(tstate->curexc_type);
851 Py_CLEAR(tstate->curexc_value);
852 Py_CLEAR(tstate->curexc_traceback);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000853
Mark Shannonae3087c2017-10-22 22:41:51 +0100854 Py_CLEAR(tstate->exc_state.exc_type);
855 Py_CLEAR(tstate->exc_state.exc_value);
856 Py_CLEAR(tstate->exc_state.exc_traceback);
Serhiy Storchakabdf42982017-10-26 16:59:40 +0300857
Mark Shannonae3087c2017-10-22 22:41:51 +0100858 /* The stack of exception states should contain just this thread. */
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200859 if (verbose && tstate->exc_info != &tstate->exc_state) {
Mark Shannonae3087c2017-10-22 22:41:51 +0100860 fprintf(stderr,
861 "PyThreadState_Clear: warning: thread still has a generator\n");
862 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000863
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 tstate->c_profilefunc = NULL;
865 tstate->c_tracefunc = NULL;
866 Py_CLEAR(tstate->c_profileobj);
867 Py_CLEAR(tstate->c_traceobj);
Yury Selivanov75445082015-05-11 22:57:16 -0400868
Yury Selivanoveb636452016-09-08 22:01:51 -0700869 Py_CLEAR(tstate->async_gen_firstiter);
870 Py_CLEAR(tstate->async_gen_finalizer);
Yury Selivanovf23746a2018-01-22 19:11:18 -0500871
872 Py_CLEAR(tstate->context);
Victor Stinner4d96b462020-02-01 02:30:25 +0100873
874 if (tstate->on_delete != NULL) {
875 tstate->on_delete(tstate->on_delete_data);
876 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000877}
878
879
Joannah Nanjekye8855e472019-10-04 08:35:42 -0300880/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
Guido van Rossum29757862001-01-23 01:46:06 +0000881static void
Victor Stinner9da74302019-11-20 11:17:17 +0100882tstate_delete_common(PyThreadState *tstate,
883 struct _gilstate_runtime_state *gilstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000884{
Victor Stinner3026cad2020-06-01 16:02:40 +0200885 _Py_EnsureTstateNotNULL(tstate);
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200886 PyInterpreterState *interp = tstate->interp;
887 if (interp == NULL) {
Victor Stinner87d3b9d2020-03-25 19:27:36 +0100888 Py_FatalError("NULL interpreter");
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200889 }
Victor Stinner87d3b9d2020-03-25 19:27:36 +0100890 _PyRuntimeState *runtime = interp->runtime;
891
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200892 HEAD_LOCK(runtime);
Victor Stinner87d3b9d2020-03-25 19:27:36 +0100893 if (tstate->prev) {
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200894 tstate->prev->next = tstate->next;
Victor Stinner87d3b9d2020-03-25 19:27:36 +0100895 }
896 else {
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200897 interp->tstate_head = tstate->next;
Victor Stinner87d3b9d2020-03-25 19:27:36 +0100898 }
899 if (tstate->next) {
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200900 tstate->next->prev = tstate->prev;
Victor Stinner87d3b9d2020-03-25 19:27:36 +0100901 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200902 HEAD_UNLOCK(runtime);
Victor Stinner4d96b462020-02-01 02:30:25 +0100903
Victor Stinner9da74302019-11-20 11:17:17 +0100904 if (gilstate->autoInterpreterState &&
905 PyThread_tss_get(&gilstate->autoTSSkey) == tstate)
906 {
907 PyThread_tss_set(&gilstate->autoTSSkey, NULL);
908 }
909}
910
911
912static void
913_PyThreadState_Delete(PyThreadState *tstate, int check_current)
914{
915 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
916 if (check_current) {
917 if (tstate == _PyRuntimeGILState_GetThreadState(gilstate)) {
Victor Stinner87d3b9d2020-03-25 19:27:36 +0100918 _Py_FatalErrorFormat(__func__, "tstate %p is still current", tstate);
Victor Stinner9da74302019-11-20 11:17:17 +0100919 }
920 }
921 tstate_delete_common(tstate, gilstate);
Victor Stinner23ef89d2020-03-18 02:26:04 +0100922 PyMem_RawFree(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000923}
924
925
Victor Stinner01b1cc12019-11-20 02:27:56 +0100926void
927PyThreadState_Delete(PyThreadState *tstate)
Guido van Rossum29757862001-01-23 01:46:06 +0000928{
Victor Stinner9da74302019-11-20 11:17:17 +0100929 _PyThreadState_Delete(tstate, 1);
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200930}
931
932
Joannah Nanjekye2bc43cd2019-09-05 13:06:49 -0300933void
Victor Stinner23ef89d2020-03-18 02:26:04 +0100934_PyThreadState_DeleteCurrent(PyThreadState *tstate)
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200935{
Victor Stinner3026cad2020-06-01 16:02:40 +0200936 _Py_EnsureTstateNotNULL(tstate);
Victor Stinner23ef89d2020-03-18 02:26:04 +0100937 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
Victor Stinner9da74302019-11-20 11:17:17 +0100938 tstate_delete_common(tstate, gilstate);
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200939 _PyRuntimeGILState_SetThreadState(gilstate, NULL);
Victor Stinner23ef89d2020-03-18 02:26:04 +0100940 _PyEval_ReleaseLock(tstate);
941 PyMem_RawFree(tstate);
Guido van Rossum29757862001-01-23 01:46:06 +0000942}
Guido van Rossum29757862001-01-23 01:46:06 +0000943
Joannah Nanjekye8855e472019-10-04 08:35:42 -0300944void
945PyThreadState_DeleteCurrent(void)
946{
Victor Stinner23ef89d2020-03-18 02:26:04 +0100947 struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
948 PyThreadState *tstate = _PyRuntimeGILState_GetThreadState(gilstate);
949 _PyThreadState_DeleteCurrent(tstate);
Joannah Nanjekye8855e472019-10-04 08:35:42 -0300950}
951
Guido van Rossum29757862001-01-23 01:46:06 +0000952
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200953/*
954 * Delete all thread states except the one passed as argument.
955 * Note that, if there is a current thread state, it *must* be the one
956 * passed as argument. Also, this won't touch any other interpreters
957 * than the current one, since we don't know which thread state should
Min ho Kim39d87b52019-08-31 06:21:19 +1000958 * be kept in those other interpreters.
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200959 */
960void
Victor Stinner0fd2c302019-06-04 03:15:09 +0200961_PyThreadState_DeleteExcept(_PyRuntimeState *runtime, PyThreadState *tstate)
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200962{
963 PyInterpreterState *interp = tstate->interp;
Victor Stinner9ad58ac2020-03-09 23:37:49 +0100964
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200965 HEAD_LOCK(runtime);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200966 /* Remove all thread states, except tstate, from the linked list of
967 thread states. This will allow calling PyThreadState_Clear()
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200968 without holding the lock. */
Victor Stinner9ad58ac2020-03-09 23:37:49 +0100969 PyThreadState *list = interp->tstate_head;
970 if (list == tstate) {
971 list = tstate->next;
972 }
973 if (tstate->prev) {
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200974 tstate->prev->next = tstate->next;
Victor Stinner9ad58ac2020-03-09 23:37:49 +0100975 }
976 if (tstate->next) {
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200977 tstate->next->prev = tstate->prev;
Victor Stinner9ad58ac2020-03-09 23:37:49 +0100978 }
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200979 tstate->prev = tstate->next = NULL;
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200980 interp->tstate_head = tstate;
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200981 HEAD_UNLOCK(runtime);
Victor Stinner9ad58ac2020-03-09 23:37:49 +0100982
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200983 /* Clear and deallocate all stale thread states. Even if this
984 executes Python code, we should be safe since it executes
985 in the current thread, not one of the stale threads. */
Victor Stinner9ad58ac2020-03-09 23:37:49 +0100986 PyThreadState *p, *next;
987 for (p = list; p; p = next) {
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200988 next = p->next;
989 PyThreadState_Clear(p);
Victor Stinner1a7425f2013-07-07 16:25:15 +0200990 PyMem_RawFree(p);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200991 }
992}
993
994
Victor Stinnere838a932020-05-05 19:56:48 +0200995#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
996PyThreadState*
997_PyThreadState_GetTSS(void) {
998 return PyThread_tss_get(&_PyRuntime.gilstate.autoTSSkey);
999}
1000#endif
1001
1002
Guido van Rossuma027efa1997-05-05 20:56:21 +00001003PyThreadState *
Victor Stinnerbfd316e2016-01-20 11:12:38 +01001004_PyThreadState_UncheckedGet(void)
1005{
Victor Stinner50b48572018-11-01 01:51:40 +01001006 return _PyThreadState_GET();
Victor Stinnerbfd316e2016-01-20 11:12:38 +01001007}
1008
1009
1010PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001011PyThreadState_Get(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +00001012{
Victor Stinner50b48572018-11-01 01:51:40 +01001013 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner3026cad2020-06-01 16:02:40 +02001014 _Py_EnsureTstateNotNULL(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 return tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +00001016}
1017
1018
Victor Stinner09532fe2019-05-10 23:39:09 +02001019PyThreadState *
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001020_PyThreadState_Swap(struct _gilstate_runtime_state *gilstate, PyThreadState *newts)
Guido van Rossuma027efa1997-05-05 20:56:21 +00001021{
Victor Stinnere838a932020-05-05 19:56:48 +02001022#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
1023 PyThreadState *oldts = _PyThreadState_GetTSS();
1024#else
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001025 PyThreadState *oldts = _PyRuntimeGILState_GetThreadState(gilstate);
Victor Stinnere838a932020-05-05 19:56:48 +02001026#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +00001027
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001028 _PyRuntimeGILState_SetThreadState(gilstate, newts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 /* It should not be possible for more than one thread state
1030 to be used for a thread. Check this the best we can in debug
1031 builds.
1032 */
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001033#if defined(Py_DEBUG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 if (newts) {
1035 /* This can be called from PyEval_RestoreThread(). Similar
1036 to it, we need to ensure errno doesn't change.
1037 */
1038 int err = errno;
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001039 PyThreadState *check = _PyGILState_GetThisThreadState(gilstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 if (check && check->interp == newts->interp && check != newts)
1041 Py_FatalError("Invalid thread state for this thread");
1042 errno = err;
1043 }
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001044#endif
Victor Stinnere838a932020-05-05 19:56:48 +02001045#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
1046 PyThread_tss_set(&gilstate->autoTSSkey, newts);
1047#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 return oldts;
Guido van Rossuma027efa1997-05-05 20:56:21 +00001049}
Guido van Rossumede04391998-04-10 20:18:25 +00001050
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001051PyThreadState *
1052PyThreadState_Swap(PyThreadState *newts)
1053{
1054 return _PyThreadState_Swap(&_PyRuntime.gilstate, newts);
1055}
1056
Guido van Rossumede04391998-04-10 20:18:25 +00001057/* An extension mechanism to store arbitrary additional per-thread state.
1058 PyThreadState_GetDict() returns a dictionary that can be used to hold such
1059 state; the caller should pick a unique key and store its state there. If
Guido van Rossum0fc8f002003-04-15 15:12:39 +00001060 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
1061 and the caller should assume no per-thread state is available. */
Guido van Rossumede04391998-04-10 20:18:25 +00001062
1063PyObject *
Victor Stinner0e427c62020-03-25 21:22:55 +01001064_PyThreadState_GetDict(PyThreadState *tstate)
1065{
1066 assert(tstate != NULL);
1067 if (tstate->dict == NULL) {
1068 tstate->dict = PyDict_New();
1069 if (tstate->dict == NULL) {
1070 _PyErr_Clear(tstate);
1071 }
1072 }
1073 return tstate->dict;
1074}
1075
1076
1077PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001078PyThreadState_GetDict(void)
Guido van Rossumede04391998-04-10 20:18:25 +00001079{
Victor Stinner50b48572018-11-01 01:51:40 +01001080 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner0e427c62020-03-25 21:22:55 +01001081 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 }
Victor Stinner0e427c62020-03-25 21:22:55 +01001084 return _PyThreadState_GetDict(tstate);
Guido van Rossumede04391998-04-10 20:18:25 +00001085}
Guido van Rossumf5df46d2001-07-19 12:19:27 +00001086
1087
Victor Stinner8fb02b62020-03-13 23:38:08 +01001088PyInterpreterState *
1089PyThreadState_GetInterpreter(PyThreadState *tstate)
1090{
1091 assert(tstate != NULL);
Victor Stinner8fb02b62020-03-13 23:38:08 +01001092 return tstate->interp;
1093}
1094
1095
Victor Stinner4386b902020-04-29 03:01:43 +02001096PyFrameObject*
Victor Stinnerfd1e1a12020-03-20 15:51:45 +01001097PyThreadState_GetFrame(PyThreadState *tstate)
1098{
1099 assert(tstate != NULL);
Victor Stinner4386b902020-04-29 03:01:43 +02001100 PyFrameObject *frame = tstate->frame;
1101 Py_XINCREF(frame);
1102 return frame;
Victor Stinnerfd1e1a12020-03-20 15:51:45 +01001103}
1104
1105
Victor Stinner5c3cda02020-03-25 21:23:53 +01001106uint64_t
1107PyThreadState_GetID(PyThreadState *tstate)
1108{
1109 assert(tstate != NULL);
1110 return tstate->id;
1111}
1112
1113
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001114/* Asynchronously raise an exception in a thread.
1115 Requested by Just van Rossum and Alex Martelli.
Guido van Rossum0f1f63c2005-02-08 02:07:57 +00001116 To prevent naive misuse, you must write your own extension
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001117 to call this, or use ctypes. Must be called with the GIL held.
1118 Returns the number of tstates modified (normally 1, but 0 if `id` didn't
1119 match any known thread id). Can be called with exc=NULL to clear an
1120 existing async exception. This raises no exceptions. */
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001121
1122int
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001123PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc)
1124{
Victor Stinner09532fe2019-05-10 23:39:09 +02001125 _PyRuntimeState *runtime = &_PyRuntime;
1126 PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 /* Although the GIL is held, a few C API functions can be called
1129 * without the GIL held, and in particular some that create and
1130 * destroy thread and interpreter states. Those can mutate the
1131 * list of thread states we're traversing, so to prevent that we lock
1132 * head_mutex for the duration.
1133 */
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001134 HEAD_LOCK(runtime);
Victor Stinner56bfdeb2020-03-18 09:26:25 +01001135 for (PyThreadState *tstate = interp->tstate_head; tstate != NULL; tstate = tstate->next) {
1136 if (tstate->thread_id != id) {
1137 continue;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 }
Victor Stinner56bfdeb2020-03-18 09:26:25 +01001139
1140 /* Tricky: we need to decref the current value
1141 * (if any) in tstate->async_exc, but that can in turn
1142 * allow arbitrary Python code to run, including
1143 * perhaps calls to this function. To prevent
1144 * deadlock, we need to release head_mutex before
1145 * the decref.
1146 */
1147 PyObject *old_exc = tstate->async_exc;
1148 Py_XINCREF(exc);
1149 tstate->async_exc = exc;
1150 HEAD_UNLOCK(runtime);
1151
1152 Py_XDECREF(old_exc);
Victor Stinnerbcb094b2021-02-19 15:10:45 +01001153 _PyEval_SignalAsyncExc(tstate->interp);
Victor Stinner56bfdeb2020-03-18 09:26:25 +01001154 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001156 HEAD_UNLOCK(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 return 0;
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001158}
1159
1160
Guido van Rossumf5df46d2001-07-19 12:19:27 +00001161/* Routines for advanced debuggers, requested by David Beazley.
1162 Don't use unless you know what you are doing! */
1163
1164PyInterpreterState *
1165PyInterpreterState_Head(void)
1166{
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001167 return _PyRuntime.interpreters.head;
Guido van Rossumf5df46d2001-07-19 12:19:27 +00001168}
1169
1170PyInterpreterState *
Eric Snow6b4be192017-05-22 21:36:03 -07001171PyInterpreterState_Main(void)
1172{
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001173 return _PyRuntime.interpreters.main;
Eric Snow6b4be192017-05-22 21:36:03 -07001174}
1175
1176PyInterpreterState *
Guido van Rossumf5df46d2001-07-19 12:19:27 +00001177PyInterpreterState_Next(PyInterpreterState *interp) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 return interp->next;
Guido van Rossumf5df46d2001-07-19 12:19:27 +00001179}
1180
1181PyThreadState *
1182PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 return interp->tstate_head;
Guido van Rossumf5df46d2001-07-19 12:19:27 +00001184}
1185
1186PyThreadState *
1187PyThreadState_Next(PyThreadState *tstate) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 return tstate->next;
Guido van Rossumf5df46d2001-07-19 12:19:27 +00001189}
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001190
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001191/* The implementation of sys._current_frames(). This is intended to be
1192 called with the GIL held, as it will be when called via
1193 sys._current_frames(). It's possible it would work fine even without
1194 the GIL held, but haven't thought enough about that.
1195*/
1196PyObject *
1197_PyThread_CurrentFrames(void)
1198{
Victor Stinner71a35222020-03-26 22:46:14 +01001199 PyThreadState *tstate = _PyThreadState_GET();
1200 if (_PySys_Audit(tstate, "sys._current_frames", NULL) < 0) {
Steve Dowerb82e17e2019-05-23 08:45:22 -07001201 return NULL;
1202 }
1203
Victor Stinner71a35222020-03-26 22:46:14 +01001204 PyObject *result = PyDict_New();
1205 if (result == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 return NULL;
Victor Stinner71a35222020-03-26 22:46:14 +01001207 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 /* for i in all interpreters:
1210 * for t in all of i's thread states:
1211 * if t's frame isn't NULL, map t's id to its frame
Ezio Melotti13925002011-03-16 11:05:33 +02001212 * Because these lists can mutate even when the GIL is held, we
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 * need to grab head_mutex for the duration.
1214 */
Victor Stinner71a35222020-03-26 22:46:14 +01001215 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001216 HEAD_LOCK(runtime);
Victor Stinner71a35222020-03-26 22:46:14 +01001217 PyInterpreterState *i;
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001218 for (i = runtime->interpreters.head; i != NULL; i = i->next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 PyThreadState *t;
1220 for (t = i->tstate_head; t != NULL; t = t->next) {
Victor Stinner4386b902020-04-29 03:01:43 +02001221 PyFrameObject *frame = t->frame;
Victor Stinner71a35222020-03-26 22:46:14 +01001222 if (frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 continue;
Victor Stinner71a35222020-03-26 22:46:14 +01001224 }
1225 PyObject *id = PyLong_FromUnsignedLong(t->thread_id);
1226 if (id == NULL) {
1227 goto fail;
1228 }
1229 int stat = PyDict_SetItem(result, id, (PyObject *)frame);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 Py_DECREF(id);
Victor Stinner71a35222020-03-26 22:46:14 +01001231 if (stat < 0) {
1232 goto fail;
1233 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 }
1235 }
Victor Stinner71a35222020-03-26 22:46:14 +01001236 goto done;
1237
1238fail:
1239 Py_CLEAR(result);
1240
1241done:
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001242 HEAD_UNLOCK(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001244}
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001245
Julien Danjou64366fa2020-11-02 15:16:25 +01001246PyObject *
1247_PyThread_CurrentExceptions(void)
1248{
1249 PyThreadState *tstate = _PyThreadState_GET();
1250
1251 _Py_EnsureTstateNotNULL(tstate);
1252
1253 if (_PySys_Audit(tstate, "sys._current_exceptions", NULL) < 0) {
1254 return NULL;
1255 }
1256
1257 PyObject *result = PyDict_New();
1258 if (result == NULL) {
1259 return NULL;
1260 }
1261
1262 /* for i in all interpreters:
1263 * for t in all of i's thread states:
1264 * if t's frame isn't NULL, map t's id to its frame
1265 * Because these lists can mutate even when the GIL is held, we
1266 * need to grab head_mutex for the duration.
1267 */
1268 _PyRuntimeState *runtime = tstate->interp->runtime;
1269 HEAD_LOCK(runtime);
1270 PyInterpreterState *i;
1271 for (i = runtime->interpreters.head; i != NULL; i = i->next) {
1272 PyThreadState *t;
1273 for (t = i->tstate_head; t != NULL; t = t->next) {
1274 _PyErr_StackItem *err_info = _PyErr_GetTopmostException(t);
1275 if (err_info == NULL) {
1276 continue;
1277 }
1278 PyObject *id = PyLong_FromUnsignedLong(t->thread_id);
1279 if (id == NULL) {
1280 goto fail;
1281 }
1282 PyObject *exc_info = PyTuple_Pack(
1283 3,
1284 err_info->exc_type != NULL ? err_info->exc_type : Py_None,
1285 err_info->exc_value != NULL ? err_info->exc_value : Py_None,
1286 err_info->exc_traceback != NULL ? err_info->exc_traceback : Py_None);
1287 if (exc_info == NULL) {
1288 Py_DECREF(id);
1289 goto fail;
1290 }
1291 int stat = PyDict_SetItem(result, id, exc_info);
1292 Py_DECREF(id);
1293 Py_DECREF(exc_info);
1294 if (stat < 0) {
1295 goto fail;
1296 }
1297 }
1298 }
1299 goto done;
1300
1301fail:
1302 Py_CLEAR(result);
1303
1304done:
1305 HEAD_UNLOCK(runtime);
1306 return result;
1307}
1308
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001309/* Python "auto thread state" API. */
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001310
1311/* Keep this as a static, as it is not reliable! It can only
1312 ever be compared to the state for the *current* thread.
1313 * If not equal, then it doesn't matter that the actual
1314 value may change immediately after comparison, as it can't
1315 possibly change to the current thread's state.
1316 * If equal, then the current thread holds the lock, so the value can't
1317 change until we yield the lock.
1318*/
1319static int
1320PyThreadState_IsCurrent(PyThreadState *tstate)
1321{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 /* Must be the tstate for this thread */
Victor Stinner0fd2c302019-06-04 03:15:09 +02001323 struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001324 assert(_PyGILState_GetThisThreadState(gilstate) == tstate);
1325 return tstate == _PyRuntimeGILState_GetThreadState(gilstate);
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001326}
1327
Tim Peters4c1f5ec2004-10-09 17:25:05 +00001328/* Internal initialization/finalization functions called by
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001329 Py_Initialize/Py_FinalizeEx
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001330*/
Victor Stinner4e53abb2020-03-10 23:49:16 +01001331PyStatus
Victor Stinner87f649a2021-03-10 20:00:46 +01001332_PyGILState_Init(_PyRuntimeState *runtime)
1333{
1334 struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
1335 if (PyThread_tss_create(&gilstate->autoTSSkey) != 0) {
1336 return _PyStatus_NO_MEMORY();
1337 }
1338 // PyThreadState_New() calls _PyGILState_NoteThreadState() which does
1339 // nothing before autoInterpreterState is set.
1340 assert(gilstate->autoInterpreterState == NULL);
1341 return _PyStatus_OK();
1342}
1343
1344
1345PyStatus
1346_PyGILState_SetTstate(PyThreadState *tstate)
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001347{
Victor Stinner101bf692021-02-19 13:33:31 +01001348 if (!_Py_IsMainInterpreter(tstate->interp)) {
Victor Stinnerdda5d6e2020-04-08 17:54:59 +02001349 /* Currently, PyGILState is shared by all interpreters. The main
1350 * interpreter is responsible to initialize it. */
1351 return _PyStatus_OK();
1352 }
1353
Victor Stinner8bb32302019-04-24 16:47:40 +02001354 /* must init with valid states */
Victor Stinner0fd2c302019-06-04 03:15:09 +02001355 assert(tstate != NULL);
Victor Stinnerb45d2592019-06-20 00:05:23 +02001356 assert(tstate->interp != NULL);
Victor Stinner8bb32302019-04-24 16:47:40 +02001357
Victor Stinner01b1cc12019-11-20 02:27:56 +01001358 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
Victor Stinner8bb32302019-04-24 16:47:40 +02001359
Victor Stinnerb45d2592019-06-20 00:05:23 +02001360 gilstate->autoInterpreterState = tstate->interp;
Victor Stinner8bb32302019-04-24 16:47:40 +02001361 assert(PyThread_tss_get(&gilstate->autoTSSkey) == NULL);
1362 assert(tstate->gilstate_counter == 0);
Michael W. Hudson188d4362005-06-20 16:52:57 +00001363
Victor Stinner8bb32302019-04-24 16:47:40 +02001364 _PyGILState_NoteThreadState(gilstate, tstate);
Victor Stinner4e53abb2020-03-10 23:49:16 +01001365 return _PyStatus_OK();
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001366}
1367
Victor Stinner861d9ab2016-03-16 22:45:24 +01001368PyInterpreterState *
1369_PyGILState_GetInterpreterStateUnsafe(void)
1370{
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001371 return _PyRuntime.gilstate.autoInterpreterState;
Victor Stinner861d9ab2016-03-16 22:45:24 +01001372}
1373
Tim Peters19717fa2004-10-09 17:38:29 +00001374void
Victor Stinnerbcb094b2021-02-19 15:10:45 +01001375_PyGILState_Fini(PyInterpreterState *interp)
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001376{
Victor Stinnerbcb094b2021-02-19 15:10:45 +01001377 struct _gilstate_runtime_state *gilstate = &interp->runtime->gilstate;
Victor Stinner8e91c242019-04-24 17:24:01 +02001378 PyThread_tss_delete(&gilstate->autoTSSkey);
1379 gilstate->autoInterpreterState = NULL;
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001380}
1381
Victor Stinner26881c82020-06-02 15:51:37 +02001382#ifdef HAVE_FORK
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001383/* Reset the TSS key - called by PyOS_AfterFork_Child().
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001384 * This should not be necessary, but some - buggy - pthread implementations
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001385 * don't reset TSS upon fork(), see issue #10517.
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001386 */
Victor Stinner26881c82020-06-02 15:51:37 +02001387PyStatus
Victor Stinnerb930a2d2019-04-24 17:14:33 +02001388_PyGILState_Reinit(_PyRuntimeState *runtime)
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001389{
Victor Stinnerb930a2d2019-04-24 17:14:33 +02001390 struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001391 PyThreadState *tstate = _PyGILState_GetThisThreadState(gilstate);
Victor Stinnerb930a2d2019-04-24 17:14:33 +02001392
1393 PyThread_tss_delete(&gilstate->autoTSSkey);
1394 if (PyThread_tss_create(&gilstate->autoTSSkey) != 0) {
Victor Stinner26881c82020-06-02 15:51:37 +02001395 return _PyStatus_NO_MEMORY();
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001396 }
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001397
Charles-François Natalia233df82011-11-22 19:49:51 +01001398 /* If the thread had an associated auto thread state, reassociate it with
1399 * the new key. */
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001400 if (tstate &&
Victor Stinnerb930a2d2019-04-24 17:14:33 +02001401 PyThread_tss_set(&gilstate->autoTSSkey, (void *)tstate) != 0)
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001402 {
Victor Stinner26881c82020-06-02 15:51:37 +02001403 return _PyStatus_ERR("failed to set autoTSSkey");
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001404 }
Victor Stinner26881c82020-06-02 15:51:37 +02001405 return _PyStatus_OK();
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001406}
Victor Stinner26881c82020-06-02 15:51:37 +02001407#endif
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001408
Michael W. Hudson188d4362005-06-20 16:52:57 +00001409/* When a thread state is created for a thread by some mechanism other than
1410 PyGILState_Ensure, it's important that the GILState machinery knows about
1411 it so it doesn't try to create another thread state for the thread (this is
1412 a better fix for SF bug #1010677 than the first one attempted).
1413*/
Thomas Wouters89f507f2006-12-13 04:49:30 +00001414static void
Victor Stinner8bb32302019-04-24 16:47:40 +02001415_PyGILState_NoteThreadState(struct _gilstate_runtime_state *gilstate, PyThreadState* tstate)
Michael W. Hudson188d4362005-06-20 16:52:57 +00001416{
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001417 /* If autoTSSkey isn't initialized, this must be the very first
Antoine Pitrou079ce542010-09-08 12:37:10 +00001418 threadstate created in Py_Initialize(). Don't do anything for now
1419 (we'll be back here when _PyGILState_Init is called). */
Victor Stinner8bb32302019-04-24 16:47:40 +02001420 if (!gilstate->autoInterpreterState) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 return;
Victor Stinner8bb32302019-04-24 16:47:40 +02001422 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001423
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001424 /* Stick the thread state for this thread in thread specific storage.
Michael W. Hudson188d4362005-06-20 16:52:57 +00001425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 The only situation where you can legitimately have more than one
1427 thread state for an OS level thread is when there are multiple
Victor Stinner590cebe2013-12-13 11:08:56 +01001428 interpreters.
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001429
Victor Stinner590cebe2013-12-13 11:08:56 +01001430 You shouldn't really be using the PyGILState_ APIs anyway (see issues
1431 #10915 and #15751).
Michael W. Hudson188d4362005-06-20 16:52:57 +00001432
Victor Stinner590cebe2013-12-13 11:08:56 +01001433 The first thread state created for that given OS level thread will
1434 "win", which seems reasonable behaviour.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 */
Victor Stinner8bb32302019-04-24 16:47:40 +02001436 if (PyThread_tss_get(&gilstate->autoTSSkey) == NULL) {
1437 if ((PyThread_tss_set(&gilstate->autoTSSkey, (void *)tstate)) != 0) {
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001438 Py_FatalError("Couldn't create autoTSSkey mapping");
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001439 }
Victor Stinner590cebe2013-12-13 11:08:56 +01001440 }
Michael W. Hudson188d4362005-06-20 16:52:57 +00001441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 /* PyGILState_Release must not try to delete this thread state. */
1443 tstate->gilstate_counter = 1;
Michael W. Hudson188d4362005-06-20 16:52:57 +00001444}
1445
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001446/* The public functions */
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001447static PyThreadState *
1448_PyGILState_GetThisThreadState(struct _gilstate_runtime_state *gilstate)
1449{
1450 if (gilstate->autoInterpreterState == NULL)
1451 return NULL;
1452 return (PyThreadState *)PyThread_tss_get(&gilstate->autoTSSkey);
1453}
1454
Tim Peters19717fa2004-10-09 17:38:29 +00001455PyThreadState *
1456PyGILState_GetThisThreadState(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001457{
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001458 return _PyGILState_GetThisThreadState(&_PyRuntime.gilstate);
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001459}
1460
Kristján Valur Jónsson684cd0e2013-03-23 03:36:16 -07001461int
1462PyGILState_Check(void)
1463{
Victor Stinner1c4cbdf2020-04-13 11:45:21 +02001464 struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
1465 if (!gilstate->check_enabled) {
Victor Stinner8a1be612016-03-14 22:07:55 +01001466 return 1;
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001467 }
Victor Stinner8a1be612016-03-14 22:07:55 +01001468
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001469 if (!PyThread_tss_is_created(&gilstate->autoTSSkey)) {
1470 return 1;
1471 }
Victor Stinner8a1be612016-03-14 22:07:55 +01001472
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001473 PyThreadState *tstate = _PyRuntimeGILState_GetThreadState(gilstate);
1474 if (tstate == NULL) {
1475 return 0;
1476 }
1477
1478 return (tstate == _PyGILState_GetThisThreadState(gilstate));
Kristján Valur Jónsson684cd0e2013-03-23 03:36:16 -07001479}
1480
Tim Peters19717fa2004-10-09 17:38:29 +00001481PyGILState_STATE
1482PyGILState_Ensure(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001483{
Victor Stinner175a7042020-03-10 00:37:48 +01001484 _PyRuntimeState *runtime = &_PyRuntime;
1485 struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 /* Note that we do not auto-init Python here - apart from
1488 potential races with 2 threads auto-initializing, pep-311
1489 spells out other issues. Embedders are expected to have
Victor Stinner175a7042020-03-10 00:37:48 +01001490 called Py_Initialize(). */
1491
1492 /* Ensure that _PyEval_InitThreads() and _PyGILState_Init() have been
1493 called by Py_Initialize() */
Victor Stinnere838a932020-05-05 19:56:48 +02001494#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinner175a7042020-03-10 00:37:48 +01001495 assert(_PyEval_ThreadsInitialized(runtime));
Victor Stinnere838a932020-05-05 19:56:48 +02001496#endif
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001497 assert(gilstate->autoInterpreterState);
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001498
Victor Stinner175a7042020-03-10 00:37:48 +01001499 PyThreadState *tcur = (PyThreadState *)PyThread_tss_get(&gilstate->autoTSSkey);
1500 int current;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 if (tcur == NULL) {
Victor Stinner175a7042020-03-10 00:37:48 +01001502 /* Create a new Python thread state for this thread */
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001503 tcur = PyThreadState_New(gilstate->autoInterpreterState);
Victor Stinner175a7042020-03-10 00:37:48 +01001504 if (tcur == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 Py_FatalError("Couldn't create thread-state for new thread");
Victor Stinner175a7042020-03-10 00:37:48 +01001506 }
1507
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001508 /* This is our thread state! We'll need to delete it in the
1509 matching call to PyGILState_Release(). */
1510 tcur->gilstate_counter = 0;
1511 current = 0; /* new thread state is never current */
1512 }
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001513 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 current = PyThreadState_IsCurrent(tcur);
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001515 }
1516
1517 if (current == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001518 PyEval_RestoreThread(tcur);
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001519 }
1520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 /* Update our counter in the thread-state - no need for locks:
1522 - tcur will remain valid as we hold the GIL.
1523 - the counter is safe as we are the only thread "allowed"
1524 to modify this value
1525 */
1526 ++tcur->gilstate_counter;
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001529}
1530
Tim Peters19717fa2004-10-09 17:38:29 +00001531void
1532PyGILState_Release(PyGILState_STATE oldstate)
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001533{
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001534 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner23ef89d2020-03-18 02:26:04 +01001535 PyThreadState *tstate = PyThread_tss_get(&runtime->gilstate.autoTSSkey);
1536 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537 Py_FatalError("auto-releasing thread-state, "
1538 "but no thread-state for this thread");
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001539 }
1540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001541 /* We must hold the GIL and have our thread state current */
1542 /* XXX - remove the check - the assert should be fine,
1543 but while this is very new (April 2003), the extra check
1544 by release-only users can't hurt.
1545 */
Victor Stinner23ef89d2020-03-18 02:26:04 +01001546 if (!PyThreadState_IsCurrent(tstate)) {
Victor Stinner87d3b9d2020-03-25 19:27:36 +01001547 _Py_FatalErrorFormat(__func__,
1548 "thread state %p must be current when releasing",
1549 tstate);
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001550 }
Victor Stinner23ef89d2020-03-18 02:26:04 +01001551 assert(PyThreadState_IsCurrent(tstate));
1552 --tstate->gilstate_counter;
1553 assert(tstate->gilstate_counter >= 0); /* illegal counter value */
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 /* If we're going to destroy this thread-state, we must
1556 * clear it while the GIL is held, as destructors may run.
1557 */
Victor Stinner23ef89d2020-03-18 02:26:04 +01001558 if (tstate->gilstate_counter == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001559 /* can't have been locked when we created it */
1560 assert(oldstate == PyGILState_UNLOCKED);
Victor Stinner23ef89d2020-03-18 02:26:04 +01001561 PyThreadState_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 /* Delete the thread-state. Note this releases the GIL too!
1563 * It's vital that the GIL be held here, to avoid shutdown
1564 * races; see bugs 225673 and 1061968 (that nasty bug has a
1565 * habit of coming back).
1566 */
Victor Stinner23ef89d2020-03-18 02:26:04 +01001567 assert(_PyRuntimeGILState_GetThreadState(&runtime->gilstate) == tstate);
1568 _PyThreadState_DeleteCurrent(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569 }
1570 /* Release the lock if necessary */
1571 else if (oldstate == PyGILState_UNLOCKED)
1572 PyEval_SaveThread();
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001573}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001574
Benjamin Peterson3bf01752012-04-13 18:06:36 -04001575
Eric Snow7f8bfc92018-01-29 18:23:44 -07001576/**************************/
1577/* cross-interpreter data */
1578/**************************/
1579
1580/* cross-interpreter data */
1581
1582crossinterpdatafunc _PyCrossInterpreterData_Lookup(PyObject *);
1583
1584/* This is a separate func from _PyCrossInterpreterData_Lookup in order
1585 to keep the registry code separate. */
1586static crossinterpdatafunc
1587_lookup_getdata(PyObject *obj)
1588{
1589 crossinterpdatafunc getdata = _PyCrossInterpreterData_Lookup(obj);
1590 if (getdata == NULL && PyErr_Occurred() == 0)
1591 PyErr_Format(PyExc_ValueError,
1592 "%S does not support cross-interpreter data", obj);
1593 return getdata;
1594}
1595
1596int
1597_PyObject_CheckCrossInterpreterData(PyObject *obj)
1598{
1599 crossinterpdatafunc getdata = _lookup_getdata(obj);
1600 if (getdata == NULL) {
1601 return -1;
1602 }
1603 return 0;
1604}
1605
1606static int
Victor Stinner71a35222020-03-26 22:46:14 +01001607_check_xidata(PyThreadState *tstate, _PyCrossInterpreterData *data)
Eric Snow7f8bfc92018-01-29 18:23:44 -07001608{
1609 // data->data can be anything, including NULL, so we don't check it.
1610
1611 // data->obj may be NULL, so we don't check it.
1612
1613 if (data->interp < 0) {
Victor Stinner71a35222020-03-26 22:46:14 +01001614 _PyErr_SetString(tstate, PyExc_SystemError, "missing interp");
Eric Snow7f8bfc92018-01-29 18:23:44 -07001615 return -1;
1616 }
1617
1618 if (data->new_object == NULL) {
Victor Stinner71a35222020-03-26 22:46:14 +01001619 _PyErr_SetString(tstate, PyExc_SystemError, "missing new_object func");
Eric Snow7f8bfc92018-01-29 18:23:44 -07001620 return -1;
1621 }
1622
1623 // data->free may be NULL, so we don't check it.
1624
1625 return 0;
1626}
1627
1628int
1629_PyObject_GetCrossInterpreterData(PyObject *obj, _PyCrossInterpreterData *data)
1630{
Victor Stinner71a35222020-03-26 22:46:14 +01001631 // PyThreadState_Get() aborts if tstate is NULL.
1632 PyThreadState *tstate = PyThreadState_Get();
1633 PyInterpreterState *interp = tstate->interp;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001634
1635 // Reset data before re-populating.
1636 *data = (_PyCrossInterpreterData){0};
1637 data->free = PyMem_RawFree; // Set a default that may be overridden.
1638
1639 // Call the "getdata" func for the object.
1640 Py_INCREF(obj);
1641 crossinterpdatafunc getdata = _lookup_getdata(obj);
1642 if (getdata == NULL) {
1643 Py_DECREF(obj);
1644 return -1;
1645 }
1646 int res = getdata(obj, data);
1647 Py_DECREF(obj);
1648 if (res != 0) {
1649 return -1;
1650 }
1651
1652 // Fill in the blanks and validate the result.
Eric Snow7f8bfc92018-01-29 18:23:44 -07001653 data->interp = interp->id;
Victor Stinner71a35222020-03-26 22:46:14 +01001654 if (_check_xidata(tstate, data) != 0) {
Eric Snow7f8bfc92018-01-29 18:23:44 -07001655 _PyCrossInterpreterData_Release(data);
1656 return -1;
1657 }
1658
1659 return 0;
1660}
1661
Victor Stinnere225beb2019-06-03 18:14:24 +02001662static void
Eric Snow63799132018-06-01 18:45:20 -06001663_release_xidata(void *arg)
1664{
1665 _PyCrossInterpreterData *data = (_PyCrossInterpreterData *)arg;
1666 if (data->free != NULL) {
1667 data->free(data->data);
1668 }
1669 Py_XDECREF(data->obj);
Victor Stinnere225beb2019-06-03 18:14:24 +02001670}
1671
1672static void
1673_call_in_interpreter(struct _gilstate_runtime_state *gilstate,
1674 PyInterpreterState *interp,
1675 void (*func)(void *), void *arg)
1676{
1677 /* We would use Py_AddPendingCall() if it weren't specific to the
1678 * main interpreter (see bpo-33608). In the meantime we take a
1679 * naive approach.
1680 */
1681 PyThreadState *save_tstate = NULL;
1682 if (interp != _PyRuntimeGILState_GetThreadState(gilstate)->interp) {
1683 // XXX Using the "head" thread isn't strictly correct.
1684 PyThreadState *tstate = PyInterpreterState_ThreadHead(interp);
1685 // XXX Possible GILState issues?
1686 save_tstate = _PyThreadState_Swap(gilstate, tstate);
1687 }
1688
1689 func(arg);
1690
1691 // Switch back.
1692 if (save_tstate != NULL) {
1693 _PyThreadState_Swap(gilstate, save_tstate);
1694 }
Eric Snow63799132018-06-01 18:45:20 -06001695}
1696
Eric Snow7f8bfc92018-01-29 18:23:44 -07001697void
1698_PyCrossInterpreterData_Release(_PyCrossInterpreterData *data)
1699{
1700 if (data->data == NULL && data->obj == NULL) {
1701 // Nothing to release!
1702 return;
1703 }
1704
Victor Stinnere225beb2019-06-03 18:14:24 +02001705 // Switch to the original interpreter.
Eric Snow7f8bfc92018-01-29 18:23:44 -07001706 PyInterpreterState *interp = _PyInterpreterState_LookUpID(data->interp);
1707 if (interp == NULL) {
Min ho Kimc4cacc82019-07-31 08:16:13 +10001708 // The interpreter was already destroyed.
Eric Snow7f8bfc92018-01-29 18:23:44 -07001709 if (data->free != NULL) {
1710 // XXX Someone leaked some memory...
1711 }
1712 return;
1713 }
Eric Snowf53d9f22018-02-20 16:30:17 -07001714
Eric Snow7f8bfc92018-01-29 18:23:44 -07001715 // "Release" the data and/or the object.
Victor Stinnere225beb2019-06-03 18:14:24 +02001716 struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
1717 _call_in_interpreter(gilstate, interp, _release_xidata, data);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001718}
1719
1720PyObject *
1721_PyCrossInterpreterData_NewObject(_PyCrossInterpreterData *data)
1722{
1723 return data->new_object(data);
1724}
1725
1726/* registry of {type -> crossinterpdatafunc} */
1727
1728/* For now we use a global registry of shareable classes. An
1729 alternative would be to add a tp_* slot for a class's
1730 crossinterpdatafunc. It would be simpler and more efficient. */
1731
1732static int
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001733_register_xidata(struct _xidregistry *xidregistry, PyTypeObject *cls,
1734 crossinterpdatafunc getdata)
Eric Snow7f8bfc92018-01-29 18:23:44 -07001735{
1736 // Note that we effectively replace already registered classes
1737 // rather than failing.
1738 struct _xidregitem *newhead = PyMem_RawMalloc(sizeof(struct _xidregitem));
1739 if (newhead == NULL)
1740 return -1;
1741 newhead->cls = cls;
1742 newhead->getdata = getdata;
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001743 newhead->next = xidregistry->head;
1744 xidregistry->head = newhead;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001745 return 0;
1746}
1747
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001748static void _register_builtins_for_crossinterpreter_data(struct _xidregistry *xidregistry);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001749
1750int
Eric Snowc11183c2019-03-15 16:35:46 -06001751_PyCrossInterpreterData_RegisterClass(PyTypeObject *cls,
Eric Snow7f8bfc92018-01-29 18:23:44 -07001752 crossinterpdatafunc getdata)
1753{
1754 if (!PyType_Check(cls)) {
1755 PyErr_Format(PyExc_ValueError, "only classes may be registered");
1756 return -1;
1757 }
1758 if (getdata == NULL) {
1759 PyErr_Format(PyExc_ValueError, "missing 'getdata' func");
1760 return -1;
1761 }
1762
1763 // Make sure the class isn't ever deallocated.
1764 Py_INCREF((PyObject *)cls);
1765
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001766 struct _xidregistry *xidregistry = &_PyRuntime.xidregistry ;
1767 PyThread_acquire_lock(xidregistry->mutex, WAIT_LOCK);
1768 if (xidregistry->head == NULL) {
1769 _register_builtins_for_crossinterpreter_data(xidregistry);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001770 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001771 int res = _register_xidata(xidregistry, cls, getdata);
1772 PyThread_release_lock(xidregistry->mutex);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001773 return res;
1774}
1775
Eric Snow6d2cd902018-05-16 15:04:57 -04001776/* Cross-interpreter objects are looked up by exact match on the class.
1777 We can reassess this policy when we move from a global registry to a
1778 tp_* slot. */
1779
Eric Snow7f8bfc92018-01-29 18:23:44 -07001780crossinterpdatafunc
1781_PyCrossInterpreterData_Lookup(PyObject *obj)
1782{
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001783 struct _xidregistry *xidregistry = &_PyRuntime.xidregistry ;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001784 PyObject *cls = PyObject_Type(obj);
1785 crossinterpdatafunc getdata = NULL;
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001786 PyThread_acquire_lock(xidregistry->mutex, WAIT_LOCK);
1787 struct _xidregitem *cur = xidregistry->head;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001788 if (cur == NULL) {
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001789 _register_builtins_for_crossinterpreter_data(xidregistry);
1790 cur = xidregistry->head;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001791 }
1792 for(; cur != NULL; cur = cur->next) {
1793 if (cur->cls == (PyTypeObject *)cls) {
1794 getdata = cur->getdata;
1795 break;
1796 }
1797 }
Eric Snow4e9da0d2018-02-02 21:49:49 -07001798 Py_DECREF(cls);
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001799 PyThread_release_lock(xidregistry->mutex);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001800 return getdata;
1801}
1802
1803/* cross-interpreter data for builtin types */
1804
Eric Snow6d2cd902018-05-16 15:04:57 -04001805struct _shared_bytes_data {
1806 char *bytes;
1807 Py_ssize_t len;
1808};
1809
Eric Snow7f8bfc92018-01-29 18:23:44 -07001810static PyObject *
1811_new_bytes_object(_PyCrossInterpreterData *data)
1812{
Eric Snow6d2cd902018-05-16 15:04:57 -04001813 struct _shared_bytes_data *shared = (struct _shared_bytes_data *)(data->data);
1814 return PyBytes_FromStringAndSize(shared->bytes, shared->len);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001815}
1816
1817static int
1818_bytes_shared(PyObject *obj, _PyCrossInterpreterData *data)
1819{
Eric Snow6d2cd902018-05-16 15:04:57 -04001820 struct _shared_bytes_data *shared = PyMem_NEW(struct _shared_bytes_data, 1);
1821 if (PyBytes_AsStringAndSize(obj, &shared->bytes, &shared->len) < 0) {
1822 return -1;
1823 }
1824 data->data = (void *)shared;
Eric Snow63799132018-06-01 18:45:20 -06001825 Py_INCREF(obj);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001826 data->obj = obj; // Will be "released" (decref'ed) when data released.
1827 data->new_object = _new_bytes_object;
Eric Snow6d2cd902018-05-16 15:04:57 -04001828 data->free = PyMem_Free;
1829 return 0;
1830}
1831
1832struct _shared_str_data {
1833 int kind;
1834 const void *buffer;
1835 Py_ssize_t len;
1836};
1837
1838static PyObject *
1839_new_str_object(_PyCrossInterpreterData *data)
1840{
1841 struct _shared_str_data *shared = (struct _shared_str_data *)(data->data);
1842 return PyUnicode_FromKindAndData(shared->kind, shared->buffer, shared->len);
1843}
1844
1845static int
1846_str_shared(PyObject *obj, _PyCrossInterpreterData *data)
1847{
1848 struct _shared_str_data *shared = PyMem_NEW(struct _shared_str_data, 1);
1849 shared->kind = PyUnicode_KIND(obj);
1850 shared->buffer = PyUnicode_DATA(obj);
An Long29c11722020-06-13 20:26:01 +08001851 shared->len = PyUnicode_GET_LENGTH(obj);
Eric Snow6d2cd902018-05-16 15:04:57 -04001852 data->data = (void *)shared;
Eric Snow63799132018-06-01 18:45:20 -06001853 Py_INCREF(obj);
Eric Snow6d2cd902018-05-16 15:04:57 -04001854 data->obj = obj; // Will be "released" (decref'ed) when data released.
1855 data->new_object = _new_str_object;
1856 data->free = PyMem_Free;
1857 return 0;
1858}
1859
1860static PyObject *
1861_new_long_object(_PyCrossInterpreterData *data)
1862{
Alexey Izbyshev16f842d2019-02-12 19:06:43 +03001863 return PyLong_FromSsize_t((Py_ssize_t)(data->data));
Eric Snow6d2cd902018-05-16 15:04:57 -04001864}
1865
1866static int
1867_long_shared(PyObject *obj, _PyCrossInterpreterData *data)
1868{
Alexey Izbyshev16f842d2019-02-12 19:06:43 +03001869 /* Note that this means the size of shareable ints is bounded by
1870 * sys.maxsize. Hence on 32-bit architectures that is half the
1871 * size of maximum shareable ints on 64-bit.
1872 */
1873 Py_ssize_t value = PyLong_AsSsize_t(obj);
Eric Snow6d2cd902018-05-16 15:04:57 -04001874 if (value == -1 && PyErr_Occurred()) {
1875 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
1876 PyErr_SetString(PyExc_OverflowError, "try sending as bytes");
1877 }
1878 return -1;
1879 }
1880 data->data = (void *)value;
1881 data->obj = NULL;
1882 data->new_object = _new_long_object;
1883 data->free = NULL;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001884 return 0;
1885}
1886
1887static PyObject *
1888_new_none_object(_PyCrossInterpreterData *data)
1889{
1890 // XXX Singleton refcounts are problematic across interpreters...
1891 Py_INCREF(Py_None);
1892 return Py_None;
1893}
1894
1895static int
1896_none_shared(PyObject *obj, _PyCrossInterpreterData *data)
1897{
1898 data->data = NULL;
1899 // data->obj remains NULL
1900 data->new_object = _new_none_object;
1901 data->free = NULL; // There is nothing to free.
1902 return 0;
1903}
1904
1905static void
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001906_register_builtins_for_crossinterpreter_data(struct _xidregistry *xidregistry)
Eric Snow7f8bfc92018-01-29 18:23:44 -07001907{
1908 // None
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001909 if (_register_xidata(xidregistry, (PyTypeObject *)PyObject_Type(Py_None), _none_shared) != 0) {
Eric Snow7f8bfc92018-01-29 18:23:44 -07001910 Py_FatalError("could not register None for cross-interpreter sharing");
1911 }
1912
Eric Snow6d2cd902018-05-16 15:04:57 -04001913 // int
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001914 if (_register_xidata(xidregistry, &PyLong_Type, _long_shared) != 0) {
Eric Snow6d2cd902018-05-16 15:04:57 -04001915 Py_FatalError("could not register int for cross-interpreter sharing");
1916 }
1917
Eric Snow7f8bfc92018-01-29 18:23:44 -07001918 // bytes
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001919 if (_register_xidata(xidregistry, &PyBytes_Type, _bytes_shared) != 0) {
Eric Snow7f8bfc92018-01-29 18:23:44 -07001920 Py_FatalError("could not register bytes for cross-interpreter sharing");
1921 }
Eric Snow6d2cd902018-05-16 15:04:57 -04001922
1923 // str
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001924 if (_register_xidata(xidregistry, &PyUnicode_Type, _str_shared) != 0) {
Eric Snow6d2cd902018-05-16 15:04:57 -04001925 Py_FatalError("could not register str for cross-interpreter sharing");
1926 }
Eric Snow7f8bfc92018-01-29 18:23:44 -07001927}
1928
1929
Victor Stinner0b72b232020-03-12 23:18:39 +01001930_PyFrameEvalFunction
1931_PyInterpreterState_GetEvalFrameFunc(PyInterpreterState *interp)
1932{
1933 return interp->eval_frame;
1934}
1935
1936
1937void
1938_PyInterpreterState_SetEvalFrameFunc(PyInterpreterState *interp,
1939 _PyFrameEvalFunction eval_frame)
1940{
1941 interp->eval_frame = eval_frame;
1942}
1943
Victor Stinnerda7933e2020-04-13 03:04:28 +02001944
1945const PyConfig*
1946_PyInterpreterState_GetConfig(PyInterpreterState *interp)
1947{
1948 return &interp->config;
1949}
1950
1951
Victor Stinner048a3562020-11-05 00:45:56 +01001952int
1953_PyInterpreterState_GetConfigCopy(PyConfig *config)
Victor Stinnerda7933e2020-04-13 03:04:28 +02001954{
Victor Stinner048a3562020-11-05 00:45:56 +01001955 PyInterpreterState *interp = PyInterpreterState_Get();
1956
1957 PyStatus status = _PyConfig_Copy(config, &interp->config);
1958 if (PyStatus_Exception(status)) {
1959 _PyErr_SetFromPyStatus(status);
1960 return -1;
1961 }
1962 return 0;
Victor Stinnerda7933e2020-04-13 03:04:28 +02001963}
1964
1965
1966const PyConfig*
1967_Py_GetConfig(void)
1968{
1969 assert(PyGILState_Check());
1970 PyThreadState *tstate = _PyThreadState_GET();
1971 return _PyInterpreterState_GetConfig(tstate->interp);
1972}
1973
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001974#ifdef __cplusplus
1975}
1976#endif