bpo-35582: Argument Clinic: inline parsing code for positional parameters. (GH-11313)

This commit is contained in:
Serhiy Storchaka 2019-01-11 16:01:14 +02:00 committed by GitHub
parent 5485085b32
commit 4fa9591025
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
60 changed files with 6194 additions and 778 deletions

View file

@ -55,10 +55,40 @@ stringlib_ljust(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
Py_ssize_t width;
char fillchar = ' ';
if (!_PyArg_ParseStack(args, nargs, "n|c:ljust",
&width, &fillchar)) {
if (!_PyArg_CheckPositional("ljust", nargs, 1, 2)) {
goto exit;
}
if (PyFloat_Check(args[0])) {
PyErr_SetString(PyExc_TypeError,
"integer argument expected, got float" );
goto exit;
}
{
Py_ssize_t ival = -1;
PyObject *iobj = PyNumber_Index(args[0]);
if (iobj != NULL) {
ival = PyLong_AsSsize_t(iobj);
Py_DECREF(iobj);
}
if (ival == -1 && PyErr_Occurred()) {
goto exit;
}
width = ival;
}
if (nargs < 2) {
goto skip_optional;
}
if (PyBytes_Check(args[1]) && PyBytes_GET_SIZE(args[1]) == 1) {
fillchar = PyBytes_AS_STRING(args[1])[0];
}
else if (PyByteArray_Check(args[1]) && PyByteArray_GET_SIZE(args[1]) == 1) {
fillchar = PyByteArray_AS_STRING(args[1])[0];
}
else {
_PyArg_BadArgument("ljust", 2, "a byte string of length 1", args[1]);
goto exit;
}
skip_optional:
return_value = stringlib_ljust_impl(self, width, fillchar);
exit:
@ -86,10 +116,40 @@ stringlib_rjust(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
Py_ssize_t width;
char fillchar = ' ';
if (!_PyArg_ParseStack(args, nargs, "n|c:rjust",
&width, &fillchar)) {
if (!_PyArg_CheckPositional("rjust", nargs, 1, 2)) {
goto exit;
}
if (PyFloat_Check(args[0])) {
PyErr_SetString(PyExc_TypeError,
"integer argument expected, got float" );
goto exit;
}
{
Py_ssize_t ival = -1;
PyObject *iobj = PyNumber_Index(args[0]);
if (iobj != NULL) {
ival = PyLong_AsSsize_t(iobj);
Py_DECREF(iobj);
}
if (ival == -1 && PyErr_Occurred()) {
goto exit;
}
width = ival;
}
if (nargs < 2) {
goto skip_optional;
}
if (PyBytes_Check(args[1]) && PyBytes_GET_SIZE(args[1]) == 1) {
fillchar = PyBytes_AS_STRING(args[1])[0];
}
else if (PyByteArray_Check(args[1]) && PyByteArray_GET_SIZE(args[1]) == 1) {
fillchar = PyByteArray_AS_STRING(args[1])[0];
}
else {
_PyArg_BadArgument("rjust", 2, "a byte string of length 1", args[1]);
goto exit;
}
skip_optional:
return_value = stringlib_rjust_impl(self, width, fillchar);
exit:
@ -117,10 +177,40 @@ stringlib_center(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
Py_ssize_t width;
char fillchar = ' ';
if (!_PyArg_ParseStack(args, nargs, "n|c:center",
&width, &fillchar)) {
if (!_PyArg_CheckPositional("center", nargs, 1, 2)) {
goto exit;
}
if (PyFloat_Check(args[0])) {
PyErr_SetString(PyExc_TypeError,
"integer argument expected, got float" );
goto exit;
}
{
Py_ssize_t ival = -1;
PyObject *iobj = PyNumber_Index(args[0]);
if (iobj != NULL) {
ival = PyLong_AsSsize_t(iobj);
Py_DECREF(iobj);
}
if (ival == -1 && PyErr_Occurred()) {
goto exit;
}
width = ival;
}
if (nargs < 2) {
goto skip_optional;
}
if (PyBytes_Check(args[1]) && PyBytes_GET_SIZE(args[1]) == 1) {
fillchar = PyBytes_AS_STRING(args[1])[0];
}
else if (PyByteArray_Check(args[1]) && PyByteArray_GET_SIZE(args[1]) == 1) {
fillchar = PyByteArray_AS_STRING(args[1])[0];
}
else {
_PyArg_BadArgument("center", 2, "a byte string of length 1", args[1]);
goto exit;
}
skip_optional:
return_value = stringlib_center_impl(self, width, fillchar);
exit:
@ -169,4 +259,4 @@ stringlib_zfill(PyObject *self, PyObject *arg)
exit:
return return_value;
}
/*[clinic end generated code: output=bf2ef501639e1190 input=a9049054013a1b77]*/
/*[clinic end generated code: output=787248a980f6a00e input=a9049054013a1b77]*/