-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample2.cpp
More file actions
38 lines (33 loc) · 989 Bytes
/
example2.cpp
File metadata and controls
38 lines (33 loc) · 989 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
32
33
34
35
36
37
38
#include <Python.h>
// 函数的实现
static PyObject* add(PyObject* self, PyObject* args) {
int i, j;
if (!PyArg_ParseTuple(args, "ii", &i, &j))
return NULL;
return PyLong_FromLong(i + j);
}
// 方法表
static PyMethodDef ExampleMethods[] = {
{"add", add, METH_VARARGS, "Add two numbers."},
{NULL, NULL, 0, NULL} /* Sentinel */
};
// static PyModuleDef_Slot slots[] = {
// {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
// {0, NULL}
// };
// 模块定义
static struct PyModuleDef examplemodule = {
PyModuleDef_HEAD_INIT,
"example2", /* name of module */
NULL, /* module documentation, may be NULL */
-1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
ExampleMethods,
NULL,
NULL,
NULL,
NULL
};
// 模块初始化函数
PyMODINIT_FUNC PyInit_example2(void) {
return PyModule_Create(&examplemodule);
}