8
8
#include "pycore_pylifecycle.h"
9
9
#include "pycore_pymem.h"
10
10
#include "pycore_pystate.h"
11
+ #include "pycore_sysmodule.h"
11
12
12
13
/* --------------------------------------------------------------------------
13
14
CAUTION
@@ -203,7 +204,10 @@ _PyInterpreterState_Enable(_PyRuntimeState *runtime)
203
204
PyInterpreterState *
204
205
PyInterpreterState_New (void )
205
206
{
206
- if (PySys_Audit ("cpython.PyInterpreterState_New" , NULL ) < 0 ) {
207
+ PyThreadState * tstate = _PyThreadState_GET ();
208
+ /* tstate is NULL when Py_InitializeFromConfig() calls
209
+ PyInterpreterState_New() to create the main interpreter. */
210
+ if (_PySys_Audit (tstate , "cpython.PyInterpreterState_New" , NULL ) < 0 ) {
207
211
return NULL ;
208
212
}
209
213
@@ -214,6 +218,7 @@ PyInterpreterState_New(void)
214
218
215
219
interp -> id_refcount = -1 ;
216
220
221
+ /* Don't get runtime from tstate since tstate can be NULL */
217
222
_PyRuntimeState * runtime = & _PyRuntime ;
218
223
interp -> runtime = runtime ;
219
224
@@ -235,8 +240,10 @@ PyInterpreterState_New(void)
235
240
HEAD_LOCK (runtime );
236
241
if (interpreters -> next_id < 0 ) {
237
242
/* overflow or Py_Initialize() not called! */
238
- PyErr_SetString (PyExc_RuntimeError ,
239
- "failed to get an interpreter ID" );
243
+ if (tstate != NULL ) {
244
+ _PyErr_SetString (tstate , PyExc_RuntimeError ,
245
+ "failed to get an interpreter ID" );
246
+ }
240
247
PyMem_RawFree (interp );
241
248
interp = NULL ;
242
249
}
@@ -268,8 +275,11 @@ PyInterpreterState_Clear(PyInterpreterState *interp)
268
275
{
269
276
_PyRuntimeState * runtime = interp -> runtime ;
270
277
271
- if (PySys_Audit ("cpython.PyInterpreterState_Clear" , NULL ) < 0 ) {
272
- PyErr_Clear ();
278
+ /* Use the current Python thread state to call audit hooks,
279
+ not the current Python thread state of 'interp'. */
280
+ PyThreadState * tstate = _PyThreadState_GET ();
281
+ if (_PySys_Audit (tstate , "cpython.PyInterpreterState_Clear" , NULL ) < 0 ) {
282
+ _PyErr_Clear (tstate );
273
283
}
274
284
275
285
HEAD_LOCK (runtime );
@@ -655,12 +665,13 @@ int
655
665
_PyState_AddModule (PyThreadState * tstate , PyObject * module , struct PyModuleDef * def )
656
666
{
657
667
if (!def ) {
658
- assert (PyErr_Occurred ( ));
668
+ assert (_PyErr_Occurred ( tstate ));
659
669
return -1 ;
660
670
}
661
671
if (def -> m_slots ) {
662
- PyErr_SetString (PyExc_SystemError ,
663
- "PyState_AddModule called on module with slots" );
672
+ _PyErr_SetString (tstate ,
673
+ PyExc_SystemError ,
674
+ "PyState_AddModule called on module with slots" );
664
675
return -1 ;
665
676
}
666
677
@@ -707,28 +718,29 @@ PyState_AddModule(PyObject* module, struct PyModuleDef* def)
707
718
int
708
719
PyState_RemoveModule (struct PyModuleDef * def )
709
720
{
710
- PyInterpreterState * state ;
711
- Py_ssize_t index = def -> m_base .m_index ;
721
+ PyThreadState * tstate = _PyThreadState_GET ();
722
+ PyInterpreterState * interp = tstate -> interp ;
723
+
712
724
if (def -> m_slots ) {
713
- PyErr_SetString (PyExc_SystemError ,
714
- "PyState_RemoveModule called on module with slots" );
725
+ _PyErr_SetString (tstate ,
726
+ PyExc_SystemError ,
727
+ "PyState_RemoveModule called on module with slots" );
715
728
return -1 ;
716
729
}
717
- state = _PyInterpreterState_GET_UNSAFE ();
730
+
731
+ Py_ssize_t index = def -> m_base .m_index ;
718
732
if (index == 0 ) {
719
733
Py_FatalError ("invalid module index" );
720
- return -1 ;
721
734
}
722
- if (state -> modules_by_index == NULL ) {
735
+ if (interp -> modules_by_index == NULL ) {
723
736
Py_FatalError ("Interpreters module-list not accessible." );
724
- return -1 ;
725
737
}
726
- if (index > PyList_GET_SIZE (state -> modules_by_index )) {
738
+ if (index > PyList_GET_SIZE (interp -> modules_by_index )) {
727
739
Py_FatalError ("Module index out of bounds." );
728
- return -1 ;
729
740
}
741
+
730
742
Py_INCREF (Py_None );
731
- return PyList_SetItem (state -> modules_by_index , index , Py_None );
743
+ return PyList_SetItem (interp -> modules_by_index , index , Py_None );
732
744
}
733
745
734
746
/* Used by PyImport_Cleanup() */
@@ -1114,49 +1126,51 @@ PyThreadState_Next(PyThreadState *tstate) {
1114
1126
PyObject *
1115
1127
_PyThread_CurrentFrames (void )
1116
1128
{
1117
- PyObject * result ;
1118
- PyInterpreterState * i ;
1119
-
1120
- if (PySys_Audit ("sys._current_frames" , NULL ) < 0 ) {
1129
+ PyThreadState * tstate = _PyThreadState_GET ();
1130
+ if (_PySys_Audit (tstate , "sys._current_frames" , NULL ) < 0 ) {
1121
1131
return NULL ;
1122
1132
}
1123
1133
1124
- result = PyDict_New ();
1125
- if (result == NULL )
1134
+ PyObject * result = PyDict_New ();
1135
+ if (result == NULL ) {
1126
1136
return NULL ;
1137
+ }
1127
1138
1128
1139
/* for i in all interpreters:
1129
1140
* for t in all of i's thread states:
1130
1141
* if t's frame isn't NULL, map t's id to its frame
1131
1142
* Because these lists can mutate even when the GIL is held, we
1132
1143
* need to grab head_mutex for the duration.
1133
1144
*/
1134
- _PyRuntimeState * runtime = & _PyRuntime ;
1145
+ _PyRuntimeState * runtime = tstate -> interp -> runtime ;
1135
1146
HEAD_LOCK (runtime );
1147
+ PyInterpreterState * i ;
1136
1148
for (i = runtime -> interpreters .head ; i != NULL ; i = i -> next ) {
1137
1149
PyThreadState * t ;
1138
1150
for (t = i -> tstate_head ; t != NULL ; t = t -> next ) {
1139
- PyObject * id ;
1140
- int stat ;
1141
1151
struct _frame * frame = t -> frame ;
1142
- if (frame == NULL )
1152
+ if (frame == NULL ) {
1143
1153
continue ;
1144
- id = PyLong_FromUnsignedLong (t -> thread_id );
1145
- if (id == NULL )
1146
- goto Fail ;
1147
- stat = PyDict_SetItem (result , id , (PyObject * )frame );
1154
+ }
1155
+ PyObject * id = PyLong_FromUnsignedLong (t -> thread_id );
1156
+ if (id == NULL ) {
1157
+ goto fail ;
1158
+ }
1159
+ int stat = PyDict_SetItem (result , id , (PyObject * )frame );
1148
1160
Py_DECREF (id );
1149
- if (stat < 0 )
1150
- goto Fail ;
1161
+ if (stat < 0 ) {
1162
+ goto fail ;
1163
+ }
1151
1164
}
1152
1165
}
1153
- HEAD_UNLOCK (runtime );
1154
- return result ;
1166
+ goto done ;
1167
+
1168
+ fail :
1169
+ Py_CLEAR (result );
1155
1170
1156
- Fail :
1171
+ done :
1157
1172
HEAD_UNLOCK (runtime );
1158
- Py_DECREF (result );
1159
- return NULL ;
1173
+ return result ;
1160
1174
}
1161
1175
1162
1176
/* Python "auto thread state" API. */
@@ -1436,19 +1450,19 @@ _PyObject_CheckCrossInterpreterData(PyObject *obj)
1436
1450
}
1437
1451
1438
1452
static int
1439
- _check_xidata (_PyCrossInterpreterData * data )
1453
+ _check_xidata (PyThreadState * tstate , _PyCrossInterpreterData * data )
1440
1454
{
1441
1455
// data->data can be anything, including NULL, so we don't check it.
1442
1456
1443
1457
// data->obj may be NULL, so we don't check it.
1444
1458
1445
1459
if (data -> interp < 0 ) {
1446
- PyErr_SetString ( PyExc_SystemError , "missing interp" );
1460
+ _PyErr_SetString ( tstate , PyExc_SystemError , "missing interp" );
1447
1461
return -1 ;
1448
1462
}
1449
1463
1450
1464
if (data -> new_object == NULL ) {
1451
- PyErr_SetString ( PyExc_SystemError , "missing new_object func" );
1465
+ _PyErr_SetString ( tstate , PyExc_SystemError , "missing new_object func" );
1452
1466
return -1 ;
1453
1467
}
1454
1468
@@ -1460,9 +1474,9 @@ _check_xidata(_PyCrossInterpreterData *data)
1460
1474
int
1461
1475
_PyObject_GetCrossInterpreterData (PyObject * obj , _PyCrossInterpreterData * data )
1462
1476
{
1463
- // PyInterpreterState_Get () aborts if lookup fails, so we don't need
1464
- // to check the result for NULL.
1465
- PyInterpreterState * interp = PyInterpreterState_Get () ;
1477
+ // PyThreadState_Get () aborts if tstate is NULL.
1478
+ PyThreadState * tstate = PyThreadState_Get ();
1479
+ PyInterpreterState * interp = tstate -> interp ;
1466
1480
1467
1481
// Reset data before re-populating.
1468
1482
* data = (_PyCrossInterpreterData ){0 };
@@ -1483,7 +1497,7 @@ _PyObject_GetCrossInterpreterData(PyObject *obj, _PyCrossInterpreterData *data)
1483
1497
1484
1498
// Fill in the blanks and validate the result.
1485
1499
data -> interp = interp -> id ;
1486
- if (_check_xidata (data ) != 0 ) {
1500
+ if (_check_xidata (tstate , data ) != 0 ) {
1487
1501
_PyCrossInterpreterData_Release (data );
1488
1502
return -1 ;
1489
1503
}
0 commit comments