Skip to content

Commit d2e54e6

Browse files
committed
Fix more methods
1 parent 390f941 commit d2e54e6

8 files changed

Lines changed: 37 additions & 35 deletions

File tree

Objects/abstract.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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 type 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 positive int", o);
147149
return -1;
148150
}
149151
return res;
@@ -2434,10 +2436,8 @@ method_output_as_list(PyObject *o, PyObject *meth)
24342436
PyThreadState *tstate = _PyThreadState_GET();
24352437
if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError)) {
24362438
_PyErr_Format(tstate, PyExc_TypeError,
2437-
"%.200s.%U() returned a non-iterable (type %.200s)",
2438-
Py_TYPE(o)->tp_name,
2439-
meth,
2440-
Py_TYPE(meth_output)->tp_name);
2439+
"%T.%U() must return type iterable (not %T)",
2440+
o, meth, meth_output);
24412441
}
24422442
Py_DECREF(meth_output);
24432443
return NULL;
@@ -2837,8 +2837,8 @@ PyObject_GetAIter(PyObject *o) {
28372837
PyObject *it = (*f)(o);
28382838
if (it != NULL && !PyAIter_Check(it)) {
28392839
PyErr_Format(PyExc_TypeError,
2840-
"aiter() returned not an async iterator of type '%.100s'",
2841-
Py_TYPE(it)->tp_name);
2840+
"%T.aiter() must return type async iterator of type '%T'",
2841+
o, it);
28422842
Py_SETREF(it, NULL);
28432843
}
28442844
return it;

Objects/fileobject.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ PyFile_GetLine(PyObject *f, int n)
6868
}
6969
if (result != NULL && !PyBytes_Check(result) &&
7070
!PyUnicode_Check(result)) {
71+
PyErr_Format(PyExc_TypeError,
72+
"%T.readline() must return type str (not %T)", f, result);
7173
Py_SETREF(result, NULL);
72-
PyErr_SetString(PyExc_TypeError,
73-
"object.readline() returned non-string");
7474
}
7575

7676
if (n < 0 && result != NULL && PyBytes_Check(result)) {
@@ -193,8 +193,8 @@ PyObject_AsFileDescriptor(PyObject *o)
193193
Py_DECREF(fno);
194194
}
195195
else {
196-
PyErr_SetString(PyExc_TypeError,
197-
"fileno() returned a non-integer");
196+
PyErr_Format(PyExc_TypeError,
197+
"%T.fileno() must return type int (not %T)", o, fno);
198198
Py_DECREF(fno);
199199
return -1;
200200
}

Objects/floatobject.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,16 +288,16 @@ PyFloat_AsDouble(PyObject *op)
288288
if (!PyFloat_CheckExact(res)) {
289289
if (!PyFloat_Check(res)) {
290290
PyErr_Format(PyExc_TypeError,
291-
"%.50s.__float__ returned non-float (type %.50s)",
292-
Py_TYPE(op)->tp_name, Py_TYPE(res)->tp_name);
291+
"%T.__float__() must return type float (not %T)",
292+
op, res);
293293
Py_DECREF(res);
294294
return -1;
295295
}
296296
if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
297-
"%.50s.__float__ returned non-float (type %.50s). "
297+
"%T.__float__() must return type float (not %T). "
298298
"The ability to return an instance of a strict subclass of float "
299299
"is deprecated, and may be removed in a future version of Python.",
300-
Py_TYPE(op)->tp_name, Py_TYPE(res)->tp_name)) {
300+
op, res)) {
301301
Py_DECREF(res);
302302
return -1;
303303
}

Objects/funcobject.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,8 +557,9 @@ func_get_annotation_dict(PyFunctionObject *op)
557557
return NULL;
558558
}
559559
if (!PyDict_Check(ann_dict)) {
560-
PyErr_Format(PyExc_TypeError, "__annotate__ returned non-dict of type '%.100s'",
561-
Py_TYPE(ann_dict)->tp_name);
560+
PyErr_Format(PyExc_TypeError,
561+
"__annotate__() must return type dict of type '%T'",
562+
ann_dict);
562563
Py_DECREF(ann_dict);
563564
return NULL;
564565
}

Objects/genobject.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,14 +1082,13 @@ _PyCoro_GetAwaitableIter(PyObject *o)
10821082
if (PyCoro_CheckExact(res) || gen_is_coroutine(res)) {
10831083
/* __await__ must return an *iterator*, not
10841084
a coroutine or another awaitable (see PEP 492) */
1085-
PyErr_SetString(PyExc_TypeError,
1086-
"__await__() returned a coroutine");
1085+
PyErr_Format(PyExc_TypeError,
1086+
"%T.__await__() must return type iterator (not coroutine)", o);
10871087
Py_CLEAR(res);
10881088
} else if (!PyIter_Check(res)) {
10891089
PyErr_Format(PyExc_TypeError,
1090-
"__await__() returned non-iterator "
1091-
"of type '%.100s'",
1092-
Py_TYPE(res)->tp_name);
1090+
"%T.__await__() must return type iterator "
1091+
"of type '%T'", o, res);
10931092
Py_CLEAR(res);
10941093
}
10951094
}

Objects/iterobject.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,9 @@ anextawaitable_getiter(anextawaitableobject *obj)
357357
}
358358
Py_SETREF(awaitable, new_awaitable);
359359
if (!PyIter_Check(awaitable)) {
360-
PyErr_SetString(PyExc_TypeError,
361-
"__await__ returned a non-iterable");
360+
PyErr_Format(PyExc_TypeError,
361+
"%T.__await__() must return type iterable (not %T)",
362+
obj, awaitable);
362363
Py_DECREF(awaitable);
363364
return NULL;
364365
}

Objects/moduleobject.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,8 +1261,9 @@ module_get_annotations(PyObject *self, void *Py_UNUSED(ignored))
12611261
return NULL;
12621262
}
12631263
if (!PyDict_Check(annotations)) {
1264-
PyErr_Format(PyExc_TypeError, "__annotate__ returned non-dict of type '%.100s'",
1265-
Py_TYPE(annotations)->tp_name);
1264+
PyErr_Format(PyExc_TypeError,
1265+
"__annotate__() must return type dict of type '%T'",
1266+
annotations);
12661267
Py_DECREF(annotate);
12671268
Py_DECREF(annotations);
12681269
Py_DECREF(dict);

Objects/typeobject.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3384,19 +3384,18 @@ mro_check(PyTypeObject *type, PyObject *mro)
33843384
for (i = 0; i < n; i++) {
33853385
PyObject *obj = PyTuple_GET_ITEM(mro, i);
33863386
if (!PyType_Check(obj)) {
3387-
PyErr_Format(
3388-
PyExc_TypeError,
3389-
"mro() returned a non-class ('%.500s')",
3390-
Py_TYPE(obj)->tp_name);
3387+
PyErr_Format(PyExc_TypeError,
3388+
"%s.mro() must return class (not %T)",
3389+
type->tp_name, obj);
33913390
return -1;
33923391
}
33933392
PyTypeObject *base = (PyTypeObject*)obj;
33943393

33953394
if (!is_subtype_with_mro(lookup_tp_mro(solid), solid, solid_base(base))) {
33963395
PyErr_Format(
33973396
PyExc_TypeError,
3398-
"mro() returned base with unsuitable layout ('%.500s')",
3399-
base->tp_name);
3397+
"%s.mro() returned base with unsuitable layout ('%.500s')",
3398+
type->tp_name, base->tp_name);
34003399
return -1;
34013400
}
34023401
}
@@ -10553,7 +10552,8 @@ slot_bf_getbuffer(PyObject *self, Py_buffer *buffer, int flags)
1055310552
}
1055410553
if (!PyMemoryView_Check(ret)) {
1055510554
PyErr_Format(PyExc_TypeError,
10556-
"__buffer__ returned non-memoryview object");
10555+
"%T.__buffer__() must return type memoryview (not %T)",
10556+
self, ret);
1055710557
goto fail;
1055810558
}
1055910559

0 commit comments

Comments
 (0)