-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathfunctions_2.c
More file actions
57 lines (44 loc) · 1.61 KB
/
functions_2.c
File metadata and controls
57 lines (44 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <Python.h>
static PyObject *get_even(PyObject *self, PyObject *args){
/*
The function PyArg_ParseTuple() in the Python API
checks the argument types and converts them to C values.
PyArg_ParseTuple() returns true (nonzero)
O - get one argument as a sequence
*/
PyObject *listObj;
if(!PyArg_ParseTuple(args, "O", &listObj)){
return 0;
}
listObj = PySequence_Fast(listObj, "argument must be iterable");
if(!listObj)
return 0;
int nums_len = PyList_Size(listObj);
printf("List size = %d\n", nums_len);
PyObject *even_nums = PyList_New(0);
for(int i=0; i<nums_len; i++){
PyLongObject *item = PyList_GetItem(listObj, i);
long num = PyLong_AsLong(item);
int vOut = (int)num;
printf("index = %d | value = %d\n", i, vOut);
if(vOut % 2 == 0){
PyList_Append(even_nums, Py_BuildValue("i", vOut));
}
}
return even_nums;
}
static PyMethodDef DemoMethods[] = {
{"get_even", get_even, METH_VARARGS, "Find even numbers from given list"},
{NULL, NULL, 0, NULL} /* Sentinel */
};
static struct PyModuleDef my_demo_module = {
PyModuleDef_HEAD_INIT,
"my_demo_module", /* name of module and should be same as funnction */
"Module docs", /* module documentation, may be NULL */
-1, /* size of per-interpreter state of the module or -1 if the module keeps state in global variables. */
DemoMethods
};
PyMODINIT_FUNC PyInit_my_demo_module(void) {
PyObject *module = PyModule_Create(&my_demo_module);
return module;
}