-
Notifications
You must be signed in to change notification settings - Fork 8
/
hellomodule.c
44 lines (36 loc) · 942 Bytes
/
hellomodule.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
#include <Python.h>
static PyObject* greet(PyObject* self, PyObject* args)
{
const char* name;
/* Parse the input, from Python string to C string */
if (!PyArg_ParseTuple(args, "s", &name))
return NULL;
/* If the above function returns -1, an appropriate Python exception will
* have been set, and the function simply returns NULL
*/
printf("Hello %s\n", name);
/* Returns a None Python object */
Py_RETURN_NONE;
}
/* Define functions in module */
static PyMethodDef HelloMethods[] = {
{"greet", greet, METH_VARARGS, "Greet somebody (in C)."},
{NULL, NULL, 0, NULL} /* Sentinel */
};
/* Create PyModuleDef stucture */
static struct PyModuleDef helloStruct = {
PyModuleDef_HEAD_INIT,
"hello",
"",
-1,
HelloMethods,
NULL,
NULL,
NULL,
NULL
};
/* Module initialization */
PyObject *PyInit_hello(void)
{
return PyModule_Create(&helloStruct);
}