77
88
99static PyObject * const *
10- _PyStack_UnpackDict (PyObject * const * args , Py_ssize_t nargs , PyObject * kwargs ,
11- PyObject * * p_kwnames );
10+ _PyStack_UnpackDict (PyThreadState * tstate ,
11+ PyObject * const * args , Py_ssize_t nargs ,
12+ PyObject * kwargs , PyObject * * p_kwnames );
1213
1314static void
1415_PyStack_UnpackDict_Free (PyObject * const * stack , Py_ssize_t nargs ,
@@ -26,22 +27,23 @@ null_error(void)
2627
2728
2829PyObject *
29- _Py_CheckFunctionResult (PyObject * callable , PyObject * result , const char * where )
30+ _Py_CheckFunctionResult (PyThreadState * tstate , PyObject * callable ,
31+ PyObject * result , const char * where )
3032{
31- int err_occurred = (PyErr_Occurred ( ) != NULL );
33+ int err_occurred = (_PyErr_Occurred ( tstate ) != NULL );
3234
3335 assert ((callable != NULL ) ^ (where != NULL ));
3436
3537 if (result == NULL ) {
3638 if (!err_occurred ) {
3739 if (callable )
38- PyErr_Format ( PyExc_SystemError ,
39- "%R returned NULL without setting an error" ,
40- callable );
40+ _PyErr_Format ( tstate , PyExc_SystemError ,
41+ "%R returned NULL without setting an error" ,
42+ callable );
4143 else
42- PyErr_Format ( PyExc_SystemError ,
43- "%s returned NULL without setting an error" ,
44- where );
44+ _PyErr_Format ( tstate , PyExc_SystemError ,
45+ "%s returned NULL without setting an error" ,
46+ where );
4547#ifdef Py_DEBUG
4648 /* Ensure that the bug is caught in debug mode */
4749 Py_FatalError ("a function returned NULL without setting an error" );
@@ -54,14 +56,14 @@ _Py_CheckFunctionResult(PyObject *callable, PyObject *result, const char *where)
5456 Py_DECREF (result );
5557
5658 if (callable ) {
57- _PyErr_FormatFromCause ( PyExc_SystemError ,
58- "%R returned a result with an error set" ,
59- callable );
59+ _PyErr_FormatFromCauseTstate (
60+ tstate , PyExc_SystemError ,
61+ "%R returned a result with an error set" , callable );
6062 }
6163 else {
62- _PyErr_FormatFromCause ( PyExc_SystemError ,
63- "%s returned a result with an error set" ,
64- where );
64+ _PyErr_FormatFromCauseTstate (
65+ tstate , PyExc_SystemError ,
66+ "%s returned a result with an error set" , where );
6567 }
6668#ifdef Py_DEBUG
6769 /* Ensure that the bug is caught in debug mode */
@@ -88,11 +90,13 @@ PyObject *
8890_PyObject_FastCallDict (PyObject * callable , PyObject * const * args ,
8991 size_t nargsf , PyObject * kwargs )
9092{
93+ assert (callable != NULL );
94+
95+ PyThreadState * tstate = _PyThreadState_GET ();
9196 /* _PyObject_FastCallDict() must not be called with an exception set,
9297 because it can clear it (directly or indirectly) and so the
9398 caller loses its exception */
94- assert (!PyErr_Occurred ());
95- assert (callable != NULL );
99+ assert (!_PyErr_Occurred (tstate ));
96100
97101 Py_ssize_t nargs = PyVectorcall_NARGS (nargsf );
98102 assert (nargs >= 0 );
@@ -112,15 +116,17 @@ _PyObject_FastCallDict(PyObject *callable, PyObject *const *args,
112116 else {
113117 PyObject * kwnames ;
114118 PyObject * const * newargs ;
115- newargs = _PyStack_UnpackDict (args , nargs , kwargs , & kwnames );
119+ newargs = _PyStack_UnpackDict (tstate ,
120+ args , nargs ,
121+ kwargs , & kwnames );
116122 if (newargs == NULL ) {
117123 return NULL ;
118124 }
119125 res = func (callable , newargs ,
120126 nargs | PY_VECTORCALL_ARGUMENTS_OFFSET , kwnames );
121127 _PyStack_UnpackDict_Free (newargs , nargs , kwnames );
122128 }
123- return _Py_CheckFunctionResult (callable , res , NULL );
129+ return _Py_CheckFunctionResult (tstate , callable , res , NULL );
124130}
125131
126132
@@ -177,26 +183,30 @@ _PyObject_MakeTpCall(PyObject *callable, PyObject *const *args, Py_ssize_t nargs
177183 Py_DECREF (kwdict );
178184 }
179185
180- result = _Py_CheckFunctionResult (callable , result , NULL );
186+ result = _Py_CheckFunctionResult (tstate , callable , result , NULL );
181187 return result ;
182188}
183189
184190
185191PyObject *
186192PyVectorcall_Call (PyObject * callable , PyObject * tuple , PyObject * kwargs )
187193{
194+ PyThreadState * tstate = _PyThreadState_GET ();
195+
188196 /* get vectorcallfunc as in _PyVectorcall_Function, but without
189197 * the _Py_TPFLAGS_HAVE_VECTORCALL check */
190198 Py_ssize_t offset = Py_TYPE (callable )-> tp_vectorcall_offset ;
191199 if (offset <= 0 ) {
192- PyErr_Format (PyExc_TypeError , "'%.200s' object does not support vectorcall" ,
193- Py_TYPE (callable )-> tp_name );
200+ _PyErr_Format (tstate , PyExc_TypeError ,
201+ "'%.200s' object does not support vectorcall" ,
202+ Py_TYPE (callable )-> tp_name );
194203 return NULL ;
195204 }
196205 vectorcallfunc func = * (vectorcallfunc * )(((char * )callable ) + offset );
197206 if (func == NULL ) {
198- PyErr_Format (PyExc_TypeError , "'%.200s' object does not support vectorcall" ,
199- Py_TYPE (callable )-> tp_name );
207+ _PyErr_Format (tstate , PyExc_TypeError ,
208+ "'%.200s' object does not support vectorcall" ,
209+ Py_TYPE (callable )-> tp_name );
200210 return NULL ;
201211 }
202212
@@ -210,14 +220,16 @@ PyVectorcall_Call(PyObject *callable, PyObject *tuple, PyObject *kwargs)
210220 /* Convert arguments & call */
211221 PyObject * const * args ;
212222 PyObject * kwnames ;
213- args = _PyStack_UnpackDict (_PyTuple_ITEMS (tuple ), nargs , kwargs , & kwnames );
223+ args = _PyStack_UnpackDict (tstate ,
224+ _PyTuple_ITEMS (tuple ), nargs ,
225+ kwargs , & kwnames );
214226 if (args == NULL ) {
215227 return NULL ;
216228 }
217229 PyObject * result = func (callable , args ,
218230 nargs | PY_VECTORCALL_ARGUMENTS_OFFSET , kwnames );
219231 _PyStack_UnpackDict_Free (args , nargs , kwnames );
220- return _Py_CheckFunctionResult (callable , result , NULL );
232+ return _Py_CheckFunctionResult (tstate , callable , result , NULL );
221233}
222234
223235
@@ -255,7 +267,7 @@ PyObject_Call(PyObject *callable, PyObject *args, PyObject *kwargs)
255267
256268 _Py_LeaveRecursiveCall (tstate );
257269
258- return _Py_CheckFunctionResult (callable , result , NULL );
270+ return _Py_CheckFunctionResult (tstate , callable , result , NULL );
259271 }
260272}
261273
@@ -898,8 +910,9 @@ _PyStack_AsDict(PyObject *const *values, PyObject *kwnames)
898910
899911 When done, you must call _PyStack_UnpackDict_Free(stack, nargs, kwnames) */
900912static PyObject * const *
901- _PyStack_UnpackDict (PyObject * const * args , Py_ssize_t nargs , PyObject * kwargs ,
902- PyObject * * p_kwnames )
913+ _PyStack_UnpackDict (PyThreadState * tstate ,
914+ PyObject * const * args , Py_ssize_t nargs ,
915+ PyObject * kwargs , PyObject * * p_kwnames )
903916{
904917 assert (nargs >= 0 );
905918 assert (kwargs != NULL );
@@ -911,14 +924,14 @@ _PyStack_UnpackDict(PyObject *const *args, Py_ssize_t nargs, PyObject *kwargs,
911924 * non-negative signed integers, so their difference fits in the type. */
912925 Py_ssize_t maxnargs = PY_SSIZE_T_MAX / sizeof (args [0 ]) - 1 ;
913926 if (nargs > maxnargs - nkwargs ) {
914- PyErr_NoMemory ( );
927+ _PyErr_NoMemory ( tstate );
915928 return NULL ;
916929 }
917930
918931 /* Add 1 to support PY_VECTORCALL_ARGUMENTS_OFFSET */
919932 PyObject * * stack = PyMem_Malloc ((1 + nargs + nkwargs ) * sizeof (args [0 ]));
920933 if (stack == NULL ) {
921- PyErr_NoMemory ( );
934+ _PyErr_NoMemory ( tstate );
922935 return NULL ;
923936 }
924937
@@ -958,8 +971,8 @@ _PyStack_UnpackDict(PyObject *const *args, Py_ssize_t nargs, PyObject *kwargs,
958971 * because it simplifies the deallocation in the failing case.
959972 * It happens to also make the loop above slightly more efficient. */
960973 if (!keys_are_strings ) {
961- PyErr_SetString ( PyExc_TypeError ,
962- "keywords must be strings" );
974+ _PyErr_SetString ( tstate , PyExc_TypeError ,
975+ "keywords must be strings" );
963976 _PyStack_UnpackDict_Free (stack , nargs , kwnames );
964977 return NULL ;
965978 }
0 commit comments