Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Modules/_abc.c
Original file line number Diff line number Diff line change
Expand Up @@ -383,10 +383,14 @@ compute_abstract_methods(PyObject *self)
}
assert(PyList_Check(items));
for (Py_ssize_t pos = 0; pos < PyList_GET_SIZE(items); pos++) {
PyObject *item = PyList_GET_ITEM(items, pos);
PyObject *it = PySequence_Fast(
PyList_GET_ITEM(items, pos),
"items() returned non-iterable");
item,
NULL);
if (!it) {
PyErr_Format(PyExc_TypeError,
"items() must return an iterable, not %T",
item);
goto error;
}
if (PySequence_Fast_GET_SIZE(it) != 2) {
Expand Down
5 changes: 2 additions & 3 deletions Modules/_csv.c
Original file line number Diff line number Diff line change
Expand Up @@ -953,10 +953,9 @@ Reader_iternext_lock_held(PyObject *op)
}
if (!PyUnicode_Check(lineobj)) {
PyErr_Format(module_state->error_obj,
"iterator should return strings, "
"not %.200s "
"iterator must return str objects, not %T "
"(the file should be opened in text mode)",
Py_TYPE(lineobj)->tp_name
lineobj
);
Py_DECREF(lineobj);
return NULL;
Expand Down
4 changes: 2 additions & 2 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2177,8 +2177,8 @@ checked_divmod(PyObject *a, PyObject *b)
if (result != NULL) {
if (!PyTuple_Check(result)) {
PyErr_Format(PyExc_TypeError,
"divmod() returned non-tuple (type %.200s)",
Py_TYPE(result)->tp_name);
"divmod() must return a tuple, not %T",
result);
Py_DECREF(result);
return NULL;
}
Expand Down
12 changes: 9 additions & 3 deletions Modules/_io/bufferedio.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ _bufferediobase_readinto_generic(PyObject *self, Py_buffer *buffer, char readint

if (!PyBytes_Check(data)) {
Py_DECREF(data);
PyErr_SetString(PyExc_TypeError, "read() should return bytes");
PyErr_Format(PyExc_TypeError,
"read() must return a bytes object, not %T",
data);
return NULL;
}

Expand Down Expand Up @@ -1717,7 +1719,9 @@ _bufferedreader_read_all(buffered *self)
if (tmp == NULL)
goto cleanup;
if (tmp != Py_None && !PyBytes_Check(tmp)) {
PyErr_SetString(PyExc_TypeError, "readall() should return bytes");
PyErr_Format(PyExc_TypeError,
"readall() must return a bytes object, not %T",
tmp);
goto cleanup;
}
if (current_size == 0) {
Expand Down Expand Up @@ -1747,7 +1751,9 @@ _bufferedreader_read_all(buffered *self)
if (data == NULL)
goto cleanup;
if (data != Py_None && !PyBytes_Check(data)) {
PyErr_SetString(PyExc_TypeError, "read() should return bytes");
PyErr_Format(PyExc_TypeError,
"read() must return a bytes object, not %T",
data);
goto cleanup;
}
if (data == Py_None || PyBytes_GET_SIZE(data) == 0) {
Expand Down
4 changes: 3 additions & 1 deletion Modules/_io/iobase.c
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,9 @@ _io__RawIOBase_readall_impl(PyObject *self)
}
if (!PyBytes_Check(data)) {
Py_DECREF(data);
PyErr_SetString(PyExc_TypeError, "read() should return bytes");
PyErr_Format(PyExc_TypeError,
"read() must return a bytes object, not %T",
data);
PyBytesWriter_Discard(writer);
return NULL;
}
Expand Down
8 changes: 4 additions & 4 deletions Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,8 @@ check_decoded(PyObject *decoded)
return -1;
if (!PyUnicode_Check(decoded)) {
PyErr_Format(PyExc_TypeError,
"decoder should return a string result, not '%.200s'",
Py_TYPE(decoded)->tp_name);
"decoder must return a str object, not %T",
decoded);
Py_DECREF(decoded);
return -1;
}
Expand Down Expand Up @@ -1718,8 +1718,8 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text)
return NULL;
if (b != text && !PyBytes_Check(b)) {
PyErr_Format(PyExc_TypeError,
"encoder should return a bytes object, not '%.200s'",
Py_TYPE(b)->tp_name);
"encoder must return a bytes object, not %T",
b);
Py_DECREF(b);
return NULL;
}
Expand Down
4 changes: 2 additions & 2 deletions Modules/_pickle.c
Original file line number Diff line number Diff line change
Expand Up @@ -1302,8 +1302,8 @@ _Unpickler_ReadIntoFromFile(PickleState *state, UnpicklerObject *self, char *buf
}
if (!PyBytes_Check(data)) {
PyErr_Format(PyExc_ValueError,
Copy link
Contributor

@bkap123 bkap123 Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems strange that this raises a ValueError and not a TypeError since the error is that the return value of read() is not a bytes object, but instead a different type. Not sure the potential impact of changing this from a ValueError to a TypeError, but a TypeError feels like the better option as it makes it consistent with all the other changes in this PR and also in #130835.

"read() returned non-bytes object (%R)",
Py_TYPE(data));
"read() must return a bytes object, not %T",
data);
Py_DECREF(data);
return -1;
}
Expand Down
Loading