Skip to content

gh-117557: Improve error message for 'C' format if given string is not 1 char long #117558

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Lib/test/clinic.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1028,7 +1028,9 @@ test_int_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
goto exit;
}
if (PyUnicode_GET_LENGTH(args[2]) != 1) {
_PyArg_BadArgument("test_int_converter", "argument 3", "a unicode character", args[2]);
PyErr_SetString(PyExc_TypeError,
"test_int_converter(): argument 3 must be a string containing "
"exactly one unicode character");
goto exit;
}
c = PyUnicode_READ_CHAR(args[2], 0);
Expand All @@ -1048,7 +1050,7 @@ test_int_converter(PyObject *module, PyObject *const *args, Py_ssize_t nargs)

static PyObject *
test_int_converter_impl(PyObject *module, int a, int b, int c, myenum d)
/*[clinic end generated code: output=5aed87a7589eefb2 input=d20541fc1ca0553e]*/
/*[clinic end generated code: output=9c44e10850d9b44c input=d20541fc1ca0553e]*/


/*[clinic input]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Argument Clinic now generates slightly more accurate error messages for
``int(accept={str})`` converters if the given string is not exactly one
character long.
6 changes: 4 additions & 2 deletions Modules/clinic/_testclinic.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Modules/clinic/arraymodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 31 additions & 11 deletions Modules/clinic/unicodedata.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions PC/clinic/msvcrtmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions Python/getargs.c
Original file line number Diff line number Diff line change
Expand Up @@ -575,11 +575,13 @@ static const char *
converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize)
{
assert(expected != NULL);
assert(arg != NULL);
if (expected[0] == '(') {
PyOS_snprintf(msgbuf, bufsize,
"%.100s", expected);
}
else if (arg == NULL) {
PyOS_snprintf(msgbuf, bufsize, "must be %.100s", expected);
}
else {
PyOS_snprintf(msgbuf, bufsize,
"must be %.50s, not %.50s", expected,
Expand Down Expand Up @@ -812,7 +814,8 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
return converterr("a unicode character", arg, msgbuf, bufsize);

if (PyUnicode_GET_LENGTH(arg) != 1)
return converterr("a unicode character", arg, msgbuf, bufsize);
return converterr("a string containing exactly one unicode character",
NULL, msgbuf, bufsize);

kind = PyUnicode_KIND(arg);
data = PyUnicode_DATA(arg);
Expand Down
5 changes: 4 additions & 1 deletion Tools/clinic/libclinic/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,14 @@ def parse_arg(self, argname: str, displayname: str, *, limited_capi: bool) -> st
goto exit;
}}}}
if (PyUnicode_GET_LENGTH({argname}) != 1) {{{{
{bad_argument}
PyErr_SetString(PyExc_TypeError,
"{{name}}(): {displayname} must be a string containing "
"exactly one unicode character");
goto exit;
}}}}
{paramname} = PyUnicode_READ_CHAR({argname}, 0);
""",
displayname=displayname,
argname=argname,
bad_argument=self.bad_argument(displayname, 'a unicode character', limited_capi=limited_capi),
)
Expand Down