aboutsummaryrefslogtreecommitdiff
blob: 95d40d93ebf46db11a107291e45f395411088404 (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
#include <Python.h>
#include <stdio.h>
#include <string.h>
#include "interpreter.h"

/**
 * \internal
 * Stores modules in a linked list so that they don't have to be reloaded every time.
 */
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);
}

/**
 * \internal
 * A Python interpreter object keeps the context like the loaded modules.
 */
struct Interpreter
{
	PyObjectList *modules;
} *in = 0;


/** \defgroup layman_base Layman base
 * 
 * \brief Layman Base functions
 *
 * These functions are used when you want to begin or end using the library.
 */

/** \addtogroup layman_base
 * @{
 */

/**
 * This is the first function that must be called before using the library.
 * It initializes the Python interpreter.
 */
void laymanInit()
{
	if (in)
		return;

	if (!Py_IsInitialized())
		Py_Initialize();

	in = malloc(sizeof(struct Interpreter));
	in->modules = createObjectList();
}

/**
 * Call this function when you're done using the library.
 * It will free memory.
 */
void laymanFinalize()
{
	if (!in)
		return;
	freeList(in->modules, 1);
	free(in);

	if (Py_IsInitialized())
		Py_Finalize();
}

/** @} */

/**
 * \internal
 * printf() like function that executes a python function
 *
 * \param module name of the python module in which the function is
 * \param funcName the function name to call
 * \param format printf() like list of arguments. See Python documentation
 * \param ... arguments for the function
 *
 * \return the result of the execution. Can be NULL if the call failed.
 */
PyObject *executeFunction(const char *module, const char *funcName, const char* format, ...)
{
	assert(in);

	// Make argument list
	PyObject *args;
	if (format == NULL || strcmp(format, "") == 0)
		args = PyTuple_New(0);
	else
	{
		va_list listArgs;
		va_start(listArgs, format);

		args = Py_VaBuildValue(format, listArgs);
	}

	// Look for the module.
	PyObject *mod = 0;
	if (in->modules)
	{
		mod = moduleNamed(module, in->modules);
	}
	// If module is not loaded yet, do it.
	if (!mod)
	{
		mod = PyImport_ImportModule(module);
		if (!mod)
			return NULL;
		insert(in->modules, mod);
	}

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

	// Execute it
	PyObject *val = PyObject_CallObject(func, args);

	if (args != NULL)
	{
		Py_DECREF(args);
	}

	return val;
}