blob: df98eb11bb0a49d72ecbae7b3fd6e2e2b2c429e0 [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);
Victor Stinner26881c82020-06-02 15:51:37 +0200150 int reinit_xidregistry = _PyThread_at_fork_reinit(&runtime->xidregistry.mutex);
Victor Stinnerba3d67c2020-12-26 00:41:46 +0100151 int reinit_unicode_ids = _PyThread_at_fork_reinit(&runtime->unicode_ids.lock);
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200152
153 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
154
Miss Islington (bot)1079b3e2021-11-17 13:16:01 -0800155 /* bpo-42540: id_mutex is freed by _PyInterpreterState_Delete, which does
156 * not force the default allocator. */
157 int reinit_main_id = _PyThread_at_fork_reinit(&runtime->interpreters.main->id_mutex);
158
Victor Stinner26881c82020-06-02 15:51:37 +0200159 if (reinit_interp < 0
160 || reinit_main_id < 0
Victor Stinnerba3d67c2020-12-26 00:41:46 +0100161 || reinit_xidregistry < 0
162 || reinit_unicode_ids < 0)
Victor Stinner26881c82020-06-02 15:51:37 +0200163 {
164 return _PyStatus_ERR("Failed to reinitialize runtime locks");
Eric Snow8479a342019-03-08 23:44:33 -0700165
Eric Snow8479a342019-03-08 23:44:33 -0700166 }
Victor Stinner26881c82020-06-02 15:51:37 +0200167 return _PyStatus_OK();
Eric Snow8479a342019-03-08 23:44:33 -0700168}
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900169#endif
Eric Snow8479a342019-03-08 23:44:33 -0700170
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200171#define HEAD_LOCK(runtime) \
172 PyThread_acquire_lock((runtime)->interpreters.mutex, WAIT_LOCK)
173#define HEAD_UNLOCK(runtime) \
174 PyThread_release_lock((runtime)->interpreters.mutex)
Eric Snow05351c12017-09-05 21:43:08 -0700175
Victor Stinner8bb32302019-04-24 16:47:40 +0200176/* Forward declaration */
177static void _PyGILState_NoteThreadState(
178 struct _gilstate_runtime_state *gilstate, PyThreadState* tstate);
Michael W. Hudson188d4362005-06-20 16:52:57 +0000179
Victor Stinner331a6a52019-05-27 16:39:22 +0200180PyStatus
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600181_PyInterpreterState_Enable(_PyRuntimeState *runtime)
Eric Snowe3774162017-05-22 19:46:40 -0700182{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200183 struct pyinterpreters *interpreters = &runtime->interpreters;
184 interpreters->next_id = 0;
Victor Stinner5d926472018-03-06 14:31:37 +0100185
186 /* Py_Finalize() calls _PyRuntimeState_Fini() which clears the mutex.
187 Create a new mutex if needed. */
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200188 if (interpreters->mutex == NULL) {
Victor Stinner5d926472018-03-06 14:31:37 +0100189 /* Force default allocator, since _PyRuntimeState_Fini() must
190 use the same allocator than this function. */
191 PyMemAllocatorEx old_alloc;
192 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
193
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200194 interpreters->mutex = PyThread_allocate_lock();
Victor Stinner5d926472018-03-06 14:31:37 +0100195
196 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
197
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200198 if (interpreters->mutex == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200199 return _PyStatus_ERR("Can't initialize threads for interpreter");
Victor Stinnera7368ac2017-11-15 18:11:45 -0800200 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600201 }
Victor Stinner5d926472018-03-06 14:31:37 +0100202
Victor Stinner331a6a52019-05-27 16:39:22 +0200203 return _PyStatus_OK();
Eric Snowe3774162017-05-22 19:46:40 -0700204}
Guido van Rossuma027efa1997-05-05 20:56:21 +0000205
206PyInterpreterState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000207PyInterpreterState_New(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000208{
Victor Stinner71a35222020-03-26 22:46:14 +0100209 PyThreadState *tstate = _PyThreadState_GET();
210 /* tstate is NULL when Py_InitializeFromConfig() calls
211 PyInterpreterState_New() to create the main interpreter. */
212 if (_PySys_Audit(tstate, "cpython.PyInterpreterState_New", NULL) < 0) {
Steve Dowerb82e17e2019-05-23 08:45:22 -0700213 return NULL;
214 }
215
Andy Lester7668a8b2020-03-24 23:26:44 -0500216 PyInterpreterState *interp = PyMem_RawCalloc(1, sizeof(PyInterpreterState));
Victor Stinnerd4341102017-11-23 00:12:09 +0100217 if (interp == NULL) {
218 return NULL;
219 }
220
Eric Snow4c6955e2018-02-16 18:53:40 -0700221 interp->id_refcount = -1;
Victor Stinner022be022019-05-22 23:58:50 +0200222
Victor Stinner71a35222020-03-26 22:46:14 +0100223 /* Don't get runtime from tstate since tstate can be NULL */
Victor Stinner01b1cc12019-11-20 02:27:56 +0100224 _PyRuntimeState *runtime = &_PyRuntime;
225 interp->runtime = runtime;
226
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200227 if (_PyEval_InitState(&interp->ceval) < 0) {
228 goto out_of_memory;
229 }
230
Victor Stinner72474072019-11-20 12:25:50 +0100231 _PyGC_InitState(&interp->gc);
Victor Stinner8462a492019-10-01 12:06:16 +0200232 PyConfig_InitPythonConfig(&interp->config);
Victor Stinner41010182020-12-26 01:45:43 +0100233 _PyType_InitCache(interp);
Victor Stinner022be022019-05-22 23:58:50 +0200234
Victor Stinnerd4341102017-11-23 00:12:09 +0100235 interp->eval_frame = _PyEval_EvalFrameDefault;
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000236#ifdef HAVE_DLOPEN
Serhiy Storchakac2f7d872016-05-04 09:44:44 +0300237#if HAVE_DECL_RTLD_NOW
Victor Stinnerd4341102017-11-23 00:12:09 +0100238 interp->dlopenflags = RTLD_NOW;
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000239#else
Victor Stinnerd4341102017-11-23 00:12:09 +0100240 interp->dlopenflags = RTLD_LAZY;
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000241#endif
242#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +0000243
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200244 struct pyinterpreters *interpreters = &runtime->interpreters;
245
246 HEAD_LOCK(runtime);
247 if (interpreters->next_id < 0) {
Victor Stinnerd4341102017-11-23 00:12:09 +0100248 /* overflow or Py_Initialize() not called! */
Victor Stinner71a35222020-03-26 22:46:14 +0100249 if (tstate != NULL) {
250 _PyErr_SetString(tstate, PyExc_RuntimeError,
251 "failed to get an interpreter ID");
252 }
Pablo Galindo95d630e2018-08-31 22:49:29 +0100253 PyMem_RawFree(interp);
Victor Stinnerd4341102017-11-23 00:12:09 +0100254 interp = NULL;
Victor Stinnerd4341102017-11-23 00:12:09 +0100255 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200256 else {
257 interp->id = interpreters->next_id;
258 interpreters->next_id += 1;
259 interp->next = interpreters->head;
260 if (interpreters->main == NULL) {
261 interpreters->main = interp;
262 }
263 interpreters->head = interp;
264 }
265 HEAD_UNLOCK(runtime);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000266
Pablo Galindo95d630e2018-08-31 22:49:29 +0100267 if (interp == NULL) {
268 return NULL;
269 }
270
Yury Selivanovf23746a2018-01-22 19:11:18 -0500271 interp->tstate_next_unique_id = 0;
272
Steve Dowerb82e17e2019-05-23 08:45:22 -0700273 interp->audit_hooks = NULL;
274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 return interp;
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200276
277out_of_memory:
278 if (tstate != NULL) {
279 _PyErr_NoMemory(tstate);
280 }
281
282 PyMem_RawFree(interp);
283 return NULL;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000284}
285
286
Victor Stinnereba5bf22020-10-30 22:51:02 +0100287static void
288interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000289{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100290 _PyRuntimeState *runtime = interp->runtime;
291
Victor Stinner71a35222020-03-26 22:46:14 +0100292 if (_PySys_Audit(tstate, "cpython.PyInterpreterState_Clear", NULL) < 0) {
293 _PyErr_Clear(tstate);
Steve Dowerb82e17e2019-05-23 08:45:22 -0700294 }
295
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200296 HEAD_LOCK(runtime);
297 for (PyThreadState *p = interp->tstate_head; p != NULL; p = p->next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 PyThreadState_Clear(p);
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200299 }
300 HEAD_UNLOCK(runtime);
Steve Dowerb82e17e2019-05-23 08:45:22 -0700301
302 Py_CLEAR(interp->audit_hooks);
303
Victor Stinner331a6a52019-05-27 16:39:22 +0200304 PyConfig_Clear(&interp->config);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 Py_CLEAR(interp->codec_search_path);
306 Py_CLEAR(interp->codec_search_cache);
307 Py_CLEAR(interp->codec_error_registry);
Eric Snow93c92f72017-09-13 23:46:04 -0700308 Py_CLEAR(interp->modules);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 Py_CLEAR(interp->modules_by_index);
Serhiy Storchaka87a5c512014-02-10 18:21:34 +0200310 Py_CLEAR(interp->builtins_copy);
Brett Cannonfd074152012-04-14 14:10:13 -0400311 Py_CLEAR(interp->importlib);
Serhiy Storchaka133138a2016-08-02 22:51:21 +0300312 Py_CLEAR(interp->import_func);
Eric Snowd2fdd1f2019-03-15 17:47:43 -0600313 Py_CLEAR(interp->dict);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200314#ifdef HAVE_FORK
315 Py_CLEAR(interp->before_forkers);
316 Py_CLEAR(interp->after_forkers_parent);
317 Py_CLEAR(interp->after_forkers_child);
318#endif
Victor Stinnerfd957c12020-11-03 18:07:15 +0100319
320 _PyAST_Fini(interp);
321 _PyWarnings_Fini(interp);
Victor Stinnerb8fa1352020-12-15 14:34:19 +0100322 _PyAtExit_Fini(interp);
Victor Stinnerfd957c12020-11-03 18:07:15 +0100323
324 // All Python types must be destroyed before the last GC collection. Python
325 // types create a reference cycle to themselves in their in their
326 // PyTypeObject.tp_mro member (the tuple contains the type).
Victor Stinnereba5bf22020-10-30 22:51:02 +0100327
328 /* Last garbage collection on this interpreter */
329 _PyGC_CollectNoFail(tstate);
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100330 _PyGC_Fini(interp);
Victor Stinnereba5bf22020-10-30 22:51:02 +0100331
Hai Shi8ecc0c42020-08-13 05:23:30 +0800332 /* We don't clear sysdict and builtins until the end of this function.
333 Because clearing other attributes can execute arbitrary Python code
334 which requires sysdict and builtins. */
335 PyDict_Clear(interp->sysdict);
336 PyDict_Clear(interp->builtins);
337 Py_CLEAR(interp->sysdict);
338 Py_CLEAR(interp->builtins);
339
Eric Snow5be45a62019-03-08 22:47:07 -0700340 // XXX Once we have one allocator per interpreter (i.e.
341 // per-interpreter GC) we must ensure that all of the interpreter's
342 // objects have been cleaned up at the point.
Guido van Rossum25ce5661997-08-02 03:10:38 +0000343}
344
345
Victor Stinnereba5bf22020-10-30 22:51:02 +0100346void
347PyInterpreterState_Clear(PyInterpreterState *interp)
348{
349 // Use the current Python thread state to call audit hooks and to collect
350 // garbage. It can be different than the current Python thread state
351 // of 'interp'.
352 PyThreadState *current_tstate = _PyThreadState_GET();
353
354 interpreter_clear(interp, current_tstate);
355}
356
357
358void
359_PyInterpreterState_Clear(PyThreadState *tstate)
360{
361 interpreter_clear(tstate->interp, tstate);
362}
363
364
Guido van Rossum25ce5661997-08-02 03:10:38 +0000365static void
Victor Stinner9da74302019-11-20 11:17:17 +0100366zapthreads(PyInterpreterState *interp, int check_current)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000367{
Victor Stinner9da74302019-11-20 11:17:17 +0100368 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 /* No need to lock the mutex here because this should only happen
370 when the threads are all really dead (XXX famous last words). */
Victor Stinner9da74302019-11-20 11:17:17 +0100371 while ((tstate = interp->tstate_head) != NULL) {
372 _PyThreadState_Delete(tstate, check_current);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000374}
375
376
Victor Stinner01b1cc12019-11-20 02:27:56 +0100377void
378PyInterpreterState_Delete(PyInterpreterState *interp)
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200379{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100380 _PyRuntimeState *runtime = interp->runtime;
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200381 struct pyinterpreters *interpreters = &runtime->interpreters;
Victor Stinner9da74302019-11-20 11:17:17 +0100382 zapthreads(interp, 0);
383
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200384 _PyEval_FiniState(&interp->ceval);
385
Victor Stinner9da74302019-11-20 11:17:17 +0100386 /* Delete current thread. After this, many C API calls become crashy. */
387 _PyThreadState_Swap(&runtime->gilstate, NULL);
388
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200389 HEAD_LOCK(runtime);
390 PyInterpreterState **p;
391 for (p = &interpreters->head; ; p = &(*p)->next) {
392 if (*p == NULL) {
Victor Stinner87d3b9d2020-03-25 19:27:36 +0100393 Py_FatalError("NULL interpreter");
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200394 }
395 if (*p == interp) {
396 break;
397 }
398 }
399 if (interp->tstate_head != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100400 Py_FatalError("remaining threads");
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200401 }
402 *p = interp->next;
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200403
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200404 if (interpreters->main == interp) {
405 interpreters->main = NULL;
406 if (interpreters->head != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100407 Py_FatalError("remaining subinterpreters");
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200408 }
409 }
410 HEAD_UNLOCK(runtime);
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200411
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200412 if (interp->id_mutex != NULL) {
413 PyThread_free_lock(interp->id_mutex);
414 }
415 PyMem_RawFree(interp);
416}
417
418
Victor Stinner26881c82020-06-02 15:51:37 +0200419#ifdef HAVE_FORK
Eric Snow59032962018-09-14 14:17:20 -0700420/*
421 * Delete all interpreter states except the main interpreter. If there
422 * is a current interpreter state, it *must* be the main interpreter.
423 */
Victor Stinner26881c82020-06-02 15:51:37 +0200424PyStatus
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200425_PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime)
Eric Snow59032962018-09-14 14:17:20 -0700426{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200427 struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200428 struct pyinterpreters *interpreters = &runtime->interpreters;
429
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200430 PyThreadState *tstate = _PyThreadState_Swap(gilstate, NULL);
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200431 if (tstate != NULL && tstate->interp != interpreters->main) {
Victor Stinner26881c82020-06-02 15:51:37 +0200432 return _PyStatus_ERR("not main interpreter");
Eric Snow59032962018-09-14 14:17:20 -0700433 }
434
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200435 HEAD_LOCK(runtime);
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200436 PyInterpreterState *interp = interpreters->head;
437 interpreters->head = NULL;
Stéphane Wirtelb5409da2019-02-20 15:27:22 +0100438 while (interp != NULL) {
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200439 if (interp == interpreters->main) {
440 interpreters->main->next = NULL;
441 interpreters->head = interp;
Stéphane Wirtelb5409da2019-02-20 15:27:22 +0100442 interp = interp->next;
Eric Snow59032962018-09-14 14:17:20 -0700443 continue;
444 }
445
Victor Stinner01b1cc12019-11-20 02:27:56 +0100446 PyInterpreterState_Clear(interp); // XXX must activate?
Victor Stinner9da74302019-11-20 11:17:17 +0100447 zapthreads(interp, 1);
Eric Snow59032962018-09-14 14:17:20 -0700448 if (interp->id_mutex != NULL) {
449 PyThread_free_lock(interp->id_mutex);
450 }
Stéphane Wirtelb5409da2019-02-20 15:27:22 +0100451 PyInterpreterState *prev_interp = interp;
452 interp = interp->next;
453 PyMem_RawFree(prev_interp);
Eric Snow59032962018-09-14 14:17:20 -0700454 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200455 HEAD_UNLOCK(runtime);
Eric Snow59032962018-09-14 14:17:20 -0700456
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200457 if (interpreters->head == NULL) {
Victor Stinner26881c82020-06-02 15:51:37 +0200458 return _PyStatus_ERR("missing main interpreter");
Eric Snow59032962018-09-14 14:17:20 -0700459 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200460 _PyThreadState_Swap(gilstate, tstate);
Victor Stinner26881c82020-06-02 15:51:37 +0200461 return _PyStatus_OK();
Eric Snow59032962018-09-14 14:17:20 -0700462}
Victor Stinner26881c82020-06-02 15:51:37 +0200463#endif
Eric Snow59032962018-09-14 14:17:20 -0700464
465
Victor Stinnercaba55b2018-08-03 15:33:52 +0200466PyInterpreterState *
Victor Stinnerbe793732020-03-13 18:15:33 +0100467PyInterpreterState_Get(void)
Victor Stinnercaba55b2018-08-03 15:33:52 +0200468{
Victor Stinner50b48572018-11-01 01:51:40 +0100469 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner3026cad2020-06-01 16:02:40 +0200470 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnercaba55b2018-08-03 15:33:52 +0200471 PyInterpreterState *interp = tstate->interp;
472 if (interp == NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100473 Py_FatalError("no current interpreter");
Victor Stinnercaba55b2018-08-03 15:33:52 +0200474 }
475 return interp;
476}
477
478
Eric Snowe3774162017-05-22 19:46:40 -0700479int64_t
480PyInterpreterState_GetID(PyInterpreterState *interp)
481{
482 if (interp == NULL) {
483 PyErr_SetString(PyExc_RuntimeError, "no interpreter provided");
484 return -1;
485 }
486 return interp->id;
487}
488
489
Eric Snow5be45a62019-03-08 22:47:07 -0700490static PyInterpreterState *
Victor Stinner1a1bd2e2020-04-17 19:13:06 +0200491interp_look_up_id(_PyRuntimeState *runtime, int64_t requested_id)
Eric Snowb05b7112019-03-01 12:35:10 -0700492{
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200493 PyInterpreterState *interp = runtime->interpreters.head;
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100494 while (interp != NULL) {
Victor Stinner1a1bd2e2020-04-17 19:13:06 +0200495 int64_t id = PyInterpreterState_GetID(interp);
Eric Snow5be45a62019-03-08 22:47:07 -0700496 if (id < 0) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100497 return NULL;
Eric Snow5be45a62019-03-08 22:47:07 -0700498 }
499 if (requested_id == id) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100500 return interp;
Eric Snow5be45a62019-03-08 22:47:07 -0700501 }
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100502 interp = PyInterpreterState_Next(interp);
Eric Snowb05b7112019-03-01 12:35:10 -0700503 }
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100504 return NULL;
Eric Snowb05b7112019-03-01 12:35:10 -0700505}
506
Eric Snow5be45a62019-03-08 22:47:07 -0700507PyInterpreterState *
Victor Stinner1a1bd2e2020-04-17 19:13:06 +0200508_PyInterpreterState_LookUpID(int64_t requested_id)
Eric Snow5be45a62019-03-08 22:47:07 -0700509{
510 PyInterpreterState *interp = NULL;
511 if (requested_id >= 0) {
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200512 _PyRuntimeState *runtime = &_PyRuntime;
513 HEAD_LOCK(runtime);
514 interp = interp_look_up_id(runtime, requested_id);
515 HEAD_UNLOCK(runtime);
Eric Snow5be45a62019-03-08 22:47:07 -0700516 }
517 if (interp == NULL && !PyErr_Occurred()) {
518 PyErr_Format(PyExc_RuntimeError,
519 "unrecognized interpreter ID %lld", requested_id);
520 }
521 return interp;
522}
523
Eric Snow4c6955e2018-02-16 18:53:40 -0700524
525int
526_PyInterpreterState_IDInitref(PyInterpreterState *interp)
527{
528 if (interp->id_mutex != NULL) {
529 return 0;
530 }
531 interp->id_mutex = PyThread_allocate_lock();
532 if (interp->id_mutex == NULL) {
533 PyErr_SetString(PyExc_RuntimeError,
534 "failed to create init interpreter ID mutex");
535 return -1;
536 }
537 interp->id_refcount = 0;
538 return 0;
539}
540
541
Victor Stinner32c5a172021-04-28 13:40:44 +0200542int
Eric Snow4c6955e2018-02-16 18:53:40 -0700543_PyInterpreterState_IDIncref(PyInterpreterState *interp)
544{
Victor Stinner32c5a172021-04-28 13:40:44 +0200545 if (_PyInterpreterState_IDInitref(interp) < 0) {
546 return -1;
Eric Snow4c6955e2018-02-16 18:53:40 -0700547 }
Victor Stinner32c5a172021-04-28 13:40:44 +0200548
Eric Snow4c6955e2018-02-16 18:53:40 -0700549 PyThread_acquire_lock(interp->id_mutex, WAIT_LOCK);
550 interp->id_refcount += 1;
551 PyThread_release_lock(interp->id_mutex);
Victor Stinner32c5a172021-04-28 13:40:44 +0200552 return 0;
Eric Snow4c6955e2018-02-16 18:53:40 -0700553}
554
555
556void
557_PyInterpreterState_IDDecref(PyInterpreterState *interp)
558{
Victor Stinner32c5a172021-04-28 13:40:44 +0200559 assert(interp->id_mutex != NULL);
560
Victor Stinner0fd2c302019-06-04 03:15:09 +0200561 struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
Eric Snow4c6955e2018-02-16 18:53:40 -0700562 PyThread_acquire_lock(interp->id_mutex, WAIT_LOCK);
563 assert(interp->id_refcount != 0);
564 interp->id_refcount -= 1;
565 int64_t refcount = interp->id_refcount;
566 PyThread_release_lock(interp->id_mutex);
567
Eric Snowc11183c2019-03-15 16:35:46 -0600568 if (refcount == 0 && interp->requires_idref) {
Eric Snowf53d9f22018-02-20 16:30:17 -0700569 // XXX Using the "head" thread isn't strictly correct.
570 PyThreadState *tstate = PyInterpreterState_ThreadHead(interp);
571 // XXX Possible GILState issues?
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200572 PyThreadState *save_tstate = _PyThreadState_Swap(gilstate, tstate);
Eric Snow4c6955e2018-02-16 18:53:40 -0700573 Py_EndInterpreter(tstate);
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200574 _PyThreadState_Swap(gilstate, save_tstate);
Eric Snow4c6955e2018-02-16 18:53:40 -0700575 }
576}
577
Eric Snowc11183c2019-03-15 16:35:46 -0600578int
579_PyInterpreterState_RequiresIDRef(PyInterpreterState *interp)
580{
581 return interp->requires_idref;
582}
583
584void
585_PyInterpreterState_RequireIDRef(PyInterpreterState *interp, int required)
586{
587 interp->requires_idref = required ? 1 : 0;
588}
589
Eric Snowc11183c2019-03-15 16:35:46 -0600590PyObject *
591_PyInterpreterState_GetMainModule(PyInterpreterState *interp)
592{
593 if (interp->modules == NULL) {
594 PyErr_SetString(PyExc_RuntimeError, "interpreter not initialized");
595 return NULL;
596 }
597 return PyMapping_GetItemString(interp->modules, "__main__");
598}
599
Eric Snowd2fdd1f2019-03-15 17:47:43 -0600600PyObject *
601PyInterpreterState_GetDict(PyInterpreterState *interp)
602{
603 if (interp->dict == NULL) {
604 interp->dict = PyDict_New();
605 if (interp->dict == NULL) {
606 PyErr_Clear();
607 }
608 }
609 /* Returning NULL means no per-interpreter dict is available. */
610 return interp->dict;
611}
612
Victor Stinner45b9be52010-03-03 23:28:07 +0000613static PyThreadState *
614new_threadstate(PyInterpreterState *interp, int init)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000615{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100616 _PyRuntimeState *runtime = interp->runtime;
Victor Stinner1a7425f2013-07-07 16:25:15 +0200617 PyThreadState *tstate = (PyThreadState *)PyMem_RawMalloc(sizeof(PyThreadState));
Victor Stinner8bb32302019-04-24 16:47:40 +0200618 if (tstate == NULL) {
619 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000621
Victor Stinner8bb32302019-04-24 16:47:40 +0200622 tstate->interp = interp;
623
624 tstate->frame = NULL;
625 tstate->recursion_depth = 0;
Mark Shannon4e7a69b2020-12-02 13:30:55 +0000626 tstate->recursion_headroom = 0;
Victor Stinner8bb32302019-04-24 16:47:40 +0200627 tstate->stackcheck_counter = 0;
628 tstate->tracing = 0;
Mark Shannon9e7b2072021-04-13 11:08:14 +0100629 tstate->root_cframe.use_tracing = 0;
630 tstate->cframe = &tstate->root_cframe;
Victor Stinner8bb32302019-04-24 16:47:40 +0200631 tstate->gilstate_counter = 0;
632 tstate->async_exc = NULL;
633 tstate->thread_id = PyThread_get_thread_ident();
634
635 tstate->dict = NULL;
636
637 tstate->curexc_type = NULL;
638 tstate->curexc_value = NULL;
639 tstate->curexc_traceback = NULL;
640
641 tstate->exc_state.exc_type = NULL;
642 tstate->exc_state.exc_value = NULL;
643 tstate->exc_state.exc_traceback = NULL;
644 tstate->exc_state.previous_item = NULL;
645 tstate->exc_info = &tstate->exc_state;
646
647 tstate->c_profilefunc = NULL;
648 tstate->c_tracefunc = NULL;
649 tstate->c_profileobj = NULL;
650 tstate->c_traceobj = NULL;
651
652 tstate->trash_delete_nesting = 0;
653 tstate->trash_delete_later = NULL;
654 tstate->on_delete = NULL;
655 tstate->on_delete_data = NULL;
656
657 tstate->coroutine_origin_tracking_depth = 0;
658
Victor Stinner8bb32302019-04-24 16:47:40 +0200659 tstate->async_gen_firstiter = NULL;
660 tstate->async_gen_finalizer = NULL;
661
662 tstate->context = NULL;
663 tstate->context_ver = 1;
664
Victor Stinner8bb32302019-04-24 16:47:40 +0200665 if (init) {
Victor Stinner01b1cc12019-11-20 02:27:56 +0100666 _PyThreadState_Init(tstate);
Victor Stinner8bb32302019-04-24 16:47:40 +0200667 }
668
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200669 HEAD_LOCK(runtime);
Stefan Krahb3b9ade2020-03-02 21:22:36 +0100670 tstate->id = ++interp->tstate_next_unique_id;
Victor Stinner8bb32302019-04-24 16:47:40 +0200671 tstate->prev = NULL;
672 tstate->next = interp->tstate_head;
673 if (tstate->next)
674 tstate->next->prev = tstate;
675 interp->tstate_head = tstate;
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200676 HEAD_UNLOCK(runtime);
Victor Stinner8bb32302019-04-24 16:47:40 +0200677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 return tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000679}
680
Victor Stinner45b9be52010-03-03 23:28:07 +0000681PyThreadState *
682PyThreadState_New(PyInterpreterState *interp)
683{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 return new_threadstate(interp, 1);
Victor Stinner45b9be52010-03-03 23:28:07 +0000685}
686
687PyThreadState *
688_PyThreadState_Prealloc(PyInterpreterState *interp)
689{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 return new_threadstate(interp, 0);
Victor Stinner45b9be52010-03-03 23:28:07 +0000691}
692
693void
Victor Stinner01b1cc12019-11-20 02:27:56 +0100694_PyThreadState_Init(PyThreadState *tstate)
Victor Stinner45b9be52010-03-03 23:28:07 +0000695{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100696 _PyGILState_NoteThreadState(&tstate->interp->runtime->gilstate, tstate);
Victor Stinner45b9be52010-03-03 23:28:07 +0000697}
698
Martin v. Löwis1a214512008-06-11 05:26:20 +0000699PyObject*
Martin v. Löwis7800f752012-06-22 12:20:55 +0200700PyState_FindModule(struct PyModuleDef* module)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000701{
Martin v. Löwis7800f752012-06-22 12:20:55 +0200702 Py_ssize_t index = module->m_base.m_index;
Victor Stinner81a7be32020-04-14 15:14:01 +0200703 PyInterpreterState *state = _PyInterpreterState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 PyObject *res;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000705 if (module->m_slots) {
706 return NULL;
707 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 if (index == 0)
709 return NULL;
710 if (state->modules_by_index == NULL)
711 return NULL;
Antoine Pitrou75506e82012-08-20 19:30:46 +0200712 if (index >= PyList_GET_SIZE(state->modules_by_index))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 return NULL;
714 res = PyList_GET_ITEM(state->modules_by_index, index);
715 return res==Py_None ? NULL : res;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000716}
717
718int
Victor Stinner82c83bd2019-11-22 18:52:27 +0100719_PyState_AddModule(PyThreadState *tstate, PyObject* module, struct PyModuleDef* def)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000720{
Berker Peksag4b7b5652016-08-22 18:05:56 +0300721 if (!def) {
Victor Stinner71a35222020-03-26 22:46:14 +0100722 assert(_PyErr_Occurred(tstate));
Berker Peksag4b7b5652016-08-22 18:05:56 +0300723 return -1;
724 }
Nick Coghland5cacbb2015-05-23 22:24:10 +1000725 if (def->m_slots) {
Victor Stinner71a35222020-03-26 22:46:14 +0100726 _PyErr_SetString(tstate,
727 PyExc_SystemError,
728 "PyState_AddModule called on module with slots");
Nick Coghland5cacbb2015-05-23 22:24:10 +1000729 return -1;
730 }
Victor Stinner82c83bd2019-11-22 18:52:27 +0100731
732 PyInterpreterState *interp = tstate->interp;
733 if (!interp->modules_by_index) {
734 interp->modules_by_index = PyList_New(0);
735 if (!interp->modules_by_index) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 return -1;
Victor Stinner82c83bd2019-11-22 18:52:27 +0100737 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 }
Victor Stinner82c83bd2019-11-22 18:52:27 +0100739
740 while (PyList_GET_SIZE(interp->modules_by_index) <= def->m_base.m_index) {
741 if (PyList_Append(interp->modules_by_index, Py_None) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 return -1;
Victor Stinner82c83bd2019-11-22 18:52:27 +0100743 }
744 }
745
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 Py_INCREF(module);
Victor Stinner82c83bd2019-11-22 18:52:27 +0100747 return PyList_SetItem(interp->modules_by_index,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000748 def->m_base.m_index, module);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000749}
Guido van Rossuma027efa1997-05-05 20:56:21 +0000750
Martin v. Löwis7800f752012-06-22 12:20:55 +0200751int
752PyState_AddModule(PyObject* module, struct PyModuleDef* def)
753{
Martin v. Löwis7800f752012-06-22 12:20:55 +0200754 if (!def) {
Victor Stinner87d3b9d2020-03-25 19:27:36 +0100755 Py_FatalError("module definition is NULL");
Martin v. Löwis7800f752012-06-22 12:20:55 +0200756 return -1;
757 }
Victor Stinner82c83bd2019-11-22 18:52:27 +0100758
759 PyThreadState *tstate = _PyThreadState_GET();
760 PyInterpreterState *interp = tstate->interp;
761 Py_ssize_t index = def->m_base.m_index;
762 if (interp->modules_by_index &&
763 index < PyList_GET_SIZE(interp->modules_by_index) &&
764 module == PyList_GET_ITEM(interp->modules_by_index, index))
765 {
Victor Stinner87d3b9d2020-03-25 19:27:36 +0100766 _Py_FatalErrorFormat(__func__, "module %p already added", module);
Benjamin Peterson39de95b2019-09-12 00:43:22 +0100767 return -1;
Martin v. Löwis7800f752012-06-22 12:20:55 +0200768 }
Victor Stinner82c83bd2019-11-22 18:52:27 +0100769 return _PyState_AddModule(tstate, module, def);
Martin v. Löwis7800f752012-06-22 12:20:55 +0200770}
771
772int
773PyState_RemoveModule(struct PyModuleDef* def)
774{
Victor Stinner71a35222020-03-26 22:46:14 +0100775 PyThreadState *tstate = _PyThreadState_GET();
776 PyInterpreterState *interp = tstate->interp;
777
Nick Coghland5cacbb2015-05-23 22:24:10 +1000778 if (def->m_slots) {
Victor Stinner71a35222020-03-26 22:46:14 +0100779 _PyErr_SetString(tstate,
780 PyExc_SystemError,
781 "PyState_RemoveModule called on module with slots");
Nick Coghland5cacbb2015-05-23 22:24:10 +1000782 return -1;
783 }
Victor Stinner71a35222020-03-26 22:46:14 +0100784
785 Py_ssize_t index = def->m_base.m_index;
Martin v. Löwis7800f752012-06-22 12:20:55 +0200786 if (index == 0) {
Victor Stinner87d3b9d2020-03-25 19:27:36 +0100787 Py_FatalError("invalid module index");
Martin v. Löwis7800f752012-06-22 12:20:55 +0200788 }
Victor Stinner71a35222020-03-26 22:46:14 +0100789 if (interp->modules_by_index == NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100790 Py_FatalError("Interpreters module-list not accessible.");
Martin v. Löwis7800f752012-06-22 12:20:55 +0200791 }
Victor Stinner71a35222020-03-26 22:46:14 +0100792 if (index > PyList_GET_SIZE(interp->modules_by_index)) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100793 Py_FatalError("Module index out of bounds.");
Martin v. Löwis7800f752012-06-22 12:20:55 +0200794 }
Victor Stinner71a35222020-03-26 22:46:14 +0100795
Zackery Spytz2a893432018-12-05 00:14:00 -0700796 Py_INCREF(Py_None);
Victor Stinner71a35222020-03-26 22:46:14 +0100797 return PyList_SetItem(interp->modules_by_index, index, Py_None);
Martin v. Löwis7800f752012-06-22 12:20:55 +0200798}
799
Victor Stinner048a3562020-11-05 00:45:56 +0100800// Used by finalize_modules()
Antoine Pitrou40322e62013-08-11 00:30:09 +0200801void
Victor Stinner0a28f8d2019-06-19 02:54:39 +0200802_PyInterpreterState_ClearModules(PyInterpreterState *interp)
Antoine Pitrou40322e62013-08-11 00:30:09 +0200803{
Victor Stinner0a28f8d2019-06-19 02:54:39 +0200804 if (!interp->modules_by_index) {
805 return;
806 }
807
808 Py_ssize_t i;
809 for (i = 0; i < PyList_GET_SIZE(interp->modules_by_index); i++) {
810 PyObject *m = PyList_GET_ITEM(interp->modules_by_index, i);
811 if (PyModule_Check(m)) {
812 /* cleanup the saved copy of module dicts */
813 PyModuleDef *md = PyModule_GetDef(m);
814 if (md) {
815 Py_CLEAR(md->m_base.m_copy);
Antoine Pitrou40322e62013-08-11 00:30:09 +0200816 }
817 }
Victor Stinner0a28f8d2019-06-19 02:54:39 +0200818 }
819
820 /* Setting modules_by_index to NULL could be dangerous, so we
821 clear the list instead. */
822 if (PyList_SetSlice(interp->modules_by_index,
823 0, PyList_GET_SIZE(interp->modules_by_index),
824 NULL)) {
825 PyErr_WriteUnraisable(interp->modules_by_index);
Antoine Pitrou40322e62013-08-11 00:30:09 +0200826 }
827}
828
Guido van Rossuma027efa1997-05-05 20:56:21 +0000829void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000830PyThreadState_Clear(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000831{
Victor Stinnerda7933e2020-04-13 03:04:28 +0200832 int verbose = _PyInterpreterState_GetConfig(tstate->interp)->verbose;
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200833
Victor Stinner5804f872020-03-24 16:32:26 +0100834 if (verbose && tstate->frame != NULL) {
835 /* bpo-20526: After the main thread calls
836 _PyRuntimeState_SetFinalizing() in Py_FinalizeEx(), threads must
837 exit when trying to take the GIL. If a thread exit in the middle of
838 _PyEval_EvalFrameDefault(), tstate->frame is not reset to its
839 previous value. It is more likely with daemon threads, but it can
840 happen with regular threads if threading._shutdown() fails
841 (ex: interrupted by CTRL+C). */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 fprintf(stderr,
843 "PyThreadState_Clear: warning: thread still has a frame\n");
Victor Stinner5804f872020-03-24 16:32:26 +0100844 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000845
Victor Stinner5804f872020-03-24 16:32:26 +0100846 /* Don't clear tstate->frame: it is a borrowed reference */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000847
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 Py_CLEAR(tstate->dict);
849 Py_CLEAR(tstate->async_exc);
Guido van Rossumede04391998-04-10 20:18:25 +0000850
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 Py_CLEAR(tstate->curexc_type);
852 Py_CLEAR(tstate->curexc_value);
853 Py_CLEAR(tstate->curexc_traceback);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000854
Mark Shannonae3087c2017-10-22 22:41:51 +0100855 Py_CLEAR(tstate->exc_state.exc_type);
856 Py_CLEAR(tstate->exc_state.exc_value);
857 Py_CLEAR(tstate->exc_state.exc_traceback);
Serhiy Storchakabdf42982017-10-26 16:59:40 +0300858
Mark Shannonae3087c2017-10-22 22:41:51 +0100859 /* The stack of exception states should contain just this thread. */
Victor Stinner53b7d4e2018-07-25 01:37:05 +0200860 if (verbose && tstate->exc_info != &tstate->exc_state) {
Mark Shannonae3087c2017-10-22 22:41:51 +0100861 fprintf(stderr,
862 "PyThreadState_Clear: warning: thread still has a generator\n");
863 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 tstate->c_profilefunc = NULL;
866 tstate->c_tracefunc = NULL;
867 Py_CLEAR(tstate->c_profileobj);
868 Py_CLEAR(tstate->c_traceobj);
Yury Selivanov75445082015-05-11 22:57:16 -0400869
Yury Selivanoveb636452016-09-08 22:01:51 -0700870 Py_CLEAR(tstate->async_gen_firstiter);
871 Py_CLEAR(tstate->async_gen_finalizer);
Yury Selivanovf23746a2018-01-22 19:11:18 -0500872
873 Py_CLEAR(tstate->context);
Victor Stinner4d96b462020-02-01 02:30:25 +0100874
875 if (tstate->on_delete != NULL) {
876 tstate->on_delete(tstate->on_delete_data);
877 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000878}
879
880
Joannah Nanjekye8855e472019-10-04 08:35:42 -0300881/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
Guido van Rossum29757862001-01-23 01:46:06 +0000882static void
Victor Stinner9da74302019-11-20 11:17:17 +0100883tstate_delete_common(PyThreadState *tstate,
884 struct _gilstate_runtime_state *gilstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000885{
Victor Stinner3026cad2020-06-01 16:02:40 +0200886 _Py_EnsureTstateNotNULL(tstate);
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200887 PyInterpreterState *interp = tstate->interp;
888 if (interp == NULL) {
Victor Stinner87d3b9d2020-03-25 19:27:36 +0100889 Py_FatalError("NULL interpreter");
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200890 }
Victor Stinner87d3b9d2020-03-25 19:27:36 +0100891 _PyRuntimeState *runtime = interp->runtime;
892
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200893 HEAD_LOCK(runtime);
Victor Stinner87d3b9d2020-03-25 19:27:36 +0100894 if (tstate->prev) {
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200895 tstate->prev->next = tstate->next;
Victor Stinner87d3b9d2020-03-25 19:27:36 +0100896 }
897 else {
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200898 interp->tstate_head = tstate->next;
Victor Stinner87d3b9d2020-03-25 19:27:36 +0100899 }
900 if (tstate->next) {
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200901 tstate->next->prev = tstate->prev;
Victor Stinner87d3b9d2020-03-25 19:27:36 +0100902 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200903 HEAD_UNLOCK(runtime);
Victor Stinner4d96b462020-02-01 02:30:25 +0100904
Victor Stinner9da74302019-11-20 11:17:17 +0100905 if (gilstate->autoInterpreterState &&
906 PyThread_tss_get(&gilstate->autoTSSkey) == tstate)
907 {
908 PyThread_tss_set(&gilstate->autoTSSkey, NULL);
909 }
910}
911
912
913static void
914_PyThreadState_Delete(PyThreadState *tstate, int check_current)
915{
916 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
917 if (check_current) {
918 if (tstate == _PyRuntimeGILState_GetThreadState(gilstate)) {
Victor Stinner87d3b9d2020-03-25 19:27:36 +0100919 _Py_FatalErrorFormat(__func__, "tstate %p is still current", tstate);
Victor Stinner9da74302019-11-20 11:17:17 +0100920 }
921 }
922 tstate_delete_common(tstate, gilstate);
Victor Stinner23ef89d2020-03-18 02:26:04 +0100923 PyMem_RawFree(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000924}
925
926
Victor Stinner01b1cc12019-11-20 02:27:56 +0100927void
928PyThreadState_Delete(PyThreadState *tstate)
Guido van Rossum29757862001-01-23 01:46:06 +0000929{
Victor Stinner9da74302019-11-20 11:17:17 +0100930 _PyThreadState_Delete(tstate, 1);
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200931}
932
933
Joannah Nanjekye2bc43cd2019-09-05 13:06:49 -0300934void
Victor Stinner23ef89d2020-03-18 02:26:04 +0100935_PyThreadState_DeleteCurrent(PyThreadState *tstate)
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200936{
Victor Stinner3026cad2020-06-01 16:02:40 +0200937 _Py_EnsureTstateNotNULL(tstate);
Victor Stinner23ef89d2020-03-18 02:26:04 +0100938 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
Victor Stinner9da74302019-11-20 11:17:17 +0100939 tstate_delete_common(tstate, gilstate);
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200940 _PyRuntimeGILState_SetThreadState(gilstate, NULL);
Victor Stinner23ef89d2020-03-18 02:26:04 +0100941 _PyEval_ReleaseLock(tstate);
942 PyMem_RawFree(tstate);
Guido van Rossum29757862001-01-23 01:46:06 +0000943}
Guido van Rossum29757862001-01-23 01:46:06 +0000944
Joannah Nanjekye8855e472019-10-04 08:35:42 -0300945void
946PyThreadState_DeleteCurrent(void)
947{
Victor Stinner23ef89d2020-03-18 02:26:04 +0100948 struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
949 PyThreadState *tstate = _PyRuntimeGILState_GetThreadState(gilstate);
950 _PyThreadState_DeleteCurrent(tstate);
Joannah Nanjekye8855e472019-10-04 08:35:42 -0300951}
952
Guido van Rossum29757862001-01-23 01:46:06 +0000953
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200954/*
955 * Delete all thread states except the one passed as argument.
956 * Note that, if there is a current thread state, it *must* be the one
957 * passed as argument. Also, this won't touch any other interpreters
958 * than the current one, since we don't know which thread state should
Min ho Kim39d87b52019-08-31 06:21:19 +1000959 * be kept in those other interpreters.
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200960 */
961void
Victor Stinner0fd2c302019-06-04 03:15:09 +0200962_PyThreadState_DeleteExcept(_PyRuntimeState *runtime, PyThreadState *tstate)
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200963{
964 PyInterpreterState *interp = tstate->interp;
Victor Stinner9ad58ac2020-03-09 23:37:49 +0100965
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200966 HEAD_LOCK(runtime);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200967 /* Remove all thread states, except tstate, from the linked list of
968 thread states. This will allow calling PyThreadState_Clear()
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200969 without holding the lock. */
Victor Stinner9ad58ac2020-03-09 23:37:49 +0100970 PyThreadState *list = interp->tstate_head;
971 if (list == tstate) {
972 list = tstate->next;
973 }
974 if (tstate->prev) {
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200975 tstate->prev->next = tstate->next;
Victor Stinner9ad58ac2020-03-09 23:37:49 +0100976 }
977 if (tstate->next) {
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200978 tstate->next->prev = tstate->prev;
Victor Stinner9ad58ac2020-03-09 23:37:49 +0100979 }
Charles-Francois Natalif28dfdd2013-05-08 21:09:52 +0200980 tstate->prev = tstate->next = NULL;
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200981 interp->tstate_head = tstate;
Victor Stinner10c8e6a2019-04-26 01:53:18 +0200982 HEAD_UNLOCK(runtime);
Victor Stinner9ad58ac2020-03-09 23:37:49 +0100983
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200984 /* Clear and deallocate all stale thread states. Even if this
985 executes Python code, we should be safe since it executes
986 in the current thread, not one of the stale threads. */
Victor Stinner9ad58ac2020-03-09 23:37:49 +0100987 PyThreadState *p, *next;
988 for (p = list; p; p = next) {
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200989 next = p->next;
990 PyThreadState_Clear(p);
Victor Stinner1a7425f2013-07-07 16:25:15 +0200991 PyMem_RawFree(p);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200992 }
993}
994
995
Victor Stinnere838a932020-05-05 19:56:48 +0200996#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
997PyThreadState*
998_PyThreadState_GetTSS(void) {
999 return PyThread_tss_get(&_PyRuntime.gilstate.autoTSSkey);
1000}
1001#endif
1002
1003
Guido van Rossuma027efa1997-05-05 20:56:21 +00001004PyThreadState *
Victor Stinnerbfd316e2016-01-20 11:12:38 +01001005_PyThreadState_UncheckedGet(void)
1006{
Victor Stinner50b48572018-11-01 01:51:40 +01001007 return _PyThreadState_GET();
Victor Stinnerbfd316e2016-01-20 11:12:38 +01001008}
1009
1010
1011PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001012PyThreadState_Get(void)
Guido van Rossuma027efa1997-05-05 20:56:21 +00001013{
Victor Stinner50b48572018-11-01 01:51:40 +01001014 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner3026cad2020-06-01 16:02:40 +02001015 _Py_EnsureTstateNotNULL(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 return tstate;
Guido van Rossuma027efa1997-05-05 20:56:21 +00001017}
1018
1019
Victor Stinner09532fe2019-05-10 23:39:09 +02001020PyThreadState *
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001021_PyThreadState_Swap(struct _gilstate_runtime_state *gilstate, PyThreadState *newts)
Guido van Rossuma027efa1997-05-05 20:56:21 +00001022{
Victor Stinnere838a932020-05-05 19:56:48 +02001023#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
1024 PyThreadState *oldts = _PyThreadState_GetTSS();
1025#else
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001026 PyThreadState *oldts = _PyRuntimeGILState_GetThreadState(gilstate);
Victor Stinnere838a932020-05-05 19:56:48 +02001027#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +00001028
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001029 _PyRuntimeGILState_SetThreadState(gilstate, newts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 /* It should not be possible for more than one thread state
1031 to be used for a thread. Check this the best we can in debug
1032 builds.
1033 */
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001034#if defined(Py_DEBUG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 if (newts) {
1036 /* This can be called from PyEval_RestoreThread(). Similar
1037 to it, we need to ensure errno doesn't change.
1038 */
1039 int err = errno;
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001040 PyThreadState *check = _PyGILState_GetThisThreadState(gilstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 if (check && check->interp == newts->interp && check != newts)
1042 Py_FatalError("Invalid thread state for this thread");
1043 errno = err;
1044 }
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001045#endif
Victor Stinnere838a932020-05-05 19:56:48 +02001046#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
1047 PyThread_tss_set(&gilstate->autoTSSkey, newts);
1048#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 return oldts;
Guido van Rossuma027efa1997-05-05 20:56:21 +00001050}
Guido van Rossumede04391998-04-10 20:18:25 +00001051
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001052PyThreadState *
1053PyThreadState_Swap(PyThreadState *newts)
1054{
1055 return _PyThreadState_Swap(&_PyRuntime.gilstate, newts);
1056}
1057
Guido van Rossumede04391998-04-10 20:18:25 +00001058/* An extension mechanism to store arbitrary additional per-thread state.
1059 PyThreadState_GetDict() returns a dictionary that can be used to hold such
1060 state; the caller should pick a unique key and store its state there. If
Guido van Rossum0fc8f002003-04-15 15:12:39 +00001061 PyThreadState_GetDict() returns NULL, an exception has *not* been raised
1062 and the caller should assume no per-thread state is available. */
Guido van Rossumede04391998-04-10 20:18:25 +00001063
1064PyObject *
Victor Stinner0e427c62020-03-25 21:22:55 +01001065_PyThreadState_GetDict(PyThreadState *tstate)
1066{
1067 assert(tstate != NULL);
1068 if (tstate->dict == NULL) {
1069 tstate->dict = PyDict_New();
1070 if (tstate->dict == NULL) {
1071 _PyErr_Clear(tstate);
1072 }
1073 }
1074 return tstate->dict;
1075}
1076
1077
1078PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001079PyThreadState_GetDict(void)
Guido van Rossumede04391998-04-10 20:18:25 +00001080{
Victor Stinner50b48572018-11-01 01:51:40 +01001081 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner0e427c62020-03-25 21:22:55 +01001082 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 }
Victor Stinner0e427c62020-03-25 21:22:55 +01001085 return _PyThreadState_GetDict(tstate);
Guido van Rossumede04391998-04-10 20:18:25 +00001086}
Guido van Rossumf5df46d2001-07-19 12:19:27 +00001087
1088
Victor Stinner8fb02b62020-03-13 23:38:08 +01001089PyInterpreterState *
1090PyThreadState_GetInterpreter(PyThreadState *tstate)
1091{
1092 assert(tstate != NULL);
Victor Stinner8fb02b62020-03-13 23:38:08 +01001093 return tstate->interp;
1094}
1095
1096
Victor Stinner4386b902020-04-29 03:01:43 +02001097PyFrameObject*
Victor Stinnerfd1e1a12020-03-20 15:51:45 +01001098PyThreadState_GetFrame(PyThreadState *tstate)
1099{
1100 assert(tstate != NULL);
Victor Stinner4386b902020-04-29 03:01:43 +02001101 PyFrameObject *frame = tstate->frame;
1102 Py_XINCREF(frame);
1103 return frame;
Victor Stinnerfd1e1a12020-03-20 15:51:45 +01001104}
1105
1106
Victor Stinner5c3cda02020-03-25 21:23:53 +01001107uint64_t
1108PyThreadState_GetID(PyThreadState *tstate)
1109{
1110 assert(tstate != NULL);
1111 return tstate->id;
1112}
1113
1114
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001115/* Asynchronously raise an exception in a thread.
1116 Requested by Just van Rossum and Alex Martelli.
Guido van Rossum0f1f63c2005-02-08 02:07:57 +00001117 To prevent naive misuse, you must write your own extension
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001118 to call this, or use ctypes. Must be called with the GIL held.
1119 Returns the number of tstates modified (normally 1, but 0 if `id` didn't
1120 match any known thread id). Can be called with exc=NULL to clear an
1121 existing async exception. This raises no exceptions. */
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001122
1123int
Serhiy Storchakaaefa7eb2017-03-23 15:48:39 +02001124PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc)
1125{
Victor Stinner09532fe2019-05-10 23:39:09 +02001126 _PyRuntimeState *runtime = &_PyRuntime;
1127 PyInterpreterState *interp = _PyRuntimeState_GetThreadState(runtime)->interp;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 /* Although the GIL is held, a few C API functions can be called
1130 * without the GIL held, and in particular some that create and
1131 * destroy thread and interpreter states. Those can mutate the
1132 * list of thread states we're traversing, so to prevent that we lock
1133 * head_mutex for the duration.
1134 */
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001135 HEAD_LOCK(runtime);
Victor Stinner56bfdeb2020-03-18 09:26:25 +01001136 for (PyThreadState *tstate = interp->tstate_head; tstate != NULL; tstate = tstate->next) {
1137 if (tstate->thread_id != id) {
1138 continue;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 }
Victor Stinner56bfdeb2020-03-18 09:26:25 +01001140
1141 /* Tricky: we need to decref the current value
1142 * (if any) in tstate->async_exc, but that can in turn
1143 * allow arbitrary Python code to run, including
1144 * perhaps calls to this function. To prevent
1145 * deadlock, we need to release head_mutex before
1146 * the decref.
1147 */
1148 PyObject *old_exc = tstate->async_exc;
1149 Py_XINCREF(exc);
1150 tstate->async_exc = exc;
1151 HEAD_UNLOCK(runtime);
1152
1153 Py_XDECREF(old_exc);
Victor Stinnerbcb094b2021-02-19 15:10:45 +01001154 _PyEval_SignalAsyncExc(tstate->interp);
Victor Stinner56bfdeb2020-03-18 09:26:25 +01001155 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001157 HEAD_UNLOCK(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 return 0;
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001159}
1160
1161
Guido van Rossumf5df46d2001-07-19 12:19:27 +00001162/* Routines for advanced debuggers, requested by David Beazley.
1163 Don't use unless you know what you are doing! */
1164
1165PyInterpreterState *
1166PyInterpreterState_Head(void)
1167{
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001168 return _PyRuntime.interpreters.head;
Guido van Rossumf5df46d2001-07-19 12:19:27 +00001169}
1170
1171PyInterpreterState *
Eric Snow6b4be192017-05-22 21:36:03 -07001172PyInterpreterState_Main(void)
1173{
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001174 return _PyRuntime.interpreters.main;
Eric Snow6b4be192017-05-22 21:36:03 -07001175}
1176
1177PyInterpreterState *
Guido van Rossumf5df46d2001-07-19 12:19:27 +00001178PyInterpreterState_Next(PyInterpreterState *interp) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 return interp->next;
Guido van Rossumf5df46d2001-07-19 12:19:27 +00001180}
1181
1182PyThreadState *
1183PyInterpreterState_ThreadHead(PyInterpreterState *interp) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 return interp->tstate_head;
Guido van Rossumf5df46d2001-07-19 12:19:27 +00001185}
1186
1187PyThreadState *
1188PyThreadState_Next(PyThreadState *tstate) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 return tstate->next;
Guido van Rossumf5df46d2001-07-19 12:19:27 +00001190}
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001191
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001192/* The implementation of sys._current_frames(). This is intended to be
1193 called with the GIL held, as it will be when called via
1194 sys._current_frames(). It's possible it would work fine even without
1195 the GIL held, but haven't thought enough about that.
1196*/
1197PyObject *
1198_PyThread_CurrentFrames(void)
1199{
Victor Stinner71a35222020-03-26 22:46:14 +01001200 PyThreadState *tstate = _PyThreadState_GET();
1201 if (_PySys_Audit(tstate, "sys._current_frames", NULL) < 0) {
Steve Dowerb82e17e2019-05-23 08:45:22 -07001202 return NULL;
1203 }
1204
Victor Stinner71a35222020-03-26 22:46:14 +01001205 PyObject *result = PyDict_New();
1206 if (result == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 return NULL;
Victor Stinner71a35222020-03-26 22:46:14 +01001208 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 /* for i in all interpreters:
1211 * for t in all of i's thread states:
1212 * if t's frame isn't NULL, map t's id to its frame
Ezio Melotti13925002011-03-16 11:05:33 +02001213 * Because these lists can mutate even when the GIL is held, we
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 * need to grab head_mutex for the duration.
1215 */
Victor Stinner71a35222020-03-26 22:46:14 +01001216 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001217 HEAD_LOCK(runtime);
Victor Stinner71a35222020-03-26 22:46:14 +01001218 PyInterpreterState *i;
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001219 for (i = runtime->interpreters.head; i != NULL; i = i->next) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 PyThreadState *t;
1221 for (t = i->tstate_head; t != NULL; t = t->next) {
Victor Stinner4386b902020-04-29 03:01:43 +02001222 PyFrameObject *frame = t->frame;
Victor Stinner71a35222020-03-26 22:46:14 +01001223 if (frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 continue;
Victor Stinner71a35222020-03-26 22:46:14 +01001225 }
1226 PyObject *id = PyLong_FromUnsignedLong(t->thread_id);
1227 if (id == NULL) {
1228 goto fail;
1229 }
1230 int stat = PyDict_SetItem(result, id, (PyObject *)frame);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 Py_DECREF(id);
Victor Stinner71a35222020-03-26 22:46:14 +01001232 if (stat < 0) {
1233 goto fail;
1234 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 }
1236 }
Victor Stinner71a35222020-03-26 22:46:14 +01001237 goto done;
1238
1239fail:
1240 Py_CLEAR(result);
1241
1242done:
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001243 HEAD_UNLOCK(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 return result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001245}
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001246
Julien Danjou64366fa2020-11-02 15:16:25 +01001247PyObject *
1248_PyThread_CurrentExceptions(void)
1249{
1250 PyThreadState *tstate = _PyThreadState_GET();
1251
1252 _Py_EnsureTstateNotNULL(tstate);
1253
1254 if (_PySys_Audit(tstate, "sys._current_exceptions", NULL) < 0) {
1255 return NULL;
1256 }
1257
1258 PyObject *result = PyDict_New();
1259 if (result == NULL) {
1260 return NULL;
1261 }
1262
1263 /* for i in all interpreters:
1264 * for t in all of i's thread states:
1265 * if t's frame isn't NULL, map t's id to its frame
1266 * Because these lists can mutate even when the GIL is held, we
1267 * need to grab head_mutex for the duration.
1268 */
1269 _PyRuntimeState *runtime = tstate->interp->runtime;
1270 HEAD_LOCK(runtime);
1271 PyInterpreterState *i;
1272 for (i = runtime->interpreters.head; i != NULL; i = i->next) {
1273 PyThreadState *t;
1274 for (t = i->tstate_head; t != NULL; t = t->next) {
1275 _PyErr_StackItem *err_info = _PyErr_GetTopmostException(t);
1276 if (err_info == NULL) {
1277 continue;
1278 }
1279 PyObject *id = PyLong_FromUnsignedLong(t->thread_id);
1280 if (id == NULL) {
1281 goto fail;
1282 }
1283 PyObject *exc_info = PyTuple_Pack(
1284 3,
1285 err_info->exc_type != NULL ? err_info->exc_type : Py_None,
1286 err_info->exc_value != NULL ? err_info->exc_value : Py_None,
1287 err_info->exc_traceback != NULL ? err_info->exc_traceback : Py_None);
1288 if (exc_info == NULL) {
1289 Py_DECREF(id);
1290 goto fail;
1291 }
1292 int stat = PyDict_SetItem(result, id, exc_info);
1293 Py_DECREF(id);
1294 Py_DECREF(exc_info);
1295 if (stat < 0) {
1296 goto fail;
1297 }
1298 }
1299 }
1300 goto done;
1301
1302fail:
1303 Py_CLEAR(result);
1304
1305done:
1306 HEAD_UNLOCK(runtime);
1307 return result;
1308}
1309
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001310/* Python "auto thread state" API. */
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001311
1312/* Keep this as a static, as it is not reliable! It can only
1313 ever be compared to the state for the *current* thread.
1314 * If not equal, then it doesn't matter that the actual
1315 value may change immediately after comparison, as it can't
1316 possibly change to the current thread's state.
1317 * If equal, then the current thread holds the lock, so the value can't
1318 change until we yield the lock.
1319*/
1320static int
1321PyThreadState_IsCurrent(PyThreadState *tstate)
1322{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 /* Must be the tstate for this thread */
Victor Stinner0fd2c302019-06-04 03:15:09 +02001324 struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001325 assert(_PyGILState_GetThisThreadState(gilstate) == tstate);
1326 return tstate == _PyRuntimeGILState_GetThreadState(gilstate);
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001327}
1328
Tim Peters4c1f5ec2004-10-09 17:25:05 +00001329/* Internal initialization/finalization functions called by
Martin Panterb4ce1fc2015-11-30 03:18:29 +00001330 Py_Initialize/Py_FinalizeEx
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001331*/
Victor Stinner4e53abb2020-03-10 23:49:16 +01001332PyStatus
Victor Stinner87f649a2021-03-10 20:00:46 +01001333_PyGILState_Init(_PyRuntimeState *runtime)
1334{
1335 struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
1336 if (PyThread_tss_create(&gilstate->autoTSSkey) != 0) {
1337 return _PyStatus_NO_MEMORY();
1338 }
1339 // PyThreadState_New() calls _PyGILState_NoteThreadState() which does
1340 // nothing before autoInterpreterState is set.
1341 assert(gilstate->autoInterpreterState == NULL);
1342 return _PyStatus_OK();
1343}
1344
1345
1346PyStatus
1347_PyGILState_SetTstate(PyThreadState *tstate)
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001348{
Victor Stinner101bf692021-02-19 13:33:31 +01001349 if (!_Py_IsMainInterpreter(tstate->interp)) {
Victor Stinnerdda5d6e2020-04-08 17:54:59 +02001350 /* Currently, PyGILState is shared by all interpreters. The main
1351 * interpreter is responsible to initialize it. */
1352 return _PyStatus_OK();
1353 }
1354
Victor Stinner8bb32302019-04-24 16:47:40 +02001355 /* must init with valid states */
Victor Stinner0fd2c302019-06-04 03:15:09 +02001356 assert(tstate != NULL);
Victor Stinnerb45d2592019-06-20 00:05:23 +02001357 assert(tstate->interp != NULL);
Victor Stinner8bb32302019-04-24 16:47:40 +02001358
Victor Stinner01b1cc12019-11-20 02:27:56 +01001359 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
Victor Stinner8bb32302019-04-24 16:47:40 +02001360
Victor Stinnerb45d2592019-06-20 00:05:23 +02001361 gilstate->autoInterpreterState = tstate->interp;
Victor Stinner8bb32302019-04-24 16:47:40 +02001362 assert(PyThread_tss_get(&gilstate->autoTSSkey) == NULL);
1363 assert(tstate->gilstate_counter == 0);
Michael W. Hudson188d4362005-06-20 16:52:57 +00001364
Victor Stinner8bb32302019-04-24 16:47:40 +02001365 _PyGILState_NoteThreadState(gilstate, tstate);
Victor Stinner4e53abb2020-03-10 23:49:16 +01001366 return _PyStatus_OK();
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001367}
1368
Victor Stinner861d9ab2016-03-16 22:45:24 +01001369PyInterpreterState *
1370_PyGILState_GetInterpreterStateUnsafe(void)
1371{
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001372 return _PyRuntime.gilstate.autoInterpreterState;
Victor Stinner861d9ab2016-03-16 22:45:24 +01001373}
1374
Tim Peters19717fa2004-10-09 17:38:29 +00001375void
Victor Stinnerbcb094b2021-02-19 15:10:45 +01001376_PyGILState_Fini(PyInterpreterState *interp)
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001377{
Victor Stinnerbcb094b2021-02-19 15:10:45 +01001378 struct _gilstate_runtime_state *gilstate = &interp->runtime->gilstate;
Victor Stinner8e91c242019-04-24 17:24:01 +02001379 PyThread_tss_delete(&gilstate->autoTSSkey);
1380 gilstate->autoInterpreterState = NULL;
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001381}
1382
Victor Stinner26881c82020-06-02 15:51:37 +02001383#ifdef HAVE_FORK
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001384/* Reset the TSS key - called by PyOS_AfterFork_Child().
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001385 * This should not be necessary, but some - buggy - pthread implementations
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001386 * don't reset TSS upon fork(), see issue #10517.
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001387 */
Victor Stinner26881c82020-06-02 15:51:37 +02001388PyStatus
Victor Stinnerb930a2d2019-04-24 17:14:33 +02001389_PyGILState_Reinit(_PyRuntimeState *runtime)
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001390{
Victor Stinnerb930a2d2019-04-24 17:14:33 +02001391 struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001392 PyThreadState *tstate = _PyGILState_GetThisThreadState(gilstate);
Victor Stinnerb930a2d2019-04-24 17:14:33 +02001393
1394 PyThread_tss_delete(&gilstate->autoTSSkey);
1395 if (PyThread_tss_create(&gilstate->autoTSSkey) != 0) {
Victor Stinner26881c82020-06-02 15:51:37 +02001396 return _PyStatus_NO_MEMORY();
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001397 }
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001398
Charles-François Natalia233df82011-11-22 19:49:51 +01001399 /* If the thread had an associated auto thread state, reassociate it with
1400 * the new key. */
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001401 if (tstate &&
Victor Stinnerb930a2d2019-04-24 17:14:33 +02001402 PyThread_tss_set(&gilstate->autoTSSkey, (void *)tstate) != 0)
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001403 {
Victor Stinner26881c82020-06-02 15:51:37 +02001404 return _PyStatus_ERR("failed to set autoTSSkey");
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001405 }
Victor Stinner26881c82020-06-02 15:51:37 +02001406 return _PyStatus_OK();
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001407}
Victor Stinner26881c82020-06-02 15:51:37 +02001408#endif
Antoine Pitrou0c759fe2011-04-27 19:28:05 +02001409
Michael W. Hudson188d4362005-06-20 16:52:57 +00001410/* When a thread state is created for a thread by some mechanism other than
1411 PyGILState_Ensure, it's important that the GILState machinery knows about
1412 it so it doesn't try to create another thread state for the thread (this is
1413 a better fix for SF bug #1010677 than the first one attempted).
1414*/
Thomas Wouters89f507f2006-12-13 04:49:30 +00001415static void
Victor Stinner8bb32302019-04-24 16:47:40 +02001416_PyGILState_NoteThreadState(struct _gilstate_runtime_state *gilstate, PyThreadState* tstate)
Michael W. Hudson188d4362005-06-20 16:52:57 +00001417{
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001418 /* If autoTSSkey isn't initialized, this must be the very first
Antoine Pitrou079ce542010-09-08 12:37:10 +00001419 threadstate created in Py_Initialize(). Don't do anything for now
1420 (we'll be back here when _PyGILState_Init is called). */
Victor Stinner8bb32302019-04-24 16:47:40 +02001421 if (!gilstate->autoInterpreterState) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 return;
Victor Stinner8bb32302019-04-24 16:47:40 +02001423 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001424
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001425 /* Stick the thread state for this thread in thread specific storage.
Michael W. Hudson188d4362005-06-20 16:52:57 +00001426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 The only situation where you can legitimately have more than one
1428 thread state for an OS level thread is when there are multiple
Victor Stinner590cebe2013-12-13 11:08:56 +01001429 interpreters.
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001430
Victor Stinner590cebe2013-12-13 11:08:56 +01001431 You shouldn't really be using the PyGILState_ APIs anyway (see issues
1432 #10915 and #15751).
Michael W. Hudson188d4362005-06-20 16:52:57 +00001433
Victor Stinner590cebe2013-12-13 11:08:56 +01001434 The first thread state created for that given OS level thread will
1435 "win", which seems reasonable behaviour.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 */
Victor Stinner8bb32302019-04-24 16:47:40 +02001437 if (PyThread_tss_get(&gilstate->autoTSSkey) == NULL) {
1438 if ((PyThread_tss_set(&gilstate->autoTSSkey, (void *)tstate)) != 0) {
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001439 Py_FatalError("Couldn't create autoTSSkey mapping");
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001440 }
Victor Stinner590cebe2013-12-13 11:08:56 +01001441 }
Michael W. Hudson188d4362005-06-20 16:52:57 +00001442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 /* PyGILState_Release must not try to delete this thread state. */
1444 tstate->gilstate_counter = 1;
Michael W. Hudson188d4362005-06-20 16:52:57 +00001445}
1446
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001447/* The public functions */
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001448static PyThreadState *
1449_PyGILState_GetThisThreadState(struct _gilstate_runtime_state *gilstate)
1450{
1451 if (gilstate->autoInterpreterState == NULL)
1452 return NULL;
1453 return (PyThreadState *)PyThread_tss_get(&gilstate->autoTSSkey);
1454}
1455
Tim Peters19717fa2004-10-09 17:38:29 +00001456PyThreadState *
1457PyGILState_GetThisThreadState(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001458{
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001459 return _PyGILState_GetThisThreadState(&_PyRuntime.gilstate);
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001460}
1461
Kristján Valur Jónsson684cd0e2013-03-23 03:36:16 -07001462int
1463PyGILState_Check(void)
1464{
Victor Stinner1c4cbdf2020-04-13 11:45:21 +02001465 struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
1466 if (!gilstate->check_enabled) {
Victor Stinner8a1be612016-03-14 22:07:55 +01001467 return 1;
Masayuki Yamamoto731e1892017-10-06 19:41:34 +09001468 }
Victor Stinner8a1be612016-03-14 22:07:55 +01001469
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001470 if (!PyThread_tss_is_created(&gilstate->autoTSSkey)) {
1471 return 1;
1472 }
Victor Stinner8a1be612016-03-14 22:07:55 +01001473
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001474 PyThreadState *tstate = _PyRuntimeGILState_GetThreadState(gilstate);
1475 if (tstate == NULL) {
1476 return 0;
1477 }
1478
1479 return (tstate == _PyGILState_GetThisThreadState(gilstate));
Kristján Valur Jónsson684cd0e2013-03-23 03:36:16 -07001480}
1481
Tim Peters19717fa2004-10-09 17:38:29 +00001482PyGILState_STATE
1483PyGILState_Ensure(void)
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001484{
Victor Stinner175a7042020-03-10 00:37:48 +01001485 _PyRuntimeState *runtime = &_PyRuntime;
1486 struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001488 /* Note that we do not auto-init Python here - apart from
1489 potential races with 2 threads auto-initializing, pep-311
1490 spells out other issues. Embedders are expected to have
Victor Stinner175a7042020-03-10 00:37:48 +01001491 called Py_Initialize(). */
1492
1493 /* Ensure that _PyEval_InitThreads() and _PyGILState_Init() have been
1494 called by Py_Initialize() */
Victor Stinnere838a932020-05-05 19:56:48 +02001495#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinner175a7042020-03-10 00:37:48 +01001496 assert(_PyEval_ThreadsInitialized(runtime));
Victor Stinnere838a932020-05-05 19:56:48 +02001497#endif
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001498 assert(gilstate->autoInterpreterState);
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001499
Victor Stinner175a7042020-03-10 00:37:48 +01001500 PyThreadState *tcur = (PyThreadState *)PyThread_tss_get(&gilstate->autoTSSkey);
1501 int current;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001502 if (tcur == NULL) {
Victor Stinner175a7042020-03-10 00:37:48 +01001503 /* Create a new Python thread state for this thread */
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001504 tcur = PyThreadState_New(gilstate->autoInterpreterState);
Victor Stinner175a7042020-03-10 00:37:48 +01001505 if (tcur == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506 Py_FatalError("Couldn't create thread-state for new thread");
Victor Stinner175a7042020-03-10 00:37:48 +01001507 }
1508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509 /* This is our thread state! We'll need to delete it in the
1510 matching call to PyGILState_Release(). */
1511 tcur->gilstate_counter = 0;
1512 current = 0; /* new thread state is never current */
1513 }
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001514 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 current = PyThreadState_IsCurrent(tcur);
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001516 }
1517
1518 if (current == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519 PyEval_RestoreThread(tcur);
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001520 }
1521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001522 /* Update our counter in the thread-state - no need for locks:
1523 - tcur will remain valid as we hold the GIL.
1524 - the counter is safe as we are the only thread "allowed"
1525 to modify this value
1526 */
1527 ++tcur->gilstate_counter;
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +01001528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001529 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001530}
1531
Tim Peters19717fa2004-10-09 17:38:29 +00001532void
1533PyGILState_Release(PyGILState_STATE oldstate)
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001534{
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001535 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner23ef89d2020-03-18 02:26:04 +01001536 PyThreadState *tstate = PyThread_tss_get(&runtime->gilstate.autoTSSkey);
1537 if (tstate == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 Py_FatalError("auto-releasing thread-state, "
1539 "but no thread-state for this thread");
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001540 }
1541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001542 /* We must hold the GIL and have our thread state current */
1543 /* XXX - remove the check - the assert should be fine,
1544 but while this is very new (April 2003), the extra check
1545 by release-only users can't hurt.
1546 */
Victor Stinner23ef89d2020-03-18 02:26:04 +01001547 if (!PyThreadState_IsCurrent(tstate)) {
Victor Stinner87d3b9d2020-03-25 19:27:36 +01001548 _Py_FatalErrorFormat(__func__,
1549 "thread state %p must be current when releasing",
1550 tstate);
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001551 }
Victor Stinner23ef89d2020-03-18 02:26:04 +01001552 assert(PyThreadState_IsCurrent(tstate));
1553 --tstate->gilstate_counter;
1554 assert(tstate->gilstate_counter >= 0); /* illegal counter value */
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 /* If we're going to destroy this thread-state, we must
1557 * clear it while the GIL is held, as destructors may run.
1558 */
Victor Stinner23ef89d2020-03-18 02:26:04 +01001559 if (tstate->gilstate_counter == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560 /* can't have been locked when we created it */
1561 assert(oldstate == PyGILState_UNLOCKED);
Victor Stinner23ef89d2020-03-18 02:26:04 +01001562 PyThreadState_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 /* Delete the thread-state. Note this releases the GIL too!
1564 * It's vital that the GIL be held here, to avoid shutdown
1565 * races; see bugs 225673 and 1061968 (that nasty bug has a
1566 * habit of coming back).
1567 */
Victor Stinner23ef89d2020-03-18 02:26:04 +01001568 assert(_PyRuntimeGILState_GetThreadState(&runtime->gilstate) == tstate);
1569 _PyThreadState_DeleteCurrent(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 }
1571 /* Release the lock if necessary */
1572 else if (oldstate == PyGILState_UNLOCKED)
1573 PyEval_SaveThread();
Mark Hammond8d98d2c2003-04-19 15:41:53 +00001574}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001575
Benjamin Peterson3bf01752012-04-13 18:06:36 -04001576
Eric Snow7f8bfc92018-01-29 18:23:44 -07001577/**************************/
1578/* cross-interpreter data */
1579/**************************/
1580
1581/* cross-interpreter data */
1582
1583crossinterpdatafunc _PyCrossInterpreterData_Lookup(PyObject *);
1584
1585/* This is a separate func from _PyCrossInterpreterData_Lookup in order
1586 to keep the registry code separate. */
1587static crossinterpdatafunc
1588_lookup_getdata(PyObject *obj)
1589{
1590 crossinterpdatafunc getdata = _PyCrossInterpreterData_Lookup(obj);
1591 if (getdata == NULL && PyErr_Occurred() == 0)
1592 PyErr_Format(PyExc_ValueError,
1593 "%S does not support cross-interpreter data", obj);
1594 return getdata;
1595}
1596
1597int
1598_PyObject_CheckCrossInterpreterData(PyObject *obj)
1599{
1600 crossinterpdatafunc getdata = _lookup_getdata(obj);
1601 if (getdata == NULL) {
1602 return -1;
1603 }
1604 return 0;
1605}
1606
1607static int
Victor Stinner71a35222020-03-26 22:46:14 +01001608_check_xidata(PyThreadState *tstate, _PyCrossInterpreterData *data)
Eric Snow7f8bfc92018-01-29 18:23:44 -07001609{
1610 // data->data can be anything, including NULL, so we don't check it.
1611
1612 // data->obj may be NULL, so we don't check it.
1613
1614 if (data->interp < 0) {
Victor Stinner71a35222020-03-26 22:46:14 +01001615 _PyErr_SetString(tstate, PyExc_SystemError, "missing interp");
Eric Snow7f8bfc92018-01-29 18:23:44 -07001616 return -1;
1617 }
1618
1619 if (data->new_object == NULL) {
Victor Stinner71a35222020-03-26 22:46:14 +01001620 _PyErr_SetString(tstate, PyExc_SystemError, "missing new_object func");
Eric Snow7f8bfc92018-01-29 18:23:44 -07001621 return -1;
1622 }
1623
1624 // data->free may be NULL, so we don't check it.
1625
1626 return 0;
1627}
1628
1629int
1630_PyObject_GetCrossInterpreterData(PyObject *obj, _PyCrossInterpreterData *data)
1631{
Victor Stinner71a35222020-03-26 22:46:14 +01001632 // PyThreadState_Get() aborts if tstate is NULL.
1633 PyThreadState *tstate = PyThreadState_Get();
1634 PyInterpreterState *interp = tstate->interp;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001635
1636 // Reset data before re-populating.
1637 *data = (_PyCrossInterpreterData){0};
1638 data->free = PyMem_RawFree; // Set a default that may be overridden.
1639
1640 // Call the "getdata" func for the object.
1641 Py_INCREF(obj);
1642 crossinterpdatafunc getdata = _lookup_getdata(obj);
1643 if (getdata == NULL) {
1644 Py_DECREF(obj);
1645 return -1;
1646 }
1647 int res = getdata(obj, data);
1648 Py_DECREF(obj);
1649 if (res != 0) {
1650 return -1;
1651 }
1652
1653 // Fill in the blanks and validate the result.
Eric Snow7f8bfc92018-01-29 18:23:44 -07001654 data->interp = interp->id;
Victor Stinner71a35222020-03-26 22:46:14 +01001655 if (_check_xidata(tstate, data) != 0) {
Eric Snow7f8bfc92018-01-29 18:23:44 -07001656 _PyCrossInterpreterData_Release(data);
1657 return -1;
1658 }
1659
1660 return 0;
1661}
1662
Victor Stinnere225beb2019-06-03 18:14:24 +02001663static void
Eric Snow63799132018-06-01 18:45:20 -06001664_release_xidata(void *arg)
1665{
1666 _PyCrossInterpreterData *data = (_PyCrossInterpreterData *)arg;
1667 if (data->free != NULL) {
1668 data->free(data->data);
1669 }
1670 Py_XDECREF(data->obj);
Victor Stinnere225beb2019-06-03 18:14:24 +02001671}
1672
1673static void
1674_call_in_interpreter(struct _gilstate_runtime_state *gilstate,
1675 PyInterpreterState *interp,
1676 void (*func)(void *), void *arg)
1677{
1678 /* We would use Py_AddPendingCall() if it weren't specific to the
1679 * main interpreter (see bpo-33608). In the meantime we take a
1680 * naive approach.
1681 */
1682 PyThreadState *save_tstate = NULL;
1683 if (interp != _PyRuntimeGILState_GetThreadState(gilstate)->interp) {
1684 // XXX Using the "head" thread isn't strictly correct.
1685 PyThreadState *tstate = PyInterpreterState_ThreadHead(interp);
1686 // XXX Possible GILState issues?
1687 save_tstate = _PyThreadState_Swap(gilstate, tstate);
1688 }
1689
1690 func(arg);
1691
1692 // Switch back.
1693 if (save_tstate != NULL) {
1694 _PyThreadState_Swap(gilstate, save_tstate);
1695 }
Eric Snow63799132018-06-01 18:45:20 -06001696}
1697
Eric Snow7f8bfc92018-01-29 18:23:44 -07001698void
1699_PyCrossInterpreterData_Release(_PyCrossInterpreterData *data)
1700{
1701 if (data->data == NULL && data->obj == NULL) {
1702 // Nothing to release!
1703 return;
1704 }
1705
Victor Stinnere225beb2019-06-03 18:14:24 +02001706 // Switch to the original interpreter.
Eric Snow7f8bfc92018-01-29 18:23:44 -07001707 PyInterpreterState *interp = _PyInterpreterState_LookUpID(data->interp);
1708 if (interp == NULL) {
Min ho Kimc4cacc82019-07-31 08:16:13 +10001709 // The interpreter was already destroyed.
Eric Snow7f8bfc92018-01-29 18:23:44 -07001710 if (data->free != NULL) {
1711 // XXX Someone leaked some memory...
1712 }
1713 return;
1714 }
Eric Snowf53d9f22018-02-20 16:30:17 -07001715
Eric Snow7f8bfc92018-01-29 18:23:44 -07001716 // "Release" the data and/or the object.
Victor Stinnere225beb2019-06-03 18:14:24 +02001717 struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
1718 _call_in_interpreter(gilstate, interp, _release_xidata, data);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001719}
1720
1721PyObject *
1722_PyCrossInterpreterData_NewObject(_PyCrossInterpreterData *data)
1723{
1724 return data->new_object(data);
1725}
1726
1727/* registry of {type -> crossinterpdatafunc} */
1728
1729/* For now we use a global registry of shareable classes. An
1730 alternative would be to add a tp_* slot for a class's
1731 crossinterpdatafunc. It would be simpler and more efficient. */
1732
1733static int
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001734_register_xidata(struct _xidregistry *xidregistry, PyTypeObject *cls,
1735 crossinterpdatafunc getdata)
Eric Snow7f8bfc92018-01-29 18:23:44 -07001736{
1737 // Note that we effectively replace already registered classes
1738 // rather than failing.
1739 struct _xidregitem *newhead = PyMem_RawMalloc(sizeof(struct _xidregitem));
1740 if (newhead == NULL)
1741 return -1;
1742 newhead->cls = cls;
1743 newhead->getdata = getdata;
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001744 newhead->next = xidregistry->head;
1745 xidregistry->head = newhead;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001746 return 0;
1747}
1748
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001749static void _register_builtins_for_crossinterpreter_data(struct _xidregistry *xidregistry);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001750
1751int
Eric Snowc11183c2019-03-15 16:35:46 -06001752_PyCrossInterpreterData_RegisterClass(PyTypeObject *cls,
Eric Snow7f8bfc92018-01-29 18:23:44 -07001753 crossinterpdatafunc getdata)
1754{
1755 if (!PyType_Check(cls)) {
1756 PyErr_Format(PyExc_ValueError, "only classes may be registered");
1757 return -1;
1758 }
1759 if (getdata == NULL) {
1760 PyErr_Format(PyExc_ValueError, "missing 'getdata' func");
1761 return -1;
1762 }
1763
1764 // Make sure the class isn't ever deallocated.
1765 Py_INCREF((PyObject *)cls);
1766
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001767 struct _xidregistry *xidregistry = &_PyRuntime.xidregistry ;
1768 PyThread_acquire_lock(xidregistry->mutex, WAIT_LOCK);
1769 if (xidregistry->head == NULL) {
1770 _register_builtins_for_crossinterpreter_data(xidregistry);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001771 }
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001772 int res = _register_xidata(xidregistry, cls, getdata);
1773 PyThread_release_lock(xidregistry->mutex);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001774 return res;
1775}
1776
Eric Snow6d2cd902018-05-16 15:04:57 -04001777/* Cross-interpreter objects are looked up by exact match on the class.
1778 We can reassess this policy when we move from a global registry to a
1779 tp_* slot. */
1780
Eric Snow7f8bfc92018-01-29 18:23:44 -07001781crossinterpdatafunc
1782_PyCrossInterpreterData_Lookup(PyObject *obj)
1783{
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001784 struct _xidregistry *xidregistry = &_PyRuntime.xidregistry ;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001785 PyObject *cls = PyObject_Type(obj);
1786 crossinterpdatafunc getdata = NULL;
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001787 PyThread_acquire_lock(xidregistry->mutex, WAIT_LOCK);
1788 struct _xidregitem *cur = xidregistry->head;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001789 if (cur == NULL) {
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001790 _register_builtins_for_crossinterpreter_data(xidregistry);
1791 cur = xidregistry->head;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001792 }
1793 for(; cur != NULL; cur = cur->next) {
1794 if (cur->cls == (PyTypeObject *)cls) {
1795 getdata = cur->getdata;
1796 break;
1797 }
1798 }
Eric Snow4e9da0d2018-02-02 21:49:49 -07001799 Py_DECREF(cls);
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001800 PyThread_release_lock(xidregistry->mutex);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001801 return getdata;
1802}
1803
1804/* cross-interpreter data for builtin types */
1805
Eric Snow6d2cd902018-05-16 15:04:57 -04001806struct _shared_bytes_data {
1807 char *bytes;
1808 Py_ssize_t len;
1809};
1810
Eric Snow7f8bfc92018-01-29 18:23:44 -07001811static PyObject *
1812_new_bytes_object(_PyCrossInterpreterData *data)
1813{
Eric Snow6d2cd902018-05-16 15:04:57 -04001814 struct _shared_bytes_data *shared = (struct _shared_bytes_data *)(data->data);
1815 return PyBytes_FromStringAndSize(shared->bytes, shared->len);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001816}
1817
1818static int
1819_bytes_shared(PyObject *obj, _PyCrossInterpreterData *data)
1820{
Eric Snow6d2cd902018-05-16 15:04:57 -04001821 struct _shared_bytes_data *shared = PyMem_NEW(struct _shared_bytes_data, 1);
1822 if (PyBytes_AsStringAndSize(obj, &shared->bytes, &shared->len) < 0) {
1823 return -1;
1824 }
1825 data->data = (void *)shared;
Eric Snow63799132018-06-01 18:45:20 -06001826 Py_INCREF(obj);
Eric Snow7f8bfc92018-01-29 18:23:44 -07001827 data->obj = obj; // Will be "released" (decref'ed) when data released.
1828 data->new_object = _new_bytes_object;
Eric Snow6d2cd902018-05-16 15:04:57 -04001829 data->free = PyMem_Free;
1830 return 0;
1831}
1832
1833struct _shared_str_data {
1834 int kind;
1835 const void *buffer;
1836 Py_ssize_t len;
1837};
1838
1839static PyObject *
1840_new_str_object(_PyCrossInterpreterData *data)
1841{
1842 struct _shared_str_data *shared = (struct _shared_str_data *)(data->data);
1843 return PyUnicode_FromKindAndData(shared->kind, shared->buffer, shared->len);
1844}
1845
1846static int
1847_str_shared(PyObject *obj, _PyCrossInterpreterData *data)
1848{
1849 struct _shared_str_data *shared = PyMem_NEW(struct _shared_str_data, 1);
1850 shared->kind = PyUnicode_KIND(obj);
1851 shared->buffer = PyUnicode_DATA(obj);
An Long29c11722020-06-13 20:26:01 +08001852 shared->len = PyUnicode_GET_LENGTH(obj);
Eric Snow6d2cd902018-05-16 15:04:57 -04001853 data->data = (void *)shared;
Eric Snow63799132018-06-01 18:45:20 -06001854 Py_INCREF(obj);
Eric Snow6d2cd902018-05-16 15:04:57 -04001855 data->obj = obj; // Will be "released" (decref'ed) when data released.
1856 data->new_object = _new_str_object;
1857 data->free = PyMem_Free;
1858 return 0;
1859}
1860
1861static PyObject *
1862_new_long_object(_PyCrossInterpreterData *data)
1863{
Alexey Izbyshev16f842d2019-02-12 19:06:43 +03001864 return PyLong_FromSsize_t((Py_ssize_t)(data->data));
Eric Snow6d2cd902018-05-16 15:04:57 -04001865}
1866
1867static int
1868_long_shared(PyObject *obj, _PyCrossInterpreterData *data)
1869{
Alexey Izbyshev16f842d2019-02-12 19:06:43 +03001870 /* Note that this means the size of shareable ints is bounded by
1871 * sys.maxsize. Hence on 32-bit architectures that is half the
1872 * size of maximum shareable ints on 64-bit.
1873 */
1874 Py_ssize_t value = PyLong_AsSsize_t(obj);
Eric Snow6d2cd902018-05-16 15:04:57 -04001875 if (value == -1 && PyErr_Occurred()) {
1876 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
1877 PyErr_SetString(PyExc_OverflowError, "try sending as bytes");
1878 }
1879 return -1;
1880 }
1881 data->data = (void *)value;
1882 data->obj = NULL;
1883 data->new_object = _new_long_object;
1884 data->free = NULL;
Eric Snow7f8bfc92018-01-29 18:23:44 -07001885 return 0;
1886}
1887
1888static PyObject *
1889_new_none_object(_PyCrossInterpreterData *data)
1890{
1891 // XXX Singleton refcounts are problematic across interpreters...
1892 Py_INCREF(Py_None);
1893 return Py_None;
1894}
1895
1896static int
1897_none_shared(PyObject *obj, _PyCrossInterpreterData *data)
1898{
1899 data->data = NULL;
1900 // data->obj remains NULL
1901 data->new_object = _new_none_object;
1902 data->free = NULL; // There is nothing to free.
1903 return 0;
1904}
1905
1906static void
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001907_register_builtins_for_crossinterpreter_data(struct _xidregistry *xidregistry)
Eric Snow7f8bfc92018-01-29 18:23:44 -07001908{
1909 // None
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001910 if (_register_xidata(xidregistry, (PyTypeObject *)PyObject_Type(Py_None), _none_shared) != 0) {
Eric Snow7f8bfc92018-01-29 18:23:44 -07001911 Py_FatalError("could not register None for cross-interpreter sharing");
1912 }
1913
Eric Snow6d2cd902018-05-16 15:04:57 -04001914 // int
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001915 if (_register_xidata(xidregistry, &PyLong_Type, _long_shared) != 0) {
Eric Snow6d2cd902018-05-16 15:04:57 -04001916 Py_FatalError("could not register int for cross-interpreter sharing");
1917 }
1918
Eric Snow7f8bfc92018-01-29 18:23:44 -07001919 // bytes
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001920 if (_register_xidata(xidregistry, &PyBytes_Type, _bytes_shared) != 0) {
Eric Snow7f8bfc92018-01-29 18:23:44 -07001921 Py_FatalError("could not register bytes for cross-interpreter sharing");
1922 }
Eric Snow6d2cd902018-05-16 15:04:57 -04001923
1924 // str
Victor Stinner10c8e6a2019-04-26 01:53:18 +02001925 if (_register_xidata(xidregistry, &PyUnicode_Type, _str_shared) != 0) {
Eric Snow6d2cd902018-05-16 15:04:57 -04001926 Py_FatalError("could not register str for cross-interpreter sharing");
1927 }
Eric Snow7f8bfc92018-01-29 18:23:44 -07001928}
1929
1930
Victor Stinner0b72b232020-03-12 23:18:39 +01001931_PyFrameEvalFunction
1932_PyInterpreterState_GetEvalFrameFunc(PyInterpreterState *interp)
1933{
1934 return interp->eval_frame;
1935}
1936
1937
1938void
1939_PyInterpreterState_SetEvalFrameFunc(PyInterpreterState *interp,
1940 _PyFrameEvalFunction eval_frame)
1941{
1942 interp->eval_frame = eval_frame;
1943}
1944
Victor Stinnerda7933e2020-04-13 03:04:28 +02001945
1946const PyConfig*
1947_PyInterpreterState_GetConfig(PyInterpreterState *interp)
1948{
1949 return &interp->config;
1950}
1951
1952
Victor Stinner048a3562020-11-05 00:45:56 +01001953int
1954_PyInterpreterState_GetConfigCopy(PyConfig *config)
Victor Stinnerda7933e2020-04-13 03:04:28 +02001955{
Victor Stinner048a3562020-11-05 00:45:56 +01001956 PyInterpreterState *interp = PyInterpreterState_Get();
1957
1958 PyStatus status = _PyConfig_Copy(config, &interp->config);
1959 if (PyStatus_Exception(status)) {
1960 _PyErr_SetFromPyStatus(status);
1961 return -1;
1962 }
1963 return 0;
Victor Stinnerda7933e2020-04-13 03:04:28 +02001964}
1965
1966
1967const PyConfig*
1968_Py_GetConfig(void)
1969{
1970 assert(PyGILState_Check());
1971 PyThreadState *tstate = _PyThreadState_GET();
1972 return _PyInterpreterState_GetConfig(tstate->interp);
1973}
1974
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001975#ifdef __cplusplus
1976}
1977#endif