diff options
| author | Serhiy Storchaka <storchaka@gmail.com> | 2024-05-28 09:01:37 (GMT) |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-28 09:01:37 (GMT) |
| commit | b313cc68d50de5fb5f43acffd402c5c4da6516fc (patch) | |
| tree | 3d25305bb921d8e3748db524ec9267e65ecb242f /Modules/arraymodule.c | |
| parent | bf08f0a5fe5750904aa4a239945db16d2c43f6e7 (diff) | |
| download | cpython-b313cc68d50de5fb5f43acffd402c5c4da6516fc.zip cpython-b313cc68d50de5fb5f43acffd402c5c4da6516fc.tar.gz cpython-b313cc68d50de5fb5f43acffd402c5c4da6516fc.tar.bz2 | |
gh-117557: Improve error messages when a string, bytes or bytearray of length 1 are expected (GH-117631)
Diffstat (limited to 'Modules/arraymodule.c')
| -rw-r--r-- | Modules/arraymodule.c | 40 |
1 files changed, 28 insertions, 12 deletions
diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index a3b833d..e6c84d5 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -260,20 +260,32 @@ u_getitem(arrayobject *ap, Py_ssize_t i) static int u_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) { - PyObject *u; - if (!PyArg_Parse(v, "U;array item must be unicode character", &u)) { + if (!PyUnicode_Check(v)) { + PyErr_Format(PyExc_TypeError, + "array item must be a unicode character, not %T", + v); return -1; } - Py_ssize_t len = PyUnicode_AsWideChar(u, NULL, 0); + Py_ssize_t len = PyUnicode_AsWideChar(v, NULL, 0); if (len != 2) { - PyErr_SetString(PyExc_TypeError, - "array item must be unicode character"); + if (PyUnicode_GET_LENGTH(v) != 1) { + PyErr_Format(PyExc_TypeError, + "array item must be a unicode character, " + "not a string of length %zd", + PyUnicode_GET_LENGTH(v)); + } + else { + PyErr_Format(PyExc_TypeError, + "string %A cannot be converted to " + "a single wchar_t character", + v); + } return -1; } wchar_t w; - len = PyUnicode_AsWideChar(u, &w, 1); + len = PyUnicode_AsWideChar(v, &w, 1); assert(len == 1); if (i >= 0) { @@ -291,19 +303,23 @@ w_getitem(arrayobject *ap, Py_ssize_t i) static int w_setitem(arrayobject *ap, Py_ssize_t i, PyObject *v) { - PyObject *u; - if (!PyArg_Parse(v, "U;array item must be unicode character", &u)) { + if (!PyUnicode_Check(v)) { + PyErr_Format(PyExc_TypeError, + "array item must be a unicode character, not %T", + v); return -1; } - if (PyUnicode_GetLength(u) != 1) { - PyErr_SetString(PyExc_TypeError, - "array item must be unicode character"); + if (PyUnicode_GET_LENGTH(v) != 1) { + PyErr_Format(PyExc_TypeError, + "array item must be a unicode character, " + "not a string of length %zd", + PyUnicode_GET_LENGTH(v)); return -1; } if (i >= 0) { - ((Py_UCS4 *)ap->ob_item)[i] = PyUnicode_READ_CHAR(u, 0); + ((Py_UCS4 *)ap->ob_item)[i] = PyUnicode_READ_CHAR(v, 0); } return 0; } |
