-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmycpppymodule.cpp
More file actions
153 lines (119 loc) · 3.3 KB
/
Copy pathmycpppymodule.cpp
File metadata and controls
153 lines (119 loc) · 3.3 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// mycpppymodule.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include <Python.h>
#include <iostream>
#include "mycpppymodule.h"
// Copy & Pasted from...
// http://www.tutorialspoint.com/python/python_further_extensions.htm
// https://docs.python.org/2/extending/extending.html
// https://github.com/sushantkhurana/Python-C-Extension/blob/master/helloWorld.cpp
static PyObject *my_callback = nullptr;
static PyObject *my_set_callback(PyObject *self, PyObject *args)
{
PyObject *result = nullptr;
PyObject *temp = nullptr;
if (PyArg_ParseTuple(args, "O:set_callback", &temp))
{
if (!PyCallable_Check(temp))
{
PyErr_SetString(PyExc_TypeError, "parameter must be callable");
return nullptr;
}
Py_XINCREF(temp); /* Add a reference to new callback */
Py_XDECREF(my_callback); /* Dispose of previous callback */
my_callback = temp; /* Remember new callback */
/* Boilerplate to return "None" */
Py_INCREF(Py_None);
result = Py_None;
}
return result;
}
static PyObject* my_testcallback(PyObject* self)
{
int arg = 1234;
PyObject *arglist = nullptr;
PyObject *result = nullptr;
/* Time to call the callback */
arglist = Py_BuildValue("(i)", arg);
result = PyObject_CallObject(my_callback, arglist);
Py_DECREF(arglist);
if (result == NULL)
return NULL;
Py_DECREF(result);
return result;
}
static PyObject* mycpppymodule_callback(PyObject* self, PyObject* arg)
{
PyObject* result = PyObject_CallObject(arg, nullptr);
if (result != NULL)
Py_DECREF(result);
return result;
}
static PyObject *mycpppymodule_add(PyObject *self, PyObject *args)
{
int a = 0;
int b = 0;
if (!PyArg_ParseTuple(args, "ii", &a, &b))
{
return NULL;
}
return Py_BuildValue("i", a + b);
}
static PyObject *add_subtract(PyObject *self, PyObject *args)
{
int a = 0;
int b = 0;
if (!PyArg_ParseTuple(args, "ii", &a, &b))
{
return NULL;
}
return Py_BuildValue("ii", a + b, a - b);
}
static PyObject* mycpppymodule_helloworld(PyObject* self, PyObject *args)
{
std::string str = "hello, ";
char* name = nullptr;
if (!PyArg_ParseTuple(args, "s", &name))
{
return NULL;
}
str.append(name);
return Py_BuildValue("s", str.c_str());
}
static char mycpppymodule_docs[] = "helloworld(name): say hello to name!!\n";
static PyMethodDef mycpppymodule_funcs[] =
{
{ "helloworld", (PyCFunction)mycpppymodule_helloworld, METH_VARARGS, mycpppymodule_docs },
{ "add_subtract", add_subtract, METH_VARARGS, "receives and returns typle" },
{ "add", mycpppymodule_add, METH_VARARGS, "add only" },
{ "my_set_callback", my_set_callback, METH_VARARGS, "set callback" },
{ "my_testcallback", (PyCFunction)my_testcallback, METH_NOARGS, "call registered callback" },
{ NULL, NULL, 0, NULL }
};
extern "C"
{
MYCPPPYMODULE_API void initmycpppymodule(void);
}
MYCPPPYMODULE_API void initmycpppymodule(void)
{
auto pyObj = Py_InitModule3(
"mycpppymodule",
mycpppymodule_funcs,
"!!!docstring!!! Extension module example!");
}
/*
// This is an example of an exported variable
MYCPPPYMODULE_API int nmycpppymodule=0;
// This is an example of an exported function.
MYCPPPYMODULE_API int fnmycpppymodule(void)
{
return 42;
}
// This is the constructor of a class that has been exported.
// see mycpppymodule.h for the class definition
Cmycpppymodule::Cmycpppymodule()
{
return;
}
*/