-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTestPyInclude.cxx
More file actions
53 lines (38 loc) · 1.28 KB
/
TestPyInclude.cxx
File metadata and controls
53 lines (38 loc) · 1.28 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
#include <Python.h>
#include <stdio.h>
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <numpy/arrayobject.h>
float Array [] = {1.2, 3.4, 5.6, 7.8};
// taken from: https://stackoverflow.com/questions/30388170/sending-a-c-array-to-python-and-back-extending-c-with-numpy
//int main (int argc, char *argv[])
int TestPyInclude()
{
float *ptr = Array;
PyObject *pName, *pModule, *pDict, *pFunc, *pArgs;
npy_intp dims[1] = { 4 };
PyObject *py_array;
setenv("PYTHONPATH",".",1);
Py_Initialize ();
pName = PyUnicode_FromString ("TestPyInclude");
pModule = PyImport_Import(pName);
pDict = PyModule_GetDict(pModule);
import_array ();
py_array = PyArray_SimpleNewFromData(1, dims, NPY_FLOAT, ptr);
pArgs = PyTuple_New (1);
PyTuple_SetItem (pArgs, 0, py_array);
pFunc = PyDict_GetItemString (pDict, (char*)"pyArray");
if (PyCallable_Check (pFunc))
{
PyObject_CallObject(pFunc, pArgs);
} else
{
cout << "Function is not callable !" << endl;
}
Py_DECREF(pName);
Py_DECREF (py_array);
Py_DECREF (pModule);
Py_DECREF (pDict);
Py_DECREF (pFunc);
Py_Finalize ();
return 0;
}