From 779229138e78e3ba886ed91e78053af007d5b3c1 Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Sat, 2 Jul 2022 22:54:57 +0300 Subject: [PATCH 1/5] Fix forced arg format in AC-processed resource --- Modules/clinic/resource.c.h | 49 +++++++++++++++++++------------------ Modules/resource.c | 20 +++++++++------ 2 files changed, 38 insertions(+), 31 deletions(-) diff --git a/Modules/clinic/resource.c.h b/Modules/clinic/resource.c.h index c591823ed7170c..f95321176a73a3 100644 --- a/Modules/clinic/resource.c.h +++ b/Modules/clinic/resource.c.h @@ -95,41 +95,42 @@ resource_setrlimit(PyObject *module, PyObject *const *args, Py_ssize_t nargs) #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 @@ resource_getpagesize(PyObject *module, PyObject *Py_UNUSED(ignored)) #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 d8bba2e39847a1..100d205584270c 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) { if (py2rlimit(limits, &new_limit) < 0) { return NULL; } From c10054d0f0f9e465ebd2c05e1a9d8cc3216ca010 Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Sun, 3 Jul 2022 23:09:54 +0300 Subject: [PATCH 2/5] Add a NEWS entry --- .../next/Library/2022-07-03-23-09-45.gh-issue-94512.9wtOYT.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2022-07-03-23-09-45.gh-issue-94512.9wtOYT.rst diff --git a/Misc/NEWS.d/next/Library/2022-07-03-23-09-45.gh-issue-94512.9wtOYT.rst b/Misc/NEWS.d/next/Library/2022-07-03-23-09-45.gh-issue-94512.9wtOYT.rst new file mode 100644 index 00000000000000..c86042d76b6a57 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-07-03-23-09-45.gh-issue-94512.9wtOYT.rst @@ -0,0 +1 @@ +Convert :mod:`resource` to use Argument Clinic. From 7c5907a6e16882c175de94b6d783c6b0232c45d0 Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Mon, 4 Jul 2022 15:25:14 +0300 Subject: [PATCH 3/5] Actualize a NEWS entry --- .../Library/2022-07-03-23-09-45.gh-issue-94512.9wtOYT.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2022-07-03-23-09-45.gh-issue-94512.9wtOYT.rst b/Misc/NEWS.d/next/Library/2022-07-03-23-09-45.gh-issue-94512.9wtOYT.rst index c86042d76b6a57..0b087a1892aa56 100644 --- a/Misc/NEWS.d/next/Library/2022-07-03-23-09-45.gh-issue-94512.9wtOYT.rst +++ b/Misc/NEWS.d/next/Library/2022-07-03-23-09-45.gh-issue-94512.9wtOYT.rst @@ -1 +1,3 @@ -Convert :mod:`resource` to use Argument Clinic. +:meth:`resource.prlimit` changed its signature from +``prlimit(pid, resource, [limits], /)`` to +``prlimit($module, pid, resource, limits=None, /)``. From c0a4f845bb0dd2f62a11d019057a0a25694165ba Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Mon, 4 Jul 2022 23:37:43 +0300 Subject: [PATCH 4/5] Fix an optionality check (we need to compare with Py_None, not NULL) --- Modules/resource.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/resource.c b/Modules/resource.c index 100d205584270c..a97fb870062b82 100644 --- a/Modules/resource.c +++ b/Modules/resource.c @@ -300,7 +300,7 @@ resource_prlimit_impl(PyObject *module, pid_t pid, int resource, return NULL; } - if (limits) { + if (limits != Py_None) { if (py2rlimit(limits, &new_limit) < 0) { return NULL; } From d349c5cc3d425989dffe70e2f01d7ac6b1d95e20 Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Fri, 29 Jul 2022 05:28:38 +0300 Subject: [PATCH 5/5] Delete 2022-07-03-23-09-45.gh-issue-94512.9wtOYT.rst --- .../next/Library/2022-07-03-23-09-45.gh-issue-94512.9wtOYT.rst | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 Misc/NEWS.d/next/Library/2022-07-03-23-09-45.gh-issue-94512.9wtOYT.rst diff --git a/Misc/NEWS.d/next/Library/2022-07-03-23-09-45.gh-issue-94512.9wtOYT.rst b/Misc/NEWS.d/next/Library/2022-07-03-23-09-45.gh-issue-94512.9wtOYT.rst deleted file mode 100644 index 0b087a1892aa56..00000000000000 --- a/Misc/NEWS.d/next/Library/2022-07-03-23-09-45.gh-issue-94512.9wtOYT.rst +++ /dev/null @@ -1,3 +0,0 @@ -:meth:`resource.prlimit` changed its signature from -``prlimit(pid, resource, [limits], /)`` to -``prlimit($module, pid, resource, limits=None, /)``.