@@ -129,9 +129,8 @@ PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
129129 return defaultvalue ;
130130 }
131131 if (!PyLong_Check (result )) {
132- PyErr_Format (PyExc_TypeError ,
133- "%T.__length_hint__ must return type int (not %T)" ,
134- o , result );
132+ PyErr_Format (PyExc_TypeError , "__length_hint__ must be an integer, not %.100s" ,
133+ Py_TYPE (result )-> tp_name );
135134 Py_DECREF (result );
136135 return -1 ;
137136 }
@@ -141,9 +140,7 @@ PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
141140 return -1 ;
142141 }
143142 if (res < 0 ) {
144- PyErr_Format (PyExc_ValueError ,
145- "%T.__length_hint__ must return positive int (not %T)" ,
146- o , result );
143+ PyErr_Format (PyExc_ValueError , "__length_hint__() should return >= 0" );
147144 return -1 ;
148145 }
149146 return res ;
@@ -887,8 +884,8 @@ PyObject_Format(PyObject *obj, PyObject *format_spec)
887884
888885 if (result && !PyUnicode_Check (result )) {
889886 PyErr_Format (PyExc_TypeError ,
890- "%T. __format__ must return type str ( not %T) " ,
891- obj , result );
887+ "__format__ must return a str, not %.200s " ,
888+ Py_TYPE ( result ) -> tp_name );
892889 Py_SETREF (result , NULL );
893890 goto done ;
894891 }
@@ -1421,14 +1418,13 @@ _PyNumber_Index(PyObject *item)
14211418
14221419 if (!PyLong_Check (result )) {
14231420 PyErr_Format (PyExc_TypeError ,
1424- "%T.__index__ must return type int (not %T)" ,
1425- item , result );
1421+ "%T.__index__ returned non-int (type %T)" , item , result );
14261422 Py_DECREF (result );
14271423 return NULL ;
14281424 }
14291425 /* Issue #17576: warn if 'result' not of exact type int. */
14301426 if (PyErr_WarnFormat (PyExc_DeprecationWarning , 1 ,
1431- "%T.__index__ must return type int (not %T). "
1427+ "%T.__index__ returned non- int (type %T). "
14321428 "The ability to return an instance of a strict subclass of int "
14331429 "is deprecated, and may be removed in a future version of Python." ,
14341430 item , result )) {
@@ -1531,13 +1527,13 @@ PyNumber_Long(PyObject *o)
15311527
15321528 if (!PyLong_Check (result )) {
15331529 PyErr_Format (PyExc_TypeError ,
1534- "%T.__int__ must return type int (not %T)" , o , result );
1530+ "%T.__int__ returned non- int (type %T)" , o , result );
15351531 Py_DECREF (result );
15361532 return NULL ;
15371533 }
15381534 /* Issue #17576: warn if 'result' not of exact type int. */
15391535 if (PyErr_WarnFormat (PyExc_DeprecationWarning , 1 ,
1540- "%T.__int__ must return type int (not %T). "
1536+ "%T.__int__ returned non- int (type %T). "
15411537 "The ability to return an instance of a strict subclass of int "
15421538 "is deprecated, and may be removed in a future version of Python." ,
15431539 o , result )) {
@@ -1608,14 +1604,13 @@ PyNumber_Float(PyObject *o)
16081604
16091605 if (!PyFloat_Check (res )) {
16101606 PyErr_Format (PyExc_TypeError ,
1611- "%T.__float__ must return type float (not %T)" ,
1612- o , res );
1607+ "%T.__float__ returned non-float (type %T)" , o , res );
16131608 Py_DECREF (res );
16141609 return NULL ;
16151610 }
16161611 /* Issue #26983: warn if 'res' not of exact type float. */
16171612 if (PyErr_WarnFormat (PyExc_DeprecationWarning , 1 ,
1618- "%T.__float__ must return type float (not %T). "
1613+ "%T.__float__ returned non- float (type %T). "
16191614 "The ability to return an instance of a strict subclass of float "
16201615 "is deprecated, and may be removed in a future version of Python." ,
16211616 o , res )) {
@@ -2434,8 +2429,10 @@ method_output_as_list(PyObject *o, PyObject *meth)
24342429 PyThreadState * tstate = _PyThreadState_GET ();
24352430 if (_PyErr_ExceptionMatches (tstate , PyExc_TypeError )) {
24362431 _PyErr_Format (tstate , PyExc_TypeError ,
2437- "%T.%U() must return type iterable (not %T)" ,
2438- o , meth , meth_output );
2432+ "%.200s.%U() returned a non-iterable (type %.200s)" ,
2433+ Py_TYPE (o )-> tp_name ,
2434+ meth ,
2435+ Py_TYPE (meth_output )-> tp_name );
24392436 }
24402437 Py_DECREF (meth_output );
24412438 return NULL ;
@@ -2815,7 +2812,7 @@ PyObject_GetIter(PyObject *o)
28152812 PyObject * res = (* f )(o );
28162813 if (res != NULL && !PyIter_Check (res )) {
28172814 PyErr_Format (PyExc_TypeError ,
2818- "%T.iter() must return type iterator of type '%T'" ,
2815+ "%T.iter() returned non- iterator of type '%T'" ,
28192816 o , res );
28202817 Py_SETREF (res , NULL );
28212818 }
@@ -2835,8 +2832,8 @@ PyObject_GetAIter(PyObject *o) {
28352832 PyObject * it = (* f )(o );
28362833 if (it != NULL && !PyAIter_Check (it )) {
28372834 PyErr_Format (PyExc_TypeError ,
2838- "%T. aiter() must return type async iterator of type '%T '" ,
2839- o , it );
2835+ "aiter() returned not an async iterator of type '%.100s '" ,
2836+ Py_TYPE ( it ) -> tp_name );
28402837 Py_SETREF (it , NULL );
28412838 }
28422839 return it ;
0 commit comments