gh-117557: Improve error messages when a string, bytes or bytearray of length 1 are expected (GH-117631)

This commit is contained in:
Serhiy Storchaka 2024-05-28 12:01:37 +03:00 committed by GitHub
parent bf08f0a5fe
commit b313cc68d5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 811 additions and 161 deletions

View file

@ -113,10 +113,24 @@ stringlib_ljust(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
if (nargs < 2) {
goto skip_optional;
}
if (PyBytes_Check(args[1]) && PyBytes_GET_SIZE(args[1]) == 1) {
if (PyBytes_Check(args[1])) {
if (PyBytes_GET_SIZE(args[1]) != 1) {
PyErr_Format(PyExc_TypeError,
"ljust(): argument 2 must be a byte string of length 1, "
"not a bytes object of length %zd",
PyBytes_GET_SIZE(args[1]));
goto exit;
}
fillchar = PyBytes_AS_STRING(args[1])[0];
}
else if (PyByteArray_Check(args[1]) && PyByteArray_GET_SIZE(args[1]) == 1) {
else if (PyByteArray_Check(args[1])) {
if (PyByteArray_GET_SIZE(args[1]) != 1) {
PyErr_Format(PyExc_TypeError,
"ljust(): argument 2 must be a byte string of length 1, "
"not a bytearray object of length %zd",
PyByteArray_GET_SIZE(args[1]));
goto exit;
}
fillchar = PyByteArray_AS_STRING(args[1])[0];
}
else {
@ -169,10 +183,24 @@ stringlib_rjust(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
if (nargs < 2) {
goto skip_optional;
}
if (PyBytes_Check(args[1]) && PyBytes_GET_SIZE(args[1]) == 1) {
if (PyBytes_Check(args[1])) {
if (PyBytes_GET_SIZE(args[1]) != 1) {
PyErr_Format(PyExc_TypeError,
"rjust(): argument 2 must be a byte string of length 1, "
"not a bytes object of length %zd",
PyBytes_GET_SIZE(args[1]));
goto exit;
}
fillchar = PyBytes_AS_STRING(args[1])[0];
}
else if (PyByteArray_Check(args[1]) && PyByteArray_GET_SIZE(args[1]) == 1) {
else if (PyByteArray_Check(args[1])) {
if (PyByteArray_GET_SIZE(args[1]) != 1) {
PyErr_Format(PyExc_TypeError,
"rjust(): argument 2 must be a byte string of length 1, "
"not a bytearray object of length %zd",
PyByteArray_GET_SIZE(args[1]));
goto exit;
}
fillchar = PyByteArray_AS_STRING(args[1])[0];
}
else {
@ -225,10 +253,24 @@ stringlib_center(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
if (nargs < 2) {
goto skip_optional;
}
if (PyBytes_Check(args[1]) && PyBytes_GET_SIZE(args[1]) == 1) {
if (PyBytes_Check(args[1])) {
if (PyBytes_GET_SIZE(args[1]) != 1) {
PyErr_Format(PyExc_TypeError,
"center(): argument 2 must be a byte string of length 1, "
"not a bytes object of length %zd",
PyBytes_GET_SIZE(args[1]));
goto exit;
}
fillchar = PyBytes_AS_STRING(args[1])[0];
}
else if (PyByteArray_Check(args[1]) && PyByteArray_GET_SIZE(args[1]) == 1) {
else if (PyByteArray_Check(args[1])) {
if (PyByteArray_GET_SIZE(args[1]) != 1) {
PyErr_Format(PyExc_TypeError,
"center(): argument 2 must be a byte string of length 1, "
"not a bytearray object of length %zd",
PyByteArray_GET_SIZE(args[1]));
goto exit;
}
fillchar = PyByteArray_AS_STRING(args[1])[0];
}
else {
@ -279,4 +321,4 @@ stringlib_zfill(PyObject *self, PyObject *arg)
exit:
return return_value;
}
/*[clinic end generated code: output=b409bdf9ab68d5a6 input=a9049054013a1b77]*/
/*[clinic end generated code: output=06dd79019356b6bb input=a9049054013a1b77]*/