3
3
#include "Python.h"
4
4
#include "pycore_abstract.h" // _PyIndex_Check()
5
5
#include "pycore_ceval.h" // _Py_EnterRecursiveCall()
6
- #include "pycore_pyerrors.h"
6
+ #include "pycore_pyerrors.h" // _PyErr_Occurred()
7
7
#include "pycore_pystate.h" // _PyThreadState_GET()
8
8
#include <ctype.h>
9
9
#include <stddef.h> // offsetof()
@@ -23,9 +23,11 @@ type_error(const char *msg, PyObject *obj)
23
23
static PyObject *
24
24
null_error (void )
25
25
{
26
- if (!PyErr_Occurred ())
27
- PyErr_SetString (PyExc_SystemError ,
28
- "null argument to internal routine" );
26
+ PyThreadState * tstate = _PyThreadState_GET ();
27
+ if (!_PyErr_Occurred (tstate )) {
28
+ _PyErr_SetString (tstate , PyExc_SystemError ,
29
+ "null argument to internal routine" );
30
+ }
29
31
return NULL ;
30
32
}
31
33
@@ -94,11 +96,12 @@ PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
94
96
if (_PyObject_HasLen (o )) {
95
97
res = PyObject_Length (o );
96
98
if (res < 0 ) {
97
- assert (PyErr_Occurred ());
98
- if (!PyErr_ExceptionMatches (PyExc_TypeError )) {
99
+ PyThreadState * tstate = _PyThreadState_GET ();
100
+ assert (_PyErr_Occurred (tstate ));
101
+ if (!_PyErr_ExceptionMatches (tstate , PyExc_TypeError )) {
99
102
return -1 ;
100
103
}
101
- PyErr_Clear ( );
104
+ _PyErr_Clear ( tstate );
102
105
}
103
106
else {
104
107
return res ;
@@ -114,8 +117,9 @@ PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
114
117
result = _PyObject_CallNoArg (hint );
115
118
Py_DECREF (hint );
116
119
if (result == NULL ) {
117
- if (PyErr_ExceptionMatches (PyExc_TypeError )) {
118
- PyErr_Clear ();
120
+ PyThreadState * tstate = _PyThreadState_GET ();
121
+ if (_PyErr_ExceptionMatches (tstate , PyExc_TypeError )) {
122
+ _PyErr_Clear (tstate );
119
123
return defaultvalue ;
120
124
}
121
125
return -1 ;
@@ -708,7 +712,7 @@ PyBuffer_FillInfo(Py_buffer *view, PyObject *obj, void *buf, Py_ssize_t len,
708
712
{
709
713
if (view == NULL ) {
710
714
PyErr_SetString (PyExc_BufferError ,
711
- "PyBuffer_FillInfo: view==NULL argument is obsolete" );
715
+ "PyBuffer_FillInfo: view==NULL argument is obsolete" );
712
716
return -1 ;
713
717
}
714
718
@@ -790,10 +794,12 @@ PyObject_Format(PyObject *obj, PyObject *format_spec)
790
794
/* Find the (unbound!) __format__ method */
791
795
meth = _PyObject_LookupSpecial (obj , & PyId___format__ );
792
796
if (meth == NULL ) {
793
- if (!PyErr_Occurred ())
794
- PyErr_Format (PyExc_TypeError ,
795
- "Type %.100s doesn't define __format__" ,
796
- Py_TYPE (obj )-> tp_name );
797
+ PyThreadState * tstate = _PyThreadState_GET ();
798
+ if (!_PyErr_Occurred (tstate )) {
799
+ _PyErr_Format (tstate , PyExc_TypeError ,
800
+ "Type %.100s doesn't define __format__" ,
801
+ Py_TYPE (obj )-> tp_name );
802
+ }
797
803
goto done ;
798
804
}
799
805
@@ -803,8 +809,8 @@ PyObject_Format(PyObject *obj, PyObject *format_spec)
803
809
804
810
if (result && !PyUnicode_Check (result )) {
805
811
PyErr_Format (PyExc_TypeError ,
806
- "__format__ must return a str, not %.200s" ,
807
- Py_TYPE (result )-> tp_name );
812
+ "__format__ must return a str, not %.200s" ,
813
+ Py_TYPE (result )-> tp_name );
808
814
Py_DECREF (result );
809
815
result = NULL ;
810
816
goto done ;
@@ -1388,17 +1394,23 @@ PyNumber_AsSsize_t(PyObject *item, PyObject *err)
1388
1394
1389
1395
/* We're done if PyLong_AsSsize_t() returns without error. */
1390
1396
result = PyLong_AsSsize_t (value );
1391
- if (result != -1 || !(runerr = PyErr_Occurred ()))
1397
+ if (result != -1 )
1398
+ goto finish ;
1399
+
1400
+ PyThreadState * tstate = _PyThreadState_GET ();
1401
+ runerr = _PyErr_Occurred (tstate );
1402
+ if (!runerr ) {
1392
1403
goto finish ;
1404
+ }
1393
1405
1394
1406
/* Error handling code -- only manage OverflowError differently */
1395
- if (!PyErr_GivenExceptionMatches (runerr , PyExc_OverflowError ))
1407
+ if (!PyErr_GivenExceptionMatches (runerr , PyExc_OverflowError )) {
1396
1408
goto finish ;
1409
+ }
1410
+ _PyErr_Clear (tstate );
1397
1411
1398
- PyErr_Clear ();
1399
1412
/* If no error-handling desired then the default clipping
1400
- is sufficient.
1401
- */
1413
+ is sufficient. */
1402
1414
if (!err ) {
1403
1415
assert (PyLong_Check (value ));
1404
1416
/* Whether or not it is less than or equal to
@@ -1411,9 +1423,9 @@ PyNumber_AsSsize_t(PyObject *item, PyObject *err)
1411
1423
}
1412
1424
else {
1413
1425
/* Otherwise replace the error with caller's error object. */
1414
- PyErr_Format ( err ,
1415
- "cannot fit '%.200s' into an index-sized integer" ,
1416
- Py_TYPE (item )-> tp_name );
1426
+ _PyErr_Format ( tstate , err ,
1427
+ "cannot fit '%.200s' into an index-sized integer" ,
1428
+ Py_TYPE (item )-> tp_name );
1417
1429
}
1418
1430
1419
1431
finish :
@@ -1448,8 +1460,8 @@ PyNumber_Long(PyObject *o)
1448
1460
return result ;
1449
1461
if (!PyLong_Check (result )) {
1450
1462
PyErr_Format (PyExc_TypeError ,
1451
- "__int__ returned non-int (type %.200s)" ,
1452
- result -> ob_type -> tp_name );
1463
+ "__int__ returned non-int (type %.200s)" ,
1464
+ result -> ob_type -> tp_name );
1453
1465
Py_DECREF (result );
1454
1466
return NULL ;
1455
1467
}
@@ -2052,8 +2064,10 @@ PySequence_Fast(PyObject *v, const char *m)
2052
2064
2053
2065
it = PyObject_GetIter (v );
2054
2066
if (it == NULL ) {
2055
- if (PyErr_ExceptionMatches (PyExc_TypeError ))
2056
- PyErr_SetString (PyExc_TypeError , m );
2067
+ PyThreadState * tstate = _PyThreadState_GET ();
2068
+ if (_PyErr_ExceptionMatches (tstate , PyExc_TypeError )) {
2069
+ _PyErr_SetString (tstate , PyExc_TypeError , m );
2070
+ }
2057
2071
return NULL ;
2058
2072
}
2059
2073
@@ -2310,12 +2324,13 @@ method_output_as_list(PyObject *o, _Py_Identifier *meth_id)
2310
2324
}
2311
2325
it = PyObject_GetIter (meth_output );
2312
2326
if (it == NULL ) {
2313
- if (PyErr_ExceptionMatches (PyExc_TypeError )) {
2314
- PyErr_Format (PyExc_TypeError ,
2315
- "%.200s.%U() returned a non-iterable (type %.200s)" ,
2316
- Py_TYPE (o )-> tp_name ,
2317
- _PyUnicode_FromId (meth_id ),
2318
- Py_TYPE (meth_output )-> tp_name );
2327
+ PyThreadState * tstate = _PyThreadState_GET ();
2328
+ if (_PyErr_ExceptionMatches (tstate , PyExc_TypeError )) {
2329
+ _PyErr_Format (tstate , PyExc_TypeError ,
2330
+ "%.200s.%U() returned a non-iterable (type %.200s)" ,
2331
+ Py_TYPE (o )-> tp_name ,
2332
+ _PyUnicode_FromId (meth_id ),
2333
+ Py_TYPE (meth_output )-> tp_name );
2319
2334
}
2320
2335
Py_DECREF (meth_output );
2321
2336
return NULL ;
@@ -2460,8 +2475,10 @@ check_class(PyObject *cls, const char *error)
2460
2475
PyObject * bases = abstract_get_bases (cls );
2461
2476
if (bases == NULL ) {
2462
2477
/* Do not mask errors. */
2463
- if (!PyErr_Occurred ())
2464
- PyErr_SetString (PyExc_TypeError , error );
2478
+ PyThreadState * tstate = _PyThreadState_GET ();
2479
+ if (!_PyErr_Occurred (tstate )) {
2480
+ _PyErr_SetString (tstate , PyExc_TypeError , error );
2481
+ }
2465
2482
return 0 ;
2466
2483
}
2467
2484
Py_DECREF (bases );
@@ -2719,10 +2736,14 @@ PyIter_Next(PyObject *iter)
2719
2736
{
2720
2737
PyObject * result ;
2721
2738
result = (* Py_TYPE (iter )-> tp_iternext )(iter );
2722
- if (result == NULL &&
2723
- PyErr_Occurred () &&
2724
- PyErr_ExceptionMatches (PyExc_StopIteration ))
2725
- PyErr_Clear ();
2739
+ if (result == NULL ) {
2740
+ PyThreadState * tstate = _PyThreadState_GET ();
2741
+ if (_PyErr_Occurred (tstate )
2742
+ && _PyErr_ExceptionMatches (tstate , PyExc_StopIteration ))
2743
+ {
2744
+ _PyErr_Clear (tstate );
2745
+ }
2746
+ }
2726
2747
return result ;
2727
2748
}
2728
2749
0 commit comments