From: ageay Date: Thu, 13 Dec 2012 16:35:09 +0000 (+0000) Subject: Generalization on empty allocated DataArrays. X-Git-Tag: V6_main_FINAL~463 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=be837dfdc53f3e6ff9c20c5b4548b59e1e4c939b;p=tools%2Fmedcoupling.git Generalization on empty allocated DataArrays. --- diff --git a/src/MEDCoupling/MEDCouplingMemArray.cxx b/src/MEDCoupling/MEDCouplingMemArray.cxx index d5420067d..0ea82aef1 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.cxx +++ b/src/MEDCoupling/MEDCouplingMemArray.cxx @@ -383,6 +383,8 @@ int DataArray::GetNumberOfItemGivenBES(int begin, int end, int step, const char std::ostringstream oss; oss << msg << " : end before begin !"; throw INTERP_KERNEL::Exception(oss.str().c_str()); } + if(end==begin) + return 0; if(step<=0) { std::ostringstream oss; oss << msg << " : invalid step should be > 0 !"; @@ -4207,7 +4209,7 @@ DataArrayInt *DataArrayInt::BuildOld2NewArrayFromSurjectiveFormat2(int nbOfOldTu { if(!arr || !arrI) throw INTERP_KERNEL::Exception("DataArrayInt::BuildOld2NewArrayFromSurjectiveFormat2 : presence of NULL ref of DataArrayInt in input !"); - DataArrayInt *ret=DataArrayInt::New(); + MEDCouplingAutoRefCountObjectPtr ret=DataArrayInt::New(); ret->alloc(nbOfOldTuples,1); int *pt=ret->getPointer(); std::fill(pt,pt+nbOfOldTuples,-1); @@ -4241,6 +4243,7 @@ DataArrayInt *DataArrayInt::BuildOld2NewArrayFromSurjectiveFormat2(int nbOfOldTu } } newNbOfTuples=newNb; + ret->incrRef(); return ret; } diff --git a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py index 5ec7e253e..68df0a3dd 100644 --- a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py +++ b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py @@ -8128,6 +8128,7 @@ class MEDCouplingBasicsTest(unittest.TestCase): self.assertEqual(7,newNbTuple); self.assertEqual(1,ret.getNumberOfComponents()); self.assertEqual(expected,ret.getValues()); + self.assertRaises(InterpKernelException,DataArrayInt.BuildOld2NewArrayFromSurjectiveFormat2,9,a,b); pass def testDADIReverse1(self): @@ -10434,6 +10435,42 @@ class MEDCouplingBasicsTest(unittest.TestCase): self.assertRaises(InterpKernelException,d.checkMonotonic,False) pass + def testSwigDASetItemOnEmpty1(self): + d=DataArrayInt(0,1) + isThrow=False + try: + d[0:1000:2]=4 + except InterpKernelException as e: + isThrow=True + pass + self.assertTrue(isThrow) + d[:]=4 + d[::2]=5 + # + d=DataArrayDouble(0,1) + isThrow=False + try: + d[0:1000:2]=4 + except InterpKernelException as e: + isThrow=True + pass + self.assertTrue(isThrow) + d[:]=4 + d[::2]=5 + d=DataArrayInt([],0,1) + d2=DataArrayInt(0) + self.assertTrue(d2.isEqual(d)) + d=DataArrayDouble([],0,1) + d2=DataArrayDouble(0) + self.assertTrue(d2.isEqual(d,1e-12)) + pass + + def testSwigDAITransformWithIndArr1(self): + arr=DataArrayInt([0,4,5,1]) + d=DataArrayInt([7,8,9,10]) + self.assertRaises(InterpKernelException,arr.transformWithIndArr,d) + pass + def setUp(self): pass pass diff --git a/src/MEDCoupling_Swig/MEDCouplingTypemaps.i b/src/MEDCoupling_Swig/MEDCouplingTypemaps.i index c16812429..3beb08f82 100644 --- a/src/MEDCoupling_Swig/MEDCouplingTypemaps.i +++ b/src/MEDCoupling_Swig/MEDCouplingTypemaps.i @@ -453,6 +453,8 @@ static std::vector fillArrayWithPyListInt2(PyObject *pyLi, int& nbOfTuples, PyObject *o=PyList_GetItem(pyLi,i); fillArrayWithPyListInt3(o,size2,ret); } + if(size1==0) + size2=1; } else if(PyTuple_Check(pyLi)) { @@ -462,6 +464,8 @@ static std::vector fillArrayWithPyListInt2(PyObject *pyLi, int& nbOfTuples, PyObject *o=PyTuple_GetItem(pyLi,i); fillArrayWithPyListInt3(o,size2,ret); } + if(size1==0) + size2=1; } else throw INTERP_KERNEL::Exception("fillArrayWithPyListInt2 : Unrecognized type ! Should be a tuple or a list !"); @@ -696,6 +700,8 @@ static std::vector fillArrayWithPyListDbl2(PyObject *pyLi, int& nbOfTupl PyObject *o=PyList_GetItem(pyLi,i); fillArrayWithPyListDbl3(o,size2,ret); } + if(size1==0) + size2=1; } else if(PyTuple_Check(pyLi)) { @@ -705,6 +711,8 @@ static std::vector fillArrayWithPyListDbl2(PyObject *pyLi, int& nbOfTupl PyObject *o=PyTuple_GetItem(pyLi,i); fillArrayWithPyListDbl3(o,size2,ret); } + if(size1==0) + size2=1; } else throw INTERP_KERNEL::Exception("fillArrayWithPyListDbl2 : Unrecognized type ! Should be a tuple or a list !"); @@ -1107,13 +1115,14 @@ static void convertObjToPossibleCpp2(PyObject *value, int nbelem, int& sw, int& } if(PySlice_Check(value)) { - Py_ssize_t strt,stp,step; + Py_ssize_t strt=2,stp=2,step=2; PySliceObject *oC=reinterpret_cast(value); if(PySlice_GetIndices(oC,nbelem,&strt,&stp,&step)!=0) - { - std::ostringstream oss; oss << "Slice in subscriptable object DataArray invalid : number of elemnts is : " << nbelem; - throw INTERP_KERNEL::Exception(oss.str().c_str()); - } + 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()); + } p.first=strt; p.second.first=stp; p.second.second=step; @@ -1197,13 +1206,14 @@ static void convertObjToPossibleCpp22(PyObject *value, int nbelem, int& sw, int& } if(PySlice_Check(value)) { - Py_ssize_t strt,stp,step; + Py_ssize_t strt=2,stp=2,step=2; PySliceObject *oC=reinterpret_cast(value); if(PySlice_GetIndices(oC,nbelem,&strt,&stp,&step)!=0) - { - std::ostringstream oss; oss << "Slice in subscriptable object DataArray invalid : number of elemnts is : " << nbelem; - throw INTERP_KERNEL::Exception(oss.str().c_str()); - } + 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()); + } p.first=strt; p.second.first=stp; p.second.second=step;