-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.c
84 lines (74 loc) · 1.74 KB
/
list.c
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <Python.h>
char * Cfib(int n)
{
char *letter = malloc(sizeof(char));
*letter = 'f';
return letter;
}
static PyObject * bfList(long x, long y)
{
PyObject *nodesPyList = PyList_New(2);
int i;
PyList_SET_ITEM(nodesPyList, 0, PyInt_FromLong(x));
PyList_SET_ITEM(nodesPyList, 1, PyInt_FromLong(y));
return nodesPyList;
}
static PyObject * makeList(PyObject *self, PyObject *args)
{
int length = 5;
PyObject *list;
int i;
for (i = 0; i < length; ++i) {
PyList_SET_ITEM(list, i, bfList((long) i + 3, (long) i * 2));
}
return list;
}
int other(int n, char *ptr)
{
fprintf(stderr, "%d Char is: %c\n", n, *ptr);
return 100;
}
static PyObject * fib(PyObject *self, PyObject *args)
{
fprintf(stderr, "here\n");
int n;
if (!PyArg_ParseTuple(args, "i", &n)) {
return NULL;
} else {
fprintf(stderr, "%d\n", n);
return Py_BuildValue("k", Cfib(n));
}
}
static PyObject * myOther(PyObject *self, PyObject *args)
{
int n;
char *p;
if (!PyArg_ParseTuple(args, "ik", &n, &p)) {
return NULL;
} else {
fprintf(stderr, "ptr val is %p\n", p);
fprintf(stderr, "ptr points to %c\n", *p);
return Py_BuildValue("i", other(n, p));
}
}
static PyObject * version(PyObject *self)
{
return Py_BuildValue("s", "Version 1.0");
}
static PyMethodDef myMethods[] = {
{"fib", fib, METH_VARARGS, "Calculates the fibonacci numbers."},
{"myOther", myOther, METH_VARARGS, "Gives pointer."},
{"version", (PyCFunction) version, METH_NOARGS, "returns the version."},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef myModule = {
PyModuleDef_HEAD_INIT,
"myFibModule",
"Fibonacci Module",
-1, // global state
myMethods
};
PyMODINIT_FUNC PyInit_myFibModule(void)
{
return PyModule_Create(&myModule);
}