Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2e8474d

Browse files
committed
Issue #18408: slot_tp_str() must not fallback on slot_tp_repr() on error
type->tp_str must not point to slot_tp_str() if type has no __str__ attribute, so there is no reason for slot_tp_str() to fallback on slot_tp_str() on lookup error. Moreover, calling PyErr_Clear() may hide a real bug like MemoryError. If __str__ attribute is removed, slots must be updated (which is done by type_setattro()).
1 parent 54e4ca7 commit 2e8474d

1 file changed

Lines changed: 2 additions & 19 deletions

File tree

Objects/typeobject.c

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5274,29 +5274,12 @@ slot_tp_str(PyObject *self)
52745274
_Py_IDENTIFIER(__str__);
52755275

52765276
func = lookup_method(self, &PyId___str__);
5277-
if (func != NULL) {
5277+
if (func == NULL)
5278+
return NULL;
52785279
res = PyEval_CallObject(func, NULL);
52795280
Py_DECREF(func);
52805281
return res;
52815282
}
5282-
else {
5283-
/* PyObject *ress; */
5284-
PyErr_Clear();
5285-
res = slot_tp_repr(self);
5286-
if (!res)
5287-
return NULL;
5288-
/* XXX this is non-sensical. Why should we return
5289-
a bytes object from __str__. Is this code even
5290-
used? - mvl */
5291-
assert(0);
5292-
return res;
5293-
/*
5294-
ress = _PyUnicode_AsDefaultEncodedString(res);
5295-
Py_DECREF(res);
5296-
return ress;
5297-
*/
5298-
}
5299-
}
53005283

53015284
static Py_hash_t
53025285
slot_tp_hash(PyObject *self)

0 commit comments

Comments
 (0)