bpo-20185: Convert the resource moduel to Argument Clinic. (#545)

Based on patch by Vajrasky Kok.
This commit is contained in:
Serhiy Storchaka 2017-03-12 13:08:30 +02:00 committed by GitHub
parent d2977a3ae2
commit 1989763f0d
2 changed files with 239 additions and 36 deletions

164
Modules/clinic/resource.c.h Normal file
View File

@ -0,0 +1,164 @@
/*[clinic input]
preserve
[clinic start generated code]*/
PyDoc_STRVAR(resource_getrusage__doc__,
"getrusage($module, who, /)\n"
"--\n"
"\n");
#define RESOURCE_GETRUSAGE_METHODDEF \
{"getrusage", (PyCFunction)resource_getrusage, METH_O, resource_getrusage__doc__},
static PyObject *
resource_getrusage_impl(PyObject *module, int who);
static PyObject *
resource_getrusage(PyObject *module, PyObject *arg)
{
PyObject *return_value = NULL;
int who;
if (!PyArg_Parse(arg, "i:getrusage", &who)) {
goto exit;
}
return_value = resource_getrusage_impl(module, who);
exit:
return return_value;
}
PyDoc_STRVAR(resource_getrlimit__doc__,
"getrlimit($module, resource, /)\n"
"--\n"
"\n");
#define RESOURCE_GETRLIMIT_METHODDEF \
{"getrlimit", (PyCFunction)resource_getrlimit, METH_O, resource_getrlimit__doc__},
static PyObject *
resource_getrlimit_impl(PyObject *module, int resource);
static PyObject *
resource_getrlimit(PyObject *module, PyObject *arg)
{
PyObject *return_value = NULL;
int resource;
if (!PyArg_Parse(arg, "i:getrlimit", &resource)) {
goto exit;
}
return_value = resource_getrlimit_impl(module, resource);
exit:
return return_value;
}
PyDoc_STRVAR(resource_setrlimit__doc__,
"setrlimit($module, resource, limits, /)\n"
"--\n"
"\n");
#define RESOURCE_SETRLIMIT_METHODDEF \
{"setrlimit", (PyCFunction)resource_setrlimit, METH_FASTCALL, resource_setrlimit__doc__},
static PyObject *
resource_setrlimit_impl(PyObject *module, int resource, PyObject *limits);
static PyObject *
resource_setrlimit(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
{
PyObject *return_value = NULL;
int resource;
PyObject *limits;
if (!_PyArg_ParseStack(args, nargs, "iO:setrlimit",
&resource, &limits)) {
goto exit;
}
if (!_PyArg_NoStackKeywords("setrlimit", kwnames)) {
goto exit;
}
return_value = resource_setrlimit_impl(module, resource, limits);
exit:
return return_value;
}
#if defined(HAVE_PRLIMIT)
PyDoc_STRVAR(resource_prlimit__doc__,
"prlimit(pid, resource, [limits])");
#define RESOURCE_PRLIMIT_METHODDEF \
{"prlimit", (PyCFunction)resource_prlimit, METH_VARARGS, resource_prlimit__doc__},
static PyObject *
resource_prlimit_impl(PyObject *module, pid_t pid, int resource,
int group_right_1, PyObject *limits);
static PyObject *
resource_prlimit(PyObject *module, PyObject *args)
{
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;
}
return_value = resource_prlimit_impl(module, pid, resource, group_right_1, limits);
exit:
return return_value;
}
#endif /* defined(HAVE_PRLIMIT) */
PyDoc_STRVAR(resource_getpagesize__doc__,
"getpagesize($module, /)\n"
"--\n"
"\n");
#define RESOURCE_GETPAGESIZE_METHODDEF \
{"getpagesize", (PyCFunction)resource_getpagesize, METH_NOARGS, resource_getpagesize__doc__},
static int
resource_getpagesize_impl(PyObject *module);
static PyObject *
resource_getpagesize(PyObject *module, PyObject *Py_UNUSED(ignored))
{
PyObject *return_value = NULL;
int _return_value;
_return_value = resource_getpagesize_impl(module);
if ((_return_value == -1) && PyErr_Occurred()) {
goto exit;
}
return_value = PyLong_FromLong((long)_return_value);
exit:
return return_value;
}
#ifndef RESOURCE_PRLIMIT_METHODDEF
#define RESOURCE_PRLIMIT_METHODDEF
#endif /* !defined(RESOURCE_PRLIMIT_METHODDEF) */
/*[clinic end generated code: output=3af613da48e0f8c9 input=a9049054013a1b77]*/

View File

@ -18,6 +18,20 @@
#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
/*[clinic input]
module resource
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=e89d38ed52609d7c]*/
/*[python input]
class pid_t_converter(CConverter):
type = 'pid_t'
format_unit = '" _Py_PARSE_PID "'
[python start generated code]*/
/*[python end generated code: output=da39a3ee5e6b4b0d input=0c1d19f640d57e48]*/
#include "clinic/resource.c.h"
PyDoc_STRVAR(struct_rusage__doc__,
"struct_rusage: Result from getrusage.\n\n"
"This object may be accessed either as a tuple of\n"
@ -55,16 +69,21 @@ static PyStructSequence_Desc struct_rusage_desc = {
static int initialized;
static PyTypeObject StructRUsageType;
/*[clinic input]
resource.getrusage
who: int
/
[clinic start generated code]*/
static PyObject *
resource_getrusage(PyObject *self, PyObject *args)
resource_getrusage_impl(PyObject *module, int who)
/*[clinic end generated code: output=8fad2880ba6a9843 input=5c857bcc5b9ccb1b]*/
{
int who;
struct rusage ru;
PyObject *result;
if (!PyArg_ParseTuple(args, "i:getrusage", &who))
return NULL;
if (getrusage(who, &ru) == -1) {
if (errno == EINVAL) {
PyErr_SetString(PyExc_ValueError,
@ -160,14 +179,19 @@ rlimit2py(struct rlimit rl)
return Py_BuildValue("ll", (long) rl.rlim_cur, (long) rl.rlim_max);
}
/*[clinic input]
resource.getrlimit
resource: int
/
[clinic start generated code]*/
static PyObject *
resource_getrlimit(PyObject *self, PyObject *args)
resource_getrlimit_impl(PyObject *module, int resource)
/*[clinic end generated code: output=98327b25061ffe39 input=a697cb0004cb3c36]*/
{
struct rlimit rl;
int resource;
if (!PyArg_ParseTuple(args, "i:getrlimit", &resource))
return NULL;
if (resource < 0 || resource >= RLIM_NLIMITS) {
PyErr_SetString(PyExc_ValueError,
@ -182,15 +206,20 @@ resource_getrlimit(PyObject *self, PyObject *args)
return rlimit2py(rl);
}
/*[clinic input]
resource.setrlimit
resource: int
limits: object
/
[clinic start generated code]*/
static PyObject *
resource_setrlimit(PyObject *self, PyObject *args)
resource_setrlimit_impl(PyObject *module, int resource, PyObject *limits)
/*[clinic end generated code: output=4e82ec3f34d013d1 input=6235a6ce23b4ca75]*/
{
struct rlimit rl;
int resource;
PyObject *limits;
if (!PyArg_ParseTuple(args, "iO:setrlimit", &resource, &limits))
return NULL;
if (resource < 0 || resource >= RLIM_NLIMITS) {
PyErr_SetString(PyExc_ValueError,
@ -217,17 +246,25 @@ resource_setrlimit(PyObject *self, PyObject *args)
}
#ifdef HAVE_PRLIMIT
/*[clinic input]
resource.prlimit
pid: pid_t
resource: int
[
limits: object
]
/
[clinic start generated code]*/
static PyObject *
resource_prlimit(PyObject *self, PyObject *args)
resource_prlimit_impl(PyObject *module, pid_t pid, int resource,
int group_right_1, PyObject *limits)
/*[clinic end generated code: output=ee976b393187a7a3 input=b77743bdccc83564]*/
{
struct rlimit old_limit, new_limit;
int resource, retval;
pid_t pid;
PyObject *limits = NULL;
if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i|O:prlimit",
&pid, &resource, &limits))
return NULL;
int retval;
if (resource < 0 || resource >= RLIM_NLIMITS) {
PyErr_SetString(PyExc_ValueError,
@ -235,7 +272,7 @@ resource_prlimit(PyObject *self, PyObject *args)
return NULL;
}
if (limits != NULL) {
if (group_right_1) {
if (py2rlimit(limits, &new_limit) < 0) {
return NULL;
}
@ -258,8 +295,13 @@ resource_prlimit(PyObject *self, PyObject *args)
}
#endif /* HAVE_PRLIMIT */
static PyObject *
resource_getpagesize(PyObject *self, PyObject *unused)
/*[clinic input]
resource.getpagesize -> int
[clinic start generated code]*/
static int
resource_getpagesize_impl(PyObject *module)
/*[clinic end generated code: output=9ba93eb0f3d6c3a9 input=546545e8c1f42085]*/
{
long pagesize = 0;
#if defined(HAVE_GETPAGESIZE)
@ -272,21 +314,18 @@ resource_getpagesize(PyObject *self, PyObject *unused)
pagesize = sysconf(_SC_PAGESIZE);
#endif
#endif
return Py_BuildValue("i", pagesize);
return pagesize;
}
/* List of functions */
static struct PyMethodDef
resource_methods[] = {
{"getrusage", resource_getrusage, METH_VARARGS},
{"getrlimit", resource_getrlimit, METH_VARARGS},
#ifdef HAVE_PRLIMIT
{"prlimit", resource_prlimit, METH_VARARGS},
#endif
{"setrlimit", resource_setrlimit, METH_VARARGS},
{"getpagesize", resource_getpagesize, METH_NOARGS},
RESOURCE_GETRUSAGE_METHODDEF
RESOURCE_GETRLIMIT_METHODDEF
RESOURCE_PRLIMIT_METHODDEF
RESOURCE_SETRLIMIT_METHODDEF
RESOURCE_GETPAGESIZE_METHODDEF
{NULL, NULL} /* sentinel */
};