gh-111089: Use PyUnicode_AsUTF8() in Argument Clinic (#111585)

Replace PyUnicode_AsUTF8AndSize() with PyUnicode_AsUTF8() to remove
the explicit check for embedded null characters.

The change avoids to have to include explicitly <string.h> to get the
strlen() function when using a recent version of the limited C API.
This commit is contained in:
Victor Stinner 2023-11-01 16:34:42 +01:00 committed by GitHub
parent 97b3cd38d1
commit d9b606b3d0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
34 changed files with 180 additions and 907 deletions

View file

@ -68,15 +68,10 @@ bytearray___init__(PyObject *self, PyObject *args, PyObject *kwargs)
_PyArg_BadArgument("bytearray", "argument 'encoding'", "str", fastargs[1]);
goto exit;
}
Py_ssize_t encoding_length;
encoding = PyUnicode_AsUTF8AndSize(fastargs[1], &encoding_length);
encoding = PyUnicode_AsUTF8(fastargs[1]);
if (encoding == NULL) {
goto exit;
}
if (strlen(encoding) != (size_t)encoding_length) {
PyErr_SetString(PyExc_ValueError, "embedded null character");
goto exit;
}
if (!--noptargs) {
goto skip_optional_pos;
}
@ -85,15 +80,10 @@ bytearray___init__(PyObject *self, PyObject *args, PyObject *kwargs)
_PyArg_BadArgument("bytearray", "argument 'errors'", "str", fastargs[2]);
goto exit;
}
Py_ssize_t errors_length;
errors = PyUnicode_AsUTF8AndSize(fastargs[2], &errors_length);
errors = PyUnicode_AsUTF8(fastargs[2]);
if (errors == NULL) {
goto exit;
}
if (strlen(errors) != (size_t)errors_length) {
PyErr_SetString(PyExc_ValueError, "embedded null character");
goto exit;
}
skip_optional_pos:
return_value = bytearray___init___impl((PyByteArrayObject *)self, arg, encoding, errors);
@ -960,15 +950,10 @@ bytearray_decode(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t narg
_PyArg_BadArgument("decode", "argument 'encoding'", "str", args[0]);
goto exit;
}
Py_ssize_t encoding_length;
encoding = PyUnicode_AsUTF8AndSize(args[0], &encoding_length);
encoding = PyUnicode_AsUTF8(args[0]);
if (encoding == NULL) {
goto exit;
}
if (strlen(encoding) != (size_t)encoding_length) {
PyErr_SetString(PyExc_ValueError, "embedded null character");
goto exit;
}
if (!--noptargs) {
goto skip_optional_pos;
}
@ -977,15 +962,10 @@ bytearray_decode(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t narg
_PyArg_BadArgument("decode", "argument 'errors'", "str", args[1]);
goto exit;
}
Py_ssize_t errors_length;
errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
errors = PyUnicode_AsUTF8(args[1]);
if (errors == NULL) {
goto exit;
}
if (strlen(errors) != (size_t)errors_length) {
PyErr_SetString(PyExc_ValueError, "embedded null character");
goto exit;
}
skip_optional_pos:
return_value = bytearray_decode_impl(self, encoding, errors);
@ -1261,4 +1241,4 @@ bytearray_sizeof(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored))
{
return bytearray_sizeof_impl(self);
}
/*[clinic end generated code: output=0797a5e03cda2a16 input=a9049054013a1b77]*/
/*[clinic end generated code: output=5a7de6295a7ce6cc input=a9049054013a1b77]*/

View file

@ -720,15 +720,10 @@ bytes_decode(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObj
_PyArg_BadArgument("decode", "argument 'encoding'", "str", args[0]);
goto exit;
}
Py_ssize_t encoding_length;
encoding = PyUnicode_AsUTF8AndSize(args[0], &encoding_length);
encoding = PyUnicode_AsUTF8(args[0]);
if (encoding == NULL) {
goto exit;
}
if (strlen(encoding) != (size_t)encoding_length) {
PyErr_SetString(PyExc_ValueError, "embedded null character");
goto exit;
}
if (!--noptargs) {
goto skip_optional_pos;
}
@ -737,15 +732,10 @@ bytes_decode(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObj
_PyArg_BadArgument("decode", "argument 'errors'", "str", args[1]);
goto exit;
}
Py_ssize_t errors_length;
errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
errors = PyUnicode_AsUTF8(args[1]);
if (errors == NULL) {
goto exit;
}
if (strlen(errors) != (size_t)errors_length) {
PyErr_SetString(PyExc_ValueError, "embedded null character");
goto exit;
}
skip_optional_pos:
return_value = bytes_decode_impl(self, encoding, errors);
@ -997,15 +987,10 @@ bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
_PyArg_BadArgument("bytes", "argument 'encoding'", "str", fastargs[1]);
goto exit;
}
Py_ssize_t encoding_length;
encoding = PyUnicode_AsUTF8AndSize(fastargs[1], &encoding_length);
encoding = PyUnicode_AsUTF8(fastargs[1]);
if (encoding == NULL) {
goto exit;
}
if (strlen(encoding) != (size_t)encoding_length) {
PyErr_SetString(PyExc_ValueError, "embedded null character");
goto exit;
}
if (!--noptargs) {
goto skip_optional_pos;
}
@ -1014,19 +999,14 @@ bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
_PyArg_BadArgument("bytes", "argument 'errors'", "str", fastargs[2]);
goto exit;
}
Py_ssize_t errors_length;
errors = PyUnicode_AsUTF8AndSize(fastargs[2], &errors_length);
errors = PyUnicode_AsUTF8(fastargs[2]);
if (errors == NULL) {
goto exit;
}
if (strlen(errors) != (size_t)errors_length) {
PyErr_SetString(PyExc_ValueError, "embedded null character");
goto exit;
}
skip_optional_pos:
return_value = bytes_new_impl(type, x, encoding, errors);
exit:
return return_value;
}
/*[clinic end generated code: output=8a49dbbd78914a6f input=a9049054013a1b77]*/
/*[clinic end generated code: output=97aab3f6ae398664 input=a9049054013a1b77]*/

View file

@ -275,15 +275,10 @@ float___getformat__(PyTypeObject *type, PyObject *arg)
_PyArg_BadArgument("__getformat__", "argument", "str", arg);
goto exit;
}
Py_ssize_t typestr_length;
typestr = PyUnicode_AsUTF8AndSize(arg, &typestr_length);
typestr = PyUnicode_AsUTF8(arg);
if (typestr == NULL) {
goto exit;
}
if (strlen(typestr) != (size_t)typestr_length) {
PyErr_SetString(PyExc_ValueError, "embedded null character");
goto exit;
}
return_value = float___getformat___impl(type, typestr);
exit:
@ -318,4 +313,4 @@ float___format__(PyObject *self, PyObject *arg)
exit:
return return_value;
}
/*[clinic end generated code: output=c79743c8551c30d9 input=a9049054013a1b77]*/
/*[clinic end generated code: output=01f6fbd082eefead input=a9049054013a1b77]*/

View file

@ -305,15 +305,10 @@ memoryview_tobytes(PyMemoryViewObject *self, PyObject *const *args, Py_ssize_t n
order = NULL;
}
else if (PyUnicode_Check(args[0])) {
Py_ssize_t order_length;
order = PyUnicode_AsUTF8AndSize(args[0], &order_length);
order = PyUnicode_AsUTF8(args[0]);
if (order == NULL) {
goto exit;
}
if (strlen(order) != (size_t)order_length) {
PyErr_SetString(PyExc_ValueError, "embedded null character");
goto exit;
}
}
else {
_PyArg_BadArgument("tobytes", "argument 'order'", "str or None", args[0]);
@ -413,4 +408,4 @@ skip_optional_pos:
exit:
return return_value;
}
/*[clinic end generated code: output=7e76a09106921ba2 input=a9049054013a1b77]*/
/*[clinic end generated code: output=abd8c0ce804d8992 input=a9049054013a1b77]*/

View file

@ -203,15 +203,10 @@ unicode_encode(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject
_PyArg_BadArgument("encode", "argument 'encoding'", "str", args[0]);
goto exit;
}
Py_ssize_t encoding_length;
encoding = PyUnicode_AsUTF8AndSize(args[0], &encoding_length);
encoding = PyUnicode_AsUTF8(args[0]);
if (encoding == NULL) {
goto exit;
}
if (strlen(encoding) != (size_t)encoding_length) {
PyErr_SetString(PyExc_ValueError, "embedded null character");
goto exit;
}
if (!--noptargs) {
goto skip_optional_pos;
}
@ -220,15 +215,10 @@ unicode_encode(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject
_PyArg_BadArgument("encode", "argument 'errors'", "str", args[1]);
goto exit;
}
Py_ssize_t errors_length;
errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
errors = PyUnicode_AsUTF8(args[1]);
if (errors == NULL) {
goto exit;
}
if (strlen(errors) != (size_t)errors_length) {
PyErr_SetString(PyExc_ValueError, "embedded null character");
goto exit;
}
skip_optional_pos:
return_value = unicode_encode_impl(self, encoding, errors);
@ -1473,15 +1463,10 @@ unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
_PyArg_BadArgument("str", "argument 'encoding'", "str", fastargs[1]);
goto exit;
}
Py_ssize_t encoding_length;
encoding = PyUnicode_AsUTF8AndSize(fastargs[1], &encoding_length);
encoding = PyUnicode_AsUTF8(fastargs[1]);
if (encoding == NULL) {
goto exit;
}
if (strlen(encoding) != (size_t)encoding_length) {
PyErr_SetString(PyExc_ValueError, "embedded null character");
goto exit;
}
if (!--noptargs) {
goto skip_optional_pos;
}
@ -1490,19 +1475,14 @@ unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
_PyArg_BadArgument("str", "argument 'errors'", "str", fastargs[2]);
goto exit;
}
Py_ssize_t errors_length;
errors = PyUnicode_AsUTF8AndSize(fastargs[2], &errors_length);
errors = PyUnicode_AsUTF8(fastargs[2]);
if (errors == NULL) {
goto exit;
}
if (strlen(errors) != (size_t)errors_length) {
PyErr_SetString(PyExc_ValueError, "embedded null character");
goto exit;
}
skip_optional_pos:
return_value = unicode_new_impl(type, x, encoding, errors);
exit:
return return_value;
}
/*[clinic end generated code: output=873d8b3d09af3095 input=a9049054013a1b77]*/
/*[clinic end generated code: output=20313d6339272ddc input=a9049054013a1b77]*/