-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhello.cpp
More file actions
63 lines (39 loc) · 1.26 KB
/
hello.cpp
File metadata and controls
63 lines (39 loc) · 1.26 KB
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
#include <Python.h>
// Step 1. Define functions that your module will contain
//-----------------------------------------------------------------------------
static PyObject *sayHello(PyObject *self, PyObject *args)
{
// Unpack a string from the arguments
const char *strArg;
if (!PyArg_ParseTuple(args, "s", &strArg))
return NULL;
// Print message and return None
PySys_WriteStdout("Hello, %s!\n", strArg);
Py_RETURN_NONE;
}
// Step 2. Package them together as an array of PyMethodDefs
//-----------------------------------------------------------------------------
static PyMethodDef functions[] = {
{
"sayHello",
sayHello,
METH_VARARGS,
"Prints back 'Hello <param>', for example example: hello.sayHello('you')"
},
{NULL, NULL, 0, NULL} /* Sentinel */
};
// Step 3. Define the hello module.
//-----------------------------------------------------------------------------
static struct PyModuleDef hello_module_def = {
PyModuleDef_HEAD_INIT,
"hello",
"Internal 'hello' module",
-1,
functions
};
// Step 4. Not sure how this is different from step 3.
//-----------------------------------------------------------------------------
PyMODINIT_FUNC PyInit_hello(void)
{
return PyModule_Create(&hello_module_def);
}