-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
48 lines (40 loc) · 1.09 KB
/
main.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
#include <Python.h>
// ===== START PYTHON PART =====
/* Will come from go */
PyObject* command(PyObject* , PyObject*);
/*
To shim go's missing variadic function support.
Ref https://docs.python.org/3/c-api/arg.html
Reminder from docs about memory management when parsing arguments:
> In general, when a format sets a pointer to a buffer,
> the buffer is managed by the corresponding Python object,
> and the buffer shares the lifetime of this object. You
> won’t have to release any memory yourself. The only
> exceptions are es, es#, et and et#.
*/
int PyArg_ParseTuple_ss(PyObject* args, char** a, char** b) {
return PyArg_ParseTuple(args, "ss", a, b);
}
static struct PyMethodDef methods[] = {
{
"command",
(PyCFunction)command,
METH_VARARGS,
"Runs a command."
},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef module = {
PyModuleDef_HEAD_INIT,
"_playground",
"Go functions for Python",
-1,
methods
};
PyMODINIT_FUNC PyInit__playground(void) {
PyObject *m;
m = PyModule_Create(&module);
if (m == NULL)
return NULL;
return m;
}