Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1989763

Browse files
bpo-20185: Convert the resource moduel to Argument Clinic. (#545)
Based on patch by Vajrasky Kok.
1 parent d2977a3 commit 1989763

File tree

2 files changed

+239
-36
lines changed

2 files changed

+239
-36
lines changed

Modules/clinic/resource.c.h

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
/*[clinic input]
2+
preserve
3+
[clinic start generated code]*/
4+
5+
PyDoc_STRVAR(resource_getrusage__doc__,
6+
"getrusage($module, who, /)\n"
7+
"--\n"
8+
"\n");
9+
10+
#define RESOURCE_GETRUSAGE_METHODDEF \
11+
{"getrusage", (PyCFunction)resource_getrusage, METH_O, resource_getrusage__doc__},
12+
13+
static PyObject *
14+
resource_getrusage_impl(PyObject *module, int who);
15+
16+
static PyObject *
17+
resource_getrusage(PyObject *module, PyObject *arg)
18+
{
19+
PyObject *return_value = NULL;
20+
int who;
21+
22+
if (!PyArg_Parse(arg, "i:getrusage", &who)) {
23+
goto exit;
24+
}
25+
return_value = resource_getrusage_impl(module, who);
26+
27+
exit:
28+
return return_value;
29+
}
30+
31+
PyDoc_STRVAR(resource_getrlimit__doc__,
32+
"getrlimit($module, resource, /)\n"
33+
"--\n"
34+
"\n");
35+
36+
#define RESOURCE_GETRLIMIT_METHODDEF \
37+
{"getrlimit", (PyCFunction)resource_getrlimit, METH_O, resource_getrlimit__doc__},
38+
39+
static PyObject *
40+
resource_getrlimit_impl(PyObject *module, int resource);
41+
42+
static PyObject *
43+
resource_getrlimit(PyObject *module, PyObject *arg)
44+
{
45+
PyObject *return_value = NULL;
46+
int resource;
47+
48+
if (!PyArg_Parse(arg, "i:getrlimit", &resource)) {
49+
goto exit;
50+
}
51+
return_value = resource_getrlimit_impl(module, resource);
52+
53+
exit:
54+
return return_value;
55+
}
56+
57+
PyDoc_STRVAR(resource_setrlimit__doc__,
58+
"setrlimit($module, resource, limits, /)\n"
59+
"--\n"
60+
"\n");
61+
62+
#define RESOURCE_SETRLIMIT_METHODDEF \
63+
{"setrlimit", (PyCFunction)resource_setrlimit, METH_FASTCALL, resource_setrlimit__doc__},
64+
65+
static PyObject *
66+
resource_setrlimit_impl(PyObject *module, int resource, PyObject *limits);
67+
68+
static PyObject *
69+
resource_setrlimit(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
70+
{
71+
PyObject *return_value = NULL;
72+
int resource;
73+
PyObject *limits;
74+
75+
if (!_PyArg_ParseStack(args, nargs, "iO:setrlimit",
76+
&resource, &limits)) {
77+
goto exit;
78+
}
79+
80+
if (!_PyArg_NoStackKeywords("setrlimit", kwnames)) {
81+
goto exit;
82+
}
83+
return_value = resource_setrlimit_impl(module, resource, limits);
84+
85+
exit:
86+
return return_value;
87+
}
88+
89+
#if defined(HAVE_PRLIMIT)
90+
91+
PyDoc_STRVAR(resource_prlimit__doc__,
92+
"prlimit(pid, resource, [limits])");
93+
94+
#define RESOURCE_PRLIMIT_METHODDEF \
95+
{"prlimit", (PyCFunction)resource_prlimit, METH_VARARGS, resource_prlimit__doc__},
96+
97+
static PyObject *
98+
resource_prlimit_impl(PyObject *module, pid_t pid, int resource,
99+
int group_right_1, PyObject *limits);
100+
101+
static PyObject *
102+
resource_prlimit(PyObject *module, PyObject *args)
103+
{
104+
PyObject *return_value = NULL;
105+
pid_t pid;
106+
int resource;
107+
int group_right_1 = 0;
108+
PyObject *limits = NULL;
109+
110+
switch (PyTuple_GET_SIZE(args)) {
111+
case 2:
112+
if (!PyArg_ParseTuple(args, "" _Py_PARSE_PID "i:prlimit", &pid, &resource)) {
113+
goto exit;
114+
}
115+
break;
116+
case 3:
117+
if (!PyArg_ParseTuple(args, "" _Py_PARSE_PID "iO:prlimit", &pid, &resource, &limits)) {
118+
goto exit;
119+
}
120+
group_right_1 = 1;
121+
break;
122+
default:
123+
PyErr_SetString(PyExc_TypeError, "resource.prlimit requires 2 to 3 arguments");
124+
goto exit;
125+
}
126+
return_value = resource_prlimit_impl(module, pid, resource, group_right_1, limits);
127+
128+
exit:
129+
return return_value;
130+
}
131+
132+
#endif /* defined(HAVE_PRLIMIT) */
133+
134+
PyDoc_STRVAR(resource_getpagesize__doc__,
135+
"getpagesize($module, /)\n"
136+
"--\n"
137+
"\n");
138+
139+
#define RESOURCE_GETPAGESIZE_METHODDEF \
140+
{"getpagesize", (PyCFunction)resource_getpagesize, METH_NOARGS, resource_getpagesize__doc__},
141+
142+
static int
143+
resource_getpagesize_impl(PyObject *module);
144+
145+
static PyObject *
146+
resource_getpagesize(PyObject *module, PyObject *Py_UNUSED(ignored))
147+
{
148+
PyObject *return_value = NULL;
149+
int _return_value;
150+
151+
_return_value = resource_getpagesize_impl(module);
152+
if ((_return_value == -1) && PyErr_Occurred()) {
153+
goto exit;
154+
}
155+
return_value = PyLong_FromLong((long)_return_value);
156+
157+
exit:
158+
return return_value;
159+
}
160+
161+
#ifndef RESOURCE_PRLIMIT_METHODDEF
162+
#define RESOURCE_PRLIMIT_METHODDEF
163+
#endif /* !defined(RESOURCE_PRLIMIT_METHODDEF) */
164+
/*[clinic end generated code: output=3af613da48e0f8c9 input=a9049054013a1b77]*/

Modules/resource.c

Lines changed: 75 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@
1818

1919
#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
2020

21+
/*[clinic input]
22+
module resource
23+
[clinic start generated code]*/
24+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=e89d38ed52609d7c]*/
25+
26+
/*[python input]
27+
class pid_t_converter(CConverter):
28+
type = 'pid_t'
29+
format_unit = '" _Py_PARSE_PID "'
30+
[python start generated code]*/
31+
/*[python end generated code: output=da39a3ee5e6b4b0d input=0c1d19f640d57e48]*/
32+
33+
#include "clinic/resource.c.h"
34+
2135
PyDoc_STRVAR(struct_rusage__doc__,
2236
"struct_rusage: Result from getrusage.\n\n"
2337
"This object may be accessed either as a tuple of\n"
@@ -55,16 +69,21 @@ static PyStructSequence_Desc struct_rusage_desc = {
5569
static int initialized;
5670
static PyTypeObject StructRUsageType;
5771

72+
/*[clinic input]
73+
resource.getrusage
74+
75+
who: int
76+
/
77+
78+
[clinic start generated code]*/
79+
5880
static PyObject *
59-
resource_getrusage(PyObject *self, PyObject *args)
81+
resource_getrusage_impl(PyObject *module, int who)
82+
/*[clinic end generated code: output=8fad2880ba6a9843 input=5c857bcc5b9ccb1b]*/
6083
{
61-
int who;
6284
struct rusage ru;
6385
PyObject *result;
6486

65-
if (!PyArg_ParseTuple(args, "i:getrusage", &who))
66-
return NULL;
67-
6887
if (getrusage(who, &ru) == -1) {
6988
if (errno == EINVAL) {
7089
PyErr_SetString(PyExc_ValueError,
@@ -160,14 +179,19 @@ rlimit2py(struct rlimit rl)
160179
return Py_BuildValue("ll", (long) rl.rlim_cur, (long) rl.rlim_max);
161180
}
162181

182+
/*[clinic input]
183+
resource.getrlimit
184+
185+
resource: int
186+
/
187+
188+
[clinic start generated code]*/
189+
163190
static PyObject *
164-
resource_getrlimit(PyObject *self, PyObject *args)
191+
resource_getrlimit_impl(PyObject *module, int resource)
192+
/*[clinic end generated code: output=98327b25061ffe39 input=a697cb0004cb3c36]*/
165193
{
166194
struct rlimit rl;
167-
int resource;
168-
169-
if (!PyArg_ParseTuple(args, "i:getrlimit", &resource))
170-
return NULL;
171195

172196
if (resource < 0 || resource >= RLIM_NLIMITS) {
173197
PyErr_SetString(PyExc_ValueError,
@@ -182,15 +206,20 @@ resource_getrlimit(PyObject *self, PyObject *args)
182206
return rlimit2py(rl);
183207
}
184208

209+
/*[clinic input]
210+
resource.setrlimit
211+
212+
resource: int
213+
limits: object
214+
/
215+
216+
[clinic start generated code]*/
217+
185218
static PyObject *
186-
resource_setrlimit(PyObject *self, PyObject *args)
219+
resource_setrlimit_impl(PyObject *module, int resource, PyObject *limits)
220+
/*[clinic end generated code: output=4e82ec3f34d013d1 input=6235a6ce23b4ca75]*/
187221
{
188222
struct rlimit rl;
189-
int resource;
190-
PyObject *limits;
191-
192-
if (!PyArg_ParseTuple(args, "iO:setrlimit", &resource, &limits))
193-
return NULL;
194223

195224
if (resource < 0 || resource >= RLIM_NLIMITS) {
196225
PyErr_SetString(PyExc_ValueError,
@@ -217,25 +246,33 @@ resource_setrlimit(PyObject *self, PyObject *args)
217246
}
218247

219248
#ifdef HAVE_PRLIMIT
249+
/*[clinic input]
250+
resource.prlimit
251+
252+
pid: pid_t
253+
resource: int
254+
[
255+
limits: object
256+
]
257+
/
258+
259+
[clinic start generated code]*/
260+
220261
static PyObject *
221-
resource_prlimit(PyObject *self, PyObject *args)
262+
resource_prlimit_impl(PyObject *module, pid_t pid, int resource,
263+
int group_right_1, PyObject *limits)
264+
/*[clinic end generated code: output=ee976b393187a7a3 input=b77743bdccc83564]*/
222265
{
223266
struct rlimit old_limit, new_limit;
224-
int resource, retval;
225-
pid_t pid;
226-
PyObject *limits = NULL;
227-
228-
if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i|O:prlimit",
229-
&pid, &resource, &limits))
230-
return NULL;
267+
int retval;
231268

232269
if (resource < 0 || resource >= RLIM_NLIMITS) {
233270
PyErr_SetString(PyExc_ValueError,
234271
"invalid resource specified");
235272
return NULL;
236273
}
237274

238-
if (limits != NULL) {
275+
if (group_right_1) {
239276
if (py2rlimit(limits, &new_limit) < 0) {
240277
return NULL;
241278
}
@@ -258,8 +295,13 @@ resource_prlimit(PyObject *self, PyObject *args)
258295
}
259296
#endif /* HAVE_PRLIMIT */
260297

261-
static PyObject *
262-
resource_getpagesize(PyObject *self, PyObject *unused)
298+
/*[clinic input]
299+
resource.getpagesize -> int
300+
[clinic start generated code]*/
301+
302+
static int
303+
resource_getpagesize_impl(PyObject *module)
304+
/*[clinic end generated code: output=9ba93eb0f3d6c3a9 input=546545e8c1f42085]*/
263305
{
264306
long pagesize = 0;
265307
#if defined(HAVE_GETPAGESIZE)
@@ -272,21 +314,18 @@ resource_getpagesize(PyObject *self, PyObject *unused)
272314
pagesize = sysconf(_SC_PAGESIZE);
273315
#endif
274316
#endif
275-
return Py_BuildValue("i", pagesize);
276-
317+
return pagesize;
277318
}
278319

279320
/* List of functions */
280321

281322
static struct PyMethodDef
282323
resource_methods[] = {
283-
{"getrusage", resource_getrusage, METH_VARARGS},
284-
{"getrlimit", resource_getrlimit, METH_VARARGS},
285-
#ifdef HAVE_PRLIMIT
286-
{"prlimit", resource_prlimit, METH_VARARGS},
287-
#endif
288-
{"setrlimit", resource_setrlimit, METH_VARARGS},
289-
{"getpagesize", resource_getpagesize, METH_NOARGS},
324+
RESOURCE_GETRUSAGE_METHODDEF
325+
RESOURCE_GETRLIMIT_METHODDEF
326+
RESOURCE_PRLIMIT_METHODDEF
327+
RESOURCE_SETRLIMIT_METHODDEF
328+
RESOURCE_GETPAGESIZE_METHODDEF
290329
{NULL, NULL} /* sentinel */
291330
};
292331

0 commit comments

Comments
 (0)