File tree Expand file tree Collapse file tree 5 files changed +40
-2
lines changed
Misc/NEWS.d/next/Core and Builtins Expand file tree Collapse file tree 5 files changed +40
-2
lines changed Original file line number Diff line number Diff line change @@ -1026,6 +1026,18 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
1026
1026
.. versionadded :: 3.7
1027
1027
1028
1028
1029
+ .. c :function :: PyObject* PyInterpreterState_GetDict (PyInterpreterState *interp)
1030
+
1031
+ Return a dictionary in which interpreter-specific data may be stored.
1032
+ If this function returns *NULL * then no exception has been raised and
1033
+ the caller should assume no interpreter-specific dict is available.
1034
+
1035
+ This is not a replacement for :c:func: `PyModule_GetState() `, which
1036
+ extensions should use to store interpreter-specific state information.
1037
+
1038
+ .. versionadded :: 3.8
1039
+
1040
+
1029
1041
.. c :function :: PyObject* PyThreadState_GetDict ()
1030
1042
1031
1043
Return a dictionary in which extensions can store thread-specific state
Original file line number Diff line number Diff line change @@ -63,6 +63,8 @@ struct _is {
63
63
int dlopenflags ;
64
64
#endif
65
65
66
+ PyObject * dict ; /* Stores per-interpreter state */
67
+
66
68
PyObject * builtins_copy ;
67
69
PyObject * import_func ;
68
70
/* Initialized to PyEval_EvalFrameDefault(). */
Original file line number Diff line number Diff line change @@ -24,17 +24,23 @@ typedef struct _ts PyThreadState;
24
24
/* struct _is is defined in internal/pycore_pystate.h */
25
25
typedef struct _is PyInterpreterState ;
26
26
27
- /* State unique per thread */
28
-
29
27
PyAPI_FUNC (PyInterpreterState * ) PyInterpreterState_New (void );
30
28
PyAPI_FUNC (void ) PyInterpreterState_Clear (PyInterpreterState * );
31
29
PyAPI_FUNC (void ) PyInterpreterState_Delete (PyInterpreterState * );
32
30
31
+ #if !defined(Py_LIMITED_API ) || Py_LIMITED_API + 0 >= 0x03080000
32
+ /* New in 3.8 */
33
+ PyAPI_FUNC (PyObject * ) PyInterpreterState_GetDict (PyInterpreterState * );
34
+ #endif
35
+
33
36
#if !defined(Py_LIMITED_API ) || Py_LIMITED_API + 0 >= 0x03070000
34
37
/* New in 3.7 */
35
38
PyAPI_FUNC (int64_t ) PyInterpreterState_GetID (PyInterpreterState * );
36
39
#endif
37
40
#if !defined(Py_LIMITED_API ) || Py_LIMITED_API + 0 >= 0x03030000
41
+
42
+ /* State unique per thread */
43
+
38
44
/* New in 3.3 */
39
45
PyAPI_FUNC (int ) PyState_AddModule (PyObject * , struct PyModuleDef * );
40
46
PyAPI_FUNC (int ) PyState_RemoveModule (struct PyModuleDef * );
Original file line number Diff line number Diff line change
1
+ Add a new interpreter-specific dict and expose it in the C-API via
2
+ PyInterpreterState_GetDict(). This parallels PyThreadState_GetDict().
3
+ However, extension modules should continue using PyModule_GetState() for
4
+ their own internal per-interpreter state.
Original file line number Diff line number Diff line change @@ -224,6 +224,7 @@ PyInterpreterState_Clear(PyInterpreterState *interp)
224
224
Py_CLEAR (interp -> builtins_copy );
225
225
Py_CLEAR (interp -> importlib );
226
226
Py_CLEAR (interp -> import_func );
227
+ Py_CLEAR (interp -> dict );
227
228
#ifdef HAVE_FORK
228
229
Py_CLEAR (interp -> before_forkers );
229
230
Py_CLEAR (interp -> after_forkers_parent );
@@ -462,6 +463,19 @@ _PyInterpreterState_GetMainModule(PyInterpreterState *interp)
462
463
return PyMapping_GetItemString (interp -> modules , "__main__" );
463
464
}
464
465
466
+ PyObject *
467
+ PyInterpreterState_GetDict (PyInterpreterState * interp )
468
+ {
469
+ if (interp -> dict == NULL ) {
470
+ interp -> dict = PyDict_New ();
471
+ if (interp -> dict == NULL ) {
472
+ PyErr_Clear ();
473
+ }
474
+ }
475
+ /* Returning NULL means no per-interpreter dict is available. */
476
+ return interp -> dict ;
477
+ }
478
+
465
479
/* Default implementation for _PyThreadState_GetFrame */
466
480
static struct _frame *
467
481
threadstate_getframe (PyThreadState * self )
You can’t perform that action at this time.
0 commit comments