1 | /*\file FetchPythonData.cpp:
|
---|
2 | *\brief: general I/O interface to fetch data in python
|
---|
3 | */
|
---|
4 |
|
---|
5 | #ifdef HAVE_CONFIG_H
|
---|
6 | #include <config.h>
|
---|
7 | #else
|
---|
8 | #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
|
---|
9 | #endif
|
---|
10 |
|
---|
11 | #define PY_ARRAY_UNIQUE_SYMBOL PythonIOSymbol
|
---|
12 | #define NO_IMPORT
|
---|
13 |
|
---|
14 | #include "./pythonio.h"
|
---|
15 | #include "../../c/shared/shared.h"
|
---|
16 |
|
---|
17 | /*Primitive data types*/
|
---|
18 | /*FUNCTION FetchData(double* pscalar,PyObject* py_float){{{*/
|
---|
19 | void FetchData(double* pscalar,PyObject* py_float){
|
---|
20 |
|
---|
21 | double dscalar;
|
---|
22 |
|
---|
23 | /*return internal value: */
|
---|
24 | #if _PYTHON_MAJOR_ == 3
|
---|
25 | if (PyFloat_Check(py_float))
|
---|
26 | dscalar=PyFloat_AsDouble(py_float);
|
---|
27 | else if (PyLong_Check(py_float))
|
---|
28 | dscalar=(double)PyLong_AsLong(py_float);
|
---|
29 | else if (PyBool_Check(py_float))
|
---|
30 | dscalar=(double)PyLong_AsLong(py_float);
|
---|
31 | else if (PyTuple_Check(py_float) && (int)PyTuple_Size(py_float)==1)
|
---|
32 | FetchData(&dscalar,PyTuple_GetItem(py_float,(Py_ssize_t)0));
|
---|
33 | else if (PyList_Check(py_float) && (int)PyList_Size(py_float)==1)
|
---|
34 | FetchData(&dscalar,PyList_GetItem(py_float,(Py_ssize_t)0));
|
---|
35 | else
|
---|
36 | _error_("unrecognized float type py3 in input!");
|
---|
37 |
|
---|
38 | #else
|
---|
39 | if (PyFloat_Check(py_float))
|
---|
40 | dscalar=PyFloat_AsDouble(py_float);
|
---|
41 | else if (PyLong_Check(py_float))
|
---|
42 | dscalar=PyLong_AsDouble(py_float);
|
---|
43 | else if (PyInt_Check(py_float))
|
---|
44 | dscalar=(double)PyInt_AsLong(py_float);
|
---|
45 | else if (PyBool_Check(py_float))
|
---|
46 | dscalar=(double)PyLong_AsLong(py_float);
|
---|
47 | else if (PyTuple_Check(py_float) && (int)PyTuple_Size(py_float)==1)
|
---|
48 | FetchData(&dscalar,PyTuple_GetItem(py_float,(Py_ssize_t)0));
|
---|
49 | else if (PyList_Check(py_float) && (int)PyList_Size(py_float)==1)
|
---|
50 | FetchData(&dscalar,PyList_GetItem(py_float,(Py_ssize_t)0));
|
---|
51 | else
|
---|
52 | _error_("unrecognized float type in py2 input!");
|
---|
53 | #endif
|
---|
54 | /*output: */
|
---|
55 | *pscalar=dscalar;
|
---|
56 | }
|
---|
57 | /*}}}*/
|
---|
58 | /*FUNCTION FetchData(float* pscalar,PyObject* py_float){{{*/
|
---|
59 | void FetchData(float* pscalar,PyObject* py_float){
|
---|
60 |
|
---|
61 | float fscalar;
|
---|
62 |
|
---|
63 | /*return internal value: */
|
---|
64 | #if _PYTHON_MAJOR_ == 3
|
---|
65 | if (PyFloat_Check(py_float))
|
---|
66 | fscalar=PyFloat_AsDouble(py_float);
|
---|
67 | else if (PyLong_Check(py_float))
|
---|
68 | fscalar=(float)PyLong_AsLong(py_float);
|
---|
69 | else if (PyBool_Check(py_float))
|
---|
70 | fscalar=(float)PyLong_AsLong(py_float);
|
---|
71 | else if (PyTuple_Check(py_float) && (int)PyTuple_Size(py_float)==1)
|
---|
72 | FetchData(&fscalar,PyTuple_GetItem(py_float,(Py_ssize_t)0));
|
---|
73 | else if (PyList_Check(py_float) && (int)PyList_Size(py_float)==1)
|
---|
74 | FetchData(&fscalar,PyList_GetItem(py_float,(Py_ssize_t)0));
|
---|
75 | else
|
---|
76 | _error_("unrecognized float type in input!");
|
---|
77 | #else
|
---|
78 | if (PyFloat_Check(py_float))
|
---|
79 | fscalar=PyFloat_AsDouble(py_float);
|
---|
80 | else if (PyLong_Check(py_float))
|
---|
81 | fscalar=(float)PyLong_AsDouble(py_float);
|
---|
82 | else if (PyInt_Check(py_float))
|
---|
83 | fscalar=(float)PyInt_AsLong(py_float);
|
---|
84 | else if (PyBool_Check(py_float))
|
---|
85 | fscalar=(float)PyLong_AsLong(py_float);
|
---|
86 | else if (PyTuple_Check(py_float) && (int)PyTuple_Size(py_float)==1)
|
---|
87 | FetchData(&fscalar,PyTuple_GetItem(py_float,(Py_ssize_t)0));
|
---|
88 | else if (PyList_Check(py_float) && (int)PyList_Size(py_float)==1)
|
---|
89 | FetchData(&fscalar,PyList_GetItem(py_float,(Py_ssize_t)0));
|
---|
90 | else
|
---|
91 | _error_("unrecognized float type in input!");
|
---|
92 | #endif
|
---|
93 | /*output: */
|
---|
94 | *pscalar=fscalar;
|
---|
95 | }
|
---|
96 | /*}}}*/
|
---|
97 | /*FUNCTION FetchData(int* pscalar,PyObject* py_long){{{*/
|
---|
98 | void FetchData(int* pscalar, PyObject* py_long){
|
---|
99 |
|
---|
100 | int iscalar;
|
---|
101 |
|
---|
102 | /*return internal value: */
|
---|
103 | #if _PYTHON_MAJOR_ == 3
|
---|
104 | if (PyLong_Check(py_long))
|
---|
105 | iscalar=(int)PyLong_AsLong(py_long);
|
---|
106 | else if (PyFloat_Check(py_long))
|
---|
107 | iscalar=(int)PyFloat_AsDouble(py_long);
|
---|
108 | else if (PyBool_Check(py_long))
|
---|
109 | iscalar=(int)PyLong_AsLong(py_long);
|
---|
110 | else if (PyTuple_Check(py_long) && (int)PyTuple_Size(py_long)==1)
|
---|
111 | FetchData(&iscalar,PyTuple_GetItem(py_long,(Py_ssize_t)0));
|
---|
112 | else if (PyList_Check(py_long) && (int)PyList_Size(py_long)==1)
|
---|
113 | FetchData(&iscalar,PyList_GetItem(py_long,(Py_ssize_t)0));
|
---|
114 | else
|
---|
115 | _error_("unrecognized long type in input!");
|
---|
116 |
|
---|
117 | #else
|
---|
118 | if (PyLong_Check(py_long))
|
---|
119 | iscalar=(int)PyLong_AsLong(py_long);
|
---|
120 | else if (PyInt_Check(py_long))
|
---|
121 | iscalar=(int)PyInt_AsLong(py_long);
|
---|
122 | else if (PyFloat_Check(py_long))
|
---|
123 | iscalar=(int)PyFloat_AsDouble(py_long);
|
---|
124 | else if (PyBool_Check(py_long))
|
---|
125 | iscalar=(int)PyLong_AsLong(py_long);
|
---|
126 | else if (PyTuple_Check(py_long) && (int)PyTuple_Size(py_long)==1)
|
---|
127 | FetchData(&iscalar,PyTuple_GetItem(py_long,(Py_ssize_t)0));
|
---|
128 | else if (PyList_Check(py_long) && (int)PyList_Size(py_long)==1)
|
---|
129 | FetchData(&iscalar,PyList_GetItem(py_long,(Py_ssize_t)0));
|
---|
130 | else
|
---|
131 | _error_("unrecognized long type in input!");
|
---|
132 | #endif
|
---|
133 | /*output: */
|
---|
134 | *pscalar=iscalar;
|
---|
135 | }
|
---|
136 | /*}}}*/
|
---|
137 | /*FUNCTION FetchData(bool* pscalar,PyObject* py_boolean){{{*/
|
---|
138 | void FetchData(bool* pscalar,PyObject* py_boolean){
|
---|
139 |
|
---|
140 | bool bscalar;
|
---|
141 |
|
---|
142 | /*return internal value: */
|
---|
143 | #if _PYTHON_MAJOR_ == 3
|
---|
144 | if (PyBool_Check(py_boolean))
|
---|
145 | bscalar=(bool)PyLong_AsLong(py_boolean);
|
---|
146 | else if (PyLong_Check(py_boolean))
|
---|
147 | bscalar=(bool)PyLong_AsLong(py_boolean);
|
---|
148 | else if (PyLong_Check(py_boolean))
|
---|
149 | bscalar=(bool)PyLong_AsLong(py_boolean);
|
---|
150 | else if (PyTuple_Check(py_boolean) && (int)PyTuple_Size(py_boolean)==1)
|
---|
151 | FetchData(&bscalar,PyTuple_GetItem(py_boolean,(Py_ssize_t)0));
|
---|
152 | else if (PyList_Check(py_boolean) && (int)PyList_Size(py_boolean)==1)
|
---|
153 | FetchData(&bscalar,PyList_GetItem(py_boolean,(Py_ssize_t)0));
|
---|
154 | else
|
---|
155 | _error_("unrecognized boolean type in input!");
|
---|
156 |
|
---|
157 | #else
|
---|
158 | if (PyBool_Check(py_boolean))
|
---|
159 | bscalar=(bool)PyLong_AsLong(py_boolean);
|
---|
160 | else if (PyLong_Check(py_boolean))
|
---|
161 | bscalar=(bool)PyLong_AsLong(py_boolean);
|
---|
162 | else if (PyLong_Check(py_boolean))
|
---|
163 | bscalar=(bool)PyLong_AsLong(py_boolean);
|
---|
164 | else if (PyInt_Check(py_boolean))
|
---|
165 | bscalar=(bool)PyInt_AsLong(py_boolean);
|
---|
166 | else if (PyTuple_Check(py_boolean) && (int)PyTuple_Size(py_boolean)==1)
|
---|
167 | FetchData(&bscalar,PyTuple_GetItem(py_boolean,(Py_ssize_t)0));
|
---|
168 | else if (PyList_Check(py_boolean) && (int)PyList_Size(py_boolean)==1)
|
---|
169 | FetchData(&bscalar,PyList_GetItem(py_boolean,(Py_ssize_t)0));
|
---|
170 | else
|
---|
171 | _error_("unrecognized boolean type in input!");
|
---|
172 | #endif
|
---|
173 | /*output: */
|
---|
174 | *pscalar=bscalar;
|
---|
175 | }
|
---|
176 | /*}}}*/
|
---|
177 | /*FUNCTION FetchData(double** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
|
---|
178 | void FetchData(double** pmatrix,int* pM,int *pN,PyObject* py_matrix){
|
---|
179 |
|
---|
180 | /*output: */
|
---|
181 | double* dmatrix=NULL;
|
---|
182 | double* matrix=NULL;
|
---|
183 | int M=0;
|
---|
184 | int N=0;
|
---|
185 | int ndim;
|
---|
186 | npy_intp* dims=NULL;
|
---|
187 |
|
---|
188 | /*intermediary:*/
|
---|
189 | long* lmatrix=NULL;
|
---|
190 | bool* bmatrix=NULL;
|
---|
191 | int* imatrix=NULL;
|
---|
192 | float* smatrix=NULL;
|
---|
193 | int i;
|
---|
194 | PyObject* py_matrix2=NULL;
|
---|
195 |
|
---|
196 | if (PyArray_Check((PyArrayObject*)py_matrix)) {
|
---|
197 | /*retrieve dimensions: */
|
---|
198 | ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
|
---|
199 | if (ndim==2) {
|
---|
200 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
---|
201 | M=dims[0]; N=dims[1];
|
---|
202 | }
|
---|
203 | else if (ndim==1) {
|
---|
204 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
---|
205 | M=dims[0]; N=1;
|
---|
206 | }
|
---|
207 | else
|
---|
208 | _error_("expecting an MxN matrix or M vector in input!");
|
---|
209 |
|
---|
210 | if (M && N) {
|
---|
211 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_matrix)) {
|
---|
212 | py_matrix2=PyArray_ContiguousFromAny(py_matrix,NPY_DOUBLE,ndim,ndim);
|
---|
213 | py_matrix=py_matrix2;
|
---|
214 | }
|
---|
215 | if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_FLOAT) {
|
---|
216 | /*retrieve internal value: */
|
---|
217 | smatrix=(float*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
218 |
|
---|
219 | /*transform into double matrix: */
|
---|
220 | matrix=xNew<double>(M*N);
|
---|
221 | for(i=0;i<M*N;i++)matrix[i]=(double)smatrix[i];
|
---|
222 | }
|
---|
223 |
|
---|
224 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
|
---|
225 | /*retrieve internal value: */
|
---|
226 | dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
227 |
|
---|
228 | /*copy matrix: */
|
---|
229 | matrix=xNew<double>(M*N);
|
---|
230 | memcpy(matrix,dmatrix,(M*N)*sizeof(double));
|
---|
231 | }
|
---|
232 |
|
---|
233 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_LONG) {
|
---|
234 | /*retrieve internal value: */
|
---|
235 | lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
236 |
|
---|
237 | /*transform into double matrix: */
|
---|
238 | matrix=xNew<double>(M*N);
|
---|
239 | for(i=0;i<M*N;i++)matrix[i]=(double)lmatrix[i];
|
---|
240 | }
|
---|
241 |
|
---|
242 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
|
---|
243 | /*retrieve internal value: */
|
---|
244 | bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
245 |
|
---|
246 | /*transform into double matrix: */
|
---|
247 | matrix=xNew<double>(M*N);
|
---|
248 | for(i=0;i<M*N;i++)matrix[i]=(double)bmatrix[i];
|
---|
249 | }
|
---|
250 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_INT32) {
|
---|
251 | /*retrieve internal value: */
|
---|
252 | imatrix=(int*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
253 |
|
---|
254 | /*transform into double matrix: */
|
---|
255 | matrix=xNew<double>(M*N);
|
---|
256 | for(i=0;i<M*N;i++)matrix[i]=(double)imatrix[i];
|
---|
257 | }
|
---|
258 |
|
---|
259 | else
|
---|
260 | _error_("unrecognized double pyarray type in input!");
|
---|
261 |
|
---|
262 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
263 | }
|
---|
264 | else
|
---|
265 | matrix=NULL;
|
---|
266 | }
|
---|
267 | else if (PyList_Check(py_matrix)) {
|
---|
268 | /*retrieve dimensions: */
|
---|
269 | M=(int)PyList_Size(py_matrix);
|
---|
270 | N=1;
|
---|
271 | if (M) {
|
---|
272 | matrix=xNew<double>(M);
|
---|
273 | for (int index = 0; index < M; index++) {
|
---|
274 | PyObject *item;
|
---|
275 | item = PyList_GetItem(py_matrix, index);
|
---|
276 | if ((int)PyList_Size(item)>1)
|
---|
277 | _error_("2D lists are not suported");
|
---|
278 | FetchData(&(matrix[index]),item);
|
---|
279 | }
|
---|
280 | }
|
---|
281 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
282 | else
|
---|
283 | matrix=NULL;
|
---|
284 | }
|
---|
285 | else if (PyTuple_Check(py_matrix)) {
|
---|
286 | /*retrieve dimensions: */
|
---|
287 | M=(int)PyTuple_Size(py_matrix);
|
---|
288 | N=1;
|
---|
289 | if (M) {
|
---|
290 | matrix=xNew<double>(M);
|
---|
291 | for (int index = 0; index < M; index++) {
|
---|
292 | PyObject *item;
|
---|
293 | item = PyTuple_GetItem(py_matrix, index);
|
---|
294 | if ((int)PyTuple_Size(item)>1)
|
---|
295 | _error_("2D tuple are not suported");
|
---|
296 | FetchData(&(matrix[index]),item);
|
---|
297 | }
|
---|
298 | }
|
---|
299 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
300 | else
|
---|
301 | matrix=NULL;
|
---|
302 | }
|
---|
303 |
|
---|
304 | else {
|
---|
305 | M=1;
|
---|
306 | N=1;
|
---|
307 | matrix=xNew<double>(M*N);
|
---|
308 | FetchData(&(matrix[0]),py_matrix);
|
---|
309 | }
|
---|
310 |
|
---|
311 | /*output: */
|
---|
312 | if(pM)*pM=M;
|
---|
313 | if(pN)*pN=N;
|
---|
314 | if(pmatrix)*pmatrix=matrix;
|
---|
315 | }
|
---|
316 | /*}}}*/
|
---|
317 | /*FUNCTION FetchData(int** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
|
---|
318 | void FetchData(int** pmatrix,int* pM,int *pN,PyObject* py_matrix){
|
---|
319 |
|
---|
320 | /*output: */
|
---|
321 | int* matrix=NULL;
|
---|
322 | int M=0;
|
---|
323 | int N=0;
|
---|
324 | int ndim;
|
---|
325 | npy_intp* dims=NULL;
|
---|
326 |
|
---|
327 | /*intermediary:*/
|
---|
328 | double* dmatrix=NULL;
|
---|
329 | long* lmatrix=NULL;
|
---|
330 | bool* bmatrix=NULL;
|
---|
331 | int i;
|
---|
332 | PyObject* py_matrix2=NULL;
|
---|
333 |
|
---|
334 | if (PyArray_Check((PyArrayObject*)py_matrix)) {
|
---|
335 | /*retrieve dimensions: */
|
---|
336 | ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
|
---|
337 | if (ndim==2) {
|
---|
338 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
---|
339 | M=dims[0]; N=dims[1];
|
---|
340 | }
|
---|
341 | else if (ndim==1) {
|
---|
342 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
---|
343 | M=dims[0]; N=1;
|
---|
344 | }
|
---|
345 | else
|
---|
346 | _error_("expecting an MxN matrix or M vector in input!");
|
---|
347 |
|
---|
348 | if (M && N) {
|
---|
349 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_matrix)) {
|
---|
350 | py_matrix2=PyArray_ContiguousFromAny(py_matrix,NPY_LONG,ndim,ndim);
|
---|
351 | py_matrix=py_matrix2;
|
---|
352 | }
|
---|
353 |
|
---|
354 | if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
|
---|
355 | /*retrieve internal value: */
|
---|
356 | dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
357 |
|
---|
358 | /*transform into integer matrix: */
|
---|
359 | matrix=xNew<int>(M*N);
|
---|
360 | for(i=0;i<M*N;i++)matrix[i]=(int)dmatrix[i];
|
---|
361 | }
|
---|
362 |
|
---|
363 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_LONG) {
|
---|
364 | /*retrieve internal value: */
|
---|
365 | lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
366 |
|
---|
367 | /*transform into integer matrix: */
|
---|
368 | matrix=xNew<int>(M*N);
|
---|
369 | for(i=0;i<M*N;i++)matrix[i]=(int)lmatrix[i];
|
---|
370 | }
|
---|
371 |
|
---|
372 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
|
---|
373 | /*retrieve internal value: */
|
---|
374 | bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
375 |
|
---|
376 | /*transform into integer matrix: */
|
---|
377 | matrix=xNew<int>(M*N);
|
---|
378 | for(i=0;i<M*N;i++)matrix[i]=(int)bmatrix[i];
|
---|
379 | }
|
---|
380 |
|
---|
381 | else
|
---|
382 | _error_("unrecognized int pyarray type in input!");
|
---|
383 |
|
---|
384 | /* These lines are causing a segfault
|
---|
385 | if (py_matrix2)
|
---|
386 | delete(py_matrix2);
|
---|
387 | */
|
---|
388 | }
|
---|
389 | else
|
---|
390 | matrix=NULL;
|
---|
391 | }
|
---|
392 | else if (PyList_Check(py_matrix)) {
|
---|
393 | /*retrieve dimensions: */
|
---|
394 | M=(int)PyList_Size(py_matrix);
|
---|
395 | N=1;
|
---|
396 | if (M) {
|
---|
397 | matrix=xNew<int>(M);
|
---|
398 | for (int index = 0; index < M; index++) {
|
---|
399 | PyObject *item;
|
---|
400 | item = PyList_GetItem(py_matrix, index);
|
---|
401 | if ((int)PyList_Size(item)>1)
|
---|
402 | _error_("2D lists are not suported");
|
---|
403 | FetchData(&(matrix[index]),item);
|
---|
404 | }
|
---|
405 | }
|
---|
406 | else
|
---|
407 | matrix=NULL;
|
---|
408 | }
|
---|
409 | else if (PyTuple_Check(py_matrix)) {
|
---|
410 | /*retrieve dimensions: */
|
---|
411 | M=(int)PyTuple_Size(py_matrix);
|
---|
412 | N=1;
|
---|
413 | if (M) {
|
---|
414 | matrix=xNew<int>(M);
|
---|
415 | for (int index = 0; index < M; index++) {
|
---|
416 | PyObject *item;
|
---|
417 | item = PyTuple_GetItem(py_matrix, index);
|
---|
418 | if ((int)PyTuple_Size(item)>1)
|
---|
419 | _error_("2D tuple are not suported");
|
---|
420 | FetchData(&(matrix[index]),item);
|
---|
421 | }
|
---|
422 | }
|
---|
423 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
424 | else
|
---|
425 | matrix=NULL;
|
---|
426 | }
|
---|
427 |
|
---|
428 | else {
|
---|
429 | M=1;
|
---|
430 | N=1;
|
---|
431 | matrix=xNew<int>(M*N);
|
---|
432 | FetchData(&(matrix[0]),py_matrix);
|
---|
433 | }
|
---|
434 |
|
---|
435 | /*output: */
|
---|
436 | if(pM)*pM=M;
|
---|
437 | if(pN)*pN=N;
|
---|
438 | if(pmatrix)*pmatrix=matrix;
|
---|
439 | }
|
---|
440 | /*}}}*/
|
---|
441 | /*FUNCTION FetchData(bool** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
|
---|
442 | void FetchData(bool** pmatrix,int* pM,int *pN,PyObject* py_matrix){
|
---|
443 |
|
---|
444 | /*output: */
|
---|
445 | bool* bmatrix=NULL;
|
---|
446 | bool* matrix=NULL;
|
---|
447 | int M=0;
|
---|
448 | int N=0;
|
---|
449 | int ndim;
|
---|
450 | npy_intp* dims=NULL;
|
---|
451 |
|
---|
452 | /*intermediary:*/
|
---|
453 | double* dmatrix=NULL;
|
---|
454 | long* lmatrix=NULL;
|
---|
455 | int i;
|
---|
456 | PyObject* py_matrix2=NULL;
|
---|
457 |
|
---|
458 | if (PyArray_Check((PyArrayObject*)py_matrix)) {
|
---|
459 | /*retrieve dimensions: */
|
---|
460 | ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
|
---|
461 | if (ndim==2) {
|
---|
462 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
---|
463 | M=dims[0]; N=dims[1];
|
---|
464 | }
|
---|
465 | else if (ndim==1) {
|
---|
466 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
---|
467 | M=dims[0]; N=1;
|
---|
468 | }
|
---|
469 | else
|
---|
470 | _error_("expecting an MxN matrix or M vector in input!");
|
---|
471 |
|
---|
472 | if (M && N) {
|
---|
473 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_matrix)) {
|
---|
474 | py_matrix2=PyArray_ContiguousFromAny(py_matrix,NPY_BOOL,ndim,ndim);
|
---|
475 | py_matrix=py_matrix2;
|
---|
476 | }
|
---|
477 |
|
---|
478 | if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
|
---|
479 | /*retrieve internal value: */
|
---|
480 | dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
481 |
|
---|
482 | /*transform into bool matrix: */
|
---|
483 | matrix=xNew<bool>(M*N);
|
---|
484 | for(i=0;i<M*N;i++)matrix[i]=(bool)dmatrix[i];
|
---|
485 | }
|
---|
486 |
|
---|
487 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_LONG) {
|
---|
488 | /*retrieve internal value: */
|
---|
489 | lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
490 |
|
---|
491 | /*transform into bool matrix: */
|
---|
492 | matrix=xNew<bool>(M*N);
|
---|
493 | for(i=0;i<M*N;i++)matrix[i]=(bool)lmatrix[i];
|
---|
494 | }
|
---|
495 |
|
---|
496 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
|
---|
497 | /*retrieve internal value: */
|
---|
498 | bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
499 |
|
---|
500 | /*copy matrix: */
|
---|
501 | matrix=xNew<bool>(M*N);
|
---|
502 | memcpy(matrix,bmatrix,(M*N)*sizeof(bool));
|
---|
503 | }
|
---|
504 |
|
---|
505 | else
|
---|
506 | _error_("unrecognized bool pyarray type in input!");
|
---|
507 |
|
---|
508 | if (py_matrix2)
|
---|
509 | delete(py_matrix2);
|
---|
510 | }
|
---|
511 | else
|
---|
512 | matrix=NULL;
|
---|
513 | }
|
---|
514 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
515 |
|
---|
516 | else if (PyList_Check(py_matrix)) {
|
---|
517 | /*retrieve dimensions: */
|
---|
518 | M=(int)PyList_Size(py_matrix);
|
---|
519 | N=1;
|
---|
520 | if (M) {
|
---|
521 | matrix=xNew<bool>(M);
|
---|
522 | for (int index = 0; index < M; index++) {
|
---|
523 | PyObject *item;
|
---|
524 | item = PyList_GetItem(py_matrix, index);
|
---|
525 | if ((int)PyList_Size(item)>1)
|
---|
526 | _error_("2D lists are not suported");
|
---|
527 | FetchData(&(matrix[index]),item);
|
---|
528 | }
|
---|
529 | }
|
---|
530 | else
|
---|
531 | matrix=NULL;
|
---|
532 | }
|
---|
533 | else if (PyTuple_Check(py_matrix)) {
|
---|
534 | /*retrieve dimensions: */
|
---|
535 | M=(int)PyTuple_Size(py_matrix);
|
---|
536 | N=1;
|
---|
537 | if (M) {
|
---|
538 | matrix=xNew<bool>(M);
|
---|
539 | for (int index = 0; index < M; index++) {
|
---|
540 | PyObject *item;
|
---|
541 | item = PyTuple_GetItem(py_matrix, index);
|
---|
542 | if ((int)PyTuple_Size(item)>1)
|
---|
543 | _error_("2D tuples are not suported");
|
---|
544 | FetchData(&(matrix[index]),item);
|
---|
545 | }
|
---|
546 | }
|
---|
547 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
548 | else
|
---|
549 | matrix=NULL;
|
---|
550 | }
|
---|
551 |
|
---|
552 | else {
|
---|
553 | M=1;
|
---|
554 | N=1;
|
---|
555 | matrix=xNew<bool>(M*N);
|
---|
556 | FetchData(&(matrix[0]),py_matrix);
|
---|
557 | }
|
---|
558 |
|
---|
559 | /*output: */
|
---|
560 | if(pM)*pM=M;
|
---|
561 | if(pN)*pN=N;
|
---|
562 | if(pmatrix)*pmatrix=matrix;
|
---|
563 | }
|
---|
564 | /*}}}*/
|
---|
565 | /*FUNCTION FetchData(double** pvector,int* pM, PyObject* py_vector){{{*/
|
---|
566 | void FetchData(double** pvector,int* pM,PyObject* py_vector){
|
---|
567 |
|
---|
568 | /*output: */
|
---|
569 | double* dvector=NULL;
|
---|
570 | double* vector=NULL;
|
---|
571 | int M=0;
|
---|
572 | int ndim;
|
---|
573 | npy_intp* dims=NULL;
|
---|
574 |
|
---|
575 | /*intermediary:*/
|
---|
576 | long* lvector=NULL;
|
---|
577 | bool* bvector=NULL;
|
---|
578 | float* svector=NULL;
|
---|
579 | int i;
|
---|
580 | PyObject* py_vector2=NULL;
|
---|
581 |
|
---|
582 | if (PyArray_Check((PyArrayObject*)py_vector)) {
|
---|
583 | /*retrieve dimensions: */
|
---|
584 | ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
|
---|
585 | if (ndim==1) {
|
---|
586 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
587 | M=dims[0];
|
---|
588 | }
|
---|
589 | else if (ndim==2) {
|
---|
590 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
591 | if (dims[1]==1)
|
---|
592 | M=dims[0];
|
---|
593 | else
|
---|
594 | _error_("expecting an Mx1 matrix or M vector in input!");
|
---|
595 | }
|
---|
596 | else
|
---|
597 | _error_("expecting an Mx1 matrix or M vector in input!");
|
---|
598 |
|
---|
599 | if (M) {
|
---|
600 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_vector)) {
|
---|
601 | py_vector2=PyArray_ContiguousFromAny(py_vector,NPY_DOUBLE,ndim,ndim);
|
---|
602 | py_vector=py_vector2;
|
---|
603 | }
|
---|
604 |
|
---|
605 | if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_FLOAT) {
|
---|
606 | /*retrieve internal value: */
|
---|
607 | svector=(float*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
608 |
|
---|
609 | /*transform into double matrix: */
|
---|
610 | vector=xNew<double>(M);
|
---|
611 | for(i=0;i<M;i++)vector[i]=(double)svector[i];
|
---|
612 | }
|
---|
613 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
|
---|
614 | /*retrieve internal value: */
|
---|
615 | dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
616 |
|
---|
617 | /*copy vector: */
|
---|
618 | vector=xNew<double>(M);
|
---|
619 | memcpy(vector,dvector,(M)*sizeof(double));
|
---|
620 | }
|
---|
621 |
|
---|
622 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
|
---|
623 | /*retrieve internal value: */
|
---|
624 | lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
625 |
|
---|
626 | /*transform into double vector: */
|
---|
627 | vector=xNew<double>(M);
|
---|
628 | for(i=0;i<M;i++)vector[i]=(double)lvector[i];
|
---|
629 | }
|
---|
630 |
|
---|
631 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
|
---|
632 | /*retrieve internal value: */
|
---|
633 | bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
634 |
|
---|
635 | /*transform into double vector: */
|
---|
636 | vector=xNew<double>(M);
|
---|
637 | for(i=0;i<M;i++)vector[i]=(double)bvector[i];
|
---|
638 | }
|
---|
639 |
|
---|
640 | else
|
---|
641 | _error_("unrecognized double pyarray type in input!");
|
---|
642 |
|
---|
643 | /* Causing a seg fault.
|
---|
644 | if (py_vector2)
|
---|
645 | delete(py_vector2);
|
---|
646 | */
|
---|
647 | }
|
---|
648 | else
|
---|
649 | vector=NULL;
|
---|
650 | }
|
---|
651 | else if (PyList_Check(py_vector)) {
|
---|
652 | /*retrieve dimensions: */
|
---|
653 | M=(int)PyList_Size(py_vector);
|
---|
654 |
|
---|
655 | if (M) {
|
---|
656 | vector=xNew<double>(M);
|
---|
657 | for (int index = 0; index < M; index++) {
|
---|
658 | PyObject *item;
|
---|
659 | item = PyList_GetItem(py_vector, index);
|
---|
660 | FetchData(&(vector[index]),item);
|
---|
661 | }
|
---|
662 | }
|
---|
663 | else
|
---|
664 | vector=NULL;
|
---|
665 | }
|
---|
666 | else if (PyTuple_Check(py_vector)) {
|
---|
667 | /*retrieve dimensions: */
|
---|
668 | M=(int)PyTuple_Size(py_vector);
|
---|
669 |
|
---|
670 | if (M) {
|
---|
671 | vector=xNew<double>(M);
|
---|
672 | for (int index = 0; index < M; index++) {
|
---|
673 | PyObject *item;
|
---|
674 | item = PyTuple_GetItem(py_vector, index);
|
---|
675 | FetchData(&(vector[index]),item);
|
---|
676 | }
|
---|
677 | }
|
---|
678 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
679 | else
|
---|
680 | vector=NULL;
|
---|
681 | }
|
---|
682 |
|
---|
683 | else {
|
---|
684 | M=1;
|
---|
685 | vector=xNew<double>(M);
|
---|
686 | FetchData(&(vector[0]),py_vector);
|
---|
687 | }
|
---|
688 |
|
---|
689 | /*output: */
|
---|
690 | if(pM)*pM=M;
|
---|
691 | if(pvector)*pvector=vector;
|
---|
692 | }
|
---|
693 | /*}}}*/
|
---|
694 | /*FUNCTION FetchData(float** pvector,int* pM, PyObject* py_vector){{{*/
|
---|
695 | void FetchData(float** pvector,int* pM,PyObject* py_vector){
|
---|
696 |
|
---|
697 | /*output: */
|
---|
698 | float* vector=NULL;
|
---|
699 | int M=0;
|
---|
700 | int ndim;
|
---|
701 | npy_intp* dims=NULL;
|
---|
702 |
|
---|
703 | /*intermediary:*/
|
---|
704 | long* lvector=NULL;
|
---|
705 | bool* bvector=NULL;
|
---|
706 | double* dvector=NULL;
|
---|
707 | int i;
|
---|
708 | PyObject* py_vector2=NULL;
|
---|
709 |
|
---|
710 | if (PyArray_Check((PyArrayObject*)py_vector)) {
|
---|
711 | /*retrieve dimensions: */
|
---|
712 | ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
|
---|
713 | if (ndim==1) {
|
---|
714 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
715 | M=dims[0];
|
---|
716 | }
|
---|
717 | else if (ndim==2) {
|
---|
718 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
719 | if (dims[1]==1)
|
---|
720 | M=dims[0];
|
---|
721 | else
|
---|
722 | _error_("expecting an Mx1 matrix or M vector in input!");
|
---|
723 | }
|
---|
724 | else
|
---|
725 | _error_("expecting an Mx1 matrix or M vector in input!");
|
---|
726 |
|
---|
727 | if (M) {
|
---|
728 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_vector)) {
|
---|
729 | py_vector2=PyArray_ContiguousFromAny(py_vector,NPY_LONG,ndim,ndim);
|
---|
730 | py_vector=py_vector2;
|
---|
731 | }
|
---|
732 |
|
---|
733 | if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
|
---|
734 | /*retrieve internal value: */
|
---|
735 | dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
736 |
|
---|
737 | /*transform into int vector: */
|
---|
738 | vector=xNew<float>(M);
|
---|
739 | for(i=0;i<M;i++)vector[i]=(float)dvector[i];
|
---|
740 | }
|
---|
741 |
|
---|
742 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
|
---|
743 | /*retrieve internal value: */
|
---|
744 | lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
745 |
|
---|
746 | /*transform into int vector: */
|
---|
747 | vector=xNew<float>(M);
|
---|
748 | for(i=0;i<M;i++)vector[i]=(float)lvector[i];
|
---|
749 | }
|
---|
750 |
|
---|
751 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
|
---|
752 | /*retrieve internal value: */
|
---|
753 | bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
754 |
|
---|
755 | /*transform into int vector: */
|
---|
756 | vector=xNew<float>(M);
|
---|
757 | for(i=0;i<M;i++)vector[i]=(float)bvector[i];
|
---|
758 | }
|
---|
759 |
|
---|
760 | else
|
---|
761 | _error_("unrecognized int pyarray type in input!");
|
---|
762 |
|
---|
763 | if(py_vector2) delete(py_vector2);
|
---|
764 | }
|
---|
765 | else
|
---|
766 | vector=NULL;
|
---|
767 | }
|
---|
768 | else if (PyList_Check(py_vector)) {
|
---|
769 | /*retrieve dimensions: */
|
---|
770 | M=(int)PyList_Size(py_vector);
|
---|
771 |
|
---|
772 | if (M) {
|
---|
773 | vector=xNew<float>(M);
|
---|
774 | for (int index = 0; index < M; index++) {
|
---|
775 | PyObject *item;
|
---|
776 | item = PyList_GetItem(py_vector, index);
|
---|
777 | FetchData(&(vector[index]),item);
|
---|
778 | }
|
---|
779 | }
|
---|
780 | else
|
---|
781 | vector=NULL;
|
---|
782 | }
|
---|
783 |
|
---|
784 | else if (PyTuple_Check(py_vector)) {
|
---|
785 | /*retrieve dimensions: */
|
---|
786 | M=(int)PyTuple_Size(py_vector);
|
---|
787 |
|
---|
788 | if (M) {
|
---|
789 | vector=xNew<float>(M);
|
---|
790 | for (int index = 0; index < M; index++) {
|
---|
791 | PyObject *item;
|
---|
792 | item = PyTuple_GetItem(py_vector, index);
|
---|
793 | FetchData(&(vector[index]),item);
|
---|
794 | }
|
---|
795 | }
|
---|
796 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
797 | else
|
---|
798 | vector=NULL;
|
---|
799 | }
|
---|
800 |
|
---|
801 | else{
|
---|
802 | M=1;
|
---|
803 | vector=xNew<float>(M);
|
---|
804 | FetchData(&(vector[0]),py_vector);
|
---|
805 | }
|
---|
806 |
|
---|
807 | /*output: */
|
---|
808 | if(pM)*pM=M;
|
---|
809 | if(pvector)*pvector=vector;
|
---|
810 | }
|
---|
811 | /*}}}*/
|
---|
812 | /*FUNCTION FetchData(int** pvector,int* pM, PyObject* py_vector){{{*/
|
---|
813 | void FetchData(int** pvector,int* pM,PyObject* py_vector){
|
---|
814 |
|
---|
815 | /*output: */
|
---|
816 | int* vector=NULL;
|
---|
817 | int M=0;
|
---|
818 | int ndim;
|
---|
819 | npy_intp* dims=NULL;
|
---|
820 |
|
---|
821 | /*intermediary:*/
|
---|
822 | long* lvector=NULL;
|
---|
823 | bool* bvector=NULL;
|
---|
824 | double* dvector=NULL;
|
---|
825 | int i;
|
---|
826 | PyObject* py_vector2=NULL;
|
---|
827 |
|
---|
828 | if (PyArray_Check((PyArrayObject*)py_vector)) {
|
---|
829 | /*retrieve dimensions: */
|
---|
830 | ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
|
---|
831 | if (ndim==1) {
|
---|
832 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
833 | M=dims[0];
|
---|
834 | }
|
---|
835 | else if (ndim==2) {
|
---|
836 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
837 | if (dims[1]==1)
|
---|
838 | M=dims[0];
|
---|
839 | else
|
---|
840 | _error_("expecting an Mx1 matrix or M vector in input!");
|
---|
841 | }
|
---|
842 | else
|
---|
843 | _error_("expecting an Mx1 matrix or M vector in input!");
|
---|
844 |
|
---|
845 | if (M) {
|
---|
846 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_vector)) {
|
---|
847 | py_vector2=PyArray_ContiguousFromAny(py_vector,NPY_LONG,ndim,ndim);
|
---|
848 | py_vector=py_vector2;
|
---|
849 | }
|
---|
850 |
|
---|
851 | if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
|
---|
852 | /*retrieve internal value: */
|
---|
853 | dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
854 |
|
---|
855 | /*transform into int vector: */
|
---|
856 | vector=xNew<int>(M);
|
---|
857 | for(i=0;i<M;i++)vector[i]=(int)dvector[i];
|
---|
858 | }
|
---|
859 |
|
---|
860 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
|
---|
861 | /*retrieve internal value: */
|
---|
862 | lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
863 |
|
---|
864 | /*transform into int vector: */
|
---|
865 | vector=xNew<int>(M);
|
---|
866 | for(i=0;i<M;i++)vector[i]=(int)lvector[i];
|
---|
867 | }
|
---|
868 |
|
---|
869 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
|
---|
870 | /*retrieve internal value: */
|
---|
871 | bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
872 |
|
---|
873 | /*transform into int vector: */
|
---|
874 | vector=xNew<int>(M);
|
---|
875 | for(i=0;i<M;i++)vector[i]=(int)bvector[i];
|
---|
876 | }
|
---|
877 |
|
---|
878 | else
|
---|
879 | _error_("unrecognized int pyarray type in input!");
|
---|
880 |
|
---|
881 | if (py_vector2)
|
---|
882 | delete(py_vector2);
|
---|
883 | }
|
---|
884 | else
|
---|
885 | vector=NULL;
|
---|
886 | }
|
---|
887 |
|
---|
888 | else if (PyList_Check(py_vector)) {
|
---|
889 | /*retrieve dimensions: */
|
---|
890 | M=(int)PyList_Size(py_vector);
|
---|
891 |
|
---|
892 | if (M) {
|
---|
893 | vector=xNew<int>(M);
|
---|
894 | for (int index = 0; index < M; index++) {
|
---|
895 | PyObject *item;
|
---|
896 | item = PyList_GetItem(py_vector, index);
|
---|
897 | FetchData(&(vector[index]),item);
|
---|
898 | }
|
---|
899 | }
|
---|
900 | else
|
---|
901 | vector=NULL;
|
---|
902 | }
|
---|
903 |
|
---|
904 | else if (PyTuple_Check(py_vector)) {
|
---|
905 | /*retrieve dimensions: */
|
---|
906 | M=(int)PyTuple_Size(py_vector);
|
---|
907 |
|
---|
908 | if (M) {
|
---|
909 | vector=xNew<int>(M);
|
---|
910 | for (int index = 0; index < M; index++) {
|
---|
911 | PyObject *item;
|
---|
912 | item = PyTuple_GetItem(py_vector, index);
|
---|
913 | FetchData(&(vector[index]),item);
|
---|
914 | }
|
---|
915 | }
|
---|
916 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
917 | else
|
---|
918 | vector=NULL;
|
---|
919 | }
|
---|
920 |
|
---|
921 | else {
|
---|
922 | M=1;
|
---|
923 | vector=xNew<int>(M);
|
---|
924 | FetchData(&(vector[0]),py_vector);
|
---|
925 | }
|
---|
926 |
|
---|
927 | /*output: */
|
---|
928 | if(pM)*pM=M;
|
---|
929 | if(pvector)*pvector=vector;
|
---|
930 | }
|
---|
931 | /*}}}*/
|
---|
932 | /*FUNCTION FetchData(bool** pvector,int* pM, PyObject* py_vector){{{*/
|
---|
933 | void FetchData(bool** pvector,int* pM,PyObject* py_vector){
|
---|
934 |
|
---|
935 | /*output: */
|
---|
936 | bool* bvector=NULL;
|
---|
937 | bool* vector=NULL;
|
---|
938 | int M=0;
|
---|
939 | int ndim;
|
---|
940 | npy_intp* dims=NULL;
|
---|
941 |
|
---|
942 | /*intermediary:*/
|
---|
943 | double* dvector=NULL;
|
---|
944 | long* lvector=NULL;
|
---|
945 | int i;
|
---|
946 | PyObject* py_vector2=NULL;
|
---|
947 |
|
---|
948 | if (PyArray_Check((PyArrayObject*)py_vector)) {
|
---|
949 | /*retrieve dimensions: */
|
---|
950 | ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
|
---|
951 | if (ndim==1) {
|
---|
952 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
953 | M=dims[0];
|
---|
954 | }
|
---|
955 | else if (ndim==2) {
|
---|
956 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
957 | if (dims[1]==1)
|
---|
958 | M=dims[0];
|
---|
959 | else
|
---|
960 | _error_("expecting an Mx1 matrix or M vector in input!");
|
---|
961 | }
|
---|
962 | else
|
---|
963 | _error_("expecting an Mx1 matrix or M vector in input!");
|
---|
964 |
|
---|
965 | if (M) {
|
---|
966 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_vector)) {
|
---|
967 | py_vector2=PyArray_ContiguousFromAny(py_vector,NPY_BOOL,ndim,ndim);
|
---|
968 | py_vector=py_vector2;
|
---|
969 | }
|
---|
970 |
|
---|
971 | if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
|
---|
972 | /*retrieve internal value: */
|
---|
973 | dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
974 |
|
---|
975 | /*transform into bool vector: */
|
---|
976 | vector=xNew<bool>(M);
|
---|
977 | for(i=0;i<M;i++)vector[i]=(bool)dvector[i];
|
---|
978 | }
|
---|
979 |
|
---|
980 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
|
---|
981 | /*retrieve internal value: */
|
---|
982 | lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
983 |
|
---|
984 | /*transform into bool vector: */
|
---|
985 | vector=xNew<bool>(M);
|
---|
986 | for(i=0;i<M;i++)vector[i]=(bool)lvector[i];
|
---|
987 | }
|
---|
988 |
|
---|
989 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
|
---|
990 | /*retrieve internal value: */
|
---|
991 | bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
992 |
|
---|
993 | /*copy vector: */
|
---|
994 | vector=xNew<bool>(M);
|
---|
995 | memcpy(vector,bvector,(M)*sizeof(bool));
|
---|
996 | }
|
---|
997 |
|
---|
998 | else
|
---|
999 | _error_("unrecognized bool pyarray type in input!");
|
---|
1000 |
|
---|
1001 | if (py_vector2)
|
---|
1002 | delete(py_vector2);
|
---|
1003 | }
|
---|
1004 | else
|
---|
1005 | vector=NULL;
|
---|
1006 | }
|
---|
1007 | else if (PyList_Check(py_vector)) {
|
---|
1008 | /*retrieve dimensions: */
|
---|
1009 | M=(int)PyList_Size(py_vector);
|
---|
1010 | if (M) {
|
---|
1011 | vector=xNew<bool>(M);
|
---|
1012 | for (int index = 0; index < M; index++) {
|
---|
1013 | PyObject *item;
|
---|
1014 | item = PyList_GetItem(py_vector, index);
|
---|
1015 | FetchData(&(vector[index]),item);
|
---|
1016 | }
|
---|
1017 | }
|
---|
1018 | else
|
---|
1019 | vector=NULL;
|
---|
1020 | }
|
---|
1021 |
|
---|
1022 | else if (PyTuple_Check(py_vector)) {
|
---|
1023 | /*retrieve dimensions: */
|
---|
1024 | M=(int)PyTuple_Size(py_vector);
|
---|
1025 |
|
---|
1026 | if (M) {
|
---|
1027 | vector=xNew<bool>(M);
|
---|
1028 | for (int index = 0; index < M; index++) {
|
---|
1029 | PyObject *item;
|
---|
1030 | item = PyTuple_GetItem(py_vector, index);
|
---|
1031 | FetchData(&(vector[index]),item);
|
---|
1032 | }
|
---|
1033 | }
|
---|
1034 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
1035 | else
|
---|
1036 | vector=NULL;
|
---|
1037 | }
|
---|
1038 |
|
---|
1039 | else {
|
---|
1040 | M=1;
|
---|
1041 | vector=xNew<bool>(M);
|
---|
1042 | FetchData(&(vector[0]),py_vector);
|
---|
1043 | }
|
---|
1044 |
|
---|
1045 | /*output: */
|
---|
1046 | if(pM)*pM=M;
|
---|
1047 | if(pvector)*pvector=vector;
|
---|
1048 | }
|
---|
1049 | /*}}}*/
|
---|
1050 |
|
---|
1051 | /*ISSM objects*/
|
---|
1052 | /*FUNCTION FetchData(BamgGeom** pbamggeom,PyObject* py_dict){{{*/
|
---|
1053 | void FetchData(BamgGeom** pbamggeom,PyObject* py_dict){
|
---|
1054 |
|
---|
1055 | /*Initialize output*/
|
---|
1056 | BamgGeom* bamggeom=new BamgGeom();
|
---|
1057 |
|
---|
1058 | /*Fetch all fields*/
|
---|
1059 | FetchData(&bamggeom->Vertices,&bamggeom->VerticesSize[0],&bamggeom->VerticesSize[1],PyDict_GetItemString(py_dict,"Vertices"));
|
---|
1060 | FetchData(&bamggeom->Edges, &bamggeom->EdgesSize[0], &bamggeom->EdgesSize[1], PyDict_GetItemString(py_dict,"Edges"));
|
---|
1061 | FetchData(&bamggeom->Corners, &bamggeom->CornersSize[0], &bamggeom->CornersSize[1], PyDict_GetItemString(py_dict,"Corners"));
|
---|
1062 | FetchData(&bamggeom->RequiredVertices,&bamggeom->RequiredVerticesSize[0],&bamggeom->RequiredVerticesSize[1],PyDict_GetItemString(py_dict,"RequiredVertices"));
|
---|
1063 | FetchData(&bamggeom->RequiredEdges, &bamggeom->RequiredEdgesSize[0], &bamggeom->RequiredEdgesSize[1], PyDict_GetItemString(py_dict,"RequiredEdges"));
|
---|
1064 | FetchData(&bamggeom->CrackedEdges,&bamggeom->CrackedEdgesSize[0],&bamggeom->CrackedEdgesSize[1],PyDict_GetItemString(py_dict,"CrackedEdges"));
|
---|
1065 | FetchData(&bamggeom->SubDomains,&bamggeom->SubDomainsSize[0],&bamggeom->SubDomainsSize[1],PyDict_GetItemString(py_dict,"SubDomains"));
|
---|
1066 |
|
---|
1067 | /*Assign output pointers:*/
|
---|
1068 | *pbamggeom=bamggeom;
|
---|
1069 | }
|
---|
1070 | /*}}}*/
|
---|
1071 | /*FUNCTION FetchData(BamgMesh** pbamgmesh,PyObject* py_dict){{{*/
|
---|
1072 | void FetchData(BamgMesh** pbamgmesh,PyObject* py_dict){
|
---|
1073 |
|
---|
1074 | /*Initialize output*/
|
---|
1075 | BamgMesh* bamgmesh=new BamgMesh();
|
---|
1076 |
|
---|
1077 | /*Fetch all fields*/
|
---|
1078 | FetchData(&bamgmesh->Vertices,&bamgmesh->VerticesSize[0],&bamgmesh->VerticesSize[1],PyDict_GetItemString(py_dict,"Vertices"));
|
---|
1079 | FetchData(&bamgmesh->Edges, &bamgmesh->EdgesSize[0], &bamgmesh->EdgesSize[1], PyDict_GetItemString(py_dict,"Edges"));
|
---|
1080 | FetchData(&bamgmesh->Triangles, &bamgmesh->TrianglesSize[0], &bamgmesh->TrianglesSize[1], PyDict_GetItemString(py_dict,"Triangles"));
|
---|
1081 | FetchData(&bamgmesh->CrackedEdges,&bamgmesh->CrackedEdgesSize[0],&bamgmesh->CrackedEdgesSize[1],PyDict_GetItemString(py_dict,"CrackedEdges"));
|
---|
1082 | FetchData(&bamgmesh->VerticesOnGeomEdge,&bamgmesh->VerticesOnGeomEdgeSize[0],&bamgmesh->VerticesOnGeomEdgeSize[1],PyDict_GetItemString(py_dict,"VerticesOnGeomEdge"));
|
---|
1083 | FetchData(&bamgmesh->VerticesOnGeomVertex,&bamgmesh->VerticesOnGeomVertexSize[0],&bamgmesh->VerticesOnGeomVertexSize[1],PyDict_GetItemString(py_dict,"VerticesOnGeomVertex"));
|
---|
1084 | FetchData(&bamgmesh->EdgesOnGeomEdge, &bamgmesh->EdgesOnGeomEdgeSize[0], &bamgmesh->EdgesOnGeomEdgeSize[1], PyDict_GetItemString(py_dict,"EdgesOnGeomEdge"));
|
---|
1085 | FetchData(&bamgmesh->IssmSegments,&bamgmesh->IssmSegmentsSize[0],&bamgmesh->IssmSegmentsSize[1],PyDict_GetItemString(py_dict,"IssmSegments"));
|
---|
1086 |
|
---|
1087 | /*Assign output pointers:*/
|
---|
1088 | *pbamgmesh=bamgmesh;
|
---|
1089 | }
|
---|
1090 | /*}}}*/
|
---|
1091 | /*FUNCTION FetchData(BamgOpts** pbamgopts,PyObject* py_dict){{{*/
|
---|
1092 | void FetchData(BamgOpts** pbamgopts,PyObject* py_dict){
|
---|
1093 |
|
---|
1094 | /*Initialize output*/
|
---|
1095 | BamgOpts* bamgopts=new BamgOpts();
|
---|
1096 |
|
---|
1097 | /*Fetch all fields*/
|
---|
1098 | FetchData(&bamgopts->anisomax,PyDict_GetItemString(py_dict,"anisomax"));
|
---|
1099 | FetchData(&bamgopts->cutoff,PyDict_GetItemString(py_dict,"cutoff"));
|
---|
1100 | FetchData(&bamgopts->coeff,PyDict_GetItemString(py_dict,"coeff"));
|
---|
1101 | FetchData(&bamgopts->errg,PyDict_GetItemString(py_dict,"errg"));
|
---|
1102 | FetchData(&bamgopts->gradation,PyDict_GetItemString(py_dict,"gradation"));
|
---|
1103 | FetchData(&bamgopts->Hessiantype,PyDict_GetItemString(py_dict,"Hessiantype"));
|
---|
1104 | FetchData(&bamgopts->maxnbv,PyDict_GetItemString(py_dict,"maxnbv"));
|
---|
1105 | FetchData(&bamgopts->maxsubdiv,PyDict_GetItemString(py_dict,"maxsubdiv"));
|
---|
1106 | FetchData(&bamgopts->Metrictype,PyDict_GetItemString(py_dict,"Metrictype"));
|
---|
1107 | FetchData(&bamgopts->nbjacobi,PyDict_GetItemString(py_dict,"nbjacobi"));
|
---|
1108 | FetchData(&bamgopts->nbsmooth,PyDict_GetItemString(py_dict,"nbsmooth"));
|
---|
1109 | FetchData(&bamgopts->omega,PyDict_GetItemString(py_dict,"omega"));
|
---|
1110 | FetchData(&bamgopts->power,PyDict_GetItemString(py_dict,"power"));
|
---|
1111 | FetchData(&bamgopts->verbose,PyDict_GetItemString(py_dict,"verbose"));
|
---|
1112 |
|
---|
1113 | FetchData(&bamgopts->Crack,PyDict_GetItemString(py_dict,"Crack"));
|
---|
1114 | FetchData(&bamgopts->KeepVertices,PyDict_GetItemString(py_dict,"KeepVertices"));
|
---|
1115 | FetchData(&bamgopts->splitcorners,PyDict_GetItemString(py_dict,"splitcorners"));
|
---|
1116 |
|
---|
1117 | FetchData(&bamgopts->hmin,PyDict_GetItemString(py_dict,"hmin"));
|
---|
1118 | FetchData(&bamgopts->hmax,PyDict_GetItemString(py_dict,"hmax"));
|
---|
1119 | FetchData(&bamgopts->hminVertices,&bamgopts->hminVerticesSize[0],&bamgopts->hminVerticesSize[1],PyDict_GetItemString(py_dict,"hminVertices"));
|
---|
1120 | FetchData(&bamgopts->hmaxVertices,&bamgopts->hmaxVerticesSize[0],&bamgopts->hmaxVerticesSize[1],PyDict_GetItemString(py_dict,"hmaxVertices"));
|
---|
1121 | FetchData(&bamgopts->hVertices,&bamgopts->hVerticesLength,PyDict_GetItemString(py_dict,"hVertices"));
|
---|
1122 | FetchData(&bamgopts->metric,&bamgopts->metricSize[0],&bamgopts->metricSize[1],PyDict_GetItemString(py_dict,"metric"));
|
---|
1123 | FetchData(&bamgopts->field,&bamgopts->fieldSize[0],&bamgopts->fieldSize[1],PyDict_GetItemString(py_dict,"field"));
|
---|
1124 | FetchData(&bamgopts->err,&bamgopts->errSize[0],&bamgopts->errSize[1],PyDict_GetItemString(py_dict,"err"));
|
---|
1125 |
|
---|
1126 | /*Additional checks*/
|
---|
1127 | bamgopts->Check();
|
---|
1128 |
|
---|
1129 | /*Assign output pointers:*/
|
---|
1130 | *pbamgopts=bamgopts;
|
---|
1131 | }
|
---|
1132 | /*}}}*/
|
---|
1133 | /*FUNCTION FetchData(Options** poptions,int istart, int nrhs,PyObject* py_tuple){{{*/
|
---|
1134 | void FetchData(Options** poptions,int istart, int nrhs,PyObject* py_tuple){
|
---|
1135 |
|
---|
1136 | char *name = NULL;
|
---|
1137 | Option *option = NULL;
|
---|
1138 |
|
---|
1139 | /*Initialize output*/
|
---|
1140 | Options* options=new Options();
|
---|
1141 |
|
---|
1142 | /*Fetch all options*/
|
---|
1143 | for (int i=istart; i<nrhs; i=i+2){
|
---|
1144 | #if _PYTHON_MAJOR_ >= 3
|
---|
1145 | if (!PyUnicode_Check(PyTuple_GetItem(py_tuple,(Py_ssize_t)i))) _error_("Argument " << i+1 << " must be name of option");
|
---|
1146 | #else
|
---|
1147 | if (!PyString_Check(PyTuple_GetItem(py_tuple,(Py_ssize_t)i))) _error_("Argument " << i+1 << " must be name of option");
|
---|
1148 | #endif
|
---|
1149 |
|
---|
1150 | FetchData(&name,PyTuple_GetItem(py_tuple,(Py_ssize_t)i));
|
---|
1151 | if(i+1 == nrhs) _error_("Argument " << i+2 << " must exist and be value of option \"" << name << "\".");
|
---|
1152 |
|
---|
1153 | _printf0_("FetchData for Options not implemented yet, ignoring option \"" << name << "\"!\n");
|
---|
1154 |
|
---|
1155 | // option=(Option*)OptionParse(name,&PyTuple_GetItem(py_tuple,(Py_ssize_t)(i+1)));
|
---|
1156 | // options->AddOption(option);
|
---|
1157 | // option=NULL;
|
---|
1158 | }
|
---|
1159 |
|
---|
1160 | /*Assign output pointers:*/
|
---|
1161 | *poptions=options;
|
---|
1162 | }
|
---|
1163 | /*}}}*/
|
---|
1164 | /*FUNCTION FetchData(Contours** pcontours,PyObject* py_list){{{*/
|
---|
1165 | void FetchData(Contours** pcontours,PyObject* py_list){
|
---|
1166 |
|
---|
1167 | int numcontours,test1,test2;
|
---|
1168 | char *contourname = NULL;
|
---|
1169 | Contours *contours = NULL;
|
---|
1170 | Contour<double> *contouri = NULL;
|
---|
1171 | PyObject *py_dicti = NULL;
|
---|
1172 | PyObject *py_item = NULL;
|
---|
1173 |
|
---|
1174 | #if _PYTHON_MAJOR_ >= 3
|
---|
1175 | if (PyUnicode_Check(py_list)){
|
---|
1176 | #else
|
---|
1177 | if (PyString_Check(py_list)){
|
---|
1178 | #endif
|
---|
1179 | FetchData(&contourname,py_list);
|
---|
1180 | contours=ExpRead<double>(contourname);
|
---|
1181 | }
|
---|
1182 | else if(PyList_Check(py_list)){
|
---|
1183 |
|
---|
1184 | contours=new Contours();
|
---|
1185 | numcontours=(int)PyList_Size(py_list);
|
---|
1186 |
|
---|
1187 | for(int i=0;i<numcontours;i++){
|
---|
1188 |
|
---|
1189 | contouri=new Contour<double>();
|
---|
1190 | py_dicti=PyList_GetItem(py_list,(Py_ssize_t)i);
|
---|
1191 |
|
---|
1192 | py_item = PyDict_GetItemString(py_dicti,"nods");
|
---|
1193 | if(!py_item) _error_("input structure does not have a 'nods' field");
|
---|
1194 | FetchData(&contouri->nods,py_item);
|
---|
1195 |
|
---|
1196 | py_item = PyDict_GetItemString(py_dicti,"x");
|
---|
1197 | if(!py_item) _error_("input structure does not have a 'x' field");
|
---|
1198 | FetchData(&contouri->x,&test1,&test2,py_item);
|
---|
1199 | if(test1!=contouri->nods || test2!=1) _error_("field x should be of size ["<<contouri->nods<<" 1]");
|
---|
1200 |
|
---|
1201 | py_item = PyDict_GetItemString(py_dicti,"y");
|
---|
1202 | if(!py_item) _error_("input structure does not have a 'y' field");
|
---|
1203 | FetchData(&contouri->y,&test1,&test2,py_item);
|
---|
1204 | if(test1!=contouri->nods || test2!=1) _error_("field y should be of size ["<<contouri->nods<<" 1]");
|
---|
1205 |
|
---|
1206 | contours->AddObject(contouri);
|
---|
1207 | }
|
---|
1208 | }
|
---|
1209 | else{
|
---|
1210 | _error_("Contour is neither a string nor a structure and cannot be loaded");
|
---|
1211 | }
|
---|
1212 |
|
---|
1213 | /*clean-up and assign output pointer*/
|
---|
1214 | xDelete<char>(contourname);
|
---|
1215 | *pcontours=contours;
|
---|
1216 | }
|
---|
1217 |
|
---|
1218 | void FetchChacoData(int* pnvtxs,int** padjacency,int** pstart,float** pewgts,PyObject* A_IN, PyObject* EWGTS_IN){/*{{{*/
|
---|
1219 |
|
---|
1220 | double* a = ((double*)PyArray_DATA((PyArrayObject*)A_IN));
|
---|
1221 | int ndim = PyArray_NDIM((PyArrayObject*)A_IN);
|
---|
1222 | int nzmax = PyArray_CountNonzero((PyArrayObject*)A_IN);
|
---|
1223 |
|
---|
1224 | /*Fetch adjacency matrix:*/
|
---|
1225 | int nvtxs = PyArray_DIMS((PyArrayObject*)A_IN)[1];
|
---|
1226 |
|
---|
1227 | int* mwstart = xNewZeroInit<int>(nvtxs+1);
|
---|
1228 | pyGetJc(a,nvtxs,mwstart);
|
---|
1229 |
|
---|
1230 | int* mwadjacency = xNewZeroInit<int>(nzmax);
|
---|
1231 | pyGetIr(a,nvtxs,nzmax,mwadjacency);
|
---|
1232 |
|
---|
1233 | int* start = xNew<int>(nvtxs+1);
|
---|
1234 | for(int i=0;i<nvtxs+1;i++) start[i]=(int)mwstart[i];
|
---|
1235 | int* adjacency = xNew<int>(nzmax);
|
---|
1236 | for(int i=0; i<nzmax; i++) adjacency[i]=(int)mwadjacency[i];
|
---|
1237 |
|
---|
1238 | /*Get edges weights*/
|
---|
1239 | int nedges = start[nvtxs];
|
---|
1240 | float* ewgts = NULL;
|
---|
1241 | int size = PyArray_SIZE((PyArrayObject*)EWGTS_IN);
|
---|
1242 | //size may be 1 and still be empty;
|
---|
1243 | //presumably size of edge_weights input will never be exactly 1
|
---|
1244 | if(size != 1 && size != 0){
|
---|
1245 | ewgts = xNewZeroInit<float>(nedges);
|
---|
1246 | for(int i = 0; i<nedges;i++){
|
---|
1247 | ewgts[i] = (float)a[i];
|
---|
1248 | }
|
---|
1249 | }
|
---|
1250 |
|
---|
1251 | /*Assign output pointers*/
|
---|
1252 | *pnvtxs = nvtxs;
|
---|
1253 | *padjacency = adjacency;
|
---|
1254 | *pstart = start;
|
---|
1255 | *pewgts = ewgts;
|
---|
1256 | }
|
---|
1257 | /*}}}*/
|
---|
1258 |
|
---|
1259 | void pyGetJc(double* a, int nvtxs, int* Jc){
|
---|
1260 | /*{{{*/
|
---|
1261 | //get the number of non-zero elements in each row, adding to the prior number;
|
---|
1262 | //always starts with 0 and has length: number_of_rows_in(a)+1 == nvtxs+1
|
---|
1263 | // eg: 0, 1, 3, 6, 8, 13, ...
|
---|
1264 | // for: 1 in the first row, 2 in the second, 3 in the third, etc.
|
---|
1265 | int c = 0;
|
---|
1266 | Jc[c++] = 0;
|
---|
1267 |
|
---|
1268 | for(int i=0;i<nvtxs;i++){
|
---|
1269 | for(int j=0;j<nvtxs;j++){
|
---|
1270 | if ((int)a[i+(j*nvtxs)] != 0){
|
---|
1271 | Jc[c] += 1;
|
---|
1272 | }
|
---|
1273 | }
|
---|
1274 | c++;
|
---|
1275 | Jc[c] = Jc[c-1];
|
---|
1276 | }
|
---|
1277 | return;
|
---|
1278 | }
|
---|
1279 | /*}}}*/
|
---|
1280 |
|
---|
1281 | void pyGetIr(double* a, int nvtxs, int nzmax, int* Ir){
|
---|
1282 | /*{{{*/
|
---|
1283 | //get the row-wise position of each non-zero element;
|
---|
1284 | //has length: number_of_non_zero_elements_in(a) == nzmax
|
---|
1285 | // eg: 10, 24, 25, 4, ...
|
---|
1286 | // the 10th, 24th, and 25th elements in the first row, the 4th in the second row
|
---|
1287 | // using pyGetJc to determine which indices correspond to which row
|
---|
1288 | int r = 0;
|
---|
1289 |
|
---|
1290 | for(int i=0;i<nvtxs;i++){
|
---|
1291 | for(int j=0;j<nvtxs;j++){
|
---|
1292 | if ((int)a[i+(j*nvtxs)] != 0){
|
---|
1293 | Ir[r] = j;
|
---|
1294 | r++;
|
---|
1295 | }
|
---|
1296 | }
|
---|
1297 | }
|
---|
1298 | return;
|
---|
1299 | }
|
---|
1300 | /*}}}*/
|
---|
1301 |
|
---|
1302 | /*Python version dependent: */
|
---|
1303 | /* #if _PYTHON_MAJOR_ >= 3 */
|
---|
1304 | /* /\*FUNCTION FetchData(char** pstring,PyObject* py_unicode){{{*\/ */
|
---|
1305 | /* void FetchData(char** pstring,PyObject* py_unicode){ */
|
---|
1306 |
|
---|
1307 | /* PyObject* py_bytes; */
|
---|
1308 | /* char* string=NULL; */
|
---|
1309 |
|
---|
1310 | /* /\*convert to bytes format: *\/ */
|
---|
1311 | /* PyUnicode_FSConverter(py_unicode,&py_bytes); */
|
---|
1312 |
|
---|
1313 | /* /\*convert from bytes to string: *\/ */
|
---|
1314 | /* string=PyBytes_AsUTF8(py_bytes); */
|
---|
1315 |
|
---|
1316 | /* /\*copy string (note strlen does not include trailing NULL): *\/ */
|
---|
1317 | /* *pstring=xNew<char>(strlen(string)+1); */
|
---|
1318 | /* memcpy(*pstring,string,(strlen(string)+1)*sizeof(char)); */
|
---|
1319 | /* } */
|
---|
1320 | /* /\*}}}*\/ */
|
---|
1321 | /* #else */
|
---|
1322 | /*FUNCTION FetchData(char** pstring,PyObject* py_string){{{*/
|
---|
1323 | void FetchData(char** pstring,PyObject* py_string){
|
---|
1324 |
|
---|
1325 | /*extract internal string: */
|
---|
1326 | #if _PYTHON_MAJOR_ == 3
|
---|
1327 | const char* string=PyUnicode_AsUTF8(py_string);
|
---|
1328 | #else
|
---|
1329 | char* string=PyString_AsString(py_string);
|
---|
1330 | #endif
|
---|
1331 | /*copy string (note strlen does not include trailing NULL): */
|
---|
1332 | *pstring=xNew<char>(strlen(string)+1);
|
---|
1333 | memcpy(*pstring,string,(strlen(string)+1)*sizeof(char));
|
---|
1334 | }
|
---|
1335 | /*}}}*/
|
---|
1336 | //#endif
|
---|