bpo-36710: Pass tstate explicitly in abstract.c (GH-21075)

In functions calling more than one PyErr function, get tstate and
then pass it explicitly.
This commit is contained in:
Victor Stinner 2020-06-23 15:55:06 +02:00 committed by GitHub
parent c41eed1a87
commit 61b6492961
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 61 additions and 40 deletions

View File

@ -3,7 +3,7 @@
#include "Python.h"
#include "pycore_abstract.h" // _PyIndex_Check()
#include "pycore_ceval.h" // _Py_EnterRecursiveCall()
#include "pycore_pyerrors.h"
#include "pycore_pyerrors.h" // _PyErr_Occurred()
#include "pycore_pystate.h" // _PyThreadState_GET()
#include <ctype.h>
#include <stddef.h> // offsetof()
@ -23,9 +23,11 @@ type_error(const char *msg, PyObject *obj)
static PyObject *
null_error(void)
{
if (!PyErr_Occurred())
PyErr_SetString(PyExc_SystemError,
PyThreadState *tstate = _PyThreadState_GET();
if (!_PyErr_Occurred(tstate)) {
_PyErr_SetString(tstate, PyExc_SystemError,
"null argument to internal routine");
}
return NULL;
}
@ -94,11 +96,12 @@ PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
if (_PyObject_HasLen(o)) {
res = PyObject_Length(o);
if (res < 0) {
assert(PyErr_Occurred());
if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
PyThreadState *tstate = _PyThreadState_GET();
assert(_PyErr_Occurred(tstate));
if (!_PyErr_ExceptionMatches(tstate, PyExc_TypeError)) {
return -1;
}
PyErr_Clear();
_PyErr_Clear(tstate);
}
else {
return res;
@ -114,8 +117,9 @@ PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
result = _PyObject_CallNoArg(hint);
Py_DECREF(hint);
if (result == NULL) {
if (PyErr_ExceptionMatches(PyExc_TypeError)) {
PyErr_Clear();
PyThreadState *tstate = _PyThreadState_GET();
if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError)) {
_PyErr_Clear(tstate);
return defaultvalue;
}
return -1;
@ -790,10 +794,12 @@ PyObject_Format(PyObject *obj, PyObject *format_spec)
/* Find the (unbound!) __format__ method */
meth = _PyObject_LookupSpecial(obj, &PyId___format__);
if (meth == NULL) {
if (!PyErr_Occurred())
PyErr_Format(PyExc_TypeError,
PyThreadState *tstate = _PyThreadState_GET();
if (!_PyErr_Occurred(tstate)) {
_PyErr_Format(tstate, PyExc_TypeError,
"Type %.100s doesn't define __format__",
Py_TYPE(obj)->tp_name);
}
goto done;
}
@ -1388,17 +1394,23 @@ PyNumber_AsSsize_t(PyObject *item, PyObject *err)
/* We're done if PyLong_AsSsize_t() returns without error. */
result = PyLong_AsSsize_t(value);
if (result != -1 || !(runerr = PyErr_Occurred()))
if (result != -1)
goto finish;
PyThreadState *tstate = _PyThreadState_GET();
runerr = _PyErr_Occurred(tstate);
if (!runerr) {
goto finish;
}
/* Error handling code -- only manage OverflowError differently */
if (!PyErr_GivenExceptionMatches(runerr, PyExc_OverflowError))
if (!PyErr_GivenExceptionMatches(runerr, PyExc_OverflowError)) {
goto finish;
}
_PyErr_Clear(tstate);
PyErr_Clear();
/* If no error-handling desired then the default clipping
is sufficient.
*/
is sufficient. */
if (!err) {
assert(PyLong_Check(value));
/* Whether or not it is less than or equal to
@ -1411,7 +1423,7 @@ PyNumber_AsSsize_t(PyObject *item, PyObject *err)
}
else {
/* Otherwise replace the error with caller's error object. */
PyErr_Format(err,
_PyErr_Format(tstate, err,
"cannot fit '%.200s' into an index-sized integer",
Py_TYPE(item)->tp_name);
}
@ -2052,8 +2064,10 @@ PySequence_Fast(PyObject *v, const char *m)
it = PyObject_GetIter(v);
if (it == NULL) {
if (PyErr_ExceptionMatches(PyExc_TypeError))
PyErr_SetString(PyExc_TypeError, m);
PyThreadState *tstate = _PyThreadState_GET();
if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError)) {
_PyErr_SetString(tstate, PyExc_TypeError, m);
}
return NULL;
}
@ -2310,8 +2324,9 @@ method_output_as_list(PyObject *o, _Py_Identifier *meth_id)
}
it = PyObject_GetIter(meth_output);
if (it == NULL) {
if (PyErr_ExceptionMatches(PyExc_TypeError)) {
PyErr_Format(PyExc_TypeError,
PyThreadState *tstate = _PyThreadState_GET();
if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError)) {
_PyErr_Format(tstate, PyExc_TypeError,
"%.200s.%U() returned a non-iterable (type %.200s)",
Py_TYPE(o)->tp_name,
_PyUnicode_FromId(meth_id),
@ -2460,8 +2475,10 @@ check_class(PyObject *cls, const char *error)
PyObject *bases = abstract_get_bases(cls);
if (bases == NULL) {
/* Do not mask errors. */
if (!PyErr_Occurred())
PyErr_SetString(PyExc_TypeError, error);
PyThreadState *tstate = _PyThreadState_GET();
if (!_PyErr_Occurred(tstate)) {
_PyErr_SetString(tstate, PyExc_TypeError, error);
}
return 0;
}
Py_DECREF(bases);
@ -2719,10 +2736,14 @@ PyIter_Next(PyObject *iter)
{
PyObject *result;
result = (*Py_TYPE(iter)->tp_iternext)(iter);
if (result == NULL &&
PyErr_Occurred() &&
PyErr_ExceptionMatches(PyExc_StopIteration))
PyErr_Clear();
if (result == NULL) {
PyThreadState *tstate = _PyThreadState_GET();
if (_PyErr_Occurred(tstate)
&& _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
{
_PyErr_Clear(tstate);
}
}
return result;
}