Skip to content

Commit a832eec

Browse files
committed
gh-130821: Make error messages consistent in Modules/
Update error messages in Modules/ directory to use consistent format with %T formatter for type names, similar to changes made in PR #130835 for Objects/ directory. Changes: - Modules/_abc.c: items() error message - Modules/_datetimemodule.c: divmod() error message - Modules/_pickle.c: read() error message - Modules/_io/bufferedio.c: read() and readall() error messages - Modules/_io/iobase.c: read() error message - Modules/_io/textio.c: decoder and encoder error messages - Modules/_csv.c: iterator error message
1 parent 51a408e commit a832eec

File tree

7 files changed

+28
-17
lines changed

7 files changed

+28
-17
lines changed

Modules/_abc.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,10 +383,14 @@ compute_abstract_methods(PyObject *self)
383383
}
384384
assert(PyList_Check(items));
385385
for (Py_ssize_t pos = 0; pos < PyList_GET_SIZE(items); pos++) {
386+
PyObject *item = PyList_GET_ITEM(items, pos);
386387
PyObject *it = PySequence_Fast(
387-
PyList_GET_ITEM(items, pos),
388-
"items() returned non-iterable");
388+
item,
389+
NULL);
389390
if (!it) {
391+
PyErr_Format(PyExc_TypeError,
392+
"items() must return an iterable, not %T",
393+
item);
390394
goto error;
391395
}
392396
if (PySequence_Fast_GET_SIZE(it) != 2) {

Modules/_csv.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -953,10 +953,9 @@ Reader_iternext_lock_held(PyObject *op)
953953
}
954954
if (!PyUnicode_Check(lineobj)) {
955955
PyErr_Format(module_state->error_obj,
956-
"iterator should return strings, "
957-
"not %.200s "
956+
"iterator must return str objects, not %T "
958957
"(the file should be opened in text mode)",
959-
Py_TYPE(lineobj)->tp_name
958+
lineobj
960959
);
961960
Py_DECREF(lineobj);
962961
return NULL;

Modules/_datetimemodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2177,8 +2177,8 @@ checked_divmod(PyObject *a, PyObject *b)
21772177
if (result != NULL) {
21782178
if (!PyTuple_Check(result)) {
21792179
PyErr_Format(PyExc_TypeError,
2180-
"divmod() returned non-tuple (type %.200s)",
2181-
Py_TYPE(result)->tp_name);
2180+
"divmod() must return a tuple, not %T",
2181+
result);
21822182
Py_DECREF(result);
21832183
return NULL;
21842184
}

Modules/_io/bufferedio.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ _bufferediobase_readinto_generic(PyObject *self, Py_buffer *buffer, char readint
6262

6363
if (!PyBytes_Check(data)) {
6464
Py_DECREF(data);
65-
PyErr_SetString(PyExc_TypeError, "read() should return bytes");
65+
PyErr_Format(PyExc_TypeError,
66+
"read() must return a bytes object, not %T",
67+
data);
6668
return NULL;
6769
}
6870

@@ -1717,7 +1719,9 @@ _bufferedreader_read_all(buffered *self)
17171719
if (tmp == NULL)
17181720
goto cleanup;
17191721
if (tmp != Py_None && !PyBytes_Check(tmp)) {
1720-
PyErr_SetString(PyExc_TypeError, "readall() should return bytes");
1722+
PyErr_Format(PyExc_TypeError,
1723+
"readall() must return a bytes object, not %T",
1724+
tmp);
17211725
goto cleanup;
17221726
}
17231727
if (current_size == 0) {
@@ -1747,7 +1751,9 @@ _bufferedreader_read_all(buffered *self)
17471751
if (data == NULL)
17481752
goto cleanup;
17491753
if (data != Py_None && !PyBytes_Check(data)) {
1750-
PyErr_SetString(PyExc_TypeError, "read() should return bytes");
1754+
PyErr_Format(PyExc_TypeError,
1755+
"read() must return a bytes object, not %T",
1756+
data);
17511757
goto cleanup;
17521758
}
17531759
if (data == Py_None || PyBytes_GET_SIZE(data) == 0) {

Modules/_io/iobase.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,9 @@ _io__RawIOBase_readall_impl(PyObject *self)
996996
}
997997
if (!PyBytes_Check(data)) {
998998
Py_DECREF(data);
999-
PyErr_SetString(PyExc_TypeError, "read() should return bytes");
999+
PyErr_Format(PyExc_TypeError,
1000+
"read() must return a bytes object, not %T",
1001+
data);
10001002
PyBytesWriter_Discard(writer);
10011003
return NULL;
10021004
}

Modules/_io/textio.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,8 @@ check_decoded(PyObject *decoded)
304304
return -1;
305305
if (!PyUnicode_Check(decoded)) {
306306
PyErr_Format(PyExc_TypeError,
307-
"decoder should return a string result, not '%.200s'",
308-
Py_TYPE(decoded)->tp_name);
307+
"decoder must return a str object, not %T",
308+
decoded);
309309
Py_DECREF(decoded);
310310
return -1;
311311
}
@@ -1718,8 +1718,8 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text)
17181718
return NULL;
17191719
if (b != text && !PyBytes_Check(b)) {
17201720
PyErr_Format(PyExc_TypeError,
1721-
"encoder should return a bytes object, not '%.200s'",
1722-
Py_TYPE(b)->tp_name);
1721+
"encoder must return a bytes object, not %T",
1722+
b);
17231723
Py_DECREF(b);
17241724
return NULL;
17251725
}

Modules/_pickle.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,8 +1302,8 @@ _Unpickler_ReadIntoFromFile(PickleState *state, UnpicklerObject *self, char *buf
13021302
}
13031303
if (!PyBytes_Check(data)) {
13041304
PyErr_Format(PyExc_ValueError,
1305-
"read() returned non-bytes object (%R)",
1306-
Py_TYPE(data));
1305+
"read() must return a bytes object, not %T",
1306+
data);
13071307
Py_DECREF(data);
13081308
return -1;
13091309
}

0 commit comments

Comments
 (0)