From a739ee412cb2342554a70f30faaa3b7752bbd0a4 Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Fri, 29 Jul 2022 14:48:11 +0300 Subject: [PATCH] gh-94512: Fix forced arg format in AC-processed resource (GH-94515) --- Modules/clinic/resource.c.h | 47 +++++++++++++++++++------------------ Modules/resource.c | 20 ++++++++++------ 2 files changed, 37 insertions(+), 30 deletions(-) diff --git a/Modules/clinic/resource.c.h b/Modules/clinic/resource.c.h index c591823ed71..f95321176a7 100644 --- a/Modules/clinic/resource.c.h +++ b/Modules/clinic/resource.c.h @@ -95,41 +95,42 @@ exit: #if defined(HAVE_PRLIMIT) PyDoc_STRVAR(resource_prlimit__doc__, -"prlimit(pid, resource, [limits])"); +"prlimit($module, pid, resource, limits=None, /)\n" +"--\n" +"\n"); #define RESOURCE_PRLIMIT_METHODDEF \ - {"prlimit", (PyCFunction)resource_prlimit, METH_VARARGS, resource_prlimit__doc__}, + {"prlimit", _PyCFunction_CAST(resource_prlimit), METH_FASTCALL, resource_prlimit__doc__}, static PyObject * resource_prlimit_impl(PyObject *module, pid_t pid, int resource, - int group_right_1, PyObject *limits); + PyObject *limits); static PyObject * -resource_prlimit(PyObject *module, PyObject *args) +resource_prlimit(PyObject *module, PyObject *const *args, Py_ssize_t nargs) { PyObject *return_value = NULL; pid_t pid; int resource; - int group_right_1 = 0; - PyObject *limits = NULL; + PyObject *limits = Py_None; - switch (PyTuple_GET_SIZE(args)) { - case 2: - if (!PyArg_ParseTuple(args, "" _Py_PARSE_PID "i:prlimit", &pid, &resource)) { - goto exit; - } - break; - case 3: - if (!PyArg_ParseTuple(args, "" _Py_PARSE_PID "iO:prlimit", &pid, &resource, &limits)) { - goto exit; - } - group_right_1 = 1; - break; - default: - PyErr_SetString(PyExc_TypeError, "resource.prlimit requires 2 to 3 arguments"); - goto exit; + if (!_PyArg_CheckPositional("prlimit", nargs, 2, 3)) { + goto exit; } - return_value = resource_prlimit_impl(module, pid, resource, group_right_1, limits); + pid = PyLong_AsPid(args[0]); + if (pid == -1 && PyErr_Occurred()) { + goto exit; + } + resource = _PyLong_AsInt(args[1]); + if (resource == -1 && PyErr_Occurred()) { + goto exit; + } + if (nargs < 3) { + goto skip_optional; + } + limits = args[2]; +skip_optional: + return_value = resource_prlimit_impl(module, pid, resource, limits); exit: return return_value; @@ -171,4 +172,4 @@ exit: #ifndef RESOURCE_PRLIMIT_METHODDEF #define RESOURCE_PRLIMIT_METHODDEF #endif /* !defined(RESOURCE_PRLIMIT_METHODDEF) */ -/*[clinic end generated code: output=7c57d4f3688d3f07 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=13441806729c6eaa input=a9049054013a1b77]*/ diff --git a/Modules/resource.c b/Modules/resource.c index d8bba2e3984..a97fb870062 100644 --- a/Modules/resource.c +++ b/Modules/resource.c @@ -24,8 +24,16 @@ module resource class pid_t_converter(CConverter): type = 'pid_t' format_unit = '" _Py_PARSE_PID "' + + def parse_arg(self, argname, displayname): + return """ + {paramname} = PyLong_AsPid({argname}); + if ({paramname} == -1 && PyErr_Occurred()) {{{{ + goto exit; + }}}} + """.format(argname=argname, paramname=self.parser_name) [python start generated code]*/ -/*[python end generated code: output=da39a3ee5e6b4b0d input=0c1d19f640d57e48]*/ +/*[python end generated code: output=da39a3ee5e6b4b0d input=5af1c116d56cbb5a]*/ #include "clinic/resource.c.h" @@ -268,17 +276,15 @@ resource.prlimit pid: pid_t resource: int - [ - limits: object - ] + limits: object = None / [clinic start generated code]*/ static PyObject * resource_prlimit_impl(PyObject *module, pid_t pid, int resource, - int group_right_1, PyObject *limits) -/*[clinic end generated code: output=ee976b393187a7a3 input=b77743bdccc83564]*/ + PyObject *limits) +/*[clinic end generated code: output=6ebc49ff8c3a816e input=54bb69c9585e33bf]*/ { struct rlimit old_limit, new_limit; int retval; @@ -294,7 +300,7 @@ resource_prlimit_impl(PyObject *module, pid_t pid, int resource, return NULL; } - if (group_right_1) { + if (limits != Py_None) { if (py2rlimit(limits, &new_limit) < 0) { return NULL; }