OpenMPI  0.1.1
pyOTF_AuxiliaryFunctions.h
1 /*
2  This is part of the OTF library. Copyright by ZIH, TU Dresden 2005-2012.
3  Authors: Andreas Knuepfer, Holger Brunst, Ronny Brendel, Thomas Kriebitzsch
4 */
5 
6 #ifndef PYOTF_AUXILIARYFUNCTIONS_H
7 #define PYOTF_AUXILIARYFUNCTIONS_H
8 
9 uint32_t* createInt32ArrayFromSequence( PyObject* list );
10 uint8_t* createInt8ArrayFromSequence( PyObject* list );
11 PyObject* createSequenceFromInt8Array( uint8_t* array, uint8_t len );
12 
13 /* conversion from python sequence to uint32_t array
14 CREATES AN ARRAY - you have to free it yourself */
15 uint32_t* createInt32ArrayFromSequence( PyObject* list ) {
16 
17  int i;
18  int dim;
19  uint32_t* ret;
20 
21  if (!PySequence_Check( list )) {
22 #ifdef OTF_VERBOSE
23  PyErr_SetString(PyExc_TypeError,"Expecting a sequence");
24 #endif /* OTF_VERBOSE */
25  return NULL;
26  }
27 
28  dim= PyObject_Length( list );
29 
30  ret= (uint32_t*) malloc( sizeof(uint32_t) * dim );
31 
32  for ( i =0; i < dim; ++i ) {
33 
34  PyObject *o = PySequence_GetItem( list ,i );
35 
36  if (!PyInt_Check(o)) {
37  Py_XDECREF(o);
38 #ifdef OTF_VERBOSE
39  PyErr_SetString(PyExc_ValueError,"Expecting a sequence of integers");
40 #endif /* OTF_VERBOSE */
41  free( ret );
42  return NULL;
43  }
44 
45  ret[i] = PyInt_AsLong(o);
46  Py_DECREF(o);
47  }
48 
49  return ret;
50 }
51 
52 /* conversion from python sequence to uint8_t array
53 CREATES AN ARRAY - you have to free it yourself */
54 uint8_t* createInt8ArrayFromSequence( PyObject* list ) {
55 
56  int i;
57  int dim;
58  uint8_t* ret;
59 
60  if (!PySequence_Check( list )) {
61 #ifdef OTF_VERBOSE
62  PyErr_SetString(PyExc_TypeError,"Expecting a sequence");
63 #endif /* OTF_VERBOSE */
64  return NULL;
65  }
66 
67  dim= PyObject_Length( list );
68 
69  ret= (uint8_t*) malloc( sizeof(uint8_t) * dim );
70 
71  for ( i =0; i < dim; ++i ) {
72 
73  PyObject *o = PySequence_GetItem( list ,i );
74 
75  if (!PyInt_Check(o)) {
76  Py_XDECREF(o);
77 #ifdef OTF_VERBOSE
78  PyErr_SetString(PyExc_ValueError,"Expecting a sequence of integers");
79 #endif /* OTF_VERBOSE */
80  free( ret );
81  return NULL;
82  }
83 
84  ret[i] = PyInt_AsLong(o);
85  Py_DECREF(o);
86  }
87 
88  return ret;
89 }
90 
91 /* conversion from uint8_t array to python list,
92  creates a python list and returns it */
93 PyObject* createSequenceFromInt8Array( uint8_t* array, uint8_t len ) {
94 
95  uint8_t i;
96  PyObject *pylist = PyList_New(len);
97 
98  if( ! PyList_Check(pylist) ) {
99  printf("Creating PyList not possible\n");
100  }
101 
102  for(i = 0; i < len; i++) {
103  PyList_SetItem( pylist, i, PyInt_FromLong(array[i]) );
104  }
105 
106 
107  return pylist;
108 
109 }
110 
111 
112 #endif /* PYOTF_AUXILIARYFUNCTIONS_H */