@@ -132,8 +132,9 @@ PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
132132 return defaultvalue ;
133133 }
134134 if (!PyLong_Check (result )) {
135- PyErr_Format (PyExc_TypeError , "__length_hint__ must be an integer, not %.100s" ,
136- Py_TYPE (result )-> tp_name );
135+ PyErr_Format (PyExc_TypeError ,
136+ "%T.__length_hint__() must return an int, not %T" ,
137+ o , result );
137138 Py_DECREF (result );
138139 return -1 ;
139140 }
@@ -143,7 +144,8 @@ PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
143144 return -1 ;
144145 }
145146 if (res < 0 ) {
146- PyErr_Format (PyExc_ValueError , "__length_hint__() should return >= 0" );
147+ PyErr_Format (PyExc_ValueError ,
148+ "%T.__length_hint__() must return a non-negative int" , o );
147149 return -1 ;
148150 }
149151 return res ;
@@ -887,8 +889,8 @@ PyObject_Format(PyObject *obj, PyObject *format_spec)
887889
888890 if (result && !PyUnicode_Check (result )) {
889891 PyErr_Format (PyExc_TypeError ,
890- "__format__ must return a str, not %.200s " ,
891- Py_TYPE ( result ) -> tp_name );
892+ "%T. __format__() must return a str, not %T " ,
893+ obj , result );
892894 Py_SETREF (result , NULL );
893895 goto done ;
894896 }
@@ -1421,17 +1423,17 @@ _PyNumber_Index(PyObject *item)
14211423
14221424 if (!PyLong_Check (result )) {
14231425 PyErr_Format (PyExc_TypeError ,
1424- "__index__ returned non- int (type %.200s) " ,
1425- Py_TYPE ( result ) -> tp_name );
1426+ "%T. __index__() must return an int, not %T " ,
1427+ item , result );
14261428 Py_DECREF (result );
14271429 return NULL ;
14281430 }
14291431 /* Issue #17576: warn if 'result' not of exact type int. */
14301432 if (PyErr_WarnFormat (PyExc_DeprecationWarning , 1 ,
1431- "__index__ returned non- int (type %.200s) . "
1433+ "%T. __index__() must return an int, not %T . "
14321434 "The ability to return an instance of a strict subclass of int "
14331435 "is deprecated, and may be removed in a future version of Python." ,
1434- Py_TYPE ( result ) -> tp_name )) {
1436+ item , result )) {
14351437 Py_DECREF (result );
14361438 return NULL ;
14371439 }
@@ -1531,17 +1533,17 @@ PyNumber_Long(PyObject *o)
15311533
15321534 if (!PyLong_Check (result )) {
15331535 PyErr_Format (PyExc_TypeError ,
1534- "__int__ returned non- int (type %.200s) " ,
1535- Py_TYPE ( result ) -> tp_name );
1536+ "%T. __int__() must return an int, not %T " ,
1537+ o , result );
15361538 Py_DECREF (result );
15371539 return NULL ;
15381540 }
15391541 /* Issue #17576: warn if 'result' not of exact type int. */
15401542 if (PyErr_WarnFormat (PyExc_DeprecationWarning , 1 ,
1541- "__int__ returned non- int (type %.200s) . "
1543+ "%T. __int__() must return an int, not %T . "
15421544 "The ability to return an instance of a strict subclass of int "
15431545 "is deprecated, and may be removed in a future version of Python." ,
1544- Py_TYPE ( result ) -> tp_name )) {
1546+ o , result )) {
15451547 Py_DECREF (result );
15461548 return NULL ;
15471549 }
@@ -1609,17 +1611,16 @@ PyNumber_Float(PyObject *o)
16091611
16101612 if (!PyFloat_Check (res )) {
16111613 PyErr_Format (PyExc_TypeError ,
1612- "%.50s.__float__ returned non-float (type %.50s)" ,
1613- Py_TYPE (o )-> tp_name , Py_TYPE (res )-> tp_name );
1614+ "%T.__float__() must return a float, not %T" , o , res );
16141615 Py_DECREF (res );
16151616 return NULL ;
16161617 }
16171618 /* Issue #26983: warn if 'res' not of exact type float. */
16181619 if (PyErr_WarnFormat (PyExc_DeprecationWarning , 1 ,
1619- "%.50s. __float__ returned non- float (type %.50s) . "
1620+ "%T. __float__() must return a float, not %T . "
16201621 "The ability to return an instance of a strict subclass of float "
16211622 "is deprecated, and may be removed in a future version of Python." ,
1622- Py_TYPE ( o ) -> tp_name , Py_TYPE ( res ) -> tp_name )) {
1623+ o , res )) {
16231624 Py_DECREF (res );
16241625 return NULL ;
16251626 }
@@ -2435,10 +2436,8 @@ method_output_as_list(PyObject *o, PyObject *meth)
24352436 PyThreadState * tstate = _PyThreadState_GET ();
24362437 if (_PyErr_ExceptionMatches (tstate , PyExc_TypeError )) {
24372438 _PyErr_Format (tstate , PyExc_TypeError ,
2438- "%.200s.%U() returned a non-iterable (type %.200s)" ,
2439- Py_TYPE (o )-> tp_name ,
2440- meth ,
2441- Py_TYPE (meth_output )-> tp_name );
2439+ "%T.%U() must return an iterable, not %T" ,
2440+ o , meth , meth_output );
24422441 }
24432442 Py_DECREF (meth_output );
24442443 return NULL ;
@@ -2818,9 +2817,8 @@ PyObject_GetIter(PyObject *o)
28182817 PyObject * res = (* f )(o );
28192818 if (res != NULL && !PyIter_Check (res )) {
28202819 PyErr_Format (PyExc_TypeError ,
2821- "iter() returned non-iterator "
2822- "of type '%.100s'" ,
2823- Py_TYPE (res )-> tp_name );
2820+ "%T.__iter__() must return an iterator, not %T" ,
2821+ o , res );
28242822 Py_SETREF (res , NULL );
28252823 }
28262824 return res ;
@@ -2839,8 +2837,8 @@ PyObject_GetAIter(PyObject *o) {
28392837 PyObject * it = (* f )(o );
28402838 if (it != NULL && !PyAIter_Check (it )) {
28412839 PyErr_Format (PyExc_TypeError ,
2842- "aiter () returned not an async iterator of type '%.100s' " ,
2843- Py_TYPE ( it ) -> tp_name );
2840+ "%T.__aiter__ () must return an async iterator, not %T " ,
2841+ o , it );
28442842 Py_SETREF (it , NULL );
28452843 }
28462844 return it ;
0 commit comments