X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FMEDCoupling_Swig%2FMEDCouplingDataArrayTypemaps.i;h=76d2b4635648ea32b00f6716b37fba88adbe983a;hb=1a9af3cb21941312cdda3f0466677b61beba7ade;hp=c31ed371c0a7541263a2285432d0a5217e843ecc;hpb=3766ce4d44647817af3763a1a8d5c4769f6c1f3e;p=tools%2Fmedcoupling.git diff --git a/src/MEDCoupling_Swig/MEDCouplingDataArrayTypemaps.i b/src/MEDCoupling_Swig/MEDCouplingDataArrayTypemaps.i index c31ed371c..76d2b4635 100644 --- a/src/MEDCoupling_Swig/MEDCouplingDataArrayTypemaps.i +++ b/src/MEDCoupling_Swig/MEDCouplingDataArrayTypemaps.i @@ -20,6 +20,53 @@ #include "InterpKernelAutoPtr.hxx" +/*! + * This method is an extention of PySlice_GetIndices but less + * open than PySlice_GetIndicesEx that accepts too many situations. + */ +void GetIndicesOfSlice(PySliceObject *slice, Py_ssize_t length, Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, const char *msgInCaseOfFailure) +{ + int ret(PySlice_GetIndices(slice,length,start,stop,step)); + if(ret==0) + return ; + if(*step>0 && *start==*stop && length==*start) + return ; + throw INTERP_KERNEL::Exception(msgInCaseOfFailure); +} + +/*! + * This method allows to retrieve slice info from \a slice. + */ +void GetIndicesOfSliceExplicitely(PySliceObject *slice, Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, const char *msgInCaseOfFailure) +{ + int ret(PySlice_GetIndices(slice,std::numeric_limits::max(),start,stop,step)); + if(ret==0) + { + if(*start!=std::numeric_limits::max() && *stop!=std::numeric_limits::max()) + return ; + std::ostringstream oss; + oss << msgInCaseOfFailure << " The input slice contains some unknowns that can't be determined in static method ! The input slice must be explicit here !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + throw INTERP_KERNEL::Exception(msgInCaseOfFailure); +} + +int InterpreteNegativeInt(int val, int nbelem) +{ + if(val<0) + { + int newVal(nbelem+val); + if(newVal<0) + { + std::ostringstream oss; oss << "interpreteNegativeInt : request for negative int=" << val << " but number of elems is equal to " << nbelem << " !"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); + } + return newVal; + } + else + return val; +} + #ifdef WITH_NUMPY #include @@ -270,7 +317,9 @@ MCData *BuildNewInstance(PyObject *elt0, int npyObjectType, PyTypeObject *pytype { ret->useArray(reinterpret_cast(data),true,ParaMEDMEM::C_DEALLOC,sz0,sz1); PyObject *ref=PyWeakref_NewRef(reinterpret_cast(eltOwning),NULL); - void **objs=new void *[2]; objs[0]=ref; objs[1]=(void*) ParaMEDMEM::MemArray::CDeallocator; + typename ParaMEDMEM::MemArray::Deallocator tmp(ParaMEDMEM::MemArray::CDeallocator); + void **tmp2(reinterpret_cast(&tmp)); + void **objs=new void *[2]; objs[0]=ref; objs[1]=*tmp2; mma.setParameterForDeallocator(objs); mma.setSpecificDeallocator(numarrdeal); } @@ -368,13 +417,15 @@ PyObject *ToNumPyArray(MCData *self, int npyObjectType, const char *MCDataStr) npy_intp dim[2]; dim[0]=(npy_intp)self->getNumberOfTuples(); dim[1]=nbComp; const T *bg=self->getConstPointer(); - PyObject *ret=PyArray_SimpleNewFromData(nbDims,dim,npyObjectType,const_cast(bg)); + PyObject *ret(PyArray_SimpleNewFromData(nbDims,dim,npyObjectType,const_cast(bg))); if(mem.isDeallocatorCalled()) { if(mem.getDeallocator()!=numarrdeal) {// case for the first call of toNumPyArray - PyObject *ref=PyWeakref_NewRef(ret,NULL); - void **objs=new void *[2]; objs[0]=ref; objs[1]=(void*) mem.getDeallocator(); + PyObject *ref(PyWeakref_NewRef(ret,NULL)); + typename ParaMEDMEM::MemArray::Deallocator tmp(mem.getDeallocator()); + void **tmp2(reinterpret_cast(&tmp)); + void **objs=new void *[2]; objs[0]=reinterpret_cast(ref); objs[1]=*tmp2; mem.setParameterForDeallocator(objs); mem.setSpecificDeallocator(numarrdeal); return ret; @@ -435,7 +486,11 @@ PyObject *ToCSRMatrix(const std::vector >& m, int nbCols) t PyObject* pdict=PyDict_New(); PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins()); PyObject *tmp(PyRun_String("from scipy.sparse import csr_matrix", Py_single_input, pdict, pdict)); + if(!tmp) + throw INTERP_KERNEL::Exception("Problem during loading csr_matrix in scipy.sparse ! Is Scipy module available in present ?"); PyObject *csrMatrixCls=PyDict_GetItemString(pdict,"csr_matrix"); + if(!csrMatrixCls) + throw INTERP_KERNEL::Exception("csr_matrix not found in scipy.sparse ! Is Scipy module available in present ?"); PyObject *ret(PyObject_Call(csrMatrixCls,args,kw)); Py_DECREF(pdict); Py_XDECREF(tmp); Py_DECREF(args); Py_DECREF(kw); return ret; @@ -1412,12 +1467,7 @@ static void convertObjToPossibleCpp2(PyObject *value, int nbelem, int& sw, int& { Py_ssize_t strt=2,stp=2,step=2; PySliceObject *oC=reinterpret_cast(value); - if(PySlice_GetIndices(oC,nbelem,&strt,&stp,&step)!=0) - if(nbelem!=0 || strt!=0 || stp!=0) - { - std::ostringstream oss; oss << "Slice in subscriptable object DataArray invalid : number of elements is : " << nbelem; - throw INTERP_KERNEL::Exception(oss.str().c_str()); - } + GetIndicesOfSlice(oC,nbelem,&strt,&stp,&step,"Slice in subscriptable object DataArray invalid !"); p.first=strt; p.second.first=stp; p.second.second=step; @@ -1454,6 +1504,18 @@ static void convertObjToPossibleCpp2(PyObject *value, int nbelem, int& sw, int& throw INTERP_KERNEL::Exception(msg); } +/*! + * Idem than convertObjToPossibleCpp2 + */ +static void convertObjToPossibleCpp2WithNegIntInterp(PyObject *value, int nbelem, int& sw, int& iTyypp, std::vector& stdvecTyypp, std::pair >& p, ParaMEDMEM::DataArrayInt *& daIntTyypp) throw(INTERP_KERNEL::Exception) +{ + convertObjToPossibleCpp2(value,nbelem,sw,iTyypp,stdvecTyypp,p,daIntTyypp); + if(sw==1) + { + iTyypp=InterpreteNegativeInt(iTyypp,nbelem); + } +} + /*! * if python int -> cpp int sw=1 * if python tuple[int] -> cpp vector sw=2 @@ -1510,12 +1572,7 @@ static void convertObjToPossibleCpp22(PyObject *value, int nbelem, int& sw, int& { Py_ssize_t strt=2,stp=2,step=2; PySliceObject *oC=reinterpret_cast(value); - if(PySlice_GetIndices(oC,nbelem,&strt,&stp,&step)!=0) - if(nbelem!=0 || strt!=0 || stp!=0) - { - std::ostringstream oss; oss << "Slice in subscriptable object DataArray invalid : number of elements is : " << nbelem; - throw INTERP_KERNEL::Exception(oss.str().c_str()); - } + GetIndicesOfSlice(oC,nbelem,&strt,&stp,&step,"Slice in subscriptable object DataArray invalid !"); p.first=strt; p.second.first=stp; p.second.second=step; @@ -1645,7 +1702,7 @@ static void convertObjToPossibleCpp3(PyObject *value, int nbTuple, int nbCompo, { if(!PyTuple_Check(value)) { - convertObjToPossibleCpp2(value,nbTuple,sw,it,vt,pt,dt); + convertObjToPossibleCpp2WithNegIntInterp(value,nbTuple,sw,it,vt,pt,dt); return ; } else @@ -1655,9 +1712,9 @@ static void convertObjToPossibleCpp3(PyObject *value, int nbTuple, int nbCompo, throw INTERP_KERNEL::Exception("Unexpected nb of slice element : 1 or 2 expected !\n1st is for tuple selection, 2nd for component selection !"); PyObject *ob0=PyTuple_GetItem(value,0); int sw1,sw2; - convertObjToPossibleCpp2(ob0,nbTuple,sw1,it,vt,pt,dt); + convertObjToPossibleCpp2WithNegIntInterp(ob0,nbTuple,sw1,it,vt,pt,dt); PyObject *ob1=PyTuple_GetItem(value,1); - convertObjToPossibleCpp2(ob1,nbCompo,sw2,ic,vc,pc,dc); + convertObjToPossibleCpp2WithNegIntInterp(ob1,nbCompo,sw2,ic,vc,pc,dc); sw=4*sw2+sw1; } }