diff --git a/Objects/abstract.c b/Objects/abstract.c index 46288dd9411..1cb4ef819a2 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -1507,8 +1507,9 @@ PySequence_Tuple(PyObject *v) /* Guess result size and allocate space. */ n = _PyObject_LengthHint(v); if (n < 0) { - if (!PyErr_ExceptionMatches(PyExc_TypeError) && - !PyErr_ExceptionMatches(PyExc_AttributeError)) { + if (PyErr_Occurred() + && !PyErr_ExceptionMatches(PyExc_TypeError) + && !PyErr_ExceptionMatches(PyExc_AttributeError)) { Py_DECREF(it); return NULL; } diff --git a/Objects/listobject.c b/Objects/listobject.c index deb3ca5a025..8389a86ea71 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -796,8 +796,9 @@ listextend(PyListObject *self, PyObject *b) /* Guess a result list size. */ n = _PyObject_LengthHint(b); if (n < 0) { - if (!PyErr_ExceptionMatches(PyExc_TypeError) && - !PyErr_ExceptionMatches(PyExc_AttributeError)) { + if (PyErr_Occurred() + && !PyErr_ExceptionMatches(PyExc_TypeError) + && !PyErr_ExceptionMatches(PyExc_AttributeError)) { Py_DECREF(it); return NULL; }