1
1
#include "Python.h"
2
+ #include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION_MUT()
2
3
#include "pycore_interp.h" // PyInterpreterState.warnings
3
4
#include "pycore_long.h" // _PyLong_GetZero()
4
5
#include "pycore_pyerrors.h" // _PyErr_Occurred()
@@ -235,14 +236,12 @@ get_warnings_attr(PyInterpreterState *interp, PyObject *attr, int try_import)
235
236
static PyObject *
236
237
get_once_registry (PyInterpreterState * interp )
237
238
{
238
- PyObject * registry ;
239
-
240
239
WarningsState * st = warnings_get_state (interp );
241
- if (st == NULL ) {
242
- return NULL ;
243
- }
240
+ assert (st != NULL );
241
+
242
+ _Py_CRITICAL_SECTION_ASSERT_MUTEX_LOCKED ( & st -> mutex );
244
243
245
- registry = GET_WARNINGS_ATTR (interp , onceregistry , 0 );
244
+ PyObject * registry = GET_WARNINGS_ATTR (interp , onceregistry , 0 );
246
245
if (registry == NULL ) {
247
246
if (PyErr_Occurred ())
248
247
return NULL ;
@@ -265,14 +264,12 @@ get_once_registry(PyInterpreterState *interp)
265
264
static PyObject *
266
265
get_default_action (PyInterpreterState * interp )
267
266
{
268
- PyObject * default_action ;
269
-
270
267
WarningsState * st = warnings_get_state (interp );
271
- if (st == NULL ) {
272
- return NULL ;
273
- }
268
+ assert (st != NULL );
274
269
275
- default_action = GET_WARNINGS_ATTR (interp , defaultaction , 0 );
270
+ _Py_CRITICAL_SECTION_ASSERT_MUTEX_LOCKED (& st -> mutex );
271
+
272
+ PyObject * default_action = GET_WARNINGS_ATTR (interp , defaultaction , 0 );
276
273
if (default_action == NULL ) {
277
274
if (PyErr_Occurred ()) {
278
275
return NULL ;
@@ -299,15 +296,12 @@ get_filter(PyInterpreterState *interp, PyObject *category,
299
296
PyObject * text , Py_ssize_t lineno ,
300
297
PyObject * module , PyObject * * item )
301
298
{
302
- PyObject * action ;
303
- Py_ssize_t i ;
304
- PyObject * warnings_filters ;
305
299
WarningsState * st = warnings_get_state (interp );
306
- if (st == NULL ) {
307
- return NULL ;
308
- }
300
+ assert (st != NULL );
309
301
310
- warnings_filters = GET_WARNINGS_ATTR (interp , filters , 0 );
302
+ _Py_CRITICAL_SECTION_ASSERT_MUTEX_LOCKED (& st -> mutex );
303
+
304
+ PyObject * warnings_filters = GET_WARNINGS_ATTR (interp , filters , 0 );
311
305
if (warnings_filters == NULL ) {
312
306
if (PyErr_Occurred ())
313
307
return NULL ;
@@ -324,7 +318,7 @@ get_filter(PyInterpreterState *interp, PyObject *category,
324
318
}
325
319
326
320
/* WarningsState.filters could change while we are iterating over it. */
327
- for (i = 0 ; i < PyList_GET_SIZE (filters ); i ++ ) {
321
+ for (Py_ssize_t i = 0 ; i < PyList_GET_SIZE (filters ); i ++ ) {
328
322
PyObject * tmp_item , * action , * msg , * cat , * mod , * ln_obj ;
329
323
Py_ssize_t ln ;
330
324
int is_subclass , good_msg , good_mod ;
@@ -384,7 +378,7 @@ get_filter(PyInterpreterState *interp, PyObject *category,
384
378
Py_DECREF (tmp_item );
385
379
}
386
380
387
- action = get_default_action (interp );
381
+ PyObject * action = get_default_action (interp );
388
382
if (action != NULL ) {
389
383
* item = Py_NewRef (Py_None );
390
384
return action ;
@@ -1000,8 +994,13 @@ do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level,
1000
994
& filename , & lineno , & module , & registry ))
1001
995
return NULL ;
1002
996
997
+ WarningsState * st = warnings_get_state (tstate -> interp );
998
+ assert (st != NULL );
999
+
1000
+ Py_BEGIN_CRITICAL_SECTION_MUT (& st -> mutex );
1003
1001
res = warn_explicit (tstate , category , message , filename , lineno , module , registry ,
1004
1002
NULL , source );
1003
+ Py_END_CRITICAL_SECTION ();
1005
1004
Py_DECREF (filename );
1006
1005
Py_DECREF (registry );
1007
1006
Py_DECREF (module );
@@ -1149,8 +1148,14 @@ warnings_warn_explicit_impl(PyObject *module, PyObject *message,
1149
1148
return NULL ;
1150
1149
}
1151
1150
}
1151
+
1152
+ WarningsState * st = warnings_get_state (tstate -> interp );
1153
+ assert (st != NULL );
1154
+
1155
+ Py_BEGIN_CRITICAL_SECTION_MUT (& st -> mutex );
1152
1156
returned = warn_explicit (tstate , category , message , filename , lineno ,
1153
1157
mod , registry , source_line , sourceobj );
1158
+ Py_END_CRITICAL_SECTION ();
1154
1159
Py_XDECREF (source_line );
1155
1160
return returned ;
1156
1161
}
@@ -1290,8 +1295,14 @@ PyErr_WarnExplicitObject(PyObject *category, PyObject *message,
1290
1295
if (tstate == NULL ) {
1291
1296
return -1 ;
1292
1297
}
1298
+
1299
+ WarningsState * st = warnings_get_state (tstate -> interp );
1300
+ assert (st != NULL );
1301
+
1302
+ Py_BEGIN_CRITICAL_SECTION_MUT (& st -> mutex );
1293
1303
res = warn_explicit (tstate , category , message , filename , lineno ,
1294
1304
module , registry , NULL , NULL );
1305
+ Py_END_CRITICAL_SECTION ();
1295
1306
if (res == NULL )
1296
1307
return -1 ;
1297
1308
Py_DECREF (res );
@@ -1356,8 +1367,13 @@ PyErr_WarnExplicitFormat(PyObject *category,
1356
1367
PyObject * res ;
1357
1368
PyThreadState * tstate = get_current_tstate ();
1358
1369
if (tstate != NULL ) {
1370
+ WarningsState * st = warnings_get_state (tstate -> interp );
1371
+ assert (st != NULL );
1372
+
1373
+ Py_BEGIN_CRITICAL_SECTION_MUT (& st -> mutex );
1359
1374
res = warn_explicit (tstate , category , message , filename , lineno ,
1360
1375
module , registry , NULL , NULL );
1376
+ Py_END_CRITICAL_SECTION ();
1361
1377
Py_DECREF (message );
1362
1378
if (res != NULL ) {
1363
1379
Py_DECREF (res );
0 commit comments