@@ -132,8 +132,9 @@ PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
132
132
return defaultvalue ;
133
133
}
134
134
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 );
137
138
Py_DECREF (result );
138
139
return -1 ;
139
140
}
@@ -143,7 +144,8 @@ PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
143
144
return -1 ;
144
145
}
145
146
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 );
147
149
return -1 ;
148
150
}
149
151
return res ;
@@ -887,8 +889,8 @@ PyObject_Format(PyObject *obj, PyObject *format_spec)
887
889
888
890
if (result && !PyUnicode_Check (result )) {
889
891
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 );
892
894
Py_SETREF (result , NULL );
893
895
goto done ;
894
896
}
@@ -1421,17 +1423,17 @@ _PyNumber_Index(PyObject *item)
1421
1423
1422
1424
if (!PyLong_Check (result )) {
1423
1425
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 );
1426
1428
Py_DECREF (result );
1427
1429
return NULL ;
1428
1430
}
1429
1431
/* Issue #17576: warn if 'result' not of exact type int. */
1430
1432
if (PyErr_WarnFormat (PyExc_DeprecationWarning , 1 ,
1431
- "__index__ returned non- int (type %.200s) . "
1433
+ "%T. __index__() must return an int, not %T . "
1432
1434
"The ability to return an instance of a strict subclass of int "
1433
1435
"is deprecated, and may be removed in a future version of Python." ,
1434
- Py_TYPE ( result ) -> tp_name )) {
1436
+ item , result )) {
1435
1437
Py_DECREF (result );
1436
1438
return NULL ;
1437
1439
}
@@ -1531,17 +1533,17 @@ PyNumber_Long(PyObject *o)
1531
1533
1532
1534
if (!PyLong_Check (result )) {
1533
1535
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 );
1536
1538
Py_DECREF (result );
1537
1539
return NULL ;
1538
1540
}
1539
1541
/* Issue #17576: warn if 'result' not of exact type int. */
1540
1542
if (PyErr_WarnFormat (PyExc_DeprecationWarning , 1 ,
1541
- "__int__ returned non- int (type %.200s) . "
1543
+ "%T. __int__() must return an int, not %T . "
1542
1544
"The ability to return an instance of a strict subclass of int "
1543
1545
"is deprecated, and may be removed in a future version of Python." ,
1544
- Py_TYPE ( result ) -> tp_name )) {
1546
+ o , result )) {
1545
1547
Py_DECREF (result );
1546
1548
return NULL ;
1547
1549
}
@@ -1609,17 +1611,16 @@ PyNumber_Float(PyObject *o)
1609
1611
1610
1612
if (!PyFloat_Check (res )) {
1611
1613
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 );
1614
1615
Py_DECREF (res );
1615
1616
return NULL ;
1616
1617
}
1617
1618
/* Issue #26983: warn if 'res' not of exact type float. */
1618
1619
if (PyErr_WarnFormat (PyExc_DeprecationWarning , 1 ,
1619
- "%.50s. __float__ returned non- float (type %.50s) . "
1620
+ "%T. __float__() must return a float, not %T . "
1620
1621
"The ability to return an instance of a strict subclass of float "
1621
1622
"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 )) {
1623
1624
Py_DECREF (res );
1624
1625
return NULL ;
1625
1626
}
@@ -2435,10 +2436,8 @@ method_output_as_list(PyObject *o, PyObject *meth)
2435
2436
PyThreadState * tstate = _PyThreadState_GET ();
2436
2437
if (_PyErr_ExceptionMatches (tstate , PyExc_TypeError )) {
2437
2438
_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 );
2442
2441
}
2443
2442
Py_DECREF (meth_output );
2444
2443
return NULL ;
@@ -2818,9 +2817,8 @@ PyObject_GetIter(PyObject *o)
2818
2817
PyObject * res = (* f )(o );
2819
2818
if (res != NULL && !PyIter_Check (res )) {
2820
2819
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 );
2824
2822
Py_SETREF (res , NULL );
2825
2823
}
2826
2824
return res ;
@@ -2839,8 +2837,8 @@ PyObject_GetAIter(PyObject *o) {
2839
2837
PyObject * it = (* f )(o );
2840
2838
if (it != NULL && !PyAIter_Check (it )) {
2841
2839
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 );
2844
2842
Py_SETREF (it , NULL );
2845
2843
}
2846
2844
return it ;
0 commit comments