]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-94512: Fix forced arg format in AC-processed resource (GH-94515)
authorOleg Iarygin <oleg@arhadthedev.net>
Fri, 29 Jul 2022 11:48:11 +0000 (14:48 +0300)
committerGitHub <noreply@github.com>
Fri, 29 Jul 2022 11:48:11 +0000 (12:48 +0100)
Modules/clinic/resource.c.h
Modules/resource.c

index c591823ed7170c5c1fed02b8a035c7a173c0738e..f95321176a73a36f59007141e8daa69381cb04bf 100644 (file)
@@ -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;
-
-    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;
+    PyObject *limits = Py_None;
+
+    if (!_PyArg_CheckPositional("prlimit", nargs, 2, 3)) {
+        goto exit;
+    }
+    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;
     }
-    return_value = resource_prlimit_impl(module, pid, resource, group_right_1, limits);
+    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]*/
index d8bba2e39847a1c502e9aa76e20b65883ba7875c..a97fb870062b822a50ee7d260ee385427ea9a2ff 100644 (file)
@@ -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;
         }