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

@ -1100,25 +1100,45 @@ O_set(void *ptr, PyObject *value, Py_ssize_t size)
static PyObject *
c_set(void *ptr, PyObject *value, Py_ssize_t size)
{
if (PyBytes_Check(value) && PyBytes_GET_SIZE(value) == 1) {
if (PyBytes_Check(value)) {
if (PyBytes_GET_SIZE(value) != 1) {
PyErr_Format(PyExc_TypeError,
"one character bytes, bytearray, or an integer "
"in range(256) expected, not bytes of length %zd",
PyBytes_GET_SIZE(value));
return NULL;
}
*(char *)ptr = PyBytes_AS_STRING(value)[0];
_RET(value);
}
if (PyByteArray_Check(value) && PyByteArray_GET_SIZE(value) == 1) {
if (PyByteArray_Check(value)) {
if (PyByteArray_GET_SIZE(value) != 1) {
PyErr_Format(PyExc_TypeError,
"one character bytes, bytearray, or an integer "
"in range(256) expected, not bytearray of length %zd",
PyByteArray_GET_SIZE(value));
return NULL;
}
*(char *)ptr = PyByteArray_AS_STRING(value)[0];
_RET(value);
}
if (PyLong_Check(value))
{
long longval = PyLong_AsLong(value);
if (longval < 0 || longval >= 256)
goto error;
if (PyLong_Check(value)) {
int overflow;
long longval = PyLong_AsLongAndOverflow(value, &overflow);
if (longval == -1 && PyErr_Occurred()) {
return NULL;
}
if (overflow || longval < 0 || longval >= 256) {
PyErr_SetString(PyExc_TypeError, "integer not in range(256)");
return NULL;
}
*(char *)ptr = (char)longval;
_RET(value);
}
error:
PyErr_Format(PyExc_TypeError,
"one character bytes, bytearray or integer expected");
"one character bytes, bytearray, or an integer "
"in range(256) expected, not %T",
value);
return NULL;
}
@ -1137,22 +1157,27 @@ u_set(void *ptr, PyObject *value, Py_ssize_t size)
wchar_t chars[2];
if (!PyUnicode_Check(value)) {
PyErr_Format(PyExc_TypeError,
"unicode string expected instead of %s instance",
Py_TYPE(value)->tp_name);
"a unicode character expected, not instance of %T",
value);
return NULL;
} else
Py_INCREF(value);
}
len = PyUnicode_AsWideChar(value, chars, 2);
if (len != 1) {
Py_DECREF(value);
PyErr_SetString(PyExc_TypeError,
"one character unicode string expected");
if (PyUnicode_GET_LENGTH(value) != 1) {
PyErr_Format(PyExc_TypeError,
"a unicode character expected, not a string of length %zd",
PyUnicode_GET_LENGTH(value));
}
else {
PyErr_Format(PyExc_TypeError,
"the string %A cannot be converted to a single wchar_t character",
value);
}
return NULL;
}
*(wchar_t *)ptr = chars[0];
Py_DECREF(value);
_RET(value);
}

View file

@ -233,13 +233,20 @@ static int
PyCurses_ConvertToChtype(PyCursesWindowObject *win, PyObject *obj, chtype *ch)
{
long value;
if(PyBytes_Check(obj) && PyBytes_Size(obj) == 1) {
if (PyBytes_Check(obj)) {
if (PyBytes_GET_SIZE(obj) != 1) {
PyErr_Format(PyExc_TypeError,
"expect int or bytes or str of length 1, "
"got a bytes of length %zd",
PyBytes_GET_SIZE(obj));
return 0;
}
value = (unsigned char)PyBytes_AsString(obj)[0];
}
else if (PyUnicode_Check(obj)) {
if (PyUnicode_GetLength(obj) != 1) {
if (PyUnicode_GET_LENGTH(obj) != 1) {
PyErr_Format(PyExc_TypeError,
"expect bytes or str of length 1, or int, "
"expect int or bytes or str of length 1, "
"got a str of length %zi",
PyUnicode_GET_LENGTH(obj));
return 0;
@ -272,7 +279,7 @@ PyCurses_ConvertToChtype(PyCursesWindowObject *win, PyObject *obj, chtype *ch)
}
else {
PyErr_Format(PyExc_TypeError,
"expect bytes or str of length 1, or int, got %s",
"expect int or bytes or str of length 1, got %s",
Py_TYPE(obj)->tp_name);
return 0;
}
@ -315,7 +322,7 @@ PyCurses_ConvertToCchar_t(PyCursesWindowObject *win, PyObject *obj,
#ifdef HAVE_NCURSESW
if (PyUnicode_AsWideChar(obj, buffer, 2) != 1) {
PyErr_Format(PyExc_TypeError,
"expect bytes or str of length 1, or int, "
"expect int or bytes or str of length 1, "
"got a str of length %zi",
PyUnicode_GET_LENGTH(obj));
return 0;
@ -326,7 +333,14 @@ PyCurses_ConvertToCchar_t(PyCursesWindowObject *win, PyObject *obj,
return PyCurses_ConvertToChtype(win, obj, ch);
#endif
}
else if(PyBytes_Check(obj) && PyBytes_Size(obj) == 1) {
else if (PyBytes_Check(obj)) {
if (PyBytes_GET_SIZE(obj) != 1) {
PyErr_Format(PyExc_TypeError,
"expect int or bytes or str of length 1, "
"got a bytes of length %zd",
PyBytes_GET_SIZE(obj));
return 0;
}
value = (unsigned char)PyBytes_AsString(obj)[0];
}
else if (PyLong_CheckExact(obj)) {
@ -340,7 +354,7 @@ PyCurses_ConvertToCchar_t(PyCursesWindowObject *win, PyObject *obj,
}
else {
PyErr_Format(PyExc_TypeError,
"expect bytes or str of length 1, or int, got %s",
"expect int or bytes or str of length 1, got %s",
Py_TYPE(obj)->tp_name);
return 0;
}
@ -4443,7 +4457,7 @@ PyCurses_ConvertToWchar_t(PyObject *obj,
wchar_t buffer[2];
if (PyUnicode_AsWideChar(obj, buffer, 2) != 1) {
PyErr_Format(PyExc_TypeError,
"expect str of length 1 or int, "
"expect int or str of length 1, "
"got a str of length %zi",
PyUnicode_GET_LENGTH(obj));
return 0;
@ -4470,7 +4484,7 @@ PyCurses_ConvertToWchar_t(PyObject *obj,
}
else {
PyErr_Format(PyExc_TypeError,
"expect str of length 1 or int, got %s",
"expect int or str of length 1, got %s",
Py_TYPE(obj)->tp_name);
return 0;
}

View file

@ -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;
}

View file

@ -234,10 +234,24 @@ char_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
if (nargs < 1) {
goto skip_optional;
}
if (PyBytes_Check(args[0]) && PyBytes_GET_SIZE(args[0]) == 1) {
if (PyBytes_Check(args[0])) {
if (PyBytes_GET_SIZE(args[0]) != 1) {
PyErr_Format(PyExc_TypeError,
"char_converter(): argument 1 must be a byte string of length 1, "
"not a bytes object of length %zd",
PyBytes_GET_SIZE(args[0]));
goto exit;
}
a = PyBytes_AS_STRING(args[0])[0];
}
else if (PyByteArray_Check(args[0]) && PyByteArray_GET_SIZE(args[0]) == 1) {
else if (PyByteArray_Check(args[0])) {
if (PyByteArray_GET_SIZE(args[0]) != 1) {
PyErr_Format(PyExc_TypeError,
"char_converter(): argument 1 must be a byte string of length 1, "
"not a bytearray object of length %zd",
PyByteArray_GET_SIZE(args[0]));
goto exit;
}
a = PyByteArray_AS_STRING(args[0])[0];
}
else {
@ -247,10 +261,24 @@ char_converter(PyObject *module, 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,
"char_converter(): argument 2 must be a byte string of length 1, "
"not a bytes object of length %zd",
PyBytes_GET_SIZE(args[1]));
goto exit;
}
b = 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,
"char_converter(): argument 2 must be a byte string of length 1, "
"not a bytearray object of length %zd",
PyByteArray_GET_SIZE(args[1]));
goto exit;
}
b = PyByteArray_AS_STRING(args[1])[0];
}
else {
@ -260,10 +288,24 @@ char_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
if (nargs < 3) {
goto skip_optional;
}
if (PyBytes_Check(args[2]) && PyBytes_GET_SIZE(args[2]) == 1) {
if (PyBytes_Check(args[2])) {
if (PyBytes_GET_SIZE(args[2]) != 1) {
PyErr_Format(PyExc_TypeError,
"char_converter(): argument 3 must be a byte string of length 1, "
"not a bytes object of length %zd",
PyBytes_GET_SIZE(args[2]));
goto exit;
}
c = PyBytes_AS_STRING(args[2])[0];
}
else if (PyByteArray_Check(args[2]) && PyByteArray_GET_SIZE(args[2]) == 1) {
else if (PyByteArray_Check(args[2])) {
if (PyByteArray_GET_SIZE(args[2]) != 1) {
PyErr_Format(PyExc_TypeError,
"char_converter(): argument 3 must be a byte string of length 1, "
"not a bytearray object of length %zd",
PyByteArray_GET_SIZE(args[2]));
goto exit;
}
c = PyByteArray_AS_STRING(args[2])[0];
}
else {
@ -273,10 +315,24 @@ char_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
if (nargs < 4) {
goto skip_optional;
}
if (PyBytes_Check(args[3]) && PyBytes_GET_SIZE(args[3]) == 1) {
if (PyBytes_Check(args[3])) {
if (PyBytes_GET_SIZE(args[3]) != 1) {
PyErr_Format(PyExc_TypeError,
"char_converter(): argument 4 must be a byte string of length 1, "
"not a bytes object of length %zd",
PyBytes_GET_SIZE(args[3]));
goto exit;
}
d = PyBytes_AS_STRING(args[3])[0];
}
else if (PyByteArray_Check(args[3]) && PyByteArray_GET_SIZE(args[3]) == 1) {
else if (PyByteArray_Check(args[3])) {
if (PyByteArray_GET_SIZE(args[3]) != 1) {
PyErr_Format(PyExc_TypeError,
"char_converter(): argument 4 must be a byte string of length 1, "
"not a bytearray object of length %zd",
PyByteArray_GET_SIZE(args[3]));
goto exit;
}
d = PyByteArray_AS_STRING(args[3])[0];
}
else {
@ -286,10 +342,24 @@ char_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
if (nargs < 5) {
goto skip_optional;
}
if (PyBytes_Check(args[4]) && PyBytes_GET_SIZE(args[4]) == 1) {
if (PyBytes_Check(args[4])) {
if (PyBytes_GET_SIZE(args[4]) != 1) {
PyErr_Format(PyExc_TypeError,
"char_converter(): argument 5 must be a byte string of length 1, "
"not a bytes object of length %zd",
PyBytes_GET_SIZE(args[4]));
goto exit;
}
e = PyBytes_AS_STRING(args[4])[0];
}
else if (PyByteArray_Check(args[4]) && PyByteArray_GET_SIZE(args[4]) == 1) {
else if (PyByteArray_Check(args[4])) {
if (PyByteArray_GET_SIZE(args[4]) != 1) {
PyErr_Format(PyExc_TypeError,
"char_converter(): argument 5 must be a byte string of length 1, "
"not a bytearray object of length %zd",
PyByteArray_GET_SIZE(args[4]));
goto exit;
}
e = PyByteArray_AS_STRING(args[4])[0];
}
else {
@ -299,10 +369,24 @@ char_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
if (nargs < 6) {
goto skip_optional;
}
if (PyBytes_Check(args[5]) && PyBytes_GET_SIZE(args[5]) == 1) {
if (PyBytes_Check(args[5])) {
if (PyBytes_GET_SIZE(args[5]) != 1) {
PyErr_Format(PyExc_TypeError,
"char_converter(): argument 6 must be a byte string of length 1, "
"not a bytes object of length %zd",
PyBytes_GET_SIZE(args[5]));
goto exit;
}
f = PyBytes_AS_STRING(args[5])[0];
}
else if (PyByteArray_Check(args[5]) && PyByteArray_GET_SIZE(args[5]) == 1) {
else if (PyByteArray_Check(args[5])) {
if (PyByteArray_GET_SIZE(args[5]) != 1) {
PyErr_Format(PyExc_TypeError,
"char_converter(): argument 6 must be a byte string of length 1, "
"not a bytearray object of length %zd",
PyByteArray_GET_SIZE(args[5]));
goto exit;
}
f = PyByteArray_AS_STRING(args[5])[0];
}
else {
@ -312,10 +396,24 @@ char_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
if (nargs < 7) {
goto skip_optional;
}
if (PyBytes_Check(args[6]) && PyBytes_GET_SIZE(args[6]) == 1) {
if (PyBytes_Check(args[6])) {
if (PyBytes_GET_SIZE(args[6]) != 1) {
PyErr_Format(PyExc_TypeError,
"char_converter(): argument 7 must be a byte string of length 1, "
"not a bytes object of length %zd",
PyBytes_GET_SIZE(args[6]));
goto exit;
}
g = PyBytes_AS_STRING(args[6])[0];
}
else if (PyByteArray_Check(args[6]) && PyByteArray_GET_SIZE(args[6]) == 1) {
else if (PyByteArray_Check(args[6])) {
if (PyByteArray_GET_SIZE(args[6]) != 1) {
PyErr_Format(PyExc_TypeError,
"char_converter(): argument 7 must be a byte string of length 1, "
"not a bytearray object of length %zd",
PyByteArray_GET_SIZE(args[6]));
goto exit;
}
g = PyByteArray_AS_STRING(args[6])[0];
}
else {
@ -325,10 +423,24 @@ char_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
if (nargs < 8) {
goto skip_optional;
}
if (PyBytes_Check(args[7]) && PyBytes_GET_SIZE(args[7]) == 1) {
if (PyBytes_Check(args[7])) {
if (PyBytes_GET_SIZE(args[7]) != 1) {
PyErr_Format(PyExc_TypeError,
"char_converter(): argument 8 must be a byte string of length 1, "
"not a bytes object of length %zd",
PyBytes_GET_SIZE(args[7]));
goto exit;
}
h = PyBytes_AS_STRING(args[7])[0];
}
else if (PyByteArray_Check(args[7]) && PyByteArray_GET_SIZE(args[7]) == 1) {
else if (PyByteArray_Check(args[7])) {
if (PyByteArray_GET_SIZE(args[7]) != 1) {
PyErr_Format(PyExc_TypeError,
"char_converter(): argument 8 must be a byte string of length 1, "
"not a bytearray object of length %zd",
PyByteArray_GET_SIZE(args[7]));
goto exit;
}
h = PyByteArray_AS_STRING(args[7])[0];
}
else {
@ -338,10 +450,24 @@ char_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
if (nargs < 9) {
goto skip_optional;
}
if (PyBytes_Check(args[8]) && PyBytes_GET_SIZE(args[8]) == 1) {
if (PyBytes_Check(args[8])) {
if (PyBytes_GET_SIZE(args[8]) != 1) {
PyErr_Format(PyExc_TypeError,
"char_converter(): argument 9 must be a byte string of length 1, "
"not a bytes object of length %zd",
PyBytes_GET_SIZE(args[8]));
goto exit;
}
i = PyBytes_AS_STRING(args[8])[0];
}
else if (PyByteArray_Check(args[8]) && PyByteArray_GET_SIZE(args[8]) == 1) {
else if (PyByteArray_Check(args[8])) {
if (PyByteArray_GET_SIZE(args[8]) != 1) {
PyErr_Format(PyExc_TypeError,
"char_converter(): argument 9 must be a byte string of length 1, "
"not a bytearray object of length %zd",
PyByteArray_GET_SIZE(args[8]));
goto exit;
}
i = PyByteArray_AS_STRING(args[8])[0];
}
else {
@ -351,10 +477,24 @@ char_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
if (nargs < 10) {
goto skip_optional;
}
if (PyBytes_Check(args[9]) && PyBytes_GET_SIZE(args[9]) == 1) {
if (PyBytes_Check(args[9])) {
if (PyBytes_GET_SIZE(args[9]) != 1) {
PyErr_Format(PyExc_TypeError,
"char_converter(): argument 10 must be a byte string of length 1, "
"not a bytes object of length %zd",
PyBytes_GET_SIZE(args[9]));
goto exit;
}
j = PyBytes_AS_STRING(args[9])[0];
}
else if (PyByteArray_Check(args[9]) && PyByteArray_GET_SIZE(args[9]) == 1) {
else if (PyByteArray_Check(args[9])) {
if (PyByteArray_GET_SIZE(args[9]) != 1) {
PyErr_Format(PyExc_TypeError,
"char_converter(): argument 10 must be a byte string of length 1, "
"not a bytearray object of length %zd",
PyByteArray_GET_SIZE(args[9]));
goto exit;
}
j = PyByteArray_AS_STRING(args[9])[0];
}
else {
@ -364,10 +504,24 @@ char_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
if (nargs < 11) {
goto skip_optional;
}
if (PyBytes_Check(args[10]) && PyBytes_GET_SIZE(args[10]) == 1) {
if (PyBytes_Check(args[10])) {
if (PyBytes_GET_SIZE(args[10]) != 1) {
PyErr_Format(PyExc_TypeError,
"char_converter(): argument 11 must be a byte string of length 1, "
"not a bytes object of length %zd",
PyBytes_GET_SIZE(args[10]));
goto exit;
}
k = PyBytes_AS_STRING(args[10])[0];
}
else if (PyByteArray_Check(args[10]) && PyByteArray_GET_SIZE(args[10]) == 1) {
else if (PyByteArray_Check(args[10])) {
if (PyByteArray_GET_SIZE(args[10]) != 1) {
PyErr_Format(PyExc_TypeError,
"char_converter(): argument 11 must be a byte string of length 1, "
"not a bytearray object of length %zd",
PyByteArray_GET_SIZE(args[10]));
goto exit;
}
k = PyByteArray_AS_STRING(args[10])[0];
}
else {
@ -377,10 +531,24 @@ char_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
if (nargs < 12) {
goto skip_optional;
}
if (PyBytes_Check(args[11]) && PyBytes_GET_SIZE(args[11]) == 1) {
if (PyBytes_Check(args[11])) {
if (PyBytes_GET_SIZE(args[11]) != 1) {
PyErr_Format(PyExc_TypeError,
"char_converter(): argument 12 must be a byte string of length 1, "
"not a bytes object of length %zd",
PyBytes_GET_SIZE(args[11]));
goto exit;
}
l = PyBytes_AS_STRING(args[11])[0];
}
else if (PyByteArray_Check(args[11]) && PyByteArray_GET_SIZE(args[11]) == 1) {
else if (PyByteArray_Check(args[11])) {
if (PyByteArray_GET_SIZE(args[11]) != 1) {
PyErr_Format(PyExc_TypeError,
"char_converter(): argument 12 must be a byte string of length 1, "
"not a bytearray object of length %zd",
PyByteArray_GET_SIZE(args[11]));
goto exit;
}
l = PyByteArray_AS_STRING(args[11])[0];
}
else {
@ -390,10 +558,24 @@ char_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
if (nargs < 13) {
goto skip_optional;
}
if (PyBytes_Check(args[12]) && PyBytes_GET_SIZE(args[12]) == 1) {
if (PyBytes_Check(args[12])) {
if (PyBytes_GET_SIZE(args[12]) != 1) {
PyErr_Format(PyExc_TypeError,
"char_converter(): argument 13 must be a byte string of length 1, "
"not a bytes object of length %zd",
PyBytes_GET_SIZE(args[12]));
goto exit;
}
m = PyBytes_AS_STRING(args[12])[0];
}
else if (PyByteArray_Check(args[12]) && PyByteArray_GET_SIZE(args[12]) == 1) {
else if (PyByteArray_Check(args[12])) {
if (PyByteArray_GET_SIZE(args[12]) != 1) {
PyErr_Format(PyExc_TypeError,
"char_converter(): argument 13 must be a byte string of length 1, "
"not a bytearray object of length %zd",
PyByteArray_GET_SIZE(args[12]));
goto exit;
}
m = PyByteArray_AS_STRING(args[12])[0];
}
else {
@ -403,10 +585,24 @@ char_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
if (nargs < 14) {
goto skip_optional;
}
if (PyBytes_Check(args[13]) && PyBytes_GET_SIZE(args[13]) == 1) {
if (PyBytes_Check(args[13])) {
if (PyBytes_GET_SIZE(args[13]) != 1) {
PyErr_Format(PyExc_TypeError,
"char_converter(): argument 14 must be a byte string of length 1, "
"not a bytes object of length %zd",
PyBytes_GET_SIZE(args[13]));
goto exit;
}
n = PyBytes_AS_STRING(args[13])[0];
}
else if (PyByteArray_Check(args[13]) && PyByteArray_GET_SIZE(args[13]) == 1) {
else if (PyByteArray_Check(args[13])) {
if (PyByteArray_GET_SIZE(args[13]) != 1) {
PyErr_Format(PyExc_TypeError,
"char_converter(): argument 14 must be a byte string of length 1, "
"not a bytearray object of length %zd",
PyByteArray_GET_SIZE(args[13]));
goto exit;
}
n = PyByteArray_AS_STRING(args[13])[0];
}
else {
@ -648,7 +844,10 @@ int_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
goto exit;
}
if (PyUnicode_GET_LENGTH(args[2]) != 1) {
_PyArg_BadArgument("int_converter", "argument 3", "a unicode character", args[2]);
PyErr_Format(PyExc_TypeError,
"int_converter(): argument 3 must be a unicode character, "
"not a string of length %zd",
PyUnicode_GET_LENGTH(args[2]));
goto exit;
}
c = PyUnicode_READ_CHAR(args[2], 0);
@ -3219,4 +3418,4 @@ _testclinic_TestClass_get_defining_class_arg(PyObject *self, PyTypeObject *cls,
exit:
return return_value;
}
/*[clinic end generated code: output=aa352c3a67300056 input=a9049054013a1b77]*/
/*[clinic end generated code: output=545d409a47f1826d input=a9049054013a1b77]*/

View file

@ -596,7 +596,10 @@ array__array_reconstructor(PyObject *module, PyObject *const *args, Py_ssize_t n
goto exit;
}
if (PyUnicode_GET_LENGTH(args[1]) != 1) {
_PyArg_BadArgument("_array_reconstructor", "argument 2", "a unicode character", args[1]);
PyErr_Format(PyExc_TypeError,
"_array_reconstructor(): argument 2 must be a unicode character, "
"not a string of length %zd",
PyUnicode_GET_LENGTH(args[1]));
goto exit;
}
typecode = PyUnicode_READ_CHAR(args[1], 0);
@ -685,4 +688,4 @@ PyDoc_STRVAR(array_arrayiterator___setstate____doc__,
#define ARRAY_ARRAYITERATOR___SETSTATE___METHODDEF \
{"__setstate__", (PyCFunction)array_arrayiterator___setstate__, METH_O, array_arrayiterator___setstate____doc__},
/*[clinic end generated code: output=52c55d9b1d026c1c input=a9049054013a1b77]*/
/*[clinic end generated code: output=9a3276ffd499c796 input=a9049054013a1b77]*/

View file

@ -36,7 +36,10 @@ unicodedata_UCD_decimal(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
goto exit;
}
if (PyUnicode_GET_LENGTH(args[0]) != 1) {
_PyArg_BadArgument("decimal", "argument 1", "a unicode character", args[0]);
PyErr_Format(PyExc_TypeError,
"decimal(): argument 1 must be a unicode character, "
"not a string of length %zd",
PyUnicode_GET_LENGTH(args[0]));
goto exit;
}
chr = PyUnicode_READ_CHAR(args[0], 0);
@ -82,7 +85,10 @@ unicodedata_UCD_digit(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
goto exit;
}
if (PyUnicode_GET_LENGTH(args[0]) != 1) {
_PyArg_BadArgument("digit", "argument 1", "a unicode character", args[0]);
PyErr_Format(PyExc_TypeError,
"digit(): argument 1 must be a unicode character, "
"not a string of length %zd",
PyUnicode_GET_LENGTH(args[0]));
goto exit;
}
chr = PyUnicode_READ_CHAR(args[0], 0);
@ -129,7 +135,10 @@ unicodedata_UCD_numeric(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
goto exit;
}
if (PyUnicode_GET_LENGTH(args[0]) != 1) {
_PyArg_BadArgument("numeric", "argument 1", "a unicode character", args[0]);
PyErr_Format(PyExc_TypeError,
"numeric(): argument 1 must be a unicode character, "
"not a string of length %zd",
PyUnicode_GET_LENGTH(args[0]));
goto exit;
}
chr = PyUnicode_READ_CHAR(args[0], 0);
@ -167,7 +176,10 @@ unicodedata_UCD_category(PyObject *self, PyObject *arg)
goto exit;
}
if (PyUnicode_GET_LENGTH(arg) != 1) {
_PyArg_BadArgument("category", "argument", "a unicode character", arg);
PyErr_Format(PyExc_TypeError,
"category(): argument must be a unicode character, "
"not a string of length %zd",
PyUnicode_GET_LENGTH(arg));
goto exit;
}
chr = PyUnicode_READ_CHAR(arg, 0);
@ -202,7 +214,10 @@ unicodedata_UCD_bidirectional(PyObject *self, PyObject *arg)
goto exit;
}
if (PyUnicode_GET_LENGTH(arg) != 1) {
_PyArg_BadArgument("bidirectional", "argument", "a unicode character", arg);
PyErr_Format(PyExc_TypeError,
"bidirectional(): argument must be a unicode character, "
"not a string of length %zd",
PyUnicode_GET_LENGTH(arg));
goto exit;
}
chr = PyUnicode_READ_CHAR(arg, 0);
@ -238,7 +253,10 @@ unicodedata_UCD_combining(PyObject *self, PyObject *arg)
goto exit;
}
if (PyUnicode_GET_LENGTH(arg) != 1) {
_PyArg_BadArgument("combining", "argument", "a unicode character", arg);
PyErr_Format(PyExc_TypeError,
"combining(): argument must be a unicode character, "
"not a string of length %zd",
PyUnicode_GET_LENGTH(arg));
goto exit;
}
chr = PyUnicode_READ_CHAR(arg, 0);
@ -279,7 +297,10 @@ unicodedata_UCD_mirrored(PyObject *self, PyObject *arg)
goto exit;
}
if (PyUnicode_GET_LENGTH(arg) != 1) {
_PyArg_BadArgument("mirrored", "argument", "a unicode character", arg);
PyErr_Format(PyExc_TypeError,
"mirrored(): argument must be a unicode character, "
"not a string of length %zd",
PyUnicode_GET_LENGTH(arg));
goto exit;
}
chr = PyUnicode_READ_CHAR(arg, 0);
@ -316,7 +337,10 @@ unicodedata_UCD_east_asian_width(PyObject *self, PyObject *arg)
goto exit;
}
if (PyUnicode_GET_LENGTH(arg) != 1) {
_PyArg_BadArgument("east_asian_width", "argument", "a unicode character", arg);
PyErr_Format(PyExc_TypeError,
"east_asian_width(): argument must be a unicode character, "
"not a string of length %zd",
PyUnicode_GET_LENGTH(arg));
goto exit;
}
chr = PyUnicode_READ_CHAR(arg, 0);
@ -351,7 +375,10 @@ unicodedata_UCD_decomposition(PyObject *self, PyObject *arg)
goto exit;
}
if (PyUnicode_GET_LENGTH(arg) != 1) {
_PyArg_BadArgument("decomposition", "argument", "a unicode character", arg);
PyErr_Format(PyExc_TypeError,
"decomposition(): argument must be a unicode character, "
"not a string of length %zd",
PyUnicode_GET_LENGTH(arg));
goto exit;
}
chr = PyUnicode_READ_CHAR(arg, 0);
@ -473,7 +500,10 @@ unicodedata_UCD_name(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
goto exit;
}
if (PyUnicode_GET_LENGTH(args[0]) != 1) {
_PyArg_BadArgument("name", "argument 1", "a unicode character", args[0]);
PyErr_Format(PyExc_TypeError,
"name(): argument 1 must be a unicode character, "
"not a string of length %zd",
PyUnicode_GET_LENGTH(args[0]));
goto exit;
}
chr = PyUnicode_READ_CHAR(args[0], 0);
@ -519,4 +549,4 @@ unicodedata_UCD_lookup(PyObject *self, PyObject *arg)
exit:
return return_value;
}
/*[clinic end generated code: output=ea30f89007b2bfff input=a9049054013a1b77]*/
/*[clinic end generated code: output=8a59d430cee41058 input=a9049054013a1b77]*/

View file

@ -260,7 +260,7 @@ termios_tcsetattr_impl(PyObject *module, int fd, int when, PyObject *term)
}
else {
PyErr_SetString(PyExc_TypeError,
"tcsetattr: elements of attributes must be characters or integers");
"tcsetattr: elements of attributes must be bytes objects of length 1 or integers");
return NULL;
}
}