Skip to content

Commit 17ba47e

Browse files
committed
gh-130821: Add type info to error message in Modules/_abc.c
When an item yielded by a class namespace's items() is not iterable during abstract-method computation, include the type of the offending item in the raised TypeError, matching the type-information convention established in gh-130835. This targets the same _abc.c hunk that gh-144737 (now stale) proposed, using PyErr_ExceptionMatches to only replace the message on a genuine TypeError instead of passing NULL through PySequence_Fast, which risks a NULL PyErr_SetString call if that path is ever reached with a different underlying error.
1 parent 91d71dd commit 17ba47e

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Include the type name in the :exc:`TypeError` raised when an item yielded by
2+
``items()`` is not iterable during abstract method computation in
3+
:mod:`abc`. Patch by Jason Mak.

Modules/_abc.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,10 +383,17 @@ compute_abstract_methods(PyObject *self)
383383
}
384384
assert(PyList_Check(items));
385385
for (Py_ssize_t pos = 0; pos < PyList_GET_SIZE(items); pos++) {
386+
PyObject *item = PyList_GET_ITEM(items, pos);
386387
PyObject *it = PySequence_Fast(
387-
PyList_GET_ITEM(items, pos),
388+
item,
388389
"items() returned non-iterable");
389390
if (!it) {
391+
if (PyErr_ExceptionMatches(PyExc_TypeError)) {
392+
PyErr_Format(PyExc_TypeError,
393+
"items() must yield iterable (key, value) "
394+
"pairs, not %T",
395+
item);
396+
}
390397
goto error;
391398
}
392399
if (PySequence_Fast_GET_SIZE(it) != 2) {

0 commit comments

Comments
 (0)