-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathpython_test_mod.cc
More file actions
98 lines (91 loc) · 3.26 KB
/
python_test_mod.cc
File metadata and controls
98 lines (91 loc) · 3.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
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
// -*- c++ -*-
/*
* Copyright (c) 2010-2012, Jim Bosch
* All rights reserved.
*
* ndarray is distributed under a simple BSD-like license;
* see the LICENSE file that should be present in the root
* of the source distribution, or alternately available at:
* https://github.com/ndarray/ndarray
*/
#include "Python.h"
#include "numpy/arrayobject.h"
#include "ndarray/swig.h"
#include <cstddef>
#ifndef _MSC_VER
#pragma GCC diagnostic ignored "-Wuninitialized"
#endif
/* Define macros for things that changed between Python V2.7 and V3.2 */
#if PY_MAJOR_VERSION >= 3
#define MOD_INIT(name) PyMODINIT_FUNC PyInit_##name(void)
#define RETURN(value) return value
#else
#define MOD_INIT(name) PyMODINIT_FUNC init##name(void)
#define RETURN(value) return
#endif
template <typename T, int N>
static PyObject * passVector(PyObject * self, PyObject * args) {
ndarray::Vector<T,N> vector;
if (!PyArg_ParseTuple(args,"O&",ndarray::PyConverter< ndarray::Vector<T,N> >::fromPython,&vector))
return NULL;
return ndarray::PyConverter< ndarray::Vector<T,N> >::toPython(vector);
}
template <typename T, int N, int C>
static PyObject * passArray(PyObject * self, PyObject * args) {
ndarray::Array<T,N,C> array;
if (!PyArg_ParseTuple(args,"O&",ndarray::PyConverter< ndarray::Array<T,N,C> >::fromPython,&array))
return NULL;
return ndarray::PyConverter< ndarray::Array<T,N,C> >::toPython(array);
}
template <typename T, int N>
static PyObject * makeArray(PyObject * self, PyObject * args) {
ndarray::Vector<ndarray::Size,N> shape;
if (!PyArg_ParseTuple(
args,
"O&",
ndarray::PyConverter< ndarray::Vector<ndarray::Size,N> >::fromPython,
&shape)
) {
return NULL;
}
ndarray::Array<T,N,N> array = ndarray::allocate(shape);
array.deep() = static_cast<T>(0);
return ndarray::PyConverter< ndarray::Array<T,N,N> >::toPython(array);
}
static PyMethodDef methods[] = {
{"passFloatVector3",&passVector<double,3>,METH_VARARGS,NULL},
{"passFloatArray33",&passArray<double,3,3>,METH_VARARGS,NULL},
{"passConstFloatArray33",&passArray<double const,3,3>,METH_VARARGS,NULL},
{"passFloatArray30",&passArray<double,3,0>,METH_VARARGS,NULL},
{"makeFloatArray3",&makeArray<double,3>,METH_VARARGS,NULL},
{"passIntVector3",&passVector<int,3>,METH_VARARGS,NULL},
{"passIntArray33",&passArray<int,3,3>,METH_VARARGS,NULL},
{"passUIntArray33",&passArray<unsigned int,3,3>,METH_VARARGS,NULL},
{"passConstIntArray33",&passArray<int const,3,3>,METH_VARARGS,NULL},
{"passIntArray30",&passArray<int,3,0>,METH_VARARGS,NULL},
{"makeIntArray3",&makeArray<int,3>,METH_VARARGS,NULL},
{NULL}
};
/* Describe the properties of the module. */
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef testmodule = {
PyModuleDef_HEAD_INIT,
"ndarray_python_test",
"Test the ndarray python interface",
-1,
methods,
NULL, NULL, NULL, NULL
};
#endif
MOD_INIT(ndarray_python_test) {
import_array();
PyObject * module = Py_InitModule("python_test_mod",methods);
if (module == NULL) return;
#if PY_MAJOR_VERSION >= 3
PyObject * module;
module = PyModule_Create(&testmodule);
#else
Py_InitModule3("ndarray_python_test", methods, "Test ndarray python interface");
#endif
RETURN(module);
}