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 /Objects/unicodeobject.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 'Objects/unicodeobject.c')
| -rw-r--r-- | Objects/unicodeobject.c | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 048f9a8..92db31f 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -14057,14 +14057,21 @@ formatchar(PyObject *v) if (PyUnicode_GET_LENGTH(v) == 1) { return PyUnicode_READ_CHAR(v, 0); } - goto onError; + PyErr_Format(PyExc_TypeError, + "%%c requires an int or a unicode character, " + "not a string of length %zd", + PyUnicode_GET_LENGTH(v)); + return (Py_UCS4) -1; } else { int overflow; long x = PyLong_AsLongAndOverflow(v, &overflow); if (x == -1 && PyErr_Occurred()) { if (PyErr_ExceptionMatches(PyExc_TypeError)) { - goto onError; + PyErr_Format(PyExc_TypeError, + "%%c requires an int or a unicode character, not %T", + v); + return (Py_UCS4) -1; } return (Py_UCS4) -1; } @@ -14078,11 +14085,6 @@ formatchar(PyObject *v) return (Py_UCS4) x; } - - onError: - PyErr_SetString(PyExc_TypeError, - "%c requires int or char"); - return (Py_UCS4) -1; } /* Parse options of an argument: flags, width, precision. |
