1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D
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, or (at your option) any later version.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony Geay (CEA/DEN)
21 #ifndef __MEDCOUPLINGDATAARRAYTYPEMAPS_I__
22 #define __MEDCOUPLINGDATAARRAYTYPEMAPS_I__
24 #include "InterpKernelAutoPtr.hxx"
29 * This method is an extention of PySlice_GetIndices but less
30 * open than PySlice_GetIndicesEx that accepts too many situations.
32 void GetIndicesOfSlice(PySliceObject *slice, Py_ssize_t length, Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, const char *msgInCaseOfFailure)
34 int ret(PySlice_GetIndices(slice,length,start,stop,step));
37 if(*step>0 && *start==*stop && length==*start)
39 throw INTERP_KERNEL::Exception(msgInCaseOfFailure);
43 * This method allows to retrieve slice info from \a slice.
45 void GetIndicesOfSliceExplicitely(PySliceObject *slice, Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, const char *msgInCaseOfFailure)
47 int ret(PySlice_GetIndices(slice,std::numeric_limits<int>::max(),start,stop,step));
50 if(*start!=std::numeric_limits<int>::max() && *stop!=std::numeric_limits<int>::max())
52 std::ostringstream oss;
53 oss << msgInCaseOfFailure << " The input slice contains some unknowns that can't be determined in static method ! The input slice must be explicit here !";
54 throw INTERP_KERNEL::Exception(oss.str().c_str());
56 throw INTERP_KERNEL::Exception(msgInCaseOfFailure);
59 int InterpreteNegativeInt(int val, int nbelem)
63 int newVal(nbelem+val);
66 std::ostringstream oss; oss << "interpreteNegativeInt : request for negative int=" << val << " but number of elems is equal to " << nbelem << " !";
67 throw INTERP_KERNEL::Exception(oss.str().c_str());
76 #include <numpy/arrayobject.h>
77 #if NPY_API_VERSION <= 0x00000006
78 # define MED_NUMPY_OWNDATA NPY_OWNDATA
80 # define MED_NUMPY_OWNDATA NPY_ARRAY_OWNDATA
83 // specific DataArray deallocator callback. This deallocator is used both in the constructor of DataArray and in the toNumPyArr
84 // method. This dellocator uses weakref to determine if the linked numArr is still alive or not. If alive the ownership is given to it.
85 // if no more alive the "standart" DataArray deallocator is called.
86 void numarrdeal(void *pt, void *wron)
88 void **wronc=(void **)wron;
89 PyObject *weakRefOnOwner=reinterpret_cast<PyObject *>(wronc[0]);
90 PyObject *obj=PyWeakref_GetObject(weakRefOnOwner);
94 PyArrayObject *objC=reinterpret_cast<PyArrayObject *>(obj);
95 objC->flags|=MED_NUMPY_OWNDATA;
96 Py_XDECREF(weakRefOnOwner);
101 typedef void (*MyDeallocator)(void *,void *);
102 MyDeallocator deall=(MyDeallocator)wronc[1];
104 Py_XDECREF(weakRefOnOwner);
109 template<class MCData>
110 struct PyCallBackDataArraySt {
115 typedef struct PyCallBackDataArraySt<MEDCoupling::DataArrayByte> PyCallBackDataArrayChar;
116 typedef struct PyCallBackDataArraySt<MEDCoupling::DataArrayInt> PyCallBackDataArrayInt;
117 typedef struct PyCallBackDataArraySt<MEDCoupling::DataArrayDouble> PyCallBackDataArrayDouble;
121 static int callbackmcdataarray___init__(PyObject *self, PyObject *args, PyObject *kwargs) { return 0; }
123 static PyObject *callbackmcdataarraychar___new__(PyTypeObject *type, PyObject *args, PyObject *kwargs)
125 PyCallBackDataArrayChar *self = (PyCallBackDataArrayChar *) ( type->tp_alloc(type, 0) );
126 return (PyObject *)self;
129 static PyObject *callbackmcdataarrayint___new__(PyTypeObject *type, PyObject *args, PyObject *kwargs)
131 PyCallBackDataArrayInt *self = (PyCallBackDataArrayInt *) ( type->tp_alloc(type, 0) );
132 return (PyObject *)self;
135 static PyObject *callbackmcdataarraydouble___new__(PyTypeObject *type, PyObject *args, PyObject *kwargs)
137 PyCallBackDataArrayDouble *self = (PyCallBackDataArrayDouble *) ( type->tp_alloc(type, 0) );
138 return (PyObject *)self;
141 static void callbackmcdataarray_dealloc(PyObject *self)
143 Py_TYPE(self)->tp_free(self);
147 // real callback called when a numpy arr having more than one DataArray instance client on it is destroyed.
148 // In this case, all the "weak" clients, except the first one, invoke this call back that desable the content of these "weak" clients.
149 static PyObject *callbackmcdataarraychar_call(PyCallBackDataArrayChar *self, PyObject *args, PyObject *kw)
153 MEDCoupling::MemArray<char>& mma=self->_pt_mc->accessToMemArray();
160 // real callback called when a numpy arr having more than one DataArray instance client on it is destroyed.
161 // In this case, all the "weak" clients, except the first one, invoke this call back that desable the content of these "weak" clients.
162 static PyObject *callbackmcdataarrayint_call(PyCallBackDataArrayInt *self, PyObject *args, PyObject *kw)
166 MEDCoupling::MemArray<int>& mma=self->_pt_mc->accessToMemArray();
173 // real callback called when a numpy arr having more than one DataArray instance client on it is destroyed.
174 // In this case, all the "weak" clients, except the first one, invoke this call back that desable the content of these "weak" clients.
175 static PyObject *callbackmcdataarraydouble_call(PyCallBackDataArrayDouble *self, PyObject *args, PyObject *kw)
179 MEDCoupling::MemArray<double>& mma=self->_pt_mc->accessToMemArray();
187 PyTypeObject PyCallBackDataArrayChar_RefType = {
188 PyVarObject_HEAD_INIT(&PyType_Type, 0)
189 "callbackmcdataarraychar",
190 sizeof(PyCallBackDataArrayChar),
192 callbackmcdataarray_dealloc, /*tp_dealloc*/
199 0, /*tp_as_sequence*/
202 (ternaryfunc)callbackmcdataarraychar_call, /*tp_call*/
207 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /*tp_flags*/
211 0, /*tp_richcompare*/
212 0, /*tp_weaklistoffset*/
223 callbackmcdataarray___init__, /*tp_init*/
224 PyType_GenericAlloc, /*tp_alloc*/
225 callbackmcdataarraychar___new__, /*tp_new*/
226 PyObject_GC_Del, /*tp_free*/
230 PyTypeObject PyCallBackDataArrayInt_RefType = {
231 PyVarObject_HEAD_INIT(&PyType_Type, 0)
232 "callbackmcdataarrayint",
233 sizeof(PyCallBackDataArrayInt),
235 callbackmcdataarray_dealloc, /*tp_dealloc*/
242 0, /*tp_as_sequence*/
245 (ternaryfunc)callbackmcdataarrayint_call, /*tp_call*/
250 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /*tp_flags*/
254 0, /*tp_richcompare*/
255 0, /*tp_weaklistoffset*/
266 callbackmcdataarray___init__, /*tp_init*/
267 PyType_GenericAlloc, /*tp_alloc*/
268 callbackmcdataarrayint___new__, /*tp_new*/
269 PyObject_GC_Del, /*tp_free*/
272 PyTypeObject PyCallBackDataArrayDouble_RefType = {
273 PyVarObject_HEAD_INIT(&PyType_Type, 0)
274 "callbackmcdataarraydouble",
275 sizeof(PyCallBackDataArrayDouble),
277 callbackmcdataarray_dealloc, /*tp_dealloc*/
284 0, /*tp_as_sequence*/
287 (ternaryfunc)callbackmcdataarraydouble_call, /*tp_call*/
292 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /*tp_flags*/
296 0, /*tp_richcompare*/
297 0, /*tp_weaklistoffset*/
308 callbackmcdataarray___init__, /*tp_init*/
309 PyType_GenericAlloc, /*tp_alloc*/
310 callbackmcdataarraydouble___new__, /*tp_new*/
311 PyObject_GC_Del, /*tp_free*/
314 // this is the second type of specific deallocator, only valid for the constructor of DataArrays taking numpy array
315 // in input when an another DataArray is already client of this.
316 template<class MCData>
317 void numarrdeal2(void *pt, void *obj)
319 typedef struct PyCallBackDataArraySt<MCData> PyCallBackDataArray;
320 void **obj1=(void **)obj;
321 PyCallBackDataArray *cbdaic=reinterpret_cast<PyCallBackDataArray *>(obj1[0]);
322 PyObject *weakRefOnOwner=reinterpret_cast<PyObject *>(obj1[1]);
324 Py_XDECREF(weakRefOnOwner);
329 template<class MCData, class T>
330 MCData *BuildNewInstance(PyObject *elt0, int npyObjectType, PyTypeObject *pytype, const char *msg)
332 int ndim=PyArray_NDIM(elt0);
333 if(ndim!=1 && ndim!=2)
334 throw INTERP_KERNEL::Exception("Input numpy array should have dimension equal to 1 or 2 !");
335 if(PyArray_DESCR(elt0)->type_num != npyObjectType)
337 std::ostringstream oss; oss << "Input numpy array has not the type " << msg << "!";
338 throw INTERP_KERNEL::Exception(oss.str().c_str());
340 npy_intp sz0=PyArray_DIM(elt0,0);
341 npy_intp sz1=ndim==2?PyArray_DIM(elt0,1):1;
343 int itemSize=PyArray_ITEMSIZE(elt0);
344 if(itemSize!=sizeof(T))
346 std::ostringstream oss; oss << "Input numpy array has not itemSize set to " << sizeof(T) << " !";
347 throw INTERP_KERNEL::Exception(oss.str().c_str());
349 if(itemSize*sz1!=PyArray_STRIDE(elt0,0))
350 throw INTERP_KERNEL::Exception("Input numpy array has stride that mismatches the item size ! Data are not packed in the right way for DataArrays !");
352 if(itemSize!=PyArray_STRIDE(elt0,1))
353 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 !");
354 const char *data=PyArray_BYTES(elt0);
355 typename MEDCoupling::MCAuto<MCData> ret=MCData::New();
356 if(PyArray_ISBEHAVED(elt0))//aligned and writeable and in machine byte-order
358 PyArrayObject *elt0C=reinterpret_cast<PyArrayObject *>(elt0);
359 PyArrayObject *eltOwning=(PyArray_FLAGS(elt0C) & MED_NUMPY_OWNDATA)?elt0C:NULL;
360 int mask=MED_NUMPY_OWNDATA; mask=~mask;
362 PyObject *deepestObj=elt0;
363 PyObject *base=elt0C->base;
364 if(base) deepestObj=base;
365 bool isSpetialCase(false);
368 if(PyArray_Check(base))
370 PyArrayObject *baseC=reinterpret_cast<PyArrayObject *>(base);
371 eltOwning=(PyArray_FLAGS(baseC) & MED_NUMPY_OWNDATA)?baseC:eltOwning;
374 if(base) deepestObj=base;
383 {// this case is present for numpy arrayint coming from load of pickelized string. The owner of elt0 is not an array -> A copy is requested.
384 std::size_t nbOfElems(sz0*sz1);
385 T *dataCpy=(T*)malloc(sizeof(T)*nbOfElems);
386 std::copy(reinterpret_cast<const T*>(data),reinterpret_cast<const T*>(data)+nbOfElems,dataCpy);
387 ret->useArray(dataCpy,true,MEDCoupling::C_DEALLOC,sz0,sz1);
390 typename MEDCoupling::MemArray<T>& mma=ret->accessToMemArray();
393 PyCallBackDataArraySt<MCData> *cb=PyObject_GC_New(PyCallBackDataArraySt<MCData>,pytype);
395 ret->useArray(reinterpret_cast<const T *>(data),true,MEDCoupling::C_DEALLOC,sz0,sz1);
396 PyObject *ref=PyWeakref_NewRef(deepestObj,(PyObject *)cb);
397 void **objs=new void *[2]; objs[0]=cb; objs[1]=ref;
398 mma.setParameterForDeallocator(objs);
399 mma.setSpecificDeallocator(numarrdeal2<MCData>);
400 //"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 !");
404 ret->useArray(reinterpret_cast<const T *>(data),true,MEDCoupling::C_DEALLOC,sz0,sz1);
405 PyObject *ref=PyWeakref_NewRef(reinterpret_cast<PyObject *>(eltOwning),NULL);
406 typename MEDCoupling::MemArray<T>::Deallocator tmp(MEDCoupling::MemArray<T>::CDeallocator);
407 void **tmp2 = reinterpret_cast<void**>(&tmp); // MSVC2010 does not support constructor()
408 void **objs=new void *[2]; objs[0]=ref; objs[1]=*tmp2;
409 mma.setParameterForDeallocator(objs);
410 mma.setSpecificDeallocator(numarrdeal);
413 else if(PyArray_ISBEHAVED_RO(elt0))
414 ret->useArray(reinterpret_cast<const T *>(data),false,MEDCoupling::CPP_DEALLOC,sz0,sz1);
419 int NumpyArrSetBaseObjectExt(PyArrayObject *arr, PyObject *obj)
422 PyErr_SetString(PyExc_ValueError,
423 "Cannot set the NumPy array 'base' "
424 "dependency to NULL after initialization");
428 * Allow the base to be set only once. Once the object which
429 * owns the data is set, it doesn't make sense to change it.
431 if (PyArray_BASE(arr) != NULL) {
433 PyErr_SetString(PyExc_ValueError,
434 "Cannot set the NumPy array 'base' "
435 "dependency more than once");
440 * Don't allow infinite chains of views, always set the base
441 * to the first owner of the data.
442 * That is, either the first object which isn't an array,
443 * or the first object which owns its own data.
446 while (PyArray_Check(obj) && (PyObject *)arr != obj) {
447 PyArrayObject *obj_arr = (PyArrayObject *)obj;
451 /* If this array owns its own data, stop collapsing */
452 if (PyArray_CHKFLAGS(obj_arr, MED_NUMPY_OWNDATA )) {
456 tmp = PyArray_BASE(obj_arr);
457 /* If there's no base, stop collapsing */
461 /* Stop the collapse new base when the would not be of the same
462 * type (i.e. different subclass).
464 if (Py_TYPE(tmp) != Py_TYPE(arr)) {
474 /* Disallow circular references */
475 if ((PyObject *)arr == obj) {
477 PyErr_SetString(PyExc_ValueError,
478 "Cannot create a circular NumPy array 'base' dependency");
487 template<class MCData, class T>
488 PyObject *ToNumPyArrayUnderground(MCData *self, int npyObjectType, const char *MCDataStr, int nbTuples, int nbComp)
490 if(!self->isAllocated())
492 std::ostringstream oss; oss << MCDataStr << "::toNumPyArray : this is not allocated !";
493 throw INTERP_KERNEL::Exception(oss.str().c_str());
495 MEDCoupling::MemArray<T>& mem=self->accessToMemArray();
498 std::ostringstream oss; oss << MCDataStr << "::toNumPyArray : number of components of this is 0 ! Should be > 0 !";
499 throw INTERP_KERNEL::Exception(oss.str().c_str());
501 int nbDims=nbComp==1?1:2;
503 dim[0]=(npy_intp)nbTuples; dim[1]=nbComp;
504 const T *bg=self->getConstPointer();
505 PyObject *ret(PyArray_SimpleNewFromData(nbDims,dim,npyObjectType,const_cast<T *>(bg)));
506 if(mem.isDeallocatorCalled())
508 if(mem.getDeallocator()!=numarrdeal)
509 {// case for the first call of toNumPyArray
510 PyObject *ref(PyWeakref_NewRef(ret,NULL));
511 typename MEDCoupling::MemArray<T>::Deallocator tmp(mem.getDeallocator());
512 void **tmp2 = reinterpret_cast<void**>(&tmp); // MSVC2010 does not support constructor()
513 void **objs=new void *[2]; objs[0]=reinterpret_cast<void*>(ref); objs[1]=*tmp2;
514 mem.setParameterForDeallocator(objs);
515 mem.setSpecificDeallocator(numarrdeal);
519 {// case for the second and other call of toNumPyArray
520 void **objs=(void **)mem.getParameterForDeallocator();
521 PyObject *weakRefOnOwner=(PyObject *)objs[0];
522 PyObject *obj=PyWeakref_GetObject(weakRefOnOwner);
524 {//the previous numArray exists let numpy deals the numpy array each other by declaring the still alive instance as base
526 NumpyArrSetBaseObjectExt((PyArrayObject*)ret,obj);
529 {//the previous numArray no more exists -> declare the newly created numpy array as the first one.
530 Py_XDECREF(weakRefOnOwner);
531 PyObject *ref=PyWeakref_NewRef(ret,NULL);
539 template<class MCData, class T>
540 PyObject *ToNumPyArray(MCData *self, int npyObjectType, const char *MCDataStr)
542 return ToNumPyArrayUnderground<MCData,T>(self,npyObjectType,MCDataStr,self->getNumberOfTuples(),self->getNumberOfComponents());
545 SWIGINTERN PyObject *MEDCoupling_DataArrayInt_toNumPyArray(MEDCoupling::DataArrayInt *self);
546 SWIGINTERN PyObject *MEDCoupling_DataArrayDouble_toNumPyArray(MEDCoupling::DataArrayDouble *self);
548 PyObject *ToCSRMatrix(const std::vector<std::map<int,double> >& m, int nbCols) throw(INTERP_KERNEL::Exception)
550 int nbRows((int)m.size());
551 MEDCoupling::MCAuto<MEDCoupling::DataArrayInt> indPtr(MEDCoupling::DataArrayInt::New()),indices(MEDCoupling::DataArrayInt::New());
552 MEDCoupling::MCAuto<MEDCoupling::DataArrayDouble> data(MEDCoupling::DataArrayDouble::New());
553 indPtr->alloc(nbRows+1,1);
554 int *intPtr_ptr(indPtr->getPointer()); intPtr_ptr[0]=0; intPtr_ptr++;
556 for(std::vector<std::map<int,double> >::const_iterator it0=m.begin();it0!=m.end();it0++,intPtr_ptr++)
558 sz2+=(int)(*it0).size();
561 indices->alloc(sz2,1); data->alloc(sz2,1);
562 int *indices_ptr(indices->getPointer());
563 double *data_ptr(data->getPointer());
564 for(std::vector<std::map<int,double> >::const_iterator it0=m.begin();it0!=m.end();it0++)
565 for(std::map<int,double>::const_iterator it1=(*it0).begin();it1!=(*it0).end();it1++,indices_ptr++,data_ptr++)
567 *indices_ptr=(*it1).first;
568 *data_ptr=(*it1).second;
570 PyObject *a(MEDCoupling_DataArrayDouble_toNumPyArray(data)),*b(MEDCoupling_DataArrayInt_toNumPyArray(indices)),*c(MEDCoupling_DataArrayInt_toNumPyArray(indPtr));
572 PyObject *args(PyTuple_New(1)),*args0(PyTuple_New(3)),*kw(PyDict_New()),*kw1(PyTuple_New(2));
573 PyTuple_SetItem(args0,0,a); PyTuple_SetItem(args0,1,b); PyTuple_SetItem(args0,2,c); PyTuple_SetItem(args,0,args0);
574 PyTuple_SetItem(kw1,0,PyInt_FromLong(nbRows)); PyTuple_SetItem(kw1,1,PyInt_FromLong(nbCols));
575 PyObject *tmp1(PyString_FromString("shape"));
576 PyDict_SetItem(kw,tmp1,kw1); Py_DECREF(tmp1); Py_DECREF(kw1);
577 PyObject* pdict=PyDict_New();
578 PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());
579 PyObject *tmp(PyRun_String("from scipy.sparse import csr_matrix", Py_single_input, pdict, pdict));
581 throw INTERP_KERNEL::Exception("Problem during loading csr_matrix in scipy.sparse ! Is Scipy module available in present ?");
582 PyObject *csrMatrixCls=PyDict_GetItemString(pdict,"csr_matrix");
584 throw INTERP_KERNEL::Exception("csr_matrix not found in scipy.sparse ! Is Scipy module available in present ?");
585 PyObject *ret(PyObject_Call(csrMatrixCls,args,kw));
586 Py_DECREF(pdict); Py_XDECREF(tmp); Py_DECREF(args); Py_DECREF(kw);
592 static PyObject *convertDataArrayChar(MEDCoupling::DataArrayChar *dac, int owner) throw(INTERP_KERNEL::Exception)
600 if(dynamic_cast<MEDCoupling::DataArrayByte *>(dac))
601 ret=SWIG_NewPointerObj((void*)dac,SWIGTYPE_p_MEDCoupling__DataArrayByte,owner);
602 if(dynamic_cast<MEDCoupling::DataArrayAsciiChar *>(dac))
603 ret=SWIG_NewPointerObj((void*)dac,SWIGTYPE_p_MEDCoupling__DataArrayAsciiChar,owner);
605 throw INTERP_KERNEL::Exception("Not recognized type of DataArrayChar on downcast !");
609 static PyObject *convertDataArray(MEDCoupling::DataArray *dac, int owner) throw(INTERP_KERNEL::Exception)
617 if(dynamic_cast<MEDCoupling::DataArrayDouble *>(dac))
618 ret=SWIG_NewPointerObj((void*)dac,SWIGTYPE_p_MEDCoupling__DataArrayDouble,owner);
619 if(dynamic_cast<MEDCoupling::DataArrayInt *>(dac))
620 ret=SWIG_NewPointerObj((void*)dac,SWIGTYPE_p_MEDCoupling__DataArrayInt,owner);
621 if(dynamic_cast<MEDCoupling::DataArrayByte *>(dac))
622 ret=SWIG_NewPointerObj((void*)dac,SWIGTYPE_p_MEDCoupling__DataArrayByte,owner);
623 if(dynamic_cast<MEDCoupling::DataArrayAsciiChar *>(dac))
624 ret=SWIG_NewPointerObj((void*)dac,SWIGTYPE_p_MEDCoupling__DataArrayAsciiChar,owner);
626 throw INTERP_KERNEL::Exception("Not recognized type of DataArray on downcast !");
630 static PyObject *convertIntArrToPyList(const int *ptr, int size) throw(INTERP_KERNEL::Exception)
632 PyObject *ret=PyList_New(size);
633 for(int i=0;i<size;i++)
634 PyList_SetItem(ret,i,PyInt_FromLong(ptr[i]));
638 static PyObject *convertIntArrToPyList2(const std::vector<int>& v) throw(INTERP_KERNEL::Exception)
641 PyObject *ret=PyList_New(size);
642 for(int i=0;i<size;i++)
643 PyList_SetItem(ret,i,PyInt_FromLong(v[i]));
647 static PyObject *convertIntArrToPyList3(const std::set<int>& v) throw(INTERP_KERNEL::Exception)
650 PyObject *ret=PyList_New(size);
651 std::set<int>::const_iterator it=v.begin();
652 for(int i=0;i<size;i++,it++)
653 PyList_SetItem(ret,i,PyInt_FromLong(*it));
657 static PyObject *convertIntArrToPyListOfTuple(const int *vals, int nbOfComp, int nbOfTuples) throw(INTERP_KERNEL::Exception)
659 PyObject *ret=PyList_New(nbOfTuples);
660 for(int i=0;i<nbOfTuples;i++)
662 PyObject *t=PyTuple_New(nbOfComp);
663 for(int j=0;j<nbOfComp;j++)
664 PyTuple_SetItem(t,j,PyInt_FromLong(vals[i*nbOfComp+j]));
665 PyList_SetItem(ret,i,t);
670 static int *convertPyToNewIntArr2(PyObject *pyLi, int *size) throw(INTERP_KERNEL::Exception)
672 if(PyList_Check(pyLi))
674 *size=PyList_Size(pyLi);
675 int *tmp=new int[*size];
676 for(int i=0;i<*size;i++)
678 PyObject *o=PyList_GetItem(pyLi,i);
681 int val=(int)PyInt_AS_LONG(o);
687 throw INTERP_KERNEL::Exception("list must contain integers only");
692 else if(PyTuple_Check(pyLi))
694 *size=PyTuple_Size(pyLi);
695 int *tmp=new int[*size];
696 for(int i=0;i<*size;i++)
698 PyObject *o=PyTuple_GetItem(pyLi,i);
701 int val=(int)PyInt_AS_LONG(o);
707 throw INTERP_KERNEL::Exception("tuple must contain integers only");
714 throw INTERP_KERNEL::Exception("convertPyToNewIntArr2 : not a list");
718 static PyObject *convertFromVectorPairInt(const std::vector< std::pair<int,int> >& arr) throw(INTERP_KERNEL::Exception)
720 PyObject *ret=PyList_New(arr.size());
721 for(std::size_t i=0;i<arr.size();i++)
723 PyObject *t=PyTuple_New(2);
724 PyTuple_SetItem(t,0,PyInt_FromLong(arr[i].first));
725 PyTuple_SetItem(t,1,PyInt_FromLong(arr[i].second));
726 PyList_SetItem(ret,i,t);
731 static void convertPyToVectorPairInt(PyObject *pyLi, std::vector< std::pair<int,int> >& arr) throw(INTERP_KERNEL::Exception)
733 const char msg[]="list must contain tuples of 2 integers only or tuple must contain tuples of 2 integers only !";
734 if(PyList_Check(pyLi))
736 int size=PyList_Size(pyLi);
738 for(int i=0;i<size;i++)
740 PyObject *o=PyList_GetItem(pyLi,i);
743 int sz2=PyTuple_Size(o);
745 throw INTERP_KERNEL::Exception(msg);
746 PyObject *o_0=PyTuple_GetItem(o,0);
747 if(!PyInt_Check(o_0))
748 throw INTERP_KERNEL::Exception(msg);
749 PyObject *o_1=PyTuple_GetItem(o,1);
750 if(!PyInt_Check(o_1))
751 throw INTERP_KERNEL::Exception(msg);
752 arr[i].first=(int)PyInt_AS_LONG(o_0);
753 arr[i].second=(int)PyInt_AS_LONG(o_1);
756 throw INTERP_KERNEL::Exception(msg);
759 else if(PyTuple_Check(pyLi))
761 int size=PyTuple_Size(pyLi);
763 for(int i=0;i<size;i++)
765 PyObject *o=PyTuple_GetItem(pyLi,i);
768 int sz2=PyTuple_Size(o);
770 throw INTERP_KERNEL::Exception(msg);
771 PyObject *o_0=PyTuple_GetItem(o,0);
772 if(!PyInt_Check(o_0))
773 throw INTERP_KERNEL::Exception(msg);
774 PyObject *o_1=PyTuple_GetItem(o,1);
775 if(!PyInt_Check(o_1))
776 throw INTERP_KERNEL::Exception(msg);
777 arr[i].first=(int)PyInt_AS_LONG(o_0);
778 arr[i].second=(int)PyInt_AS_LONG(o_1);
781 throw INTERP_KERNEL::Exception(msg);
785 throw INTERP_KERNEL::Exception(msg);
788 static void convertPyToVectorPairStringInt(PyObject *pyLi, std::vector< std::pair<std::string,int> >& arr) throw(INTERP_KERNEL::Exception)
790 const char msg[]="convertPyToVectorPairStringInt : list must contain tuples of 2 integers only or tuple must contain tuples of 1 string and 1 integer only !";
791 if(PyList_Check(pyLi))
793 int size=PyList_Size(pyLi);
795 for(int i=0;i<size;i++)
797 PyObject *o=PyList_GetItem(pyLi,i);
800 int sz2=PyTuple_Size(o);
802 throw INTERP_KERNEL::Exception(msg);
803 PyObject *o_0=PyTuple_GetItem(o,0);
804 if(!PyString_Check(o_0))
805 throw INTERP_KERNEL::Exception(msg);
806 PyObject *o_1=PyTuple_GetItem(o,1);
807 if(!PyInt_Check(o_1))
808 throw INTERP_KERNEL::Exception(msg);
809 arr[i].first=PyString_AsString(o_0);
810 arr[i].second=(int)PyInt_AS_LONG(o_1);
813 throw INTERP_KERNEL::Exception(msg);
816 else if(PyTuple_Check(pyLi))
818 int size=PyTuple_Size(pyLi);
820 for(int i=0;i<size;i++)
822 PyObject *o=PyTuple_GetItem(pyLi,i);
825 int sz2=PyTuple_Size(o);
827 throw INTERP_KERNEL::Exception(msg);
828 PyObject *o_0=PyTuple_GetItem(o,0);
829 if(!PyString_Check(o_0))
830 throw INTERP_KERNEL::Exception(msg);
831 PyObject *o_1=PyTuple_GetItem(o,1);
832 if(!PyInt_Check(o_1))
833 throw INTERP_KERNEL::Exception(msg);
834 arr[i].first=PyString_AsString(o_0);
835 arr[i].second=(int)PyInt_AS_LONG(o_1);
838 throw INTERP_KERNEL::Exception(msg);
842 throw INTERP_KERNEL::Exception(msg);
845 static void convertPyToNewIntArr3(PyObject *pyLi, std::vector<int>& arr) throw(INTERP_KERNEL::Exception)
847 if(PyList_Check(pyLi))
849 int size=PyList_Size(pyLi);
851 for(int i=0;i<size;i++)
853 PyObject *o=PyList_GetItem(pyLi,i);
856 int val=(int)PyInt_AS_LONG(o);
860 throw INTERP_KERNEL::Exception("list must contain integers only");
863 else if(PyTuple_Check(pyLi))
865 int size=PyTuple_Size(pyLi);
867 for(int i=0;i<size;i++)
869 PyObject *o=PyTuple_GetItem(pyLi,i);
872 int val=(int)PyInt_AS_LONG(o);
876 throw INTERP_KERNEL::Exception("tuple must contain integers only");
881 throw INTERP_KERNEL::Exception("convertPyToNewIntArr3 : not a list nor a tuple");
885 static void convertPyToNewIntArr4(PyObject *pyLi, int recurseLev, int nbOfSubPart, std::vector<int>& arr) throw(INTERP_KERNEL::Exception)
888 throw INTERP_KERNEL::Exception("convertPyToNewIntArr4 : invalid list of integers level of recursion !");
890 if(PyList_Check(pyLi))
892 int size=PyList_Size(pyLi);
893 for(int i=0;i<size;i++)
895 PyObject *o=PyList_GetItem(pyLi,i);
898 int val=(int)PyInt_AS_LONG(o);
903 std::vector<int> arr2;
904 convertPyToNewIntArr4(o,recurseLev-1,nbOfSubPart,arr2);
905 if(nbOfSubPart>=1 && nbOfSubPart!=(int)arr2.size())
907 std::ostringstream oss; oss << "convertPyToNewIntArr4 : input list at lev " << recurseLev << " invalid nb of subpart elts expected " << nbOfSubPart << " having " << arr2.size() << " !";
908 throw INTERP_KERNEL::Exception(oss.str().c_str());
910 arr.insert(arr.end(),arr2.begin(),arr2.end());
914 else if(PyTuple_Check(pyLi))
916 int size=PyTuple_Size(pyLi);
917 for(int i=0;i<size;i++)
919 PyObject *o=PyTuple_GetItem(pyLi,i);
922 int val=(int)PyInt_AS_LONG(o);
927 std::vector<int> arr2;
928 convertPyToNewIntArr4(o,recurseLev-1,nbOfSubPart,arr2);
929 if(nbOfSubPart>=1 && nbOfSubPart!=(int)arr2.size())
931 std::ostringstream oss; oss << "convertPyToNewIntArr4 : input list at lev " << recurseLev << " invalid nb of subpart elts expected " << nbOfSubPart << " having " << arr2.size() << " !";
932 throw INTERP_KERNEL::Exception(oss.str().c_str());
934 arr.insert(arr.end(),arr2.begin(),arr2.end());
939 throw INTERP_KERNEL::Exception("convertPyToNewIntArr4 : not a list nor a tuple recursively !");
942 static void checkFillArrayWithPyList(int size1, int size2, int& nbOfTuples, int& nbOfComp) throw(INTERP_KERNEL::Exception)
946 if(nbOfComp==-1) { nbOfTuples=size1; nbOfComp=size2; }
947 else { if(nbOfComp==size2) { nbOfTuples=size1; } else
949 std::ostringstream oss; oss << "fillArrayWithPyListDbl2 : mismatch between nb of elemts : Input has " << size1 << " tuples and " << size2 << " components";
950 oss << " whereas nb of components expected is " << nbOfComp << " !";
951 throw INTERP_KERNEL::Exception(oss.str().c_str());
958 if((nbOfTuples!=size1 || nbOfComp!=size2))
960 if(size2!=1 || size1!=nbOfComp*nbOfTuples)
962 std::ostringstream oss; oss << "fillArrayWithPyListDbl2 : mismatch between nb of elemts : Input has " << size1 << " tuples and " << size2 << " components";
963 oss << " whereas nb of tuples expected is " << nbOfTuples << " and number of components expected is " << nbOfComp << " !";
964 throw INTERP_KERNEL::Exception(oss.str().c_str());
970 if(nbOfTuples==size1)
974 std::ostringstream oss; oss << "fillArrayWithPyListDbl2 : mismatch between nb of elemts : Input has " << size1 << " tuples and " << size2 << " components";
975 oss << " whereas nb of tuples expected is " << nbOfTuples << " !";
976 throw INTERP_KERNEL::Exception(oss.str().c_str());
982 static void fillArrayWithPyListInt3(PyObject *pyLi, int& nbOfElt, std::vector<int>& ret)
984 static const char MSG[]="fillArrayWithPyListInt3 : It appears that the input list or tuple is composed by elts having different sizes !";
985 if(PyInt_Check(pyLi))
987 long val=PyInt_AS_LONG(pyLi);
992 throw INTERP_KERNEL::Exception(MSG);
995 else if(PyList_Check(pyLi))
997 int size=PyList_Size(pyLi);
999 for(int i=0;i<size;i++)
1001 PyObject *o=PyList_GetItem(pyLi,i);
1003 fillArrayWithPyListInt3(o,tmp1,ret);
1011 throw INTERP_KERNEL::Exception(MSG);
1014 else if(PyTuple_Check(pyLi))
1016 int size=PyTuple_Size(pyLi);
1018 for(int i=0;i<size;i++)
1020 PyObject *o=PyTuple_GetItem(pyLi,i);
1022 fillArrayWithPyListInt3(o,tmp1,ret);
1030 throw INTERP_KERNEL::Exception(MSG);
1034 throw INTERP_KERNEL::Exception("fillArrayWithPyListInt3 : Unrecognized type ! Should be a composition of tuple,list,int !");
1037 static std::vector<int> fillArrayWithPyListInt2(PyObject *pyLi, int& nbOfTuples, int& nbOfComp) throw(INTERP_KERNEL::Exception)
1039 std::vector<int> ret;
1040 int size1=-1,size2=-1;
1041 if(PyList_Check(pyLi))
1043 size1=PyList_Size(pyLi);
1044 for(int i=0;i<size1;i++)
1046 PyObject *o=PyList_GetItem(pyLi,i);
1047 fillArrayWithPyListInt3(o,size2,ret);
1052 else if(PyTuple_Check(pyLi))
1054 size1=PyTuple_Size(pyLi);
1055 for(int i=0;i<size1;i++)
1057 PyObject *o=PyTuple_GetItem(pyLi,i);
1058 fillArrayWithPyListInt3(o,size2,ret);
1064 throw INTERP_KERNEL::Exception("fillArrayWithPyListInt2 : Unrecognized type ! Should be a tuple or a list !");
1066 checkFillArrayWithPyList(size1,size2,nbOfTuples,nbOfComp);
1070 static bool fillStringVector(PyObject *pyLi, std::vector<std::string>& vec) throw(INTERP_KERNEL::Exception)
1072 if(PyList_Check(pyLi))
1074 Py_ssize_t sz=PyList_Size(pyLi);
1076 for(int i=0;i<sz;i++)
1078 PyObject *o=PyList_GetItem(pyLi,i);
1079 if(PyString_Check(o))
1080 vec[i]=PyString_AsString(o);
1086 else if(PyTuple_Check(pyLi))
1088 Py_ssize_t sz=PyTuple_Size(pyLi);
1090 for(int i=0;i<sz;i++)
1092 PyObject *o=PyTuple_GetItem(pyLi,i);
1093 if(PyString_Check(o))
1094 vec[i]=PyString_AsString(o);
1103 static void convertPyToVectorOfVectorOfString(PyObject *pyLi, std::vector< std::vector<std::string> >& arr) throw(INTERP_KERNEL::Exception)
1105 const char msg[]="convertPyToVectorOfVectorOfString : expecting list of list of strings !";
1106 if(PyList_Check(pyLi))
1108 Py_ssize_t sz=PyList_Size(pyLi);
1110 for(int i=0;i<sz;i++)
1112 PyObject *o=PyList_GetItem(pyLi,i);
1113 if(!fillStringVector(o,arr[i]))
1114 throw INTERP_KERNEL::Exception(msg);
1117 else if(PyTuple_Check(pyLi))
1119 Py_ssize_t sz=PyTuple_Size(pyLi);
1121 for(int i=0;i<sz;i++)
1123 PyObject *o=PyTuple_GetItem(pyLi,i);
1124 if(!fillStringVector(o,arr[i]))
1125 throw INTERP_KERNEL::Exception(msg);
1129 throw INTERP_KERNEL::Exception(msg);
1132 static bool fillIntVector(PyObject *pyLi, std::vector<int>& vec) throw(INTERP_KERNEL::Exception)
1134 if(PyList_Check(pyLi))
1136 Py_ssize_t sz=PyList_Size(pyLi);
1138 for(int i=0;i<sz;i++)
1140 PyObject *o=PyList_GetItem(pyLi,i);
1142 vec[i]=PyInt_AS_LONG(o);
1148 else if(PyTuple_Check(pyLi))
1150 Py_ssize_t sz=PyTuple_Size(pyLi);
1152 for(int i=0;i<sz;i++)
1154 PyObject *o=PyTuple_GetItem(pyLi,i);
1156 vec[i]=PyInt_AS_LONG(o);
1166 static void convertPyToVectorOfVectorOfInt(PyObject *pyLi, std::vector< std::vector<int> >& arr) throw(INTERP_KERNEL::Exception)
1168 const char msg[]="convertPyToVectorOfVectorOfInt : expecting list of list of strings !";
1169 if(PyList_Check(pyLi))
1171 Py_ssize_t sz=PyList_Size(pyLi);
1173 for(int i=0;i<sz;i++)
1175 PyObject *o=PyList_GetItem(pyLi,i);
1176 if(!fillIntVector(o,arr[i]))
1177 throw INTERP_KERNEL::Exception(msg);
1180 else if(PyTuple_Check(pyLi))
1182 Py_ssize_t sz=PyTuple_Size(pyLi);
1184 for(int i=0;i<sz;i++)
1186 PyObject *o=PyTuple_GetItem(pyLi,i);
1187 if(!fillIntVector(o,arr[i]))
1188 throw INTERP_KERNEL::Exception(msg);
1192 throw INTERP_KERNEL::Exception(msg);
1195 static void convertPyToVectorPairStringVecString(PyObject *pyLi, std::vector< std::pair<std::string, std::vector<std::string> > >& arr) throw(INTERP_KERNEL::Exception)
1197 const char msg[]="convertPyToVectorPairStringVecString : expecting list of tuples containing each exactly 2 items : one string and one vector of string !";
1198 if(PyList_Check(pyLi))
1200 Py_ssize_t sz=PyList_Size(pyLi);
1202 for(int i=0;i<sz;i++)
1204 PyObject *o=PyList_GetItem(pyLi,i);
1205 if(PyTuple_Check(o))
1207 int sz2=PyTuple_Size(o);
1209 throw INTERP_KERNEL::Exception(msg);
1210 std::pair<std::string, std::vector<std::string> > item;
1211 PyObject *o_0=PyTuple_GetItem(o,0);
1212 if(!PyString_Check(o_0))
1213 throw INTERP_KERNEL::Exception(msg);
1214 item.first=PyString_AsString(o_0);
1215 PyObject *o_1=PyTuple_GetItem(o,1);
1216 if(!fillStringVector(o_1,item.second))
1217 throw INTERP_KERNEL::Exception(msg);
1221 throw INTERP_KERNEL::Exception(msg);
1224 else if(PyTuple_Check(pyLi))
1226 Py_ssize_t sz=PyTuple_Size(pyLi);
1228 for(int i=0;i<sz;i++)
1230 PyObject *o=PyTuple_GetItem(pyLi,i);
1231 if(PyTuple_Check(o))
1233 int sz2=PyTuple_Size(o);
1235 throw INTERP_KERNEL::Exception(msg);
1236 std::pair<std::string, std::vector<std::string> > item;
1237 PyObject *o_0=PyTuple_GetItem(o,0);
1238 if(!PyString_Check(o_0))
1239 throw INTERP_KERNEL::Exception(msg);
1240 item.first=PyString_AsString(o_0);
1241 PyObject *o_1=PyTuple_GetItem(o,1);
1242 if(!fillStringVector(o_1,item.second))
1243 throw INTERP_KERNEL::Exception(msg);
1247 throw INTERP_KERNEL::Exception(msg);
1251 throw INTERP_KERNEL::Exception(msg);
1254 static PyObject *convertDblArrToPyList(const double *ptr, int size) throw(INTERP_KERNEL::Exception)
1256 PyObject *ret=PyList_New(size);
1257 for(int i=0;i<size;i++)
1258 PyList_SetItem(ret,i,PyFloat_FromDouble(ptr[i]));
1262 static PyObject *convertDblArrToPyList2(const std::vector<double>& v) throw(INTERP_KERNEL::Exception)
1265 PyObject *ret=PyList_New(size);
1266 for(int i=0;i<size;i++)
1267 PyList_SetItem(ret,i,PyFloat_FromDouble(v[i]));
1271 static PyObject *convertDblArrToPyListOfTuple(const double *vals, int nbOfComp, int nbOfTuples) throw(INTERP_KERNEL::Exception)
1273 PyObject *ret=PyList_New(nbOfTuples);
1274 for(int i=0;i<nbOfTuples;i++)
1276 PyObject *t=PyTuple_New(nbOfComp);
1277 for(int j=0;j<nbOfComp;j++)
1278 PyTuple_SetItem(t,j,PyFloat_FromDouble(vals[i*nbOfComp+j]));
1279 PyList_SetItem(ret,i,t);
1284 static PyObject *convertCharArrToPyListOfTuple(const char *vals, int nbOfComp, int nbOfTuples) throw(INTERP_KERNEL::Exception)
1286 PyObject *ret=PyList_New(nbOfTuples);
1287 INTERP_KERNEL::AutoPtr<char> tmp=new char[nbOfComp+1]; tmp[nbOfComp]='\0';
1288 for(int i=0;i<nbOfTuples;i++)
1290 std::copy(vals+i*nbOfComp,vals+(i+1)*nbOfComp,(char *)tmp);
1291 PyList_SetItem(ret,i,PyString_FromString(tmp));
1296 static double *convertPyToNewDblArr2(PyObject *pyLi, int *size) throw(INTERP_KERNEL::Exception)
1298 if(PyList_Check(pyLi))
1300 *size=PyList_Size(pyLi);
1301 double *tmp=(double *)malloc((*size)*sizeof(double));
1302 for(int i=0;i<*size;i++)
1304 PyObject *o=PyList_GetItem(pyLi,i);
1305 if(PyFloat_Check(o))
1307 double val=PyFloat_AS_DOUBLE(o);
1310 else if(PyInt_Check(o))
1312 long val0=PyInt_AS_LONG(o);
1319 throw INTERP_KERNEL::Exception("convertPyToNewDblArr2 : list must contain floats/integers only");
1324 else if(PyTuple_Check(pyLi))
1326 *size=PyTuple_Size(pyLi);
1327 double *tmp=(double *)malloc((*size)*sizeof(double));
1328 for(int i=0;i<*size;i++)
1330 PyObject *o=PyTuple_GetItem(pyLi,i);
1331 if(PyFloat_Check(o))
1333 double val=PyFloat_AS_DOUBLE(o);
1336 else if(PyInt_Check(o))
1338 long val0=PyInt_AS_LONG(o);
1345 throw INTERP_KERNEL::Exception("convertPyToNewDblArr2 : tuple must contain floats/integers only");
1351 throw INTERP_KERNEL::Exception("convertPyToNewDblArr2 : not a list");
1354 static void fillArrayWithPyListDbl3(PyObject *pyLi, int& nbOfElt, std::vector<double>& ret)
1356 static const char MSG[]="fillArrayWithPyListDbl3 : It appears that the input list or tuple is composed by elts having different sizes !";
1357 if(PyFloat_Check(pyLi))
1363 throw INTERP_KERNEL::Exception(MSG);
1364 double val=PyFloat_AS_DOUBLE(pyLi);
1367 else if(PyInt_Check(pyLi))
1369 long val0=PyInt_AS_LONG(pyLi);
1375 throw INTERP_KERNEL::Exception(MSG);
1378 else if(PyList_Check(pyLi))
1380 int size=PyList_Size(pyLi);
1382 for(int i=0;i<size;i++)
1384 PyObject *o=PyList_GetItem(pyLi,i);
1386 fillArrayWithPyListDbl3(o,tmp1,ret);
1394 throw INTERP_KERNEL::Exception(MSG);
1397 else if(PyTuple_Check(pyLi))
1399 int size=PyTuple_Size(pyLi);
1401 for(int i=0;i<size;i++)
1403 PyObject *o=PyTuple_GetItem(pyLi,i);
1405 fillArrayWithPyListDbl3(o,tmp1,ret);
1413 throw INTERP_KERNEL::Exception(MSG);
1417 throw INTERP_KERNEL::Exception("fillArrayWithPyListDbl3 : Unrecognized type ! Should be a composition of tuple,list,int and float !");
1420 static std::vector<double> fillArrayWithPyListDbl2(PyObject *pyLi, int& nbOfTuples, int& nbOfComp) throw(INTERP_KERNEL::Exception)
1422 std::vector<double> ret;
1423 int size1=-1,size2=-1;
1424 if(PyList_Check(pyLi))
1426 size1=PyList_Size(pyLi);
1427 for(int i=0;i<size1;i++)
1429 PyObject *o=PyList_GetItem(pyLi,i);
1430 fillArrayWithPyListDbl3(o,size2,ret);
1435 else if(PyTuple_Check(pyLi))
1437 size1=PyTuple_Size(pyLi);
1438 for(int i=0;i<size1;i++)
1440 PyObject *o=PyTuple_GetItem(pyLi,i);
1441 fillArrayWithPyListDbl3(o,size2,ret);
1447 throw INTERP_KERNEL::Exception("fillArrayWithPyListDbl2 : Unrecognized type ! Should be a tuple or a list !");
1449 checkFillArrayWithPyList(size1,size2,nbOfTuples,nbOfComp);
1453 //convertFromPyObjVectorOfObj<const MEDCoupling::MEDCouplingUMesh *>(pyLi,SWIGTYPE_p_MEDCoupling__MEDCouplingUMesh,"MEDCouplingUMesh")
1455 static void convertFromPyObjVectorOfObj(PyObject *pyLi, swig_type_info *ty, const char *typeStr, typename std::vector<T>& ret)
1458 if(PyList_Check(pyLi))
1460 int size=PyList_Size(pyLi);
1462 for(int i=0;i<size;i++)
1464 PyObject *obj=PyList_GetItem(pyLi,i);
1465 int status=SWIG_ConvertPtr(obj,&argp,ty,0|0);
1466 if(!SWIG_IsOK(status))
1468 std::ostringstream oss; oss << "convertFromPyObjVectorOfObj : list is excepted to contain only " << typeStr << " instances !";
1469 throw INTERP_KERNEL::Exception(oss.str().c_str());
1471 T arg=reinterpret_cast< T >(argp);
1475 else if(PyTuple_Check(pyLi))
1477 int size=PyTuple_Size(pyLi);
1479 for(int i=0;i<size;i++)
1481 PyObject *obj=PyTuple_GetItem(pyLi,i);
1482 int status=SWIG_ConvertPtr(obj,&argp,ty,0|0);
1483 if(!SWIG_IsOK(status))
1485 std::ostringstream oss; oss << "convertFromPyObjVectorOfObj : tuple is excepted to contain only " << typeStr << " instances !";
1486 throw INTERP_KERNEL::Exception(oss.str().c_str());
1488 T arg=reinterpret_cast< T >(argp);
1492 else if(SWIG_IsOK(SWIG_ConvertPtr(pyLi,&argp,ty,0|0)))
1495 T arg=reinterpret_cast< T >(argp);
1499 throw INTERP_KERNEL::Exception("convertFromPyObjVectorOfObj : not a list nor a tuple");
1503 * if python int -> cpp int sw=1
1504 * if python list[int] -> cpp vector<int> sw=2
1505 * if python tuple[int] -> cpp vector<int> sw=2
1506 * if python DataArrayInt -> cpp DataArrayInt sw=3
1507 * if python DataArrayIntTuple -> cpp DataArrayIntTuple sw=4
1509 * switch between (int,vector<int>,DataArrayInt)
1511 static void convertObjToPossibleCpp1(PyObject *value, int& sw, int& iTyypp, std::vector<int>& stdvecTyypp, MEDCoupling::DataArrayInt *& daIntTyypp, MEDCoupling::DataArrayIntTuple *&daIntTuple) throw(INTERP_KERNEL::Exception)
1514 if(PyInt_Check(value))
1516 iTyypp=(int)PyInt_AS_LONG(value);
1520 if(PyTuple_Check(value))
1522 int size=PyTuple_Size(value);
1523 stdvecTyypp.resize(size);
1524 for(int i=0;i<size;i++)
1526 PyObject *o=PyTuple_GetItem(value,i);
1528 stdvecTyypp[i]=(int)PyInt_AS_LONG(o);
1531 std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not integer ! only tuples of integers accepted !";
1532 throw INTERP_KERNEL::Exception(oss.str().c_str());
1538 if(PyList_Check(value))
1540 int size=PyList_Size(value);
1541 stdvecTyypp.resize(size);
1542 for(int i=0;i<size;i++)
1544 PyObject *o=PyList_GetItem(value,i);
1546 stdvecTyypp[i]=(int)PyInt_AS_LONG(o);
1549 std::ostringstream oss; oss << "List as been detected but element #" << i << " is not integer ! only lists of integers accepted !";
1550 throw INTERP_KERNEL::Exception(oss.str().c_str());
1557 int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayInt,0|0);
1558 if(SWIG_IsOK(status))
1560 daIntTyypp=reinterpret_cast< MEDCoupling::DataArrayInt * >(argp);
1564 status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayIntTuple,0|0);
1565 if(SWIG_IsOK(status))
1567 daIntTuple=reinterpret_cast< MEDCoupling::DataArrayIntTuple * >(argp);
1571 throw INTERP_KERNEL::Exception("5 types accepted : integer, tuple of integer, list of integer, DataArrayInt, DataArrayIntTuple");
1575 * if python double -> cpp double sw=1
1576 * if python int -> cpp double sw=1
1577 * if python list[double] -> cpp vector<double> sw=2
1578 * if python list[int] -> cpp vector<double> sw=2
1579 * if python tuple[double] -> cpp vector<double> sw=2
1580 * if python tuple[int] -> cpp vector<double> sw=2
1581 * if python DataArrayDouble -> cpp DataArrayDouble sw=3
1583 * switch between (int,vector<int>,DataArrayInt)
1585 static void convertObjToPossibleCpp4(PyObject *value, int& sw, double& iTyypp, std::vector<double>& stdvecTyypp, MEDCoupling::DataArrayDouble *& daIntTyypp) throw(INTERP_KERNEL::Exception)
1588 if(PyFloat_Check(value))
1590 iTyypp=PyFloat_AS_DOUBLE(value);
1594 if(PyInt_Check(value))
1596 iTyypp=(double)PyInt_AS_LONG(value);
1600 if(PyTuple_Check(value))
1602 int size=PyTuple_Size(value);
1603 stdvecTyypp.resize(size);
1604 for(int i=0;i<size;i++)
1606 PyObject *o=PyTuple_GetItem(value,i);
1607 if(PyFloat_Check(o))
1608 stdvecTyypp[i]=PyFloat_AS_DOUBLE(o);
1609 else if(PyInt_Check(o))
1610 stdvecTyypp[i]=(double)PyInt_AS_LONG(o);
1613 std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not double ! only tuples of doubles accepted or integer !";
1614 throw INTERP_KERNEL::Exception(oss.str().c_str());
1620 if(PyList_Check(value))
1622 int size=PyList_Size(value);
1623 stdvecTyypp.resize(size);
1624 for(int i=0;i<size;i++)
1626 PyObject *o=PyList_GetItem(value,i);
1627 if(PyFloat_Check(o))
1628 stdvecTyypp[i]=PyFloat_AS_DOUBLE(o);
1629 else if(PyInt_Check(o))
1630 stdvecTyypp[i]=(double)PyInt_AS_LONG(o);
1633 std::ostringstream oss; oss << "List as been detected but element #" << i << " is not double ! only lists of doubles accepted or integer !";
1634 throw INTERP_KERNEL::Exception(oss.str().c_str());
1641 int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayDouble,0|0);
1642 if(!SWIG_IsOK(status))
1643 throw INTERP_KERNEL::Exception("5 types accepted : double float, integer, tuple of double float or int, list of double float or int, DataArrayDouble");
1644 daIntTyypp=reinterpret_cast< MEDCoupling::DataArrayDouble * >(argp);
1649 * if python double -> cpp double sw=1
1650 * if python int -> cpp double sw=1
1651 * if python list[double] -> cpp vector<double> sw=2
1652 * if python list[int] -> cpp vector<double> sw=2
1653 * if python tuple[double] -> cpp vector<double> sw=2
1654 * if python tuple[int] -> cpp vector<double> sw=2
1655 * if python DataArrayDoubleTuple -> cpp DataArrayDoubleTuple sw=3
1657 * switch between (int,vector<int>,DataArrayInt)
1659 static void convertObjToPossibleCpp44(PyObject *value, int& sw, double& iTyypp, std::vector<double>& stdvecTyypp, MEDCoupling::DataArrayDoubleTuple *& daIntTyypp) throw(INTERP_KERNEL::Exception)
1662 if(PyFloat_Check(value))
1664 iTyypp=PyFloat_AS_DOUBLE(value);
1668 if(PyInt_Check(value))
1670 iTyypp=(double)PyInt_AS_LONG(value);
1674 if(PyTuple_Check(value))
1676 int size=PyTuple_Size(value);
1677 stdvecTyypp.resize(size);
1678 for(int i=0;i<size;i++)
1680 PyObject *o=PyTuple_GetItem(value,i);
1681 if(PyFloat_Check(o))
1682 stdvecTyypp[i]=PyFloat_AS_DOUBLE(o);
1683 else if(PyInt_Check(o))
1684 stdvecTyypp[i]=(double)PyInt_AS_LONG(o);
1687 std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not double ! only tuples of doubles accepted or integer !";
1688 throw INTERP_KERNEL::Exception(oss.str().c_str());
1694 if(PyList_Check(value))
1696 int size=PyList_Size(value);
1697 stdvecTyypp.resize(size);
1698 for(int i=0;i<size;i++)
1700 PyObject *o=PyList_GetItem(value,i);
1701 if(PyFloat_Check(o))
1702 stdvecTyypp[i]=PyFloat_AS_DOUBLE(o);
1703 else if(PyInt_Check(o))
1704 stdvecTyypp[i]=(double)PyInt_AS_LONG(o);
1707 std::ostringstream oss; oss << "List as been detected but element #" << i << " is not double ! only lists of doubles accepted or integer !";
1708 throw INTERP_KERNEL::Exception(oss.str().c_str());
1715 int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayDoubleTuple,0|0);
1716 if(!SWIG_IsOK(status))
1717 throw INTERP_KERNEL::Exception("5 types accepted : double float, integer, tuple of double float or int, list of double float or int, DataArrayDoubleTuple");
1718 daIntTyypp=reinterpret_cast< MEDCoupling::DataArrayDoubleTuple * >(argp);
1723 * if python int -> cpp int sw=1
1724 * if python list[int] -> cpp vector<int> sw=2
1725 * if python tuple[int] -> cpp vector<int> sw=2
1726 * if python slicp -> cpp pair sw=3 (begin,end,step)
1727 * if python DataArrayInt -> cpp DataArrayInt sw=4 . The returned pointer cannot be the null pointer ! If null an exception is thrown.
1729 * switch between (int,vector<int>,DataArrayInt)
1731 static void convertObjToPossibleCpp2(PyObject *value, int nbelem, int& sw, int& iTyypp, std::vector<int>& stdvecTyypp, std::pair<int, std::pair<int,int> >& p, MEDCoupling::DataArrayInt *& daIntTyypp) throw(INTERP_KERNEL::Exception)
1733 const char *msg="5 types accepted : integer, tuple of integer, list of integer, slice, DataArrayInt, DataArrayIntTuple";
1735 if(PyInt_Check(value))
1737 iTyypp=(int)PyInt_AS_LONG(value);
1741 if(PyTuple_Check(value))
1743 int size=PyTuple_Size(value);
1744 stdvecTyypp.resize(size);
1745 for(int i=0;i<size;i++)
1747 PyObject *o=PyTuple_GetItem(value,i);
1749 stdvecTyypp[i]=(int)PyInt_AS_LONG(o);
1752 std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not integer ! only tuples of integers accepted !";
1753 throw INTERP_KERNEL::Exception(oss.str().c_str());
1759 if(PyList_Check(value))
1761 int size=PyList_Size(value);
1762 stdvecTyypp.resize(size);
1763 for(int i=0;i<size;i++)
1765 PyObject *o=PyList_GetItem(value,i);
1767 stdvecTyypp[i]=(int)PyInt_AS_LONG(o);
1770 std::ostringstream oss; oss << "List as been detected but element #" << i << " is not integer ! only lists of integers accepted !";
1771 throw INTERP_KERNEL::Exception(oss.str().c_str());
1777 if(PySlice_Check(value))
1779 Py_ssize_t strt=2,stp=2,step=2;
1780 PySliceObject *oC=reinterpret_cast<PySliceObject *>(value);
1781 GetIndicesOfSlice(oC,nbelem,&strt,&stp,&step,"Slice in subscriptable object DataArray invalid !");
1784 p.second.second=step;
1789 int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayInt,0|0);
1790 if(SWIG_IsOK(status))
1792 daIntTyypp=reinterpret_cast< MEDCoupling::DataArrayInt * >(argp);
1795 std::ostringstream oss; oss << msg << " Instance in null !";
1796 throw INTERP_KERNEL::Exception(oss.str().c_str());
1801 status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayIntTuple,0|0);
1802 if(SWIG_IsOK(status))
1804 MEDCoupling::DataArrayIntTuple *tmp=reinterpret_cast< MEDCoupling::DataArrayIntTuple * >(argp);
1807 std::ostringstream oss; oss << msg << " Instance in null !";
1808 throw INTERP_KERNEL::Exception(oss.str().c_str());
1810 stdvecTyypp.resize(tmp->getNumberOfCompo());
1811 std::copy(tmp->getConstPointer(),tmp->getConstPointer()+tmp->getNumberOfCompo(),stdvecTyypp.begin());
1815 throw INTERP_KERNEL::Exception(msg);
1819 * Idem than convertObjToPossibleCpp2
1821 static void convertObjToPossibleCpp2WithNegIntInterp(PyObject *value, int nbelem, int& sw, int& iTyypp, std::vector<int>& stdvecTyypp, std::pair<int, std::pair<int,int> >& p, MEDCoupling::DataArrayInt *& daIntTyypp) throw(INTERP_KERNEL::Exception)
1823 convertObjToPossibleCpp2(value,nbelem,sw,iTyypp,stdvecTyypp,p,daIntTyypp);
1826 iTyypp=InterpreteNegativeInt(iTyypp,nbelem);
1831 * if python int -> cpp int sw=1
1832 * if python tuple[int] -> cpp vector<int> sw=2
1833 * if python list[int] -> cpp vector<int> sw=2
1834 * if python slice -> cpp pair sw=3
1835 * if python DataArrayIntTuple -> cpp DataArrayIntTuple sw=4 . WARNING The returned pointer can be the null pointer !
1837 static void convertObjToPossibleCpp22(PyObject *value, int nbelem, int& sw, int& iTyypp, std::vector<int>& stdvecTyypp, std::pair<int, std::pair<int,int> >& p, MEDCoupling::DataArrayIntTuple *& daIntTyypp) throw(INTERP_KERNEL::Exception)
1840 if(PyInt_Check(value))
1842 iTyypp=(int)PyInt_AS_LONG(value);
1846 if(PyTuple_Check(value))
1848 int size=PyTuple_Size(value);
1849 stdvecTyypp.resize(size);
1850 for(int i=0;i<size;i++)
1852 PyObject *o=PyTuple_GetItem(value,i);
1854 stdvecTyypp[i]=(int)PyInt_AS_LONG(o);
1857 std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not integer ! only tuples of integers accepted !";
1858 throw INTERP_KERNEL::Exception(oss.str().c_str());
1864 if(PyList_Check(value))
1866 int size=PyList_Size(value);
1867 stdvecTyypp.resize(size);
1868 for(int i=0;i<size;i++)
1870 PyObject *o=PyList_GetItem(value,i);
1872 stdvecTyypp[i]=(int)PyInt_AS_LONG(o);
1875 std::ostringstream oss; oss << "List as been detected but element #" << i << " is not integer ! only lists of integers accepted !";
1876 throw INTERP_KERNEL::Exception(oss.str().c_str());
1882 if(PySlice_Check(value))
1884 Py_ssize_t strt=2,stp=2,step=2;
1885 PySliceObject *oC=reinterpret_cast<PySliceObject *>(value);
1886 GetIndicesOfSlice(oC,nbelem,&strt,&stp,&step,"Slice in subscriptable object DataArray invalid !");
1889 p.second.second=step;
1894 int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayIntTuple,0|0);
1895 if(!SWIG_IsOK(status))
1896 throw INTERP_KERNEL::Exception("4 types accepted : integer, tuple of integer, list of integer, slice, DataArrayIntTuple");
1897 daIntTyypp=reinterpret_cast< MEDCoupling::DataArrayIntTuple * >(argp);
1902 * if python string with size one -> cpp char sw=1
1903 * if python string with size different from one -> cpp string sw=2
1904 * if python tuple[string] or list[string] -> vector<string> sw=3
1905 * if python not null pointer of DataArrayChar -> cpp DataArrayChar sw=4
1906 * switch between (int,string,vector<string>,DataArrayChar)
1908 static void convertObjToPossibleCpp6(PyObject *value, int& sw, char& cTyp, std::string& sType, std::vector<std::string>& vsType, MEDCoupling::DataArrayChar *& dacType) throw(INTERP_KERNEL::Exception)
1910 const char *msg="4 types accepted : string, list or tuple of strings having same size, not null DataArrayChar instance.";
1912 if(PyString_Check(value))
1914 const char *pt=PyString_AsString(value);
1915 Py_ssize_t sz=PyString_Size(value);
1929 if(PyTuple_Check(value))
1931 int size=PyTuple_Size(value);
1932 vsType.resize(size);
1933 for(int i=0;i<size;i++)
1935 PyObject *o=PyTuple_GetItem(value,i);
1936 if(PyString_Check(o))
1937 vsType[i]=PyString_AsString(o);
1940 std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not a string ! only tuples of strings accepted !";
1941 throw INTERP_KERNEL::Exception(oss.str().c_str());
1947 if(PyList_Check(value))
1949 int size=PyList_Size(value);
1950 vsType.resize(size);
1951 for(int i=0;i<size;i++)
1953 PyObject *o=PyList_GetItem(value,i);
1954 if(PyString_Check(o))
1955 vsType[i]=PyString_AsString(o);
1958 std::ostringstream oss; oss << "List as been detected but element #" << i << " is not string ! only lists of strings accepted !";
1959 throw INTERP_KERNEL::Exception(oss.str().c_str());
1966 int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayChar,0|0);
1967 if(SWIG_IsOK(status))
1969 dacType=reinterpret_cast< MEDCoupling::DataArrayChar * >(argp);
1972 std::ostringstream oss; oss << msg << " Instance in null !";
1973 throw INTERP_KERNEL::Exception(oss.str().c_str());
1978 throw INTERP_KERNEL::Exception(msg);
1982 * if value int -> cpp it sw=1
1983 * if value list[int] -> vt sw=2
1984 * if value tuple[int] -> vt sw=2
1985 * if value slice -> pt sw=3
1986 * if value DataArrayInt -> dt sw=4
1987 * if value tuple [int,int] -> cpp it,ip sw=5
1988 * if value tuple [list[int],int] -> cpp vt,ip sw=6
1989 * if value tuple [tuple[int],int] -> cpp vt,ip sw=6
1990 * if value tuple [slice,int] -> cpp pt,ip sw=7
1991 * if value tuple [DaI,int] -> cpp dt,ip sw=8
1992 * if value tuple [int,list[int]] -> cpp it,vc sw=9
1993 * if value tuple [list[int],list[int]] -> cpp vt,vc sw=10
1994 * if value tuple [tuple[int],list[int]] -> cpp vt,vc sw=10
1995 * if value tuple [slice,list[int]] -> cpp pt,vc sw=11
1996 * if value tuple [DaI,list[int]] -> cpp dt,vc sw=12
1997 * if value tuple [int,tuple[int]] -> cpp it,vc sw=9
1998 * if value tuple [list[int],tuple[int]] -> cpp vt,vc sw=10
1999 * if value tuple [tuple[int],tuple[int]] -> cpp vt,vc sw=10
2000 * if value tuple [slice,tuple[int]] -> cpp pt,vc sw=11
2001 * if value tuple [DaI,tuple[int]] -> cpp dt,vc sw=12
2002 * if value tuple [int,slice] -> cpp it,pc sw=13
2003 * if value tuple [list[int],slice] -> cpp vt,pc sw=14
2004 * if value tuple [tuple[int],slice] -> cpp vt,pc sw=14
2005 * if value tuple [slice,slice] -> cpp pt,pc sw=15
2006 * if value tuple [DaI,slice] -> cpp dt,pc sw=16
2008 * switch between (int,vector<int>,DataArrayInt)
2010 static void convertObjToPossibleCpp3(PyObject *value, int nbTuple, int nbCompo, int& sw, int& it, int& ic, std::vector<int>& vt, std::vector<int>& vc,
2011 std::pair<int, std::pair<int,int> >& pt, std::pair<int, std::pair<int,int> >& pc,
2012 MEDCoupling::DataArrayInt *&dt, MEDCoupling::DataArrayInt *&dc) throw(INTERP_KERNEL::Exception)
2014 if(!PyTuple_Check(value))
2016 convertObjToPossibleCpp2WithNegIntInterp(value,nbTuple,sw,it,vt,pt,dt);
2021 int sz=PyTuple_Size(value);
2023 throw INTERP_KERNEL::Exception("Unexpected nb of slice element : 1 or 2 expected !\n1st is for tuple selection, 2nd for component selection !");
2024 PyObject *ob0=PyTuple_GetItem(value,0);
2026 convertObjToPossibleCpp2WithNegIntInterp(ob0,nbTuple,sw1,it,vt,pt,dt);
2027 PyObject *ob1=PyTuple_GetItem(value,1);
2028 convertObjToPossibleCpp2WithNegIntInterp(ob1,nbCompo,sw2,ic,vc,pc,dc);
2034 * if value int -> cpp val sw=1
2035 * if value double -> cpp val sw=1
2036 * if value DataArrayDouble -> cpp DataArrayDouble sw=2
2037 * if value DataArrayDoubleTuple -> cpp DataArrayDoubleTuple sw=3
2038 * if value list[int,double] -> cpp std::vector<double> sw=4
2039 * if value tuple[int,double] -> cpp std::vector<double> sw=4
2041 static void convertObjToPossibleCpp5(PyObject *value, int& sw, double& val, MEDCoupling::DataArrayDouble *&d, MEDCoupling::DataArrayDoubleTuple *&e, std::vector<double>& f)
2044 if(PyFloat_Check(value))
2046 val=PyFloat_AS_DOUBLE(value);
2050 if(PyInt_Check(value))
2052 val=(double)PyInt_AS_LONG(value);
2056 if(PyTuple_Check(value))
2058 int size=PyTuple_Size(value);
2060 for(int i=0;i<size;i++)
2062 PyObject *o=PyTuple_GetItem(value,i);
2063 if(PyFloat_Check(o))
2064 f[i]=PyFloat_AS_DOUBLE(o);
2065 else if(PyInt_Check(o))
2066 f[i]=(double)PyInt_AS_LONG(o);
2069 std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not double ! only tuples of doubles accepted or integer !";
2070 throw INTERP_KERNEL::Exception(oss.str().c_str());
2076 if(PyList_Check(value))
2078 int size=PyList_Size(value);
2080 for(int i=0;i<size;i++)
2082 PyObject *o=PyList_GetItem(value,i);
2083 if(PyFloat_Check(o))
2084 f[i]=PyFloat_AS_DOUBLE(o);
2085 else if(PyInt_Check(o))
2086 f[i]=(double)PyInt_AS_LONG(o);
2089 std::ostringstream oss; oss << "List as been detected but element #" << i << " is not double ! only lists of doubles accepted or integer !";
2090 throw INTERP_KERNEL::Exception(oss.str().c_str());
2097 int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayDouble,0|0);
2098 if(SWIG_IsOK(status))
2100 d=reinterpret_cast< MEDCoupling::DataArrayDouble * >(argp);
2104 status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayDoubleTuple,0|0);
2105 if(SWIG_IsOK(status))
2107 e=reinterpret_cast< MEDCoupling::DataArrayDoubleTuple * >(argp);
2111 throw INTERP_KERNEL::Exception("4 types accepted : integer, double, DataArrayDouble, DataArrayDoubleTuple");
2115 * if value int -> cpp val sw=1
2116 * if value double -> cpp val sw=1
2117 * if value DataArrayDouble -> cpp DataArrayDouble sw=2
2118 * if value DataArrayDoubleTuple -> cpp DataArrayDoubleTuple sw=3
2119 * if value list[int,double] -> cpp std::vector<double> sw=4
2120 * if value tuple[int,double] -> cpp std::vector<double> sw=4
2122 static const double *convertObjToPossibleCpp5_Safe(PyObject *value, int& sw, double& val, MEDCoupling::DataArrayDouble *&d, MEDCoupling::DataArrayDoubleTuple *&e, std::vector<double>& f,
2123 const char *msg, int nbTuplesExpected, int nbCompExpected, bool throwIfNullPt) throw(INTERP_KERNEL::Exception)
2126 if(PyFloat_Check(value))
2128 val=PyFloat_AS_DOUBLE(value);
2130 if(nbTuplesExpected*nbCompExpected!=1)
2132 std::ostringstream oss; oss << msg << "dimension expected to be " << nbTuplesExpected*nbCompExpected << " , and your data in input has dimension one (single PyFloat) !";
2133 throw INTERP_KERNEL::Exception(oss.str().c_str());
2137 if(PyInt_Check(value))
2139 val=(double)PyInt_AS_LONG(value);
2141 if(nbTuplesExpected*nbCompExpected!=1)
2143 std::ostringstream oss; oss << msg << "dimension expected to be " << nbTuplesExpected*nbCompExpected << " , and your data in input has dimension one (single PyInt) !";
2144 throw INTERP_KERNEL::Exception(oss.str().c_str());
2148 if(PyTuple_Check(value) || PyList_Check(value))
2152 int tmp1=nbTuplesExpected,tmp2=nbCompExpected;
2153 std::vector<double> ret=fillArrayWithPyListDbl2(value,tmp1,tmp2);
2158 catch(INTERP_KERNEL::Exception& exc) { throw exc; }
2161 int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayDouble,0|0);
2162 if(SWIG_IsOK(status))
2164 d=reinterpret_cast< MEDCoupling::DataArrayDouble * >(argp);
2168 if(d->getNumberOfTuples()==nbTuplesExpected)
2170 if(d->getNumberOfComponents()==nbCompExpected)
2172 return d->getConstPointer();
2176 std::ostringstream oss; oss << msg << "nb of components expected to be " << nbCompExpected << " , and input has " << d->getNumberOfComponents() << " components !";
2177 throw INTERP_KERNEL::Exception(oss.str().c_str());
2182 std::ostringstream oss; oss << msg << " input DataArrayDouble should have a number of tuples equal to " << nbTuplesExpected << " and there are " << d->getNumberOfTuples() << " tuples !";
2183 throw INTERP_KERNEL::Exception(oss.str().c_str());
2190 std::ostringstream oss; oss << msg << " null pointer not accepted!";
2191 throw INTERP_KERNEL::Exception(oss.str().c_str());
2197 status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayDoubleTuple,0|0);
2198 if(SWIG_IsOK(status))
2200 e=reinterpret_cast< MEDCoupling::DataArrayDoubleTuple * >(argp);
2202 if(e->getNumberOfCompo()==nbCompExpected)
2204 if(nbTuplesExpected==1)
2205 return e->getConstPointer();
2208 std::ostringstream oss; oss << msg << "nb of tuples expected to be " << nbTuplesExpected << " , and input DataArrayDoubleTuple has always one tuple by contruction !";
2209 throw INTERP_KERNEL::Exception(oss.str().c_str());
2214 std::ostringstream oss; oss << msg << "nb of components expected to be " << nbCompExpected << " , and input DataArrayDoubleTuple has " << e->getNumberOfCompo() << " components !";
2215 throw INTERP_KERNEL::Exception(oss.str().c_str());
2218 throw INTERP_KERNEL::Exception("4 types accepted : integer, double, DataArrayDouble, DataArrayDoubleTuple");
2222 * if value int -> cpp val sw=1
2223 * if value double -> cpp val sw=1
2224 * if value DataArrayDouble -> cpp DataArrayDouble sw=2
2225 * if value DataArrayDoubleTuple -> cpp DataArrayDoubleTuple sw=3
2226 * if value list[int,double] -> cpp std::vector<double> sw=4
2227 * if value tuple[int,double] -> cpp std::vector<double> sw=4
2229 static const double *convertObjToPossibleCpp5_Safe2(PyObject *value, int& sw, double& val, MEDCoupling::DataArrayDouble *&d, MEDCoupling::DataArrayDoubleTuple *&e, std::vector<double>& f,
2230 const char *msg, int nbCompExpected, bool throwIfNullPt, int& nbTuples) throw(INTERP_KERNEL::Exception)
2233 if(PyFloat_Check(value))
2235 val=PyFloat_AS_DOUBLE(value);
2237 if(nbCompExpected!=1)
2239 std::ostringstream oss; oss << msg << "dimension expected to be " << nbCompExpected << " , and your data in input has dimension one (single PyFloat) !";
2240 throw INTERP_KERNEL::Exception(oss.str().c_str());
2245 if(PyInt_Check(value))
2247 val=(double)PyInt_AS_LONG(value);
2249 if(nbCompExpected!=1)
2251 std::ostringstream oss; oss << msg << "dimension expected to be " << nbCompExpected << " , and your data in input has dimension one (single PyInt) !";
2252 throw INTERP_KERNEL::Exception(oss.str().c_str());
2257 if(PyTuple_Check(value))
2259 int size=PyTuple_Size(value);
2261 for(int i=0;i<size;i++)
2263 PyObject *o=PyTuple_GetItem(value,i);
2264 if(PyFloat_Check(o))
2265 f[i]=PyFloat_AS_DOUBLE(o);
2266 else if(PyInt_Check(o))
2267 f[i]=(double)PyInt_AS_LONG(o);
2270 std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not double ! only tuples of doubles accepted or integer !";
2271 throw INTERP_KERNEL::Exception(oss.str().c_str());
2275 if(size%nbCompExpected!=0)
2277 std::ostringstream oss; oss << msg << "dimension expected to be a multiple of " << nbCompExpected << " , and your data in input has dimension " << f.size() << " !";
2278 throw INTERP_KERNEL::Exception(oss.str().c_str());
2280 nbTuples=size/nbCompExpected;
2283 if(PyList_Check(value))
2285 int size=PyList_Size(value);
2287 for(int i=0;i<size;i++)
2289 PyObject *o=PyList_GetItem(value,i);
2290 if(PyFloat_Check(o))
2291 f[i]=PyFloat_AS_DOUBLE(o);
2292 else if(PyInt_Check(o))
2293 f[i]=(double)PyInt_AS_LONG(o);
2296 std::ostringstream oss; oss << "List as been detected but element #" << i << " is not double ! only lists of doubles accepted or integer !";
2297 throw INTERP_KERNEL::Exception(oss.str().c_str());
2301 if(size%nbCompExpected!=0)
2303 std::ostringstream oss; oss << msg << "dimension expected to be a multiple of " << nbCompExpected << " , and your data in input has dimension " << f.size() << " !";
2304 throw INTERP_KERNEL::Exception(oss.str().c_str());
2306 nbTuples=size/nbCompExpected;
2310 int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayDouble,0|0);
2311 if(SWIG_IsOK(status))
2313 d=reinterpret_cast< MEDCoupling::DataArrayDouble * >(argp);
2317 if(d->getNumberOfComponents()==nbCompExpected)
2319 nbTuples=d->getNumberOfTuples();
2320 return d->getConstPointer();
2324 std::ostringstream oss; oss << msg << "nb of components expected to be a multiple of " << nbCompExpected << " , and input has " << d->getNumberOfComponents() << " components !";
2325 throw INTERP_KERNEL::Exception(oss.str().c_str());
2332 std::ostringstream oss; oss << msg << " null pointer not accepted!";
2333 throw INTERP_KERNEL::Exception(oss.str().c_str());
2336 { nbTuples=0; return 0; }
2339 status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayDoubleTuple,0|0);
2340 if(SWIG_IsOK(status))
2342 e=reinterpret_cast< MEDCoupling::DataArrayDoubleTuple * >(argp);
2346 if(e->getNumberOfCompo()==nbCompExpected)
2349 return e->getConstPointer();
2353 std::ostringstream oss; oss << msg << "nb of components expected to be " << nbCompExpected << " , and input DataArrayDoubleTuple has " << e->getNumberOfCompo() << " components !";
2354 throw INTERP_KERNEL::Exception(oss.str().c_str());
2361 std::ostringstream oss; oss << msg << " null pointer not accepted!";
2362 throw INTERP_KERNEL::Exception(oss.str().c_str());
2365 { nbTuples=0; return 0; }
2368 throw INTERP_KERNEL::Exception("4 types accepted : integer, double, DataArrayDouble, DataArrayDoubleTuple");
2372 * if value int -> cpp val sw=1
2373 * if value double -> cpp val sw=1
2374 * if value DataArrayDouble -> cpp DataArrayDouble sw=2
2375 * if value DataArrayDoubleTuple -> cpp DataArrayDoubleTuple sw=3
2376 * if value list[int,double] -> cpp std::vector<double> sw=4
2377 * if value tuple[int,double] -> cpp std::vector<double> sw=4
2379 static const double *convertObjToPossibleCpp5_SingleCompo(PyObject *value, int& sw, double& val, std::vector<double>& f,
2380 const char *msg, bool throwIfNullPt, int& nbTuples) throw(INTERP_KERNEL::Exception)
2382 MEDCoupling::DataArrayDouble *d=0;
2383 MEDCoupling::DataArrayDoubleTuple *e=0;
2385 if(PyFloat_Check(value))
2387 val=PyFloat_AS_DOUBLE(value);
2392 if(PyInt_Check(value))
2394 val=(double)PyInt_AS_LONG(value);
2399 if(PyTuple_Check(value))
2401 int size=PyTuple_Size(value);
2403 for(int i=0;i<size;i++)
2405 PyObject *o=PyTuple_GetItem(value,i);
2406 if(PyFloat_Check(o))
2407 f[i]=PyFloat_AS_DOUBLE(o);
2408 else if(PyInt_Check(o))
2409 f[i]=(double)PyInt_AS_LONG(o);
2412 std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not double ! only tuples of doubles accepted or integer !";
2413 throw INTERP_KERNEL::Exception(oss.str().c_str());
2420 if(PyList_Check(value))
2422 int size=PyList_Size(value);
2424 for(int i=0;i<size;i++)
2426 PyObject *o=PyList_GetItem(value,i);
2427 if(PyFloat_Check(o))
2428 f[i]=PyFloat_AS_DOUBLE(o);
2429 else if(PyInt_Check(o))
2430 f[i]=(double)PyInt_AS_LONG(o);
2433 std::ostringstream oss; oss << "List as been detected but element #" << i << " is not double ! only lists of doubles accepted or integer !";
2434 throw INTERP_KERNEL::Exception(oss.str().c_str());
2442 int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayDouble,0|0);
2443 if(SWIG_IsOK(status))
2445 d=reinterpret_cast< MEDCoupling::DataArrayDouble * >(argp);
2449 if(d->getNumberOfComponents()==1)
2451 nbTuples=d->getNumberOfTuples();
2452 return d->getConstPointer();
2456 std::ostringstream oss; oss << msg << "nb of components expected to be one, and input has " << d->getNumberOfComponents() << " components !";
2457 throw INTERP_KERNEL::Exception(oss.str().c_str());
2464 std::ostringstream oss; oss << msg << " null pointer not accepted!";
2465 throw INTERP_KERNEL::Exception(oss.str().c_str());
2468 { nbTuples=0; return 0; }
2471 status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayDoubleTuple,0|0);
2472 if(SWIG_IsOK(status))
2474 e=reinterpret_cast< MEDCoupling::DataArrayDoubleTuple * >(argp);
2478 nbTuples=e->getNumberOfCompo();
2479 return e->getConstPointer();
2485 std::ostringstream oss; oss << msg << " null pointer not accepted!";
2486 throw INTERP_KERNEL::Exception(oss.str().c_str());
2489 { nbTuples=0; return 0; }
2492 throw INTERP_KERNEL::Exception("4 types accepted : integer, double, DataArrayDouble, DataArrayDoubleTuple");
2496 * if python int -> cpp int sw=1
2497 * if python list[int] -> cpp vector<int> sw=2
2498 * if python tuple[int] -> cpp vector<int> sw=2
2499 * if python DataArrayInt -> cpp DataArrayInt sw=3
2500 * if python DataArrayIntTuple -> cpp DataArrayIntTuple sw=4
2502 * switch between (int,vector<int>,DataArrayInt)
2504 static const int *convertObjToPossibleCpp1_Safe(PyObject *value, int& sw, int& sz, int& iTyypp, std::vector<int>& stdvecTyypp) throw(INTERP_KERNEL::Exception)
2507 if(PyInt_Check(value))
2509 iTyypp=(int)PyInt_AS_LONG(value);
2513 if(PyTuple_Check(value))
2515 int size=PyTuple_Size(value);
2516 stdvecTyypp.resize(size);
2517 for(int i=0;i<size;i++)
2519 PyObject *o=PyTuple_GetItem(value,i);
2521 stdvecTyypp[i]=(int)PyInt_AS_LONG(o);
2524 std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not integer ! only tuples of integers accepted !";
2525 throw INTERP_KERNEL::Exception(oss.str().c_str());
2529 return &stdvecTyypp[0];
2531 if(PyList_Check(value))
2533 int size=PyList_Size(value);
2534 stdvecTyypp.resize(size);
2535 for(int i=0;i<size;i++)
2537 PyObject *o=PyList_GetItem(value,i);
2539 stdvecTyypp[i]=(int)PyInt_AS_LONG(o);
2542 std::ostringstream oss; oss << "List as been detected but element #" << i << " is not integer ! only lists of integers accepted !";
2543 throw INTERP_KERNEL::Exception(oss.str().c_str());
2547 return &stdvecTyypp[0];
2550 int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayInt,0|0);
2551 if(SWIG_IsOK(status))
2553 MEDCoupling::DataArrayInt *daIntTyypp=reinterpret_cast< MEDCoupling::DataArrayInt * >(argp);
2556 sw=3; sz=daIntTyypp->getNbOfElems();
2557 return daIntTyypp->begin();
2565 status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayIntTuple,0|0);
2566 if(SWIG_IsOK(status))
2568 MEDCoupling::DataArrayIntTuple *daIntTuple=reinterpret_cast< MEDCoupling::DataArrayIntTuple * >(argp);
2569 sw=4; sz=daIntTuple->getNumberOfCompo();
2570 return daIntTuple->getConstPointer();
2572 throw INTERP_KERNEL::Exception("5 types accepted : integer, tuple of integer, list of integer, DataArrayInt, DataArrayIntTuple");
2575 static MEDCoupling::DataArray *CheckAndRetrieveDataArrayInstance(PyObject *obj, const char *msg)
2578 int status=SWIG_ConvertPtr(obj,&aBasePtrVS,SWIGTYPE_p_MEDCoupling__DataArray,0|0);
2579 if(!SWIG_IsOK(status))
2581 status=SWIG_ConvertPtr(obj,&aBasePtrVS,SWIGTYPE_p_MEDCoupling__DataArrayDouble,0|0);
2582 if(!SWIG_IsOK(status))
2584 status=SWIG_ConvertPtr(obj,&aBasePtrVS,SWIGTYPE_p_MEDCoupling__DataArrayInt,0|0);
2585 if(!SWIG_IsOK(status))
2587 status=SWIG_ConvertPtr(obj,&aBasePtrVS,SWIGTYPE_p_MEDCoupling__DataArrayAsciiChar,0|0);
2588 if(!SWIG_IsOK(status))
2590 status=SWIG_ConvertPtr(obj,&aBasePtrVS,SWIGTYPE_p_MEDCoupling__DataArrayByte,0|0);
2591 std::ostringstream oss; oss << msg << " ! Accepted instances are DataArrayDouble, DataArrayInt, DataArrayAsciiChar, DataArrayByte !";
2592 throw INTERP_KERNEL::Exception(oss.str().c_str());
2597 return reinterpret_cast< MEDCoupling::DataArray * >(aBasePtrVS);
2600 static PyObject *NewMethWrapCallInitOnlyIfEmptyDictInInput(PyObject *cls, PyObject *args, const char *clsName)
2602 if(!PyTuple_Check(args))
2604 std::ostringstream oss; oss << clsName << ".__new__ : the args in input is expected to be a tuple !";
2605 throw INTERP_KERNEL::Exception(oss.str().c_str());
2607 PyObject *builtinsd(PyEval_GetBuiltins());//borrowed
2608 PyObject *obj(PyDict_GetItemString(builtinsd,"object"));//borrowed
2609 PyObject *selfMeth(PyObject_GetAttrString(obj,"__new__"));
2611 PyObject *tmp0(PyTuple_New(1));
2612 PyTuple_SetItem(tmp0,0,cls); Py_XINCREF(cls);
2613 PyObject *instance(PyObject_CallObject(selfMeth,tmp0));
2615 Py_DECREF(selfMeth);
2616 if(PyTuple_Size(args)==2 && PyDict_Check(PyTuple_GetItem(args,1)) && PyDict_Size(PyTuple_GetItem(args,1))==0 )
2617 {// NOT general case. only true if in unpickeling context ! call __init__. Because for all other cases, __init__ is called right after __new__ !
2618 PyObject *initMeth(PyObject_GetAttrString(instance,"__init__"));
2619 PyObject *tmp3(PyTuple_New(0));
2620 PyObject *tmp2(PyObject_CallObject(initMeth,tmp3));
2623 Py_DECREF(initMeth);
2628 static PyObject *NewMethWrapCallInitOnlyIfDictWithSingleEltInInput(PyObject *cls, PyObject *args, const char *clsName)
2630 if(!PyTuple_Check(args))
2632 std::ostringstream oss; oss << clsName << ".__new__ : the args in input is expected to be a tuple !";
2633 throw INTERP_KERNEL::Exception(oss.str().c_str());
2635 PyObject *builtinsd(PyEval_GetBuiltins());//borrowed
2636 PyObject *obj(PyDict_GetItemString(builtinsd,"object"));//borrowed
2637 PyObject *selfMeth(PyObject_GetAttrString(obj,"__new__"));
2639 PyObject *tmp0(PyTuple_New(1));
2640 PyTuple_SetItem(tmp0,0,cls); Py_XINCREF(cls);
2641 PyObject *instance(PyObject_CallObject(selfMeth,tmp0));
2643 Py_DECREF(selfMeth);
2644 if(PyTuple_Size(args)==2 && PyDict_Check(PyTuple_GetItem(args,1)) && PyDict_Size(PyTuple_GetItem(args,1))==1 )
2645 {// NOT general case. only true if in unpickeling context ! call __init__. Because for all other cases, __init__ is called right after __new__ !
2646 PyObject *initMeth(PyObject_GetAttrString(instance,"__init__"));
2647 PyObject *zeNumpyRepr(0);
2649 PyObject *tmp1(PyInt_FromLong(0));
2650 zeNumpyRepr=PyDict_GetItem(PyTuple_GetItem(args,1),tmp1);//borrowed
2654 PyObject *tmp3(PyTuple_New(1));
2655 PyTuple_SetItem(tmp3,0,zeNumpyRepr); Py_XINCREF(zeNumpyRepr);
2656 PyObject *tmp2(PyObject_CallObject(initMeth,tmp3));
2660 Py_DECREF(initMeth);