forked from fpoli/python-c-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhellomodule.c
More file actions
31 lines (25 loc) · 757 Bytes
/
hellomodule.c
File metadata and controls
31 lines (25 loc) · 757 Bytes
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
#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 */
};
/* Module initialization */
PyMODINIT_FUNC
inithello(void)
{
(void) Py_InitModule("hello", HelloMethods);
}