summaryrefslogtreecommitdiffstats
path: root/src/interpreter.c
blob: 5d5bca59dfc73f6417740679630632878eb7e4a9 (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * Compile command :
 * gcc -o interpreter -W -Wall -g --std=c99 -I/usr/include/python3.1/ -lpython3.1 interpreter.c
 */
#include <Python.h>
#include <stdio.h>
#include <string.h>

/*
 * PyObjectList
 */
typedef struct PyObjectListElem
{
	PyObject *object;
	struct PyObjectListElem *next;
} PyObjectListElem;

typedef struct PyObjectList
{
	PyObjectListElem *root;
	int count;
} PyObjectList;

PyObjectList *createObjectList()
{
	PyObjectList *ret = malloc(sizeof(PyObjectList));
	ret->count = 0;
	ret->root = 0;
	return ret;
}

void insert(PyObjectList* list, PyObject *object)
{
	if (!list || !object)
		return;
	PyObjectListElem *node = malloc(sizeof(PyObjectListElem));
	node->object = object;
	node->next = list->root;
	list->root = node;
	list->count++;
}

PyObject *moduleNamed(const char *name, PyObjectList *list)
{
	PyObjectListElem *node = list->root;
	while (node)
	{
		if (strcmp(PyModule_GetName(node->object), name) == 0)
			return node->object;
		node = node->next;
	}
	
	return NULL;
}

int listCount(PyObjectList *list)
{
	return (list ? list->count : 0);
}

void freeList(PyObjectList *list, int deref)
{
	if (!list)
		return;

	PyObjectListElem *node = list->root;
	while (node)
	{
		PyObjectListElem *tmp = node;
		node = node->next;
		if (deref)
			Py_DECREF(tmp->object);
		free(tmp);
	}

	free(list);
}

/*
 * Interpreter
 *
 * A Python interpreter object keeps the context like the loaded modules.
 */

typedef struct Interpreter
{
	PyObjectList *modules;
} Interpreter;

Interpreter *createInterpreter()
{
	Interpreter *ret = malloc(sizeof(Interpreter));
	ret->modules = createObjectList();
	return ret;
}

void freeInterpreter(Interpreter *inter)
{
	if (!inter)
		return;
	freeList(inter->modules, 1);
	free(inter);

	Py_Finalize();
}

/*
 * printf() like function that executes a python function
 * @param interpreter Python interpreter object on which the function should be ran
 * @param module name of the python module in which the function is
 * @param funcName the function name to call
 * @param arg_types printf() like list of arguments. See Python documentation
 * @param ... arguments for the function
 * FIXME:why are default arguments necessary ?
 */

PyObject *executeFunction(Interpreter *interpreter, const char *module, const char *funcName, const char* format, ...)
{
	if (!Py_IsInitialized())
		Py_Initialize();

	// Make argument list
	PyObject *args;
	if (format == NULL)
		args = Py_None;
	else
	{
		va_list listArgs;
		va_start(listArgs, format);

		args = Py_VaBuildValue(format, listArgs);
	}

	// Look for the module.
	PyObject *mod = 0;
	if (interpreter->modules)
	{
		mod = moduleNamed(module, interpreter->modules);
	}
	if (!mod)
	{
		mod = PyImport_ImportModule(module);
		if (!mod)
			return NULL;
		insert(interpreter->modules, mod);
	}

	/*printf("mod: %p ", mod);
	PyObject_Print(mod, stdout, 0);
	printf("\n");*/

	// Look for the function
	PyObject *func = PyObject_GetAttrString(mod, funcName);
	if (!PyCallable_Check(func))
		return NULL;

	// Call the function
	/*printf("func: %p\n", func);
	PyObject_Print(func, stdout, 0);
	printf("\n");*/

	PyObject *val = PyObject_Call(func, args, NULL);

	// Add return value object in a local list so it can be DECREF'ed when the interpreter is deleted.
	// TODO
	Py_DECREF(args);

	return val;
}
/*
PyObject *executeMethod(PyObject *object, const char *methName, const char* format, ...)
{
	if (!Py_IsInitialized())
		Py_Initialize();

	// Make argument list
	PyObject *args;
	if (format == NULL)
		args = Py_None;
	else
	{
		va_list listArgs;
		va_start(listArgs, format);

		args = Py_VaBuildValue(format, listArgs);
	}

	PyObject *ret = PyObject_CallMethod(object, methName, )
}*/

int main(int argc, char *argv[])
{
	Interpreter *in = createInterpreter();

	executeFunction(in, "portage.data", "portage_group_warning", NULL);

	PyObject *arg1 = executeFunction(in, "portage", "pkgsplit", "sI", "app-portage/kuroo4-4.2", 1);
	PyObject *arg2 = executeFunction(in, "portage", "pkgsplit", "sI", "app-portage/kuroo4-4.1", 1);
	PyObject *ret = executeFunction(in, "portage", "pkgcmp", "OO", arg1, arg2);

	if (!ret)
		printf("failed :-( \n");
	else
		printf("Return %ld\n", PyLong_AsLong(ret));

	Py_DECREF(ret);
	Py_DECREF(arg1);
	Py_DECREF(arg2);
	
	PyObject *tbz = executeFunction(in, "portage.output", "ProgressBar", "sIs", "titre", 100, "status");
	ret = PyObject_CallMethod(tbz, "inc", "I", 25);
	Py_DECREF(tbz);
	Py_DECREF(ret);

	freeInterpreter(in);

	return 0;
}