From e37620edfd77b78b913b5eab55cd91327c3e7fd3 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 17 Oct 2023 23:52:58 +0200 Subject: [PATCH] gh-85283: Build resource extension with limited C API (#110989) * Replace PyStructSequence_SET_ITEM() with PyStructSequence_SetItem(). * Replace PyTuple_GET_SIZE() with PyTuple_Size(). * Replace PyTuple_GET_ITEM() with PyTuple_GetItem(). --- Doc/whatsnew/3.13.rst | 6 +-- ...3-10-17-01-56-11.gh-issue-85283.V156T2.rst | 6 +-- Modules/clinic/resource.c.h | 18 +++++--- Modules/resource.c | 44 +++++++++---------- 4 files changed, 38 insertions(+), 36 deletions(-) diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index 42e059386e2..24a53b7700b 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -932,9 +932,9 @@ Build Changes * Building CPython now requires a compiler with support for the C11 atomic library, GCC built-in atomic functions, or MSVC interlocked intrinsics. -* The ``errno``, ``md5``, ``winsound``, ``_ctypes_test``, ``_stat`` and - ``_testimportmultiple`` C extensions are now built with the :ref:`limited C - API `. +* The ``errno``, ``md5``, ``resource``, ``winsound``, ``_ctypes_test``, + ``_stat`` and ``_testimportmultiple`` C extensions are now built with the + :ref:`limited C API `. (Contributed by Victor Stinner in :gh:`85283`.) diff --git a/Misc/NEWS.d/next/Build/2023-10-17-01-56-11.gh-issue-85283.V156T2.rst b/Misc/NEWS.d/next/Build/2023-10-17-01-56-11.gh-issue-85283.V156T2.rst index 33b1d0daf3c..3e85d85d37a 100644 --- a/Misc/NEWS.d/next/Build/2023-10-17-01-56-11.gh-issue-85283.V156T2.rst +++ b/Misc/NEWS.d/next/Build/2023-10-17-01-56-11.gh-issue-85283.V156T2.rst @@ -1,4 +1,4 @@ -The ``errno``, ``md5``, ``winsound``, ``_ctypes_test``, ``_stat`` and -``_testimportmultiple`` C extensions are now built with the :ref:`limited C API -`. +The ``errno``, ``md5``, ``resource``, ``winsound``, ``_ctypes_test``, ``_stat`` +and ``_testimportmultiple`` C extensions are now built with the :ref:`limited C +API `. Patch by Victor Stinner. diff --git a/Modules/clinic/resource.c.h b/Modules/clinic/resource.c.h index e4ef93900d1..9eda7de2753 100644 --- a/Modules/clinic/resource.c.h +++ b/Modules/clinic/resource.c.h @@ -2,8 +2,6 @@ preserve [clinic start generated code]*/ -#include "pycore_modsupport.h" // _PyArg_CheckPositional() - #if defined(HAVE_GETRUSAGE) PyDoc_STRVAR(resource_getrusage__doc__, @@ -68,7 +66,7 @@ PyDoc_STRVAR(resource_setrlimit__doc__, "\n"); #define RESOURCE_SETRLIMIT_METHODDEF \ - {"setrlimit", _PyCFunction_CAST(resource_setrlimit), METH_FASTCALL, resource_setrlimit__doc__}, + {"setrlimit", (PyCFunction)(void(*)(void))resource_setrlimit, METH_FASTCALL, resource_setrlimit__doc__}, static PyObject * resource_setrlimit_impl(PyObject *module, int resource, PyObject *limits); @@ -80,7 +78,8 @@ resource_setrlimit(PyObject *module, PyObject *const *args, Py_ssize_t nargs) int resource; PyObject *limits; - if (!_PyArg_CheckPositional("setrlimit", nargs, 2, 2)) { + if (nargs != 2) { + PyErr_Format(PyExc_TypeError, "setrlimit expected 2 arguments, got %zd", nargs); goto exit; } resource = PyLong_AsInt(args[0]); @@ -102,7 +101,7 @@ PyDoc_STRVAR(resource_prlimit__doc__, "\n"); #define RESOURCE_PRLIMIT_METHODDEF \ - {"prlimit", _PyCFunction_CAST(resource_prlimit), METH_FASTCALL, resource_prlimit__doc__}, + {"prlimit", (PyCFunction)(void(*)(void))resource_prlimit, METH_FASTCALL, resource_prlimit__doc__}, static PyObject * resource_prlimit_impl(PyObject *module, pid_t pid, int resource, @@ -116,7 +115,12 @@ resource_prlimit(PyObject *module, PyObject *const *args, Py_ssize_t nargs) int resource; PyObject *limits = Py_None; - if (!_PyArg_CheckPositional("prlimit", nargs, 2, 3)) { + if (nargs < 2) { + PyErr_Format(PyExc_TypeError, "prlimit expected at least 2 arguments, got %zd", nargs); + goto exit; + } + if (nargs > 3) { + PyErr_Format(PyExc_TypeError, "prlimit expected at most 3 arguments, got %zd", nargs); goto exit; } pid = PyLong_AsPid(args[0]); @@ -174,4 +178,4 @@ exit: #ifndef RESOURCE_PRLIMIT_METHODDEF #define RESOURCE_PRLIMIT_METHODDEF #endif /* !defined(RESOURCE_PRLIMIT_METHODDEF) */ -/*[clinic end generated code: output=8e905b2f5c35170e input=a9049054013a1b77]*/ +/*[clinic end generated code: output=e45883ace510414a input=a9049054013a1b77]*/ diff --git a/Modules/resource.c b/Modules/resource.c index 22ac31aa419..c973008aaa6 100644 --- a/Modules/resource.c +++ b/Modules/resource.c @@ -1,7 +1,5 @@ -// clinic/resource.c.h uses internal pycore_modsupport.h API -#ifndef Py_BUILD_CORE_BUILTIN -# define Py_BUILD_CORE_MODULE 1 -#endif +// Need limited C API version 3.13 for Py_MOD_PER_INTERPRETER_GIL_SUPPORTED +#define Py_LIMITED_API 0x030d0000 #include "Python.h" #include // errno @@ -121,24 +119,24 @@ resource_getrusage_impl(PyObject *module, int who) if (!result) return NULL; - PyStructSequence_SET_ITEM(result, 0, + PyStructSequence_SetItem(result, 0, PyFloat_FromDouble(doubletime(ru.ru_utime))); - PyStructSequence_SET_ITEM(result, 1, + PyStructSequence_SetItem(result, 1, PyFloat_FromDouble(doubletime(ru.ru_stime))); - PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong(ru.ru_maxrss)); - PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong(ru.ru_ixrss)); - PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong(ru.ru_idrss)); - PyStructSequence_SET_ITEM(result, 5, PyLong_FromLong(ru.ru_isrss)); - PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(ru.ru_minflt)); - PyStructSequence_SET_ITEM(result, 7, PyLong_FromLong(ru.ru_majflt)); - PyStructSequence_SET_ITEM(result, 8, PyLong_FromLong(ru.ru_nswap)); - PyStructSequence_SET_ITEM(result, 9, PyLong_FromLong(ru.ru_inblock)); - PyStructSequence_SET_ITEM(result, 10, PyLong_FromLong(ru.ru_oublock)); - PyStructSequence_SET_ITEM(result, 11, PyLong_FromLong(ru.ru_msgsnd)); - PyStructSequence_SET_ITEM(result, 12, PyLong_FromLong(ru.ru_msgrcv)); - PyStructSequence_SET_ITEM(result, 13, PyLong_FromLong(ru.ru_nsignals)); - PyStructSequence_SET_ITEM(result, 14, PyLong_FromLong(ru.ru_nvcsw)); - PyStructSequence_SET_ITEM(result, 15, PyLong_FromLong(ru.ru_nivcsw)); + PyStructSequence_SetItem(result, 2, PyLong_FromLong(ru.ru_maxrss)); + PyStructSequence_SetItem(result, 3, PyLong_FromLong(ru.ru_ixrss)); + PyStructSequence_SetItem(result, 4, PyLong_FromLong(ru.ru_idrss)); + PyStructSequence_SetItem(result, 5, PyLong_FromLong(ru.ru_isrss)); + PyStructSequence_SetItem(result, 6, PyLong_FromLong(ru.ru_minflt)); + PyStructSequence_SetItem(result, 7, PyLong_FromLong(ru.ru_majflt)); + PyStructSequence_SetItem(result, 8, PyLong_FromLong(ru.ru_nswap)); + PyStructSequence_SetItem(result, 9, PyLong_FromLong(ru.ru_inblock)); + PyStructSequence_SetItem(result, 10, PyLong_FromLong(ru.ru_oublock)); + PyStructSequence_SetItem(result, 11, PyLong_FromLong(ru.ru_msgsnd)); + PyStructSequence_SetItem(result, 12, PyLong_FromLong(ru.ru_msgrcv)); + PyStructSequence_SetItem(result, 13, PyLong_FromLong(ru.ru_nsignals)); + PyStructSequence_SetItem(result, 14, PyLong_FromLong(ru.ru_nvcsw)); + PyStructSequence_SetItem(result, 15, PyLong_FromLong(ru.ru_nivcsw)); if (PyErr_Occurred()) { Py_DECREF(result); @@ -158,13 +156,13 @@ py2rlimit(PyObject *limits, struct rlimit *rl_out) /* Here limits is a borrowed reference */ return -1; - if (PyTuple_GET_SIZE(limits) != 2) { + if (PyTuple_Size(limits) != 2) { PyErr_SetString(PyExc_ValueError, "expected a tuple of 2 integers"); goto error; } - curobj = PyTuple_GET_ITEM(limits, 0); - maxobj = PyTuple_GET_ITEM(limits, 1); + curobj = PyTuple_GetItem(limits, 0); // borrowed + maxobj = PyTuple_GetItem(limits, 1); // borrowed #if !defined(HAVE_LARGEFILE_SUPPORT) rl_out->rlim_cur = PyLong_AsLong(curobj); if (rl_out->rlim_cur == (rlim_t)-1 && PyErr_Occurred())