@@ -129,8 +129,9 @@ PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
129129 return defaultvalue ;
130130 }
131131 if (!PyLong_Check (result )) {
132- PyErr_Format (PyExc_TypeError , "__length_hint__ must be an integer, not %.100s" ,
133- Py_TYPE (result )-> tp_name );
132+ PyErr_Format (PyExc_TypeError ,
133+ "%T.__length_hint__ must return type int (not %T)" ,
134+ o , result );
134135 Py_DECREF (result );
135136 return -1 ;
136137 }
@@ -140,7 +141,9 @@ PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
140141 return -1 ;
141142 }
142143 if (res < 0 ) {
143- PyErr_Format (PyExc_ValueError , "__length_hint__() should return >= 0" );
144+ PyErr_Format (PyExc_ValueError ,
145+ "%T.__length_hint__ must return positive int (not %T)" ,
146+ o , result );
144147 return -1 ;
145148 }
146149 return res ;
@@ -884,8 +887,8 @@ PyObject_Format(PyObject *obj, PyObject *format_spec)
884887
885888 if (result && !PyUnicode_Check (result )) {
886889 PyErr_Format (PyExc_TypeError ,
887- "__format__ must return a str, not %.200s " ,
888- Py_TYPE ( result ) -> tp_name );
890+ "%T. __format__ must return type str ( not %T) " ,
891+ obj , result );
889892 Py_SETREF (result , NULL );
890893 goto done ;
891894 }
@@ -1418,13 +1421,14 @@ _PyNumber_Index(PyObject *item)
14181421
14191422 if (!PyLong_Check (result )) {
14201423 PyErr_Format (PyExc_TypeError ,
1421- "%T.__index__ returned non-int (type %T)" , item , result );
1424+ "%T.__index__ must return type int (not %T)" ,
1425+ item , result );
14221426 Py_DECREF (result );
14231427 return NULL ;
14241428 }
14251429 /* Issue #17576: warn if 'result' not of exact type int. */
14261430 if (PyErr_WarnFormat (PyExc_DeprecationWarning , 1 ,
1427- "%T.__index__ returned non- int (type %T). "
1431+ "%T.__index__ must return type int (not %T). "
14281432 "The ability to return an instance of a strict subclass of int "
14291433 "is deprecated, and may be removed in a future version of Python." ,
14301434 item , result )) {
@@ -1527,13 +1531,13 @@ PyNumber_Long(PyObject *o)
15271531
15281532 if (!PyLong_Check (result )) {
15291533 PyErr_Format (PyExc_TypeError ,
1530- "%T.__int__ returned non- int (type %T)" , o , result );
1534+ "%T.__int__ must return type int (not %T)" , o , result );
15311535 Py_DECREF (result );
15321536 return NULL ;
15331537 }
15341538 /* Issue #17576: warn if 'result' not of exact type int. */
15351539 if (PyErr_WarnFormat (PyExc_DeprecationWarning , 1 ,
1536- "%T.__int__ returned non- int (type %T). "
1540+ "%T.__int__ must return type int (not %T). "
15371541 "The ability to return an instance of a strict subclass of int "
15381542 "is deprecated, and may be removed in a future version of Python." ,
15391543 o , result )) {
@@ -1604,13 +1608,14 @@ PyNumber_Float(PyObject *o)
16041608
16051609 if (!PyFloat_Check (res )) {
16061610 PyErr_Format (PyExc_TypeError ,
1607- "%T.__float__ returned non-float (type %T)" , o , res );
1611+ "%T.__float__ must return type float (not %T)" ,
1612+ o , res );
16081613 Py_DECREF (res );
16091614 return NULL ;
16101615 }
16111616 /* Issue #26983: warn if 'res' not of exact type float. */
16121617 if (PyErr_WarnFormat (PyExc_DeprecationWarning , 1 ,
1613- "%T.__float__ returned non- float (type %T). "
1618+ "%T.__float__ must return type float (not %T). "
16141619 "The ability to return an instance of a strict subclass of float "
16151620 "is deprecated, and may be removed in a future version of Python." ,
16161621 o , res )) {
@@ -2429,10 +2434,8 @@ method_output_as_list(PyObject *o, PyObject *meth)
24292434 PyThreadState * tstate = _PyThreadState_GET ();
24302435 if (_PyErr_ExceptionMatches (tstate , PyExc_TypeError )) {
24312436 _PyErr_Format (tstate , PyExc_TypeError ,
2432- "%.200s.%U() returned a non-iterable (type %.200s)" ,
2433- Py_TYPE (o )-> tp_name ,
2434- meth ,
2435- Py_TYPE (meth_output )-> tp_name );
2437+ "%T.%U() must return type iterable (not %T)" ,
2438+ o , meth , meth_output );
24362439 }
24372440 Py_DECREF (meth_output );
24382441 return NULL ;
@@ -2812,7 +2815,7 @@ PyObject_GetIter(PyObject *o)
28122815 PyObject * res = (* f )(o );
28132816 if (res != NULL && !PyIter_Check (res )) {
28142817 PyErr_Format (PyExc_TypeError ,
2815- "%T.iter() returned non- iterator of type '%T'" ,
2818+ "%T.iter() must return type iterator of type '%T'" ,
28162819 o , res );
28172820 Py_SETREF (res , NULL );
28182821 }
@@ -2832,8 +2835,8 @@ PyObject_GetAIter(PyObject *o) {
28322835 PyObject * it = (* f )(o );
28332836 if (it != NULL && !PyAIter_Check (it )) {
28342837 PyErr_Format (PyExc_TypeError ,
2835- "aiter() returned not an async iterator of type '%.100s '" ,
2836- Py_TYPE ( it ) -> tp_name );
2838+ "%T. aiter() must return type async iterator of type '%T '" ,
2839+ o , it );
28372840 Py_SETREF (it , NULL );
28382841 }
28392842 return it ;
0 commit comments