Salome HOME
End of reorganization of MEDCoupling wrapper for Renumber
[modules/med.git] / src / MEDCoupling_Swig / MEDCouplingDataArrayTypemaps.i
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay (CEA/DEN)
20
21 #include "InterpKernelAutoPtr.hxx"
22
23 #ifdef WITH_NUMPY
24 #include <numpy/arrayobject.h>
25
26 // specific DataArray deallocator callback. This deallocator is used both in the constructor of DataArray and in the toNumPyArr
27 // method. This dellocator uses weakref to determine if the linked numArr is still alive or not. If alive the ownership is given to it.
28 // if no more alive the "standart" DataArray deallocator is called.
29 void numarrdeal(void *pt, void *wron)
30 {
31   void **wronc=(void **)wron;
32   PyObject *weakRefOnOwner=reinterpret_cast<PyObject *>(wronc[0]);
33   PyObject *obj=PyWeakref_GetObject(weakRefOnOwner);
34   if(obj!=Py_None)
35     {
36       Py_XINCREF(obj);
37       PyArrayObject *objC=reinterpret_cast<PyArrayObject *>(obj);
38       objC->flags|=NPY_OWNDATA;
39       Py_XDECREF(weakRefOnOwner);
40       Py_XDECREF(obj);
41     }
42   else
43     {
44       typedef void (*MyDeallocator)(void *,void *);
45       MyDeallocator deall=(MyDeallocator)wronc[1];
46       deall(pt,NULL);
47       Py_XDECREF(weakRefOnOwner);
48     }
49   delete [] wronc;
50 }
51
52 template<class MCData>
53 struct PyCallBackDataArraySt {
54     PyObject_HEAD
55     MCData *_pt_mc;
56 };
57
58 typedef struct PyCallBackDataArraySt<ParaMEDMEM::DataArrayInt> PyCallBackDataArrayInt;
59 typedef struct PyCallBackDataArraySt<ParaMEDMEM::DataArrayDouble> PyCallBackDataArrayDouble;
60
61 extern "C"
62 {
63   static int callbackmcdataarray___init__(PyObject *self, PyObject *args, PyObject *kwargs) { return 0; }
64   
65   static PyObject *callbackmcdataarrayint___new__(PyTypeObject *type, PyObject *args, PyObject *kwargs)
66   {
67     PyCallBackDataArrayInt *self = (PyCallBackDataArrayInt *) ( type->tp_alloc(type, 0) );
68     return (PyObject *)self;
69   }
70
71   static PyObject *callbackmcdataarraydouble___new__(PyTypeObject *type, PyObject *args, PyObject *kwargs)
72   {
73     PyCallBackDataArrayDouble *self = (PyCallBackDataArrayDouble *) ( type->tp_alloc(type, 0) );
74     return (PyObject *)self;
75   }
76   
77   static void callbackmcdataarray_dealloc(PyObject *self)
78   {
79     Py_TYPE(self)->tp_free(self);
80   }
81   
82   // real callback called when a numpy arr having more than one DataArray instance client on it is destroyed.
83   // In this case, all the "weak" clients, except the first one, invoke this call back that desable the content of these "weak" clients.
84   static PyObject *callbackmcdataarrayint_call(PyCallBackDataArrayInt *self, PyObject *args, PyObject *kw)
85   {
86     if(self->_pt_mc)
87       {
88         ParaMEDMEM::MemArray<int>& mma=self->_pt_mc->accessToMemArray();
89         mma.destroy();
90       }
91     Py_XINCREF(Py_None);
92     return Py_None;
93   }
94   
95   // real callback called when a numpy arr having more than one DataArray instance client on it is destroyed.
96   // In this case, all the "weak" clients, except the first one, invoke this call back that desable the content of these "weak" clients.
97   static PyObject *callbackmcdataarraydouble_call(PyCallBackDataArrayDouble *self, PyObject *args, PyObject *kw)
98   {
99     if(self->_pt_mc)
100       {
101         ParaMEDMEM::MemArray<double>& mma=self->_pt_mc->accessToMemArray();
102         mma.destroy();
103       }
104     Py_XINCREF(Py_None);
105     return Py_None;
106   }
107 }
108
109 PyTypeObject PyCallBackDataArrayInt_RefType = {
110   PyVarObject_HEAD_INIT(&PyType_Type, 0)
111   "callbackmcdataarrayint",
112   sizeof(PyCallBackDataArrayInt),
113   0,
114   callbackmcdataarray_dealloc,            /*tp_dealloc*/
115   0,                          /*tp_print*/
116   0,                          /*tp_getattr*/
117   0,                          /*tp_setattr*/
118   0,                          /*tp_compare*/
119   0,                          /*tp_repr*/
120   0,                          /*tp_as_number*/
121   0,                          /*tp_as_sequence*/
122   0,                          /*tp_as_mapping*/
123   0,                          /*tp_hash*/
124   (ternaryfunc)callbackmcdataarrayint_call,  /*tp_call*/
125   0,                          /*tp_str*/
126   0,                          /*tp_getattro*/
127   0,                          /*tp_setattro*/
128   0,                          /*tp_as_buffer*/
129   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE,  /*tp_flags*/
130   0,                          /*tp_doc*/
131   0,                          /*tp_traverse*/
132   0,                          /*tp_clear*/
133   0,                          /*tp_richcompare*/
134   0,                          /*tp_weaklistoffset*/
135   0,                          /*tp_iter*/
136   0,                          /*tp_iternext*/
137   0,                          /*tp_methods*/
138   0,                          /*tp_members*/
139   0,                          /*tp_getset*/
140   0,                          /*tp_base*/
141   0,                          /*tp_dict*/
142   0,                          /*tp_descr_get*/
143   0,                          /*tp_descr_set*/
144   0,                          /*tp_dictoffset*/
145   callbackmcdataarray___init__,           /*tp_init*/
146   PyType_GenericAlloc,        /*tp_alloc*/
147   callbackmcdataarrayint___new__,            /*tp_new*/
148   PyObject_GC_Del,            /*tp_free*/
149 };
150
151 PyTypeObject PyCallBackDataArrayDouble_RefType = {
152   PyVarObject_HEAD_INIT(&PyType_Type, 0)
153   "callbackmcdataarraydouble",
154   sizeof(PyCallBackDataArrayDouble),
155   0,
156   callbackmcdataarray_dealloc,            /*tp_dealloc*/
157   0,                          /*tp_print*/
158   0,                          /*tp_getattr*/
159   0,                          /*tp_setattr*/
160   0,                          /*tp_compare*/
161   0,                          /*tp_repr*/
162   0,                          /*tp_as_number*/
163   0,                          /*tp_as_sequence*/
164   0,                          /*tp_as_mapping*/
165   0,                          /*tp_hash*/
166   (ternaryfunc)callbackmcdataarraydouble_call,  /*tp_call*/
167   0,                          /*tp_str*/
168   0,                          /*tp_getattro*/
169   0,                          /*tp_setattro*/
170   0,                          /*tp_as_buffer*/
171   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE,  /*tp_flags*/
172   0,                          /*tp_doc*/
173   0,                          /*tp_traverse*/
174   0,                          /*tp_clear*/
175   0,                          /*tp_richcompare*/
176   0,                          /*tp_weaklistoffset*/
177   0,                          /*tp_iter*/
178   0,                          /*tp_iternext*/
179   0,                          /*tp_methods*/
180   0,                          /*tp_members*/
181   0,                          /*tp_getset*/
182   0,                          /*tp_base*/
183   0,                          /*tp_dict*/
184   0,                          /*tp_descr_get*/
185   0,                          /*tp_descr_set*/
186   0,                          /*tp_dictoffset*/
187   callbackmcdataarray___init__,           /*tp_init*/
188   PyType_GenericAlloc,        /*tp_alloc*/
189   callbackmcdataarraydouble___new__,            /*tp_new*/
190   PyObject_GC_Del,            /*tp_free*/
191 };
192
193 // this is the second type of specific deallocator, only valid for the constructor of DataArrays taking numpy array
194 // in input when an another DataArray is already client of this.
195 template<class MCData>
196 void numarrdeal2(void *pt, void *obj)
197 {
198   typedef struct PyCallBackDataArraySt<MCData> PyCallBackDataArray;
199   void **obj1=(void **)obj;
200   PyCallBackDataArray *cbdaic=reinterpret_cast<PyCallBackDataArray *>(obj1[0]);
201   PyObject *weakRefOnOwner=reinterpret_cast<PyObject *>(obj1[1]);
202   cbdaic->_pt_mc=0;
203   Py_XDECREF(weakRefOnOwner);
204   Py_XDECREF(cbdaic);
205   delete [] obj1;
206 }
207
208 template<class MCData, class T>
209 MCData *BuildNewInstance(PyObject *elt0, int npyObjectType, PyTypeObject *pytype, const char *msg)
210 {
211   int ndim=PyArray_NDIM(elt0);
212   if(ndim!=1 && ndim!=2)
213     throw INTERP_KERNEL::Exception("Input numpy array should have dimension equal to 1 or 2 !");
214   if(PyArray_DESCR(elt0)->type_num != npyObjectType)
215     {
216       std::ostringstream oss; oss << "Input numpy array has not the type " << msg << "!";
217       throw INTERP_KERNEL::Exception(oss.str().c_str());
218     }
219   npy_intp sz0=PyArray_DIM(elt0,0);
220   npy_intp sz1=ndim==2?PyArray_DIM(elt0,1):1;
221   //
222   int itemSize=PyArray_ITEMSIZE(elt0);
223   if(itemSize!=sizeof(T))
224     {
225       std::ostringstream oss; oss << "Input numpy array has not itemSize set to " << sizeof(T) << " !";
226       throw INTERP_KERNEL::Exception(oss.str().c_str());
227     }
228   if(itemSize*sz1!=PyArray_STRIDE(elt0,0))
229     throw INTERP_KERNEL::Exception("Input numpy array has stride that mismatches the item size ! Data are not packed in the right way for DataArrays !");
230   if(ndim==2)
231     if(itemSize!=PyArray_STRIDE(elt0,1))
232       throw INTERP_KERNEL::Exception("Input numpy array has stride that mismatches the item size ! Data are not packed in the right way for DataArrays for component #1 !");
233   const char *data=PyArray_BYTES(elt0);
234   typename ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<MCData> ret=MCData::New();
235   if(PyArray_ISBEHAVED(elt0))//aligned and writeable and in machine byte-order
236     {
237       PyArrayObject *elt0C=reinterpret_cast<PyArrayObject *>(elt0);
238       PyArrayObject *eltOwning=(PyArray_FLAGS(elt0C) & NPY_OWNDATA)?elt0C:NULL;
239       int mask=NPY_OWNDATA; mask=~mask;
240       elt0C->flags&=mask;
241       PyObject *deepestObj=elt0;
242       PyObject *base=elt0C->base;
243       if(base) deepestObj=base;
244       while(base)
245         {
246           if(PyArray_Check(base))
247             {
248               PyArrayObject *baseC=reinterpret_cast<PyArrayObject *>(base);
249               eltOwning=(PyArray_FLAGS(baseC) & NPY_OWNDATA)?baseC:eltOwning;
250               baseC->flags&=mask;
251               base=baseC->base;
252               if(base) deepestObj=base;
253             }
254           else
255             break;
256         }
257       typename ParaMEDMEM::MemArray<T>& mma=ret->accessToMemArray();
258       if(eltOwning==NULL)
259         {
260           PyCallBackDataArraySt<MCData> *cb=PyObject_GC_New(PyCallBackDataArraySt<MCData>,pytype);
261           cb->_pt_mc=ret;
262           ret->useArray(reinterpret_cast<const T *>(data),true,ParaMEDMEM::C_DEALLOC,sz0,sz1);
263           PyObject *ref=PyWeakref_NewRef(deepestObj,(PyObject *)cb);
264           void **objs=new void *[2]; objs[0]=cb; objs[1]=ref;
265           mma.setParameterForDeallocator(objs);
266           mma.setSpecificDeallocator(numarrdeal2<MCData>);
267           //"Impossible to share this numpy array chunk of data, because already shared by an another non numpy array object (maybe an another DataArrayInt instance) ! Release it, or perform a copy on the input array !");
268         }
269       else
270         {
271           ret->useArray(reinterpret_cast<const T *>(data),true,ParaMEDMEM::C_DEALLOC,sz0,sz1);
272           PyObject *ref=PyWeakref_NewRef(reinterpret_cast<PyObject *>(eltOwning),NULL);
273           void **objs=new void *[2]; objs[0]=ref; objs[1]=(void*) ParaMEDMEM::MemArray<T>::CDeallocator;
274           mma.setParameterForDeallocator(objs);
275           mma.setSpecificDeallocator(numarrdeal);
276         }
277     }
278   else if(PyArray_ISBEHAVED_RO(elt0))
279     ret->useArray(reinterpret_cast<const T *>(data),false,ParaMEDMEM::CPP_DEALLOC,sz0,sz1);
280   return ret.retn();
281 }
282
283
284 int NumpyArrSetBaseObjectExt(PyArrayObject *arr, PyObject *obj)
285 {
286     if (obj == NULL) {
287         PyErr_SetString(PyExc_ValueError,
288                 "Cannot set the NumPy array 'base' "
289                 "dependency to NULL after initialization");
290         return -1;
291     }
292     /*
293      * Allow the base to be set only once. Once the object which
294      * owns the data is set, it doesn't make sense to change it.
295      */
296     if (PyArray_BASE(arr) != NULL) {
297         Py_DECREF(obj);
298         PyErr_SetString(PyExc_ValueError,
299                 "Cannot set the NumPy array 'base' "
300                 "dependency more than once");
301         return -1;
302     }
303
304     /*
305      * Don't allow infinite chains of views, always set the base
306      * to the first owner of the data.  
307      * That is, either the first object which isn't an array, 
308      * or the first object which owns its own data.
309      */
310
311     while (PyArray_Check(obj) && (PyObject *)arr != obj) {
312         PyArrayObject *obj_arr = (PyArrayObject *)obj;
313         PyObject *tmp;
314  
315
316         /* If this array owns its own data, stop collapsing */
317         if (PyArray_CHKFLAGS(obj_arr, NPY_OWNDATA)) {
318             break;
319         }   
320
321         tmp = PyArray_BASE(obj_arr);
322         /* If there's no base, stop collapsing */
323         if (tmp == NULL) {
324             break;
325         }
326         /* Stop the collapse new base when the would not be of the same 
327          * type (i.e. different subclass).
328          */
329         if (Py_TYPE(tmp) != Py_TYPE(arr)) {
330             break;
331         }
332
333
334         Py_INCREF(tmp);
335         Py_DECREF(obj);
336         obj = tmp;
337     }
338
339     /* Disallow circular references */
340     if ((PyObject *)arr == obj) {
341         Py_DECREF(obj);
342         PyErr_SetString(PyExc_ValueError,
343                 "Cannot create a circular NumPy array 'base' dependency");
344         return -1;
345     }
346
347     arr->base = obj;
348
349     return 0;
350 }
351
352 template<class MCData, class T>
353 PyObject *ToNumPyArray(MCData *self, int npyObjectType, const char *MCDataStr)
354 {
355   if(!self->isAllocated())
356     {
357       std::ostringstream oss; oss << MCDataStr << "::toNumPyArray : this is not allocated !";
358       throw INTERP_KERNEL::Exception(oss.str().c_str());
359     }
360   ParaMEDMEM::MemArray<T>& mem=self->accessToMemArray();
361   int nbComp=self->getNumberOfComponents();
362   if(nbComp==0)
363     {
364       std::ostringstream oss; oss << MCDataStr << "::toNumPyArray : number of components of this is 0 ! Should be > 0 !"; 
365       throw INTERP_KERNEL::Exception(oss.str().c_str());
366     }
367   int nbDims=nbComp==1?1:2;
368   npy_intp dim[2];
369   dim[0]=(npy_intp)self->getNumberOfTuples(); dim[1]=nbComp;
370   const T *bg=self->getConstPointer();
371   PyObject *ret=PyArray_SimpleNewFromData(nbDims,dim,npyObjectType,const_cast<T *>(bg));
372   if(mem.isDeallocatorCalled())
373     {
374       if(mem.getDeallocator()!=numarrdeal)
375         {// case for the first call of toNumPyArray
376           PyObject *ref=PyWeakref_NewRef(ret,NULL);
377           void **objs=new void *[2]; objs[0]=ref; objs[1]=(void*) mem.getDeallocator();
378           mem.setParameterForDeallocator(objs);
379           mem.setSpecificDeallocator(numarrdeal);
380           return ret;
381         }
382       else
383         {// case for the second and other call of toNumPyArray
384           void **objs=(void **)mem.getParameterForDeallocator();
385           PyObject *weakRefOnOwner=(PyObject *)objs[0];
386           PyObject *obj=PyWeakref_GetObject(weakRefOnOwner);
387           if(obj!=Py_None)
388             {//the previous numArray exists let numpy deals the numpy array each other by declaring the still alive instance as base
389               Py_XINCREF(obj);
390               NumpyArrSetBaseObjectExt((PyArrayObject*)ret,obj);
391             }
392           else
393             {//the previous numArray no more exists -> declare the newly created numpy array as the first one.
394               Py_XDECREF(weakRefOnOwner);
395               PyObject *ref=PyWeakref_NewRef(ret,NULL);
396               objs[0]=ref;
397             }
398         }
399     }
400   return ret;
401 }
402
403 SWIGINTERN PyObject *ParaMEDMEM_DataArrayInt_toNumPyArray(ParaMEDMEM::DataArrayInt *self);
404 SWIGINTERN PyObject *ParaMEDMEM_DataArrayDouble_toNumPyArray(ParaMEDMEM::DataArrayDouble *self);
405
406 PyObject *ToCSRMatrix(const std::vector<std::map<int,double> >& m, int nbCols) throw(INTERP_KERNEL::Exception)
407 {
408   int nbRows((int)m.size());
409   ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::DataArrayInt> indPtr(ParaMEDMEM::DataArrayInt::New()),indices(ParaMEDMEM::DataArrayInt::New());
410   ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::DataArrayDouble> data(ParaMEDMEM::DataArrayDouble::New());
411   indPtr->alloc(nbRows+1,1);
412   int *intPtr_ptr(indPtr->getPointer()); intPtr_ptr[0]=0; intPtr_ptr++;
413   int sz2(0);
414   for(std::vector<std::map<int,double> >::const_iterator it0=m.begin();it0!=m.end();it0++,intPtr_ptr++)
415     {
416       sz2+=(int)(*it0).size();
417       *intPtr_ptr=sz2;
418     }
419   indices->alloc(sz2,1); data->alloc(sz2,1);
420   int *indices_ptr(indices->getPointer());
421   double *data_ptr(data->getPointer());
422   for(std::vector<std::map<int,double> >::const_iterator it0=m.begin();it0!=m.end();it0++)
423     for(std::map<int,double>::const_iterator it1=(*it0).begin();it1!=(*it0).end();it1++,indices_ptr++,data_ptr++)
424       {
425         *indices_ptr=(*it1).first;
426         *data_ptr=(*it1).second;
427       }
428   PyObject *a(ParaMEDMEM_DataArrayDouble_toNumPyArray(data)),*b(ParaMEDMEM_DataArrayInt_toNumPyArray(indices)),*c(ParaMEDMEM_DataArrayInt_toNumPyArray(indPtr));
429   //
430   PyObject *args(PyTuple_New(1)),*args0(PyTuple_New(3)),*kw(PyDict_New()),*kw1(PyTuple_New(2));
431   PyTuple_SetItem(args0,0,a); PyTuple_SetItem(args0,1,b); PyTuple_SetItem(args0,2,c); PyTuple_SetItem(args,0,args0);
432   PyTuple_SetItem(kw1,0,PyInt_FromLong(nbRows)); PyTuple_SetItem(kw1,1,PyInt_FromLong(nbCols));
433   PyObject *tmp1(PyString_FromString("shape"));
434   PyDict_SetItem(kw,tmp1,kw1); Py_DECREF(tmp1); Py_DECREF(kw1);
435   PyObject* pdict=PyDict_New();
436   PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());
437   PyObject *tmp(PyRun_String("from scipy.sparse import csr_matrix", Py_single_input, pdict, pdict));
438   PyObject *csrMatrixCls=PyDict_GetItemString(pdict,"csr_matrix");
439   PyObject *ret(PyObject_Call(csrMatrixCls,args,kw));
440   Py_DECREF(pdict); Py_XDECREF(tmp); Py_DECREF(args); Py_DECREF(kw);
441   return ret;
442 }
443
444 #endif
445
446 static PyObject *convertDataArrayChar(ParaMEDMEM::DataArrayChar *dac, int owner) throw(INTERP_KERNEL::Exception)
447 {
448   PyObject *ret=0;
449   if(!dac)
450     {
451       Py_XINCREF(Py_None);
452       return Py_None;
453     }
454   if(dynamic_cast<ParaMEDMEM::DataArrayByte *>(dac))
455     ret=SWIG_NewPointerObj((void*)dac,SWIGTYPE_p_ParaMEDMEM__DataArrayByte,owner);
456   if(dynamic_cast<ParaMEDMEM::DataArrayAsciiChar *>(dac))
457     ret=SWIG_NewPointerObj((void*)dac,SWIGTYPE_p_ParaMEDMEM__DataArrayAsciiChar,owner);
458   if(!ret)
459     throw INTERP_KERNEL::Exception("Not recognized type of DataArrayChar on downcast !");
460   return ret;
461 }
462
463 static PyObject *convertDataArray(ParaMEDMEM::DataArray *dac, int owner) throw(INTERP_KERNEL::Exception)
464 {
465   PyObject *ret=0;
466   if(!dac)
467     {
468       Py_XINCREF(Py_None);
469       return Py_None;
470     }
471   if(dynamic_cast<ParaMEDMEM::DataArrayDouble *>(dac))
472     ret=SWIG_NewPointerObj((void*)dac,SWIGTYPE_p_ParaMEDMEM__DataArrayDouble,owner);
473   if(dynamic_cast<ParaMEDMEM::DataArrayInt *>(dac))
474     ret=SWIG_NewPointerObj((void*)dac,SWIGTYPE_p_ParaMEDMEM__DataArrayInt,owner);
475   if(dynamic_cast<ParaMEDMEM::DataArrayByte *>(dac))
476     ret=SWIG_NewPointerObj((void*)dac,SWIGTYPE_p_ParaMEDMEM__DataArrayByte,owner);
477   if(dynamic_cast<ParaMEDMEM::DataArrayAsciiChar *>(dac))
478     ret=SWIG_NewPointerObj((void*)dac,SWIGTYPE_p_ParaMEDMEM__DataArrayAsciiChar,owner);
479   if(!ret)
480     throw INTERP_KERNEL::Exception("Not recognized type of DataArray on downcast !");
481   return ret;
482 }
483
484 static PyObject *convertIntArrToPyList(const int *ptr, int size) throw(INTERP_KERNEL::Exception)
485 {
486   PyObject *ret=PyList_New(size);
487   for(int i=0;i<size;i++)
488     PyList_SetItem(ret,i,PyInt_FromLong(ptr[i]));
489   return ret;
490 }
491
492 static PyObject *convertIntArrToPyList2(const std::vector<int>& v) throw(INTERP_KERNEL::Exception)
493 {
494   int size=v.size();
495   PyObject *ret=PyList_New(size);
496   for(int i=0;i<size;i++)
497     PyList_SetItem(ret,i,PyInt_FromLong(v[i]));
498   return ret;
499 }
500
501 static PyObject *convertIntArrToPyList3(const std::set<int>& v) throw(INTERP_KERNEL::Exception)
502 {
503   int size=v.size();
504   PyObject *ret=PyList_New(size);
505   std::set<int>::const_iterator it=v.begin();
506   for(int i=0;i<size;i++,it++)
507     PyList_SetItem(ret,i,PyInt_FromLong(*it));
508   return ret;
509 }
510
511 static PyObject *convertIntArrToPyListOfTuple(const int *vals, int nbOfComp, int nbOfTuples) throw(INTERP_KERNEL::Exception)
512 {
513   PyObject *ret=PyList_New(nbOfTuples);
514   for(int i=0;i<nbOfTuples;i++)
515     {
516       PyObject *t=PyTuple_New(nbOfComp);
517       for(int j=0;j<nbOfComp;j++)
518         PyTuple_SetItem(t,j,PyInt_FromLong(vals[i*nbOfComp+j]));
519       PyList_SetItem(ret,i,t);
520     }
521   return ret;
522 }
523
524 static int *convertPyToNewIntArr2(PyObject *pyLi, int *size) throw(INTERP_KERNEL::Exception)
525 {
526   if(PyList_Check(pyLi))
527     {
528       *size=PyList_Size(pyLi);
529       int *tmp=new int[*size];
530       for(int i=0;i<*size;i++)
531         {
532           PyObject *o=PyList_GetItem(pyLi,i);
533           if(PyInt_Check(o))
534             {
535               int val=(int)PyInt_AS_LONG(o);
536               tmp[i]=val;
537             }
538           else
539             {
540               delete [] tmp;
541               throw INTERP_KERNEL::Exception("list must contain integers only");
542             }
543         }
544       return tmp;
545     }
546   else if(PyTuple_Check(pyLi))
547     {
548       *size=PyTuple_Size(pyLi);
549       int *tmp=new int[*size];
550       for(int i=0;i<*size;i++)
551         {
552           PyObject *o=PyTuple_GetItem(pyLi,i);
553           if(PyInt_Check(o))
554             {
555               int val=(int)PyInt_AS_LONG(o);
556               tmp[i]=val;
557             }
558           else
559             {
560               delete [] tmp;
561               throw INTERP_KERNEL::Exception("tuple must contain integers only");
562             }
563         }
564       return tmp;
565     }
566   else
567     {
568       throw INTERP_KERNEL::Exception("convertPyToNewIntArr2 : not a list");
569     }
570 }
571
572 static void convertPyToVectorPairInt(PyObject *pyLi, std::vector< std::pair<int,int> >& arr) throw(INTERP_KERNEL::Exception)
573 {
574   const char msg[]="list must contain tuples of 2 integers only or tuple must contain tuples of 2 integers only !";
575   if(PyList_Check(pyLi))
576     {
577       int size=PyList_Size(pyLi);
578       arr.resize(size);
579       for(int i=0;i<size;i++)
580         {
581           PyObject *o=PyList_GetItem(pyLi,i);
582           if(PyTuple_Check(o))
583             {
584               int sz2=PyTuple_Size(o);
585               if(sz2!=2)
586                 throw INTERP_KERNEL::Exception(msg);
587               PyObject *o_0=PyTuple_GetItem(o,0);
588               if(!PyInt_Check(o_0))
589                 throw INTERP_KERNEL::Exception(msg);
590               PyObject *o_1=PyTuple_GetItem(o,1);
591               if(!PyInt_Check(o_1))
592                 throw INTERP_KERNEL::Exception(msg);
593               arr[i].first=(int)PyInt_AS_LONG(o_0);
594               arr[i].second=(int)PyInt_AS_LONG(o_1);
595             }
596           else
597             throw INTERP_KERNEL::Exception(msg);
598         }
599     }
600   else if(PyTuple_Check(pyLi))
601     {
602       int size=PyTuple_Size(pyLi);
603       arr.resize(size);
604       for(int i=0;i<size;i++)
605         {
606           PyObject *o=PyTuple_GetItem(pyLi,i);
607           if(PyTuple_Check(o))
608             {
609               int sz2=PyTuple_Size(o);
610               if(sz2!=2)
611                 throw INTERP_KERNEL::Exception(msg);
612               PyObject *o_0=PyTuple_GetItem(o,0);
613               if(!PyInt_Check(o_0))
614                 throw INTERP_KERNEL::Exception(msg);
615               PyObject *o_1=PyTuple_GetItem(o,1);
616               if(!PyInt_Check(o_1))
617                 throw INTERP_KERNEL::Exception(msg);
618               arr[i].first=(int)PyInt_AS_LONG(o_0);
619               arr[i].second=(int)PyInt_AS_LONG(o_1);
620             }
621           else
622             throw INTERP_KERNEL::Exception(msg);
623         }
624     }
625   else
626     throw INTERP_KERNEL::Exception(msg);
627 }
628
629 static void convertPyToNewIntArr3(PyObject *pyLi, std::vector<int>& arr) throw(INTERP_KERNEL::Exception)
630 {
631   if(PyList_Check(pyLi))
632     {
633       int size=PyList_Size(pyLi);
634       arr.resize(size);
635       for(int i=0;i<size;i++)
636         {
637           PyObject *o=PyList_GetItem(pyLi,i);
638           if(PyInt_Check(o))
639             {
640               int val=(int)PyInt_AS_LONG(o);
641               arr[i]=val;
642             }
643           else
644             throw INTERP_KERNEL::Exception("list must contain integers only");
645         }
646     }
647   else if(PyTuple_Check(pyLi))
648     {
649       int size=PyTuple_Size(pyLi);
650       arr.resize(size);
651       for(int i=0;i<size;i++)
652         {
653           PyObject *o=PyTuple_GetItem(pyLi,i);
654           if(PyInt_Check(o))
655             {
656               int val=(int)PyInt_AS_LONG(o);
657               arr[i]=val;
658             }
659           else
660             throw INTERP_KERNEL::Exception("tuple must contain integers only");
661         }
662     }
663   else
664     {
665       throw INTERP_KERNEL::Exception("convertPyToNewIntArr3 : not a list nor a tuple");
666     }
667 }
668
669 static void convertPyToNewIntArr4(PyObject *pyLi, int recurseLev, int nbOfSubPart, std::vector<int>& arr) throw(INTERP_KERNEL::Exception)
670 {
671   if(recurseLev<0)
672     throw INTERP_KERNEL::Exception("convertPyToNewIntArr4 : invalid list of integers level of recursion !");
673   arr.clear();
674   if(PyList_Check(pyLi))
675     {
676       int size=PyList_Size(pyLi);
677       for(int i=0;i<size;i++)
678         {
679           PyObject *o=PyList_GetItem(pyLi,i);
680           if(PyInt_Check(o))
681             {
682               int val=(int)PyInt_AS_LONG(o);
683               arr.push_back(val);
684             }
685           else
686             {
687               std::vector<int> arr2;
688               convertPyToNewIntArr4(o,recurseLev-1,nbOfSubPart,arr2);
689               if(nbOfSubPart>=1 && nbOfSubPart!=(int)arr2.size())
690                   {
691                     std::ostringstream oss; oss << "convertPyToNewIntArr4 : input list at lev " <<  recurseLev << " invalid nb of subpart elts expected " << nbOfSubPart << " having " << arr2.size() << " !";
692                     throw INTERP_KERNEL::Exception(oss.str().c_str());
693                   }
694               arr.insert(arr.end(),arr2.begin(),arr2.end());
695             }
696         }
697     }
698   else if(PyTuple_Check(pyLi))
699     {
700       int size=PyTuple_Size(pyLi);
701       for(int i=0;i<size;i++)
702         {
703           PyObject *o=PyTuple_GetItem(pyLi,i);
704           if(PyInt_Check(o))
705             {
706               int val=(int)PyInt_AS_LONG(o);
707               arr.push_back(val);
708             }
709           else
710             {
711               std::vector<int> arr2;
712               convertPyToNewIntArr4(o,recurseLev-1,nbOfSubPart,arr2);
713               if(nbOfSubPart>=1 && nbOfSubPart!=(int)arr2.size())
714                   {
715                     std::ostringstream oss; oss << "convertPyToNewIntArr4 : input list at lev " <<  recurseLev << " invalid nb of subpart elts expected " << nbOfSubPart << " having " << arr2.size() << " !";
716                     throw INTERP_KERNEL::Exception(oss.str().c_str());
717                   }
718               arr.insert(arr.end(),arr2.begin(),arr2.end());
719             }
720         }
721     }
722   else
723     throw INTERP_KERNEL::Exception("convertPyToNewIntArr4 : not a list nor a tuple recursively !");
724 }
725
726 static void checkFillArrayWithPyList(int size1, int size2, int& nbOfTuples, int& nbOfComp) throw(INTERP_KERNEL::Exception)
727 {
728   if(nbOfTuples==-1)
729     {
730       if(nbOfComp==-1) { nbOfTuples=size1; nbOfComp=size2; }
731       else { if(nbOfComp==size2) { nbOfTuples=size1; } else
732           {
733             std::ostringstream oss; oss << "fillArrayWithPyListDbl2 : mismatch between nb of elemts : Input has " << size1 << " tuples and " << size2 << " components";
734             oss << " whereas nb of components expected is " << nbOfComp << " !";
735             throw INTERP_KERNEL::Exception(oss.str().c_str());
736           } }
737     }
738   else
739     {
740       if(nbOfComp!=-1)
741         {
742           if((nbOfTuples!=size1 || nbOfComp!=size2))
743             {
744               if(size2!=1 || size1!=nbOfComp*nbOfTuples)
745                 {
746                   std::ostringstream oss; oss << "fillArrayWithPyListDbl2 : mismatch between nb of elemts : Input has " << size1 << " tuples and " << size2 << " components";
747                   oss << " whereas nb of tuples expected is " << nbOfTuples << " and number of components expected is " << nbOfComp << " !";
748                   throw INTERP_KERNEL::Exception(oss.str().c_str());
749                 }
750             }
751         }
752       else
753         {
754           if(nbOfTuples==size1)
755             nbOfComp=size2;
756           else
757             {
758               std::ostringstream oss; oss << "fillArrayWithPyListDbl2 : mismatch between nb of elemts : Input has " << size1 << " tuples and " << size2 << " components";
759               oss << " whereas nb of tuples expected is " << nbOfTuples << " !";
760               throw INTERP_KERNEL::Exception(oss.str().c_str());
761             }
762         }
763     }
764 }
765
766 static void fillArrayWithPyListInt3(PyObject *pyLi, int& nbOfElt, std::vector<int>& ret)
767 {
768   static const char MSG[]="fillArrayWithPyListInt3 : It appears that the input list or tuple is composed by elts having different sizes !";
769   if(PyInt_Check(pyLi))
770     {
771       long val=PyInt_AS_LONG(pyLi);
772       if(nbOfElt==-1)
773         nbOfElt=1;
774       else
775         if(nbOfElt!=1)
776           throw INTERP_KERNEL::Exception(MSG);
777       ret.push_back(val);
778     }
779   else if(PyList_Check(pyLi))
780     {
781       int size=PyList_Size(pyLi);
782       int tmp=0;
783       for(int i=0;i<size;i++)
784         {
785           PyObject *o=PyList_GetItem(pyLi,i);
786           int tmp1=-1;
787           fillArrayWithPyListInt3(o,tmp1,ret);
788           tmp+=tmp1;
789         }
790       if(nbOfElt==-1)
791         nbOfElt=tmp;
792       else
793         {
794           if(nbOfElt!=tmp)
795             throw INTERP_KERNEL::Exception(MSG);
796         }
797     }
798   else if(PyTuple_Check(pyLi))
799     {
800       int size=PyTuple_Size(pyLi);
801       int tmp=0;
802       for(int i=0;i<size;i++)
803         {
804           PyObject *o=PyTuple_GetItem(pyLi,i);
805           int tmp1=-1;
806           fillArrayWithPyListInt3(o,tmp1,ret);
807           tmp+=tmp1;
808         }
809       if(nbOfElt==-1)
810         nbOfElt=tmp;
811       else
812         {
813           if(nbOfElt!=tmp)
814             throw INTERP_KERNEL::Exception(MSG);
815         }
816     }
817   else
818     throw INTERP_KERNEL::Exception("fillArrayWithPyListInt3 : Unrecognized type ! Should be a composition of tuple,list,int !");
819 }
820
821 static std::vector<int> fillArrayWithPyListInt2(PyObject *pyLi, int& nbOfTuples, int& nbOfComp) throw(INTERP_KERNEL::Exception)
822 {
823   std::vector<int> ret;
824   int size1=-1,size2=-1;
825   if(PyList_Check(pyLi))
826     {
827       size1=PyList_Size(pyLi);
828       for(int i=0;i<size1;i++)
829         {
830           PyObject *o=PyList_GetItem(pyLi,i);
831           fillArrayWithPyListInt3(o,size2,ret);
832         }
833       if(size1==0)
834         size2=1;
835     }
836   else if(PyTuple_Check(pyLi))
837     {
838       size1=PyTuple_Size(pyLi);
839       for(int i=0;i<size1;i++)
840         {
841           PyObject *o=PyTuple_GetItem(pyLi,i);
842           fillArrayWithPyListInt3(o,size2,ret);
843         }
844       if(size1==0)
845         size2=1;
846     }
847   else
848     throw INTERP_KERNEL::Exception("fillArrayWithPyListInt2 : Unrecognized type ! Should be a tuple or a list !");
849   //
850   checkFillArrayWithPyList(size1,size2,nbOfTuples,nbOfComp);
851   return ret;
852 }
853
854 static bool fillStringVector(PyObject *pyLi, std::vector<std::string>& vec) throw(INTERP_KERNEL::Exception)
855 {
856   if(PyList_Check(pyLi))
857     {
858       Py_ssize_t sz=PyList_Size(pyLi);
859       vec.resize(sz);
860       for(int i=0;i<sz;i++)
861         {
862           PyObject *o=PyList_GetItem(pyLi,i);
863           if(PyString_Check(o))
864             vec[i]=PyString_AsString(o);
865           else
866             return false;
867         }
868       return true;
869     }
870   else if(PyTuple_Check(pyLi))
871     {
872       Py_ssize_t sz=PyTuple_Size(pyLi);
873       vec.resize(sz);
874       for(int i=0;i<sz;i++)
875         {
876           PyObject *o=PyTuple_GetItem(pyLi,i);
877           if(PyString_Check(o))
878             vec[i]=PyString_AsString(o);
879           else
880             return false;
881         }
882       return true;
883     }
884   else
885     return false;
886 }
887
888 static PyObject *convertDblArrToPyList(const double *ptr, int size) throw(INTERP_KERNEL::Exception)
889 {
890   PyObject *ret=PyList_New(size);
891   for(int i=0;i<size;i++)
892     PyList_SetItem(ret,i,PyFloat_FromDouble(ptr[i]));
893   return ret;
894 }
895
896 static PyObject *convertDblArrToPyList2(const std::vector<double>& v) throw(INTERP_KERNEL::Exception)
897 {
898   int size=v.size();
899   PyObject *ret=PyList_New(size);
900   for(int i=0;i<size;i++)
901     PyList_SetItem(ret,i,PyFloat_FromDouble(v[i]));
902   return ret;
903 }
904
905 static PyObject *convertDblArrToPyListOfTuple(const double *vals, int nbOfComp, int nbOfTuples) throw(INTERP_KERNEL::Exception)
906 {
907   PyObject *ret=PyList_New(nbOfTuples);
908   for(int i=0;i<nbOfTuples;i++)
909     {
910       PyObject *t=PyTuple_New(nbOfComp);
911       for(int j=0;j<nbOfComp;j++)
912         PyTuple_SetItem(t,j,PyFloat_FromDouble(vals[i*nbOfComp+j]));
913       PyList_SetItem(ret,i,t);
914     }
915   return ret;
916 }
917
918 static PyObject *convertCharArrToPyListOfTuple(const char *vals, int nbOfComp, int nbOfTuples) throw(INTERP_KERNEL::Exception)
919 {
920   PyObject *ret=PyList_New(nbOfTuples);
921   INTERP_KERNEL::AutoPtr<char> tmp=new char[nbOfComp+1]; tmp[nbOfComp]='\0';
922   for(int i=0;i<nbOfTuples;i++)
923     {
924       std::copy(vals+i*nbOfComp,vals+(i+1)*nbOfComp,(char *)tmp);
925       PyList_SetItem(ret,i,PyString_FromString(tmp));
926     }
927   return ret;
928 }
929
930 static double *convertPyToNewDblArr2(PyObject *pyLi, int *size) throw(INTERP_KERNEL::Exception)
931 {
932   if(PyList_Check(pyLi))
933     {
934       *size=PyList_Size(pyLi);
935       double *tmp=(double *)malloc((*size)*sizeof(double));
936       for(int i=0;i<*size;i++)
937         {
938           PyObject *o=PyList_GetItem(pyLi,i);
939           if(PyFloat_Check(o))
940             {
941               double val=PyFloat_AS_DOUBLE(o);
942               tmp[i]=val;
943             }
944           else if(PyInt_Check(o))
945             {
946               long val0=PyInt_AS_LONG(o);
947               double val=val0;
948               tmp[i]=val;
949             }
950           else
951             {
952               free(tmp);
953               throw INTERP_KERNEL::Exception("convertPyToNewDblArr2 : list must contain floats/integers only");
954             }
955         }
956       return tmp;
957     }
958   else if(PyTuple_Check(pyLi))
959     {
960       *size=PyTuple_Size(pyLi);
961       double *tmp=(double *)malloc((*size)*sizeof(double));
962       for(int i=0;i<*size;i++)
963         {
964           PyObject *o=PyTuple_GetItem(pyLi,i);
965           if(PyFloat_Check(o))
966             {
967               double val=PyFloat_AS_DOUBLE(o);
968               tmp[i]=val;
969             }
970           else if(PyInt_Check(o))
971             {
972               long val0=PyInt_AS_LONG(o);
973               double val=val0;
974               tmp[i]=val;
975             }
976           else
977             {
978               free(tmp);
979               throw INTERP_KERNEL::Exception("convertPyToNewDblArr2 : tuple must contain floats/integers only");
980             }
981         }
982       return tmp;
983     }
984   else
985     throw INTERP_KERNEL::Exception("convertPyToNewDblArr2 : not a list");
986 }
987
988 static void fillArrayWithPyListDbl3(PyObject *pyLi, int& nbOfElt, std::vector<double>& ret)
989 {
990   static const char MSG[]="fillArrayWithPyListDbl3 : It appears that the input list or tuple is composed by elts having different sizes !";
991   if(PyFloat_Check(pyLi))
992     {
993       if(nbOfElt==-1)
994         nbOfElt=1;
995       else
996         if(nbOfElt!=1)
997           throw INTERP_KERNEL::Exception(MSG);
998       double val=PyFloat_AS_DOUBLE(pyLi);
999       ret.push_back(val);
1000     }
1001   else if(PyInt_Check(pyLi))
1002     {
1003       long val0=PyInt_AS_LONG(pyLi);
1004       double val=val0;
1005       if(nbOfElt==-1)
1006         nbOfElt=1;
1007       else
1008         if(nbOfElt!=1)
1009           throw INTERP_KERNEL::Exception(MSG);
1010       ret.push_back(val);
1011     }
1012   else if(PyList_Check(pyLi))
1013     {
1014       int size=PyList_Size(pyLi);
1015       int tmp=0;
1016       for(int i=0;i<size;i++)
1017         {
1018           PyObject *o=PyList_GetItem(pyLi,i);
1019           int tmp1=-1;
1020           fillArrayWithPyListDbl3(o,tmp1,ret);
1021           tmp+=tmp1;
1022         }
1023       if(nbOfElt==-1)
1024         nbOfElt=tmp;
1025       else
1026         {
1027           if(nbOfElt!=tmp)
1028             throw INTERP_KERNEL::Exception(MSG);
1029         }
1030     }
1031   else if(PyTuple_Check(pyLi))
1032     {
1033       int size=PyTuple_Size(pyLi);
1034       int tmp=0;
1035       for(int i=0;i<size;i++)
1036         {
1037           PyObject *o=PyTuple_GetItem(pyLi,i);
1038           int tmp1=-1;
1039           fillArrayWithPyListDbl3(o,tmp1,ret);
1040           tmp+=tmp1;
1041         }
1042       if(nbOfElt==-1)
1043         nbOfElt=tmp;
1044       else
1045         {
1046           if(nbOfElt!=tmp)
1047             throw INTERP_KERNEL::Exception(MSG);
1048         }
1049     }
1050   else
1051     throw INTERP_KERNEL::Exception("fillArrayWithPyListDbl3 : Unrecognized type ! Should be a composition of tuple,list,int and float !");
1052 }
1053
1054 static std::vector<double> fillArrayWithPyListDbl2(PyObject *pyLi, int& nbOfTuples, int& nbOfComp) throw(INTERP_KERNEL::Exception)
1055 {
1056   std::vector<double> ret;
1057   int size1=-1,size2=-1;
1058   if(PyList_Check(pyLi))
1059     {
1060       size1=PyList_Size(pyLi);
1061       for(int i=0;i<size1;i++)
1062         {
1063           PyObject *o=PyList_GetItem(pyLi,i);
1064           fillArrayWithPyListDbl3(o,size2,ret);
1065         }
1066       if(size1==0)
1067         size2=1;
1068     }
1069   else if(PyTuple_Check(pyLi))
1070     {
1071       size1=PyTuple_Size(pyLi);
1072       for(int i=0;i<size1;i++)
1073         {
1074           PyObject *o=PyTuple_GetItem(pyLi,i);
1075           fillArrayWithPyListDbl3(o,size2,ret);
1076         }
1077       if(size1==0)
1078         size2=1;
1079     }
1080   else
1081     throw INTERP_KERNEL::Exception("fillArrayWithPyListDbl2 : Unrecognized type ! Should be a tuple or a list !");
1082   //
1083   checkFillArrayWithPyList(size1,size2,nbOfTuples,nbOfComp);
1084   return ret;
1085 }
1086
1087 //convertFromPyObjVectorOfObj<const ParaMEDMEM::MEDCouplingUMesh *>(pyLi,SWIGTYPE_p_ParaMEDMEM__MEDCouplingUMesh,"MEDCouplingUMesh")
1088 template<class T>
1089 static void convertFromPyObjVectorOfObj(PyObject *pyLi, swig_type_info *ty, const char *typeStr, typename std::vector<T>& ret)
1090 {
1091   void *argp=0;
1092   if(PyList_Check(pyLi))
1093     {
1094       int size=PyList_Size(pyLi);
1095       ret.resize(size);
1096       for(int i=0;i<size;i++)
1097         {
1098           PyObject *obj=PyList_GetItem(pyLi,i);
1099           int status=SWIG_ConvertPtr(obj,&argp,ty,0|0);
1100           if(!SWIG_IsOK(status))
1101             {
1102               std::ostringstream oss; oss << "convertFromPyObjVectorOfObj : list is excepted to contain only " << typeStr << " instances !";
1103               throw INTERP_KERNEL::Exception(oss.str().c_str());
1104             }
1105           T arg=reinterpret_cast< T >(argp);
1106           ret[i]=arg;
1107         }
1108     }
1109   else if(PyTuple_Check(pyLi))
1110     {
1111       int size=PyTuple_Size(pyLi);
1112       ret.resize(size);
1113       for(int i=0;i<size;i++)
1114         {
1115           PyObject *obj=PyTuple_GetItem(pyLi,i);
1116           int status=SWIG_ConvertPtr(obj,&argp,ty,0|0);
1117           if(!SWIG_IsOK(status))
1118             {
1119               std::ostringstream oss; oss << "convertFromPyObjVectorOfObj : tuple is excepted to contain only " << typeStr << " instances !";
1120               throw INTERP_KERNEL::Exception(oss.str().c_str());
1121             }
1122           T arg=reinterpret_cast< T >(argp);
1123           ret[i]=arg;
1124         }
1125     }
1126   else if(SWIG_IsOK(SWIG_ConvertPtr(pyLi,&argp,ty,0|0)))
1127     {
1128       ret.resize(1);
1129       T arg=reinterpret_cast< T >(argp);
1130       ret[0]=arg;
1131     }
1132   else
1133     throw INTERP_KERNEL::Exception("convertFromPyObjVectorOfObj : not a list nor a tuple");
1134 }
1135
1136 /*!
1137  * if python int -> cpp int sw=1
1138  * if python list[int] -> cpp vector<int> sw=2
1139  * if python tuple[int] -> cpp vector<int> sw=2
1140  * if python DataArrayInt -> cpp DataArrayInt sw=3
1141  * if python DataArrayIntTuple -> cpp DataArrayIntTuple sw=4
1142  *
1143  * switch between (int,vector<int>,DataArrayInt)
1144  */
1145 static void convertObjToPossibleCpp1(PyObject *value, int& sw, int& iTyypp, std::vector<int>& stdvecTyypp, ParaMEDMEM::DataArrayInt *& daIntTyypp, ParaMEDMEM::DataArrayIntTuple *&daIntTuple) throw(INTERP_KERNEL::Exception)
1146 {
1147   sw=-1;
1148   if(PyInt_Check(value))
1149     {
1150       iTyypp=(int)PyInt_AS_LONG(value);
1151       sw=1;
1152       return;
1153     }
1154   if(PyTuple_Check(value))
1155     {
1156       int size=PyTuple_Size(value);
1157       stdvecTyypp.resize(size);
1158       for(int i=0;i<size;i++)
1159         {
1160           PyObject *o=PyTuple_GetItem(value,i);
1161           if(PyInt_Check(o))
1162             stdvecTyypp[i]=(int)PyInt_AS_LONG(o);
1163           else
1164             {
1165               std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not integer ! only tuples of integers accepted !";
1166               throw INTERP_KERNEL::Exception(oss.str().c_str());
1167             }
1168         }
1169       sw=2;
1170       return;
1171     }
1172   if(PyList_Check(value))
1173     {
1174       int size=PyList_Size(value);
1175       stdvecTyypp.resize(size);
1176       for(int i=0;i<size;i++)
1177         {
1178           PyObject *o=PyList_GetItem(value,i);
1179           if(PyInt_Check(o))
1180             stdvecTyypp[i]=(int)PyInt_AS_LONG(o);
1181           else
1182             {
1183               std::ostringstream oss; oss << "List as been detected but element #" << i << " is not integer ! only lists of integers accepted !";
1184               throw INTERP_KERNEL::Exception(oss.str().c_str());
1185             }
1186         }
1187       sw=2;
1188       return;
1189     }
1190   void *argp;
1191   int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayInt,0|0);
1192   if(SWIG_IsOK(status))
1193     {
1194       daIntTyypp=reinterpret_cast< ParaMEDMEM::DataArrayInt * >(argp);
1195       sw=3;
1196       return;
1197     }
1198   status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayIntTuple,0|0);
1199   if(SWIG_IsOK(status))
1200     {  
1201       daIntTuple=reinterpret_cast< ParaMEDMEM::DataArrayIntTuple * >(argp);
1202       sw=4;
1203       return ;
1204     }
1205   throw INTERP_KERNEL::Exception("5 types accepted : integer, tuple of integer, list of integer, DataArrayInt, DataArrayIntTuple");
1206 }
1207
1208 /*!
1209  * if python double -> cpp double sw=1
1210  * if python int -> cpp double sw=1
1211  * if python list[double] -> cpp vector<double> sw=2
1212  * if python list[int] -> cpp vector<double> sw=2
1213  * if python tuple[double] -> cpp vector<double> sw=2
1214  * if python tuple[int] -> cpp vector<double> sw=2
1215  * if python DataArrayDouble -> cpp DataArrayDouble sw=3
1216  *
1217  * switch between (int,vector<int>,DataArrayInt)
1218  */
1219 static void convertObjToPossibleCpp4(PyObject *value, int& sw, double& iTyypp, std::vector<double>& stdvecTyypp, ParaMEDMEM::DataArrayDouble *& daIntTyypp) throw(INTERP_KERNEL::Exception)
1220 {
1221   sw=-1;
1222   if(PyFloat_Check(value))
1223     {
1224       iTyypp=PyFloat_AS_DOUBLE(value);
1225       sw=1;
1226       return;
1227     }
1228   if(PyInt_Check(value))
1229     {
1230       iTyypp=(double)PyInt_AS_LONG(value);
1231       sw=1;
1232       return;
1233     }
1234   if(PyTuple_Check(value))
1235     {
1236       int size=PyTuple_Size(value);
1237       stdvecTyypp.resize(size);
1238       for(int i=0;i<size;i++)
1239         {
1240           PyObject *o=PyTuple_GetItem(value,i);
1241           if(PyFloat_Check(o))
1242             stdvecTyypp[i]=PyFloat_AS_DOUBLE(o);
1243           else if(PyInt_Check(o))
1244             stdvecTyypp[i]=(double)PyInt_AS_LONG(o);
1245           else
1246             {
1247               std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not double ! only tuples of doubles accepted or integer !";
1248               throw INTERP_KERNEL::Exception(oss.str().c_str());
1249             }
1250         }
1251       sw=2;
1252       return;
1253     }
1254   if(PyList_Check(value))
1255     {
1256       int size=PyList_Size(value);
1257       stdvecTyypp.resize(size);
1258       for(int i=0;i<size;i++)
1259         {
1260           PyObject *o=PyList_GetItem(value,i);
1261           if(PyFloat_Check(o))
1262             stdvecTyypp[i]=PyFloat_AS_DOUBLE(o);
1263           else if(PyInt_Check(o))
1264             stdvecTyypp[i]=(double)PyInt_AS_LONG(o);
1265           else
1266             {
1267               std::ostringstream oss; oss << "List as been detected but element #" << i << " is not double ! only lists of doubles accepted or integer !";
1268               throw INTERP_KERNEL::Exception(oss.str().c_str());
1269             }
1270         }
1271       sw=2;
1272       return;
1273     }
1274   void *argp;
1275   int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayDouble,0|0);
1276   if(!SWIG_IsOK(status))
1277     throw INTERP_KERNEL::Exception("5 types accepted : double float, integer, tuple of double float or int, list of double float or int, DataArrayDouble");
1278   daIntTyypp=reinterpret_cast< ParaMEDMEM::DataArrayDouble * >(argp);
1279   sw=3;
1280 }
1281
1282 /*!
1283  * if python double -> cpp double sw=1
1284  * if python int -> cpp double sw=1
1285  * if python list[double] -> cpp vector<double> sw=2
1286  * if python list[int] -> cpp vector<double> sw=2
1287  * if python tuple[double] -> cpp vector<double> sw=2
1288  * if python tuple[int] -> cpp vector<double> sw=2
1289  * if python DataArrayDoubleTuple -> cpp DataArrayDoubleTuple sw=3
1290  *
1291  * switch between (int,vector<int>,DataArrayInt)
1292  */
1293 static void convertObjToPossibleCpp44(PyObject *value, int& sw, double& iTyypp, std::vector<double>& stdvecTyypp, ParaMEDMEM::DataArrayDoubleTuple *& daIntTyypp) throw(INTERP_KERNEL::Exception)
1294 {
1295   sw=-1;
1296   if(PyFloat_Check(value))
1297     {
1298       iTyypp=PyFloat_AS_DOUBLE(value);
1299       sw=1;
1300       return;
1301     }
1302   if(PyInt_Check(value))
1303     {
1304       iTyypp=(double)PyInt_AS_LONG(value);
1305       sw=1;
1306       return;
1307     }
1308   if(PyTuple_Check(value))
1309     {
1310       int size=PyTuple_Size(value);
1311       stdvecTyypp.resize(size);
1312       for(int i=0;i<size;i++)
1313         {
1314           PyObject *o=PyTuple_GetItem(value,i);
1315           if(PyFloat_Check(o))
1316             stdvecTyypp[i]=PyFloat_AS_DOUBLE(o);
1317           else if(PyInt_Check(o))
1318             stdvecTyypp[i]=(double)PyInt_AS_LONG(o);
1319           else
1320             {
1321               std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not double ! only tuples of doubles accepted or integer !";
1322               throw INTERP_KERNEL::Exception(oss.str().c_str());
1323             }
1324         }
1325       sw=2;
1326       return;
1327     }
1328   if(PyList_Check(value))
1329     {
1330       int size=PyList_Size(value);
1331       stdvecTyypp.resize(size);
1332       for(int i=0;i<size;i++)
1333         {
1334           PyObject *o=PyList_GetItem(value,i);
1335           if(PyFloat_Check(o))
1336             stdvecTyypp[i]=PyFloat_AS_DOUBLE(o);
1337           else if(PyInt_Check(o))
1338             stdvecTyypp[i]=(double)PyInt_AS_LONG(o);
1339           else
1340             {
1341               std::ostringstream oss; oss << "List as been detected but element #" << i << " is not double ! only lists of doubles accepted or integer !";
1342               throw INTERP_KERNEL::Exception(oss.str().c_str());
1343             }
1344         }
1345       sw=2;
1346       return;
1347     }
1348   void *argp;
1349   int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayDoubleTuple,0|0);
1350   if(!SWIG_IsOK(status))
1351     throw INTERP_KERNEL::Exception("5 types accepted : double float, integer, tuple of double float or int, list of double float or int, DataArrayDoubleTuple");
1352   daIntTyypp=reinterpret_cast< ParaMEDMEM::DataArrayDoubleTuple * >(argp);
1353   sw=3;
1354 }
1355
1356 /*!
1357  * if python int -> cpp int sw=1
1358  * if python list[int] -> cpp vector<int> sw=2
1359  * if python tuple[int] -> cpp vector<int> sw=2
1360  * if python slicp -> cpp pair sw=3 (begin,end,step)
1361  * if python DataArrayInt -> cpp DataArrayInt sw=4 . The returned pointer cannot be the null pointer ! If null an exception is thrown.
1362  *
1363  * switch between (int,vector<int>,DataArrayInt)
1364  */
1365 static void convertObjToPossibleCpp2(PyObject *value, int nbelem, int& sw, int& iTyypp, std::vector<int>& stdvecTyypp, std::pair<int, std::pair<int,int> >& p, ParaMEDMEM::DataArrayInt *& daIntTyypp) throw(INTERP_KERNEL::Exception)
1366 {
1367   const char *msg="5 types accepted : integer, tuple of integer, list of integer, slice, DataArrayInt, DataArrayIntTuple";
1368   sw=-1;
1369   if(PyInt_Check(value))
1370     {
1371       iTyypp=(int)PyInt_AS_LONG(value);
1372       sw=1;
1373       return;
1374     }
1375   if(PyTuple_Check(value))
1376     {
1377       int size=PyTuple_Size(value);
1378       stdvecTyypp.resize(size);
1379       for(int i=0;i<size;i++)
1380         {
1381           PyObject *o=PyTuple_GetItem(value,i);
1382           if(PyInt_Check(o))
1383             stdvecTyypp[i]=(int)PyInt_AS_LONG(o);
1384           else
1385             {
1386               std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not integer ! only tuples of integers accepted !";
1387               throw INTERP_KERNEL::Exception(oss.str().c_str());
1388             }
1389         }
1390       sw=2;
1391       return;
1392     }
1393   if(PyList_Check(value))
1394     {
1395       int size=PyList_Size(value);
1396       stdvecTyypp.resize(size);
1397       for(int i=0;i<size;i++)
1398         {
1399           PyObject *o=PyList_GetItem(value,i);
1400           if(PyInt_Check(o))
1401             stdvecTyypp[i]=(int)PyInt_AS_LONG(o);
1402           else
1403             {
1404               std::ostringstream oss; oss << "List as been detected but element #" << i << " is not integer ! only lists of integers accepted !";
1405               throw INTERP_KERNEL::Exception(oss.str().c_str());
1406             }
1407         }
1408       sw=2;
1409       return;
1410     }
1411   if(PySlice_Check(value))
1412     {
1413       Py_ssize_t strt=2,stp=2,step=2;
1414       PySliceObject *oC=reinterpret_cast<PySliceObject *>(value);
1415       if(PySlice_GetIndices(oC,nbelem,&strt,&stp,&step)!=0)
1416         if(nbelem!=0 || strt!=0 || stp!=0)
1417           {
1418             std::ostringstream oss; oss << "Slice in subscriptable object DataArray invalid : number of elements is : " << nbelem;
1419             throw INTERP_KERNEL::Exception(oss.str().c_str());
1420           }
1421       p.first=strt;
1422       p.second.first=stp;
1423       p.second.second=step;
1424       sw=3;
1425       return ;
1426     }
1427   void *argp;
1428   int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayInt,0|0);
1429   if(SWIG_IsOK(status))
1430     {
1431       daIntTyypp=reinterpret_cast< ParaMEDMEM::DataArrayInt * >(argp);
1432       if(!daIntTyypp)
1433         {
1434           std::ostringstream oss; oss << msg << " Instance in null !";
1435           throw INTERP_KERNEL::Exception(oss.str().c_str());
1436         }
1437       sw=4;
1438       return ;
1439     }
1440   status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayIntTuple,0|0);
1441   if(SWIG_IsOK(status))
1442     {
1443       ParaMEDMEM::DataArrayIntTuple *tmp=reinterpret_cast< ParaMEDMEM::DataArrayIntTuple * >(argp);
1444       if(!tmp)
1445         {
1446           std::ostringstream oss; oss << msg << " Instance in null !";
1447           throw INTERP_KERNEL::Exception(oss.str().c_str());
1448         }
1449       stdvecTyypp.resize(tmp->getNumberOfCompo());
1450       std::copy(tmp->getConstPointer(),tmp->getConstPointer()+tmp->getNumberOfCompo(),stdvecTyypp.begin());
1451       sw=2;
1452       return ;
1453     }
1454   throw INTERP_KERNEL::Exception(msg);
1455 }
1456
1457 /*!
1458  * if python int -> cpp int sw=1
1459  * if python tuple[int] -> cpp vector<int> sw=2
1460  * if python list[int] -> cpp vector<int> sw=2
1461  * if python slice -> cpp pair sw=3
1462  * if python DataArrayIntTuple -> cpp DataArrayIntTuple sw=4 . WARNING The returned pointer can be the null pointer !
1463  */
1464 static void convertObjToPossibleCpp22(PyObject *value, int nbelem, int& sw, int& iTyypp, std::vector<int>& stdvecTyypp, std::pair<int, std::pair<int,int> >& p, ParaMEDMEM::DataArrayIntTuple *& daIntTyypp) throw(INTERP_KERNEL::Exception)
1465 {
1466   sw=-1;
1467   if(PyInt_Check(value))
1468     {
1469       iTyypp=(int)PyInt_AS_LONG(value);
1470       sw=1;
1471       return;
1472     }
1473   if(PyTuple_Check(value))
1474     {
1475       int size=PyTuple_Size(value);
1476       stdvecTyypp.resize(size);
1477       for(int i=0;i<size;i++)
1478         {
1479           PyObject *o=PyTuple_GetItem(value,i);
1480           if(PyInt_Check(o))
1481             stdvecTyypp[i]=(int)PyInt_AS_LONG(o);
1482           else
1483             {
1484               std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not integer ! only tuples of integers accepted !";
1485               throw INTERP_KERNEL::Exception(oss.str().c_str());
1486             }
1487         }
1488       sw=2;
1489       return;
1490     }
1491   if(PyList_Check(value))
1492     {
1493       int size=PyList_Size(value);
1494       stdvecTyypp.resize(size);
1495       for(int i=0;i<size;i++)
1496         {
1497           PyObject *o=PyList_GetItem(value,i);
1498           if(PyInt_Check(o))
1499             stdvecTyypp[i]=(int)PyInt_AS_LONG(o);
1500           else
1501             {
1502               std::ostringstream oss; oss << "List as been detected but element #" << i << " is not integer ! only lists of integers accepted !";
1503               throw INTERP_KERNEL::Exception(oss.str().c_str());
1504             }
1505         }
1506       sw=2;
1507       return;
1508     }
1509   if(PySlice_Check(value))
1510     {
1511       Py_ssize_t strt=2,stp=2,step=2;
1512       PySliceObject *oC=reinterpret_cast<PySliceObject *>(value);
1513       if(PySlice_GetIndices(oC,nbelem,&strt,&stp,&step)!=0)
1514         if(nbelem!=0 || strt!=0 || stp!=0)
1515           {
1516             std::ostringstream oss; oss << "Slice in subscriptable object DataArray invalid : number of elements is : " << nbelem;
1517             throw INTERP_KERNEL::Exception(oss.str().c_str());
1518           }
1519       p.first=strt;
1520       p.second.first=stp;
1521       p.second.second=step;
1522       sw=3;
1523       return ;
1524     }
1525   void *argp;
1526   int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayIntTuple,0|0);
1527   if(!SWIG_IsOK(status))
1528     throw INTERP_KERNEL::Exception("4 types accepted : integer, tuple of integer, list of integer, slice, DataArrayIntTuple");
1529   daIntTyypp=reinterpret_cast< ParaMEDMEM::DataArrayIntTuple * >(argp);
1530   sw=4;
1531 }
1532
1533 /*!
1534  * if python string with size one -> cpp char sw=1
1535  * if python string with size different from one -> cpp string sw=2
1536  * if python tuple[string] or list[string] -> vector<string> sw=3
1537  * if python not null pointer of DataArrayChar -> cpp DataArrayChar sw=4
1538  * switch between (int,string,vector<string>,DataArrayChar)
1539  */
1540 static void convertObjToPossibleCpp6(PyObject *value, int& sw, char& cTyp, std::string& sType, std::vector<std::string>& vsType, ParaMEDMEM::DataArrayChar *& dacType) throw(INTERP_KERNEL::Exception)
1541 {
1542   const char *msg="4 types accepted : string, list or tuple of strings having same size, not null DataArrayChar instance.";
1543   sw=-1;
1544   if(PyString_Check(value))
1545     {
1546       const char *pt=PyString_AsString(value);
1547       Py_ssize_t sz=PyString_Size(value);
1548       if(sz==1)
1549         {
1550           cTyp=pt[0];
1551           sw=1;
1552           return;
1553         }
1554       else
1555         {
1556           sType=pt;
1557           sw=2;
1558           return;
1559         }
1560     }
1561   if(PyTuple_Check(value))
1562     {
1563       int size=PyTuple_Size(value);
1564       vsType.resize(size);
1565       for(int i=0;i<size;i++)
1566         {
1567           PyObject *o=PyTuple_GetItem(value,i);
1568           if(PyString_Check(o))
1569             vsType[i]=PyString_AsString(o);
1570           else
1571             {
1572               std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not a string ! only tuples of strings accepted !";
1573               throw INTERP_KERNEL::Exception(oss.str().c_str());
1574             }
1575         }
1576       sw=3;
1577       return;
1578     }
1579   if(PyList_Check(value))
1580     {
1581       int size=PyList_Size(value);
1582       vsType.resize(size);
1583       for(int i=0;i<size;i++)
1584         {
1585           PyObject *o=PyList_GetItem(value,i);
1586           if(PyString_Check(o))
1587             vsType[i]=PyString_AsString(o);
1588           else
1589             {
1590               std::ostringstream oss; oss << "List as been detected but element #" << i << " is not string ! only lists of strings accepted !";
1591               throw INTERP_KERNEL::Exception(oss.str().c_str());
1592             }
1593         }
1594       sw=3;
1595       return;
1596     }
1597   void *argp;
1598   int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayChar,0|0);
1599   if(SWIG_IsOK(status))
1600     {
1601       dacType=reinterpret_cast< ParaMEDMEM::DataArrayChar * >(argp);
1602       if(!dacType)
1603         {
1604           std::ostringstream oss; oss << msg << " Instance in null !";
1605           throw INTERP_KERNEL::Exception(oss.str().c_str());
1606         }
1607       sw=4;
1608       return ;
1609     }
1610   throw INTERP_KERNEL::Exception(msg);
1611 }
1612
1613 /*!
1614  * if value int -> cpp it sw=1
1615  * if value list[int] -> vt sw=2
1616  * if value tuple[int] -> vt sw=2
1617  * if value slice -> pt sw=3
1618  * if value DataArrayInt -> dt sw=4
1619  * if value tuple [int,int] -> cpp it,ip sw=5
1620  * if value tuple [list[int],int] -> cpp vt,ip sw=6
1621  * if value tuple [tuple[int],int] -> cpp vt,ip sw=6
1622  * if value tuple [slice,int] -> cpp pt,ip sw=7
1623  * if value tuple [DaI,int] -> cpp dt,ip sw=8
1624  * if value tuple [int,list[int]] -> cpp it,vc sw=9
1625  * if value tuple [list[int],list[int]] -> cpp vt,vc sw=10
1626  * if value tuple [tuple[int],list[int]] -> cpp vt,vc sw=10
1627  * if value tuple [slice,list[int]] -> cpp pt,vc sw=11
1628  * if value tuple [DaI,list[int]] -> cpp dt,vc sw=12
1629  * if value tuple [int,tuple[int]] -> cpp it,vc sw=9
1630  * if value tuple [list[int],tuple[int]] -> cpp vt,vc sw=10
1631  * if value tuple [tuple[int],tuple[int]] -> cpp vt,vc sw=10
1632  * if value tuple [slice,tuple[int]] -> cpp pt,vc sw=11
1633  * if value tuple [DaI,tuple[int]] -> cpp dt,vc sw=12
1634  * if value tuple [int,slice] -> cpp it,pc sw=13
1635  * if value tuple [list[int],slice] -> cpp vt,pc sw=14
1636  * if value tuple [tuple[int],slice] -> cpp vt,pc sw=14
1637  * if value tuple [slice,slice] -> cpp pt,pc sw=15
1638  * if value tuple [DaI,slice] -> cpp dt,pc sw=16
1639  *
1640  * switch between (int,vector<int>,DataArrayInt)
1641  */
1642 static void convertObjToPossibleCpp3(PyObject *value, int nbTuple, int nbCompo, int& sw, int& it, int& ic, std::vector<int>& vt, std::vector<int>& vc,
1643                                      std::pair<int, std::pair<int,int> >& pt, std::pair<int, std::pair<int,int> >& pc,
1644                                      ParaMEDMEM::DataArrayInt *&dt, ParaMEDMEM::DataArrayInt *&dc) throw(INTERP_KERNEL::Exception)
1645 {
1646   if(!PyTuple_Check(value))
1647     {
1648       convertObjToPossibleCpp2(value,nbTuple,sw,it,vt,pt,dt);
1649       return ;
1650     }
1651   else
1652     {
1653       int sz=PyTuple_Size(value);
1654       if(sz!=2)
1655         throw INTERP_KERNEL::Exception("Unexpected nb of slice element : 1 or 2 expected !\n1st is for tuple selection, 2nd for component selection !");
1656       PyObject *ob0=PyTuple_GetItem(value,0);
1657       int sw1,sw2;
1658       convertObjToPossibleCpp2(ob0,nbTuple,sw1,it,vt,pt,dt);
1659       PyObject *ob1=PyTuple_GetItem(value,1);
1660       convertObjToPossibleCpp2(ob1,nbCompo,sw2,ic,vc,pc,dc);
1661       sw=4*sw2+sw1;
1662     }
1663 }
1664
1665 /*!
1666  * if value int -> cpp val sw=1
1667  * if value double -> cpp val sw=1
1668  * if value DataArrayDouble -> cpp DataArrayDouble sw=2
1669  * if value DataArrayDoubleTuple -> cpp DataArrayDoubleTuple sw=3
1670  * if value list[int,double] -> cpp std::vector<double> sw=4
1671  * if value tuple[int,double] -> cpp std::vector<double> sw=4
1672  */
1673 static void convertObjToPossibleCpp5(PyObject *value, int& sw, double& val, ParaMEDMEM::DataArrayDouble *&d, ParaMEDMEM::DataArrayDoubleTuple *&e, std::vector<double>& f)
1674 {
1675   sw=-1;
1676   if(PyFloat_Check(value))
1677     {
1678       val=PyFloat_AS_DOUBLE(value);
1679       sw=1;
1680       return;
1681     }
1682   if(PyInt_Check(value))
1683     {
1684       val=(double)PyInt_AS_LONG(value);
1685       sw=1;
1686       return;
1687     }
1688   if(PyTuple_Check(value))
1689     {
1690       int size=PyTuple_Size(value);
1691       f.resize(size);
1692       for(int i=0;i<size;i++)
1693         {
1694           PyObject *o=PyTuple_GetItem(value,i);
1695           if(PyFloat_Check(o))
1696             f[i]=PyFloat_AS_DOUBLE(o);
1697           else if(PyInt_Check(o))
1698             f[i]=(double)PyInt_AS_LONG(o);
1699           else
1700             {
1701               std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not double ! only tuples of doubles accepted or integer !";
1702               throw INTERP_KERNEL::Exception(oss.str().c_str());
1703             }
1704         }
1705       sw=4;
1706       return;
1707     }
1708   if(PyList_Check(value))
1709     {
1710       int size=PyList_Size(value);
1711       f.resize(size);
1712       for(int i=0;i<size;i++)
1713         {
1714           PyObject *o=PyList_GetItem(value,i);
1715           if(PyFloat_Check(o))
1716             f[i]=PyFloat_AS_DOUBLE(o);
1717           else if(PyInt_Check(o))
1718             f[i]=(double)PyInt_AS_LONG(o);
1719           else
1720             {
1721               std::ostringstream oss; oss << "List as been detected but element #" << i << " is not double ! only lists of doubles accepted or integer !";
1722               throw INTERP_KERNEL::Exception(oss.str().c_str());
1723             }
1724         }
1725       sw=4;
1726       return;
1727     }
1728   void *argp;
1729   int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayDouble,0|0);
1730   if(SWIG_IsOK(status))
1731     {  
1732       d=reinterpret_cast< ParaMEDMEM::DataArrayDouble * >(argp);
1733       sw=2;
1734       return ;
1735     }
1736   status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayDoubleTuple,0|0);
1737   if(SWIG_IsOK(status))
1738     {  
1739       e=reinterpret_cast< ParaMEDMEM::DataArrayDoubleTuple * >(argp);
1740       sw=3;
1741       return ;
1742     }
1743   throw INTERP_KERNEL::Exception("4 types accepted : integer, double, DataArrayDouble, DataArrayDoubleTuple");
1744 }
1745
1746 /*!
1747  * if value int -> cpp val sw=1
1748  * if value double -> cpp val sw=1
1749  * if value DataArrayDouble -> cpp DataArrayDouble sw=2
1750  * if value DataArrayDoubleTuple -> cpp DataArrayDoubleTuple sw=3
1751  * if value list[int,double] -> cpp std::vector<double> sw=4
1752  * if value tuple[int,double] -> cpp std::vector<double> sw=4
1753  */
1754 static const double *convertObjToPossibleCpp5_Safe(PyObject *value, int& sw, double& val, ParaMEDMEM::DataArrayDouble *&d, ParaMEDMEM::DataArrayDoubleTuple *&e, std::vector<double>& f,
1755                                                    const char *msg, int nbTuplesExpected, int nbCompExpected, bool throwIfNullPt) throw(INTERP_KERNEL::Exception)
1756 {
1757   sw=-1;
1758   if(PyFloat_Check(value))
1759     {
1760       val=PyFloat_AS_DOUBLE(value);
1761       sw=1;
1762       if(nbTuplesExpected*nbCompExpected!=1)
1763         {
1764           std::ostringstream oss; oss << msg << "dimension expected to be " << nbTuplesExpected*nbCompExpected << " , and your data in input has dimension one (single PyFloat) !"; 
1765           throw INTERP_KERNEL::Exception(oss.str().c_str());
1766         }
1767       return &val;
1768     }
1769   if(PyInt_Check(value))
1770     {
1771       val=(double)PyInt_AS_LONG(value);
1772       sw=1;
1773       if(nbTuplesExpected*nbCompExpected!=1)
1774         {
1775           std::ostringstream oss; oss << msg << "dimension expected to be " << nbTuplesExpected*nbCompExpected << " , and your data in input has dimension one (single PyInt) !"; 
1776           throw INTERP_KERNEL::Exception(oss.str().c_str());
1777         }
1778       return &val;
1779     }
1780   if(PyTuple_Check(value) || PyList_Check(value))
1781     {
1782       try
1783         {
1784           int tmp1=nbTuplesExpected,tmp2=nbCompExpected;
1785           std::vector<double> ret=fillArrayWithPyListDbl2(value,tmp1,tmp2);
1786           sw=4;
1787           f=ret;
1788           return &f[0];
1789         }
1790       catch(INTERP_KERNEL::Exception& exc) { throw exc; }
1791     }
1792   void *argp;
1793   int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayDouble,0|0);
1794   if(SWIG_IsOK(status))
1795     {  
1796       d=reinterpret_cast< ParaMEDMEM::DataArrayDouble * >(argp);
1797       sw=2;
1798       if(d)
1799         {
1800           if(d->getNumberOfTuples()==nbTuplesExpected)
1801             {
1802               if(d->getNumberOfComponents()==nbCompExpected)
1803                 {
1804                   return d->getConstPointer();
1805                 }
1806               else
1807                 {
1808                   std::ostringstream oss; oss << msg << "nb of components expected to be " << nbCompExpected << " , and input has " << d->getNumberOfComponents() << " components !";
1809                   throw INTERP_KERNEL::Exception(oss.str().c_str());
1810                 }
1811             }
1812           else
1813             {
1814               std::ostringstream oss; oss << msg << " input DataArrayDouble should have a number of tuples equal to " << nbTuplesExpected << " and there are " << d->getNumberOfTuples() << " tuples !";
1815               throw INTERP_KERNEL::Exception(oss.str().c_str());
1816             }
1817         }
1818       else
1819         {
1820           if(throwIfNullPt)
1821             {
1822               std::ostringstream oss; oss << msg << " null pointer not accepted!";
1823               throw INTERP_KERNEL::Exception(oss.str().c_str());
1824             }
1825           else
1826             return 0;
1827         }
1828     }
1829   status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayDoubleTuple,0|0);
1830   if(SWIG_IsOK(status))
1831     {  
1832       e=reinterpret_cast< ParaMEDMEM::DataArrayDoubleTuple * >(argp);
1833       sw=3;
1834       if(e->getNumberOfCompo()==nbCompExpected)
1835         {
1836           if(nbTuplesExpected==1)
1837             return e->getConstPointer();
1838           else
1839             {
1840               std::ostringstream oss; oss << msg << "nb of tuples expected to be " << nbTuplesExpected << " , and input DataArrayDoubleTuple has always one tuple by contruction !";
1841               throw INTERP_KERNEL::Exception(oss.str().c_str());
1842             }
1843         }
1844       else
1845         {
1846           std::ostringstream oss; oss << msg << "nb of components expected to be " <<  nbCompExpected << " , and input DataArrayDoubleTuple has " << e->getNumberOfCompo() << " components !";
1847           throw INTERP_KERNEL::Exception(oss.str().c_str());
1848         }
1849     }
1850   throw INTERP_KERNEL::Exception("4 types accepted : integer, double, DataArrayDouble, DataArrayDoubleTuple");
1851 }
1852
1853 /*!
1854  * if value int -> cpp val sw=1
1855  * if value double -> cpp val sw=1
1856  * if value DataArrayDouble -> cpp DataArrayDouble sw=2
1857  * if value DataArrayDoubleTuple -> cpp DataArrayDoubleTuple sw=3
1858  * if value list[int,double] -> cpp std::vector<double> sw=4
1859  * if value tuple[int,double] -> cpp std::vector<double> sw=4
1860  */
1861 static const double *convertObjToPossibleCpp5_Safe2(PyObject *value, int& sw, double& val, ParaMEDMEM::DataArrayDouble *&d, ParaMEDMEM::DataArrayDoubleTuple *&e, std::vector<double>& f,
1862                                                     const char *msg, int nbCompExpected, bool throwIfNullPt, int& nbTuples) throw(INTERP_KERNEL::Exception)
1863 {
1864   sw=-1;
1865   if(PyFloat_Check(value))
1866     {
1867       val=PyFloat_AS_DOUBLE(value);
1868       sw=1;
1869       if(nbCompExpected!=1)
1870         {
1871           std::ostringstream oss; oss << msg << "dimension expected to be " << nbCompExpected << " , and your data in input has dimension one (single PyFloat) !"; 
1872           throw INTERP_KERNEL::Exception(oss.str().c_str());
1873         }
1874       nbTuples=1;
1875       return &val;
1876     }
1877   if(PyInt_Check(value))
1878     {
1879       val=(double)PyInt_AS_LONG(value);
1880       sw=1;
1881       if(nbCompExpected!=1)
1882         {
1883           std::ostringstream oss; oss << msg << "dimension expected to be " << nbCompExpected << " , and your data in input has dimension one (single PyInt) !"; 
1884           throw INTERP_KERNEL::Exception(oss.str().c_str());
1885         }
1886       nbTuples=1;
1887       return &val;
1888     }
1889   if(PyTuple_Check(value))
1890     {
1891       int size=PyTuple_Size(value);
1892       f.resize(size);
1893       for(int i=0;i<size;i++)
1894         {
1895           PyObject *o=PyTuple_GetItem(value,i);
1896           if(PyFloat_Check(o))
1897             f[i]=PyFloat_AS_DOUBLE(o);
1898           else if(PyInt_Check(o))
1899             f[i]=(double)PyInt_AS_LONG(o);
1900           else
1901             {
1902               std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not double ! only tuples of doubles accepted or integer !";
1903               throw INTERP_KERNEL::Exception(oss.str().c_str());
1904             }
1905         }
1906       sw=4;
1907       if(size%nbCompExpected!=0)
1908         {
1909           std::ostringstream oss; oss << msg << "dimension expected to be a multiple of " << nbCompExpected << " , and your data in input has dimension " << f.size() << " !"; 
1910           throw INTERP_KERNEL::Exception(oss.str().c_str());
1911         }
1912       nbTuples=size/nbCompExpected;
1913       return &f[0];
1914     }
1915   if(PyList_Check(value))
1916     {
1917       int size=PyList_Size(value);
1918       f.resize(size);
1919       for(int i=0;i<size;i++)
1920         {
1921           PyObject *o=PyList_GetItem(value,i);
1922           if(PyFloat_Check(o))
1923             f[i]=PyFloat_AS_DOUBLE(o);
1924           else if(PyInt_Check(o))
1925             f[i]=(double)PyInt_AS_LONG(o);
1926           else
1927             {
1928               std::ostringstream oss; oss << "List as been detected but element #" << i << " is not double ! only lists of doubles accepted or integer !";
1929               throw INTERP_KERNEL::Exception(oss.str().c_str());
1930             }
1931         }
1932       sw=4;
1933       if(size%nbCompExpected!=0)
1934         {
1935           std::ostringstream oss; oss << msg << "dimension expected to be a multiple of " << nbCompExpected << " , and your data in input has dimension " << f.size() << " !"; 
1936           throw INTERP_KERNEL::Exception(oss.str().c_str());
1937         }
1938       nbTuples=size/nbCompExpected;
1939       return &f[0];
1940     }
1941   void *argp;
1942   int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayDouble,0|0);
1943   if(SWIG_IsOK(status))
1944     {  
1945       d=reinterpret_cast< ParaMEDMEM::DataArrayDouble * >(argp);
1946       sw=2;
1947       if(d)
1948         {
1949           if(d->getNumberOfComponents()==nbCompExpected)
1950             {
1951               nbTuples=d->getNumberOfTuples();
1952               return d->getConstPointer();
1953             }
1954           else
1955             {
1956               std::ostringstream oss; oss << msg << "nb of components expected to be a multiple of " << nbCompExpected << " , and input has " << d->getNumberOfComponents() << " components !";
1957               throw INTERP_KERNEL::Exception(oss.str().c_str());
1958             }
1959         }
1960       else
1961         {
1962           if(throwIfNullPt)
1963             {
1964               std::ostringstream oss; oss << msg << " null pointer not accepted!";
1965               throw INTERP_KERNEL::Exception(oss.str().c_str());
1966             }
1967           else
1968             { nbTuples=0; return 0; }
1969         }
1970     }
1971   status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayDoubleTuple,0|0);
1972   if(SWIG_IsOK(status))
1973     {  
1974       e=reinterpret_cast< ParaMEDMEM::DataArrayDoubleTuple * >(argp);
1975       sw=3;
1976       if(e)
1977         {
1978           if(e->getNumberOfCompo()==nbCompExpected)
1979             {
1980               nbTuples=1;
1981               return e->getConstPointer();
1982             }
1983           else
1984             {
1985               std::ostringstream oss; oss << msg << "nb of components expected to be " <<  nbCompExpected << " , and input DataArrayDoubleTuple has " << e->getNumberOfCompo() << " components !";
1986               throw INTERP_KERNEL::Exception(oss.str().c_str());
1987             }
1988         }
1989       else
1990         {
1991           if(throwIfNullPt)
1992             {
1993               std::ostringstream oss; oss << msg << " null pointer not accepted!";
1994               throw INTERP_KERNEL::Exception(oss.str().c_str());
1995             }
1996           else
1997             { nbTuples=0; return 0; }
1998         }
1999     }
2000   throw INTERP_KERNEL::Exception("4 types accepted : integer, double, DataArrayDouble, DataArrayDoubleTuple");
2001 }
2002
2003 /*!
2004  * if value int -> cpp val sw=1
2005  * if value double -> cpp val sw=1
2006  * if value DataArrayDouble -> cpp DataArrayDouble sw=2
2007  * if value DataArrayDoubleTuple -> cpp DataArrayDoubleTuple sw=3
2008  * if value list[int,double] -> cpp std::vector<double> sw=4
2009  * if value tuple[int,double] -> cpp std::vector<double> sw=4
2010  */
2011 static const double *convertObjToPossibleCpp5_SingleCompo(PyObject *value, int& sw, double& val, std::vector<double>& f,
2012                                                           const char *msg, bool throwIfNullPt, int& nbTuples) throw(INTERP_KERNEL::Exception)
2013 {
2014   ParaMEDMEM::DataArrayDouble *d=0;
2015   ParaMEDMEM::DataArrayDoubleTuple *e=0;
2016   sw=-1;
2017   if(PyFloat_Check(value))
2018     {
2019       val=PyFloat_AS_DOUBLE(value);
2020       sw=1;
2021       nbTuples=1;
2022       return &val;
2023     }
2024   if(PyInt_Check(value))
2025     {
2026       val=(double)PyInt_AS_LONG(value);
2027       sw=1;
2028       nbTuples=1;
2029       return &val;
2030     }
2031   if(PyTuple_Check(value))
2032     {
2033       int size=PyTuple_Size(value);
2034       f.resize(size);
2035       for(int i=0;i<size;i++)
2036         {
2037           PyObject *o=PyTuple_GetItem(value,i);
2038           if(PyFloat_Check(o))
2039             f[i]=PyFloat_AS_DOUBLE(o);
2040           else if(PyInt_Check(o))
2041             f[i]=(double)PyInt_AS_LONG(o);
2042           else
2043             {
2044               std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not double ! only tuples of doubles accepted or integer !";
2045               throw INTERP_KERNEL::Exception(oss.str().c_str());
2046             }
2047         }
2048       sw=4;
2049       nbTuples=size;
2050       return &f[0];
2051     }
2052   if(PyList_Check(value))
2053     {
2054       int size=PyList_Size(value);
2055       f.resize(size);
2056       for(int i=0;i<size;i++)
2057         {
2058           PyObject *o=PyList_GetItem(value,i);
2059           if(PyFloat_Check(o))
2060             f[i]=PyFloat_AS_DOUBLE(o);
2061           else if(PyInt_Check(o))
2062             f[i]=(double)PyInt_AS_LONG(o);
2063           else
2064             {
2065               std::ostringstream oss; oss << "List as been detected but element #" << i << " is not double ! only lists of doubles accepted or integer !";
2066               throw INTERP_KERNEL::Exception(oss.str().c_str());
2067             }
2068         }
2069       sw=4;
2070       nbTuples=size;
2071       return &f[0];
2072     }
2073   void *argp;
2074   int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayDouble,0|0);
2075   if(SWIG_IsOK(status))
2076     {  
2077       d=reinterpret_cast< ParaMEDMEM::DataArrayDouble * >(argp);
2078       sw=2;
2079       if(d)
2080         {
2081           if(d->getNumberOfComponents()==1)
2082             {
2083               nbTuples=d->getNumberOfTuples();
2084               return d->getConstPointer();
2085             }
2086           else
2087             {
2088               std::ostringstream oss; oss << msg << "nb of components expected to be one, and input has " << d->getNumberOfComponents() << " components !";
2089               throw INTERP_KERNEL::Exception(oss.str().c_str());
2090             }
2091         }
2092       else
2093         {
2094           if(throwIfNullPt)
2095             {
2096               std::ostringstream oss; oss << msg << " null pointer not accepted!";
2097               throw INTERP_KERNEL::Exception(oss.str().c_str());
2098             }
2099           else
2100             { nbTuples=0; return 0; }
2101         }
2102     }
2103   status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayDoubleTuple,0|0);
2104   if(SWIG_IsOK(status))
2105     {  
2106       e=reinterpret_cast< ParaMEDMEM::DataArrayDoubleTuple * >(argp);
2107       sw=3;
2108       if(e)
2109         {
2110           nbTuples=e->getNumberOfCompo();
2111           return e->getConstPointer();
2112         }
2113       else
2114         {
2115           if(throwIfNullPt)
2116             {
2117               std::ostringstream oss; oss << msg << " null pointer not accepted!";
2118               throw INTERP_KERNEL::Exception(oss.str().c_str());
2119             }
2120           else
2121             { nbTuples=0; return 0; }
2122         }
2123     }
2124   throw INTERP_KERNEL::Exception("4 types accepted : integer, double, DataArrayDouble, DataArrayDoubleTuple");
2125 }
2126
2127 /*!
2128  * if python int -> cpp int sw=1
2129  * if python list[int] -> cpp vector<int> sw=2
2130  * if python tuple[int] -> cpp vector<int> sw=2
2131  * if python DataArrayInt -> cpp DataArrayInt sw=3
2132  * if python DataArrayIntTuple -> cpp DataArrayIntTuple sw=4
2133  *
2134  * switch between (int,vector<int>,DataArrayInt)
2135  */
2136 static const int *convertObjToPossibleCpp1_Safe(PyObject *value, int& sw, int& sz, int& iTyypp, std::vector<int>& stdvecTyypp) throw(INTERP_KERNEL::Exception)
2137 {
2138   sw=-1;
2139   if(PyInt_Check(value))
2140     {
2141       iTyypp=(int)PyInt_AS_LONG(value);
2142       sw=1; sz=1;
2143       return &iTyypp;
2144     }
2145   if(PyTuple_Check(value))
2146     {
2147       int size=PyTuple_Size(value);
2148       stdvecTyypp.resize(size);
2149       for(int i=0;i<size;i++)
2150         {
2151           PyObject *o=PyTuple_GetItem(value,i);
2152           if(PyInt_Check(o))
2153             stdvecTyypp[i]=(int)PyInt_AS_LONG(o);
2154           else
2155             {
2156               std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not integer ! only tuples of integers accepted !";
2157               throw INTERP_KERNEL::Exception(oss.str().c_str());
2158             }
2159         }
2160       sw=2; sz=size;
2161       return &stdvecTyypp[0];
2162     }
2163   if(PyList_Check(value))
2164     {
2165       int size=PyList_Size(value);
2166       stdvecTyypp.resize(size);
2167       for(int i=0;i<size;i++)
2168         {
2169           PyObject *o=PyList_GetItem(value,i);
2170           if(PyInt_Check(o))
2171             stdvecTyypp[i]=(int)PyInt_AS_LONG(o);
2172           else
2173             {
2174               std::ostringstream oss; oss << "List as been detected but element #" << i << " is not integer ! only lists of integers accepted !";
2175               throw INTERP_KERNEL::Exception(oss.str().c_str());
2176             }
2177         }
2178       sw=2; sz=size;
2179       return &stdvecTyypp[0];
2180     }
2181   void *argp;
2182   int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayInt,0|0);
2183   if(SWIG_IsOK(status))
2184     {
2185       ParaMEDMEM::DataArrayInt *daIntTyypp=reinterpret_cast< ParaMEDMEM::DataArrayInt * >(argp);
2186       if(daIntTyypp)
2187         {
2188           sw=3; sz=daIntTyypp->getNbOfElems();
2189           return daIntTyypp->begin();
2190         }
2191       else
2192         {
2193           sz=0;
2194           return 0;
2195         }
2196     }
2197   status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayIntTuple,0|0);
2198   if(SWIG_IsOK(status))
2199     {  
2200       ParaMEDMEM::DataArrayIntTuple *daIntTuple=reinterpret_cast< ParaMEDMEM::DataArrayIntTuple * >(argp);
2201       sw=4; sz=daIntTuple->getNumberOfCompo();
2202       return daIntTuple->getConstPointer();
2203     }
2204   throw INTERP_KERNEL::Exception("5 types accepted : integer, tuple of integer, list of integer, DataArrayInt, DataArrayIntTuple");
2205 }
2206
2207 static ParaMEDMEM::DataArray *CheckAndRetrieveDataArrayInstance(PyObject *obj, const char *msg)
2208 {
2209   void *aBasePtrVS=0;
2210   int status=SWIG_ConvertPtr(obj,&aBasePtrVS,SWIGTYPE_p_ParaMEDMEM__DataArray,0|0);
2211   if(!SWIG_IsOK(status))
2212     {
2213       status=SWIG_ConvertPtr(obj,&aBasePtrVS,SWIGTYPE_p_ParaMEDMEM__DataArrayDouble,0|0);
2214       if(!SWIG_IsOK(status))
2215         {
2216           status=SWIG_ConvertPtr(obj,&aBasePtrVS,SWIGTYPE_p_ParaMEDMEM__DataArrayInt,0|0);
2217           if(!SWIG_IsOK(status))
2218             {
2219               status=SWIG_ConvertPtr(obj,&aBasePtrVS,SWIGTYPE_p_ParaMEDMEM__DataArrayAsciiChar,0|0);
2220               if(!SWIG_IsOK(status))
2221                 {
2222                   status=SWIG_ConvertPtr(obj,&aBasePtrVS,SWIGTYPE_p_ParaMEDMEM__DataArrayByte,0|0);
2223                   std::ostringstream oss; oss << msg << " ! Accepted instances are DataArrayDouble, DataArrayInt, DataArrayAsciiChar, DataArrayByte !";
2224                   throw INTERP_KERNEL::Exception(oss.str().c_str());
2225                 }
2226             }
2227         }
2228     }
2229   return reinterpret_cast< ParaMEDMEM::DataArray * >(aBasePtrVS);
2230 }