From: geay Date: Tue, 22 Apr 2014 15:28:23 +0000 (+0200) Subject: Enable unpacking of tuples in DataArrays. X-Git-Tag: V7_4_0rc1~2^2~11 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=6da34015490b019ffb480bf92519ef268dc2de5b;p=tools%2Fmedcoupling.git Enable unpacking of tuples in DataArrays. --- diff --git a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py index efb127367..d21ab4ffd 100644 --- a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py +++ b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py @@ -14754,6 +14754,42 @@ class MEDCouplingBasicsTest(unittest.TestCase): self.assertTrue(b.isEqual(DataArrayInt([0,2,5,7,10,14,17,20,24,27,29,32,34]))) pass + def testSwigBugOnUnpackingTuplesInDataArray1(self): + inp=DataArrayDouble([(1,2,3),(4,5,6),(7,8,9),(10,11,12)]) + it=inp.__iter__() + r=it.next() + self.assertRaises(StopIteration,r.__getitem__,4) + self.assertEqual(len(r),3) + a,b,c=r + r=it.next() + self.assertEqual(len(r),3) + d,e,f=r + r=it.next() + self.assertEqual(len(r),3) + g,h,i=r + r=it.next() + self.assertEqual(len(r),3) + j,k,l=r + assert(inp.isEqual(DataArrayDouble([a,b,c,d,e,f,g,h,i,j,k,l],4,3),1e-12)) + ######## + inp=DataArrayInt([(1,2,3),(4,5,6),(7,8,9),(10,11,12)]) + it=inp.__iter__() + r=it.next() + self.assertRaises(StopIteration,r.__getitem__,4) + self.assertEqual(len(r),3) + a,b,c=r + r=it.next() + self.assertEqual(len(r),3) + d,e,f=r + r=it.next() + self.assertEqual(len(r),3) + g,h,i=r + r=it.next() + self.assertEqual(len(r),3) + j,k,l=r + assert(inp.isEqual(DataArrayInt([a,b,c,d,e,f,g,h,i,j,k,l],4,3))) + pass + def setUp(self): pass pass diff --git a/src/MEDCoupling_Swig/MEDCouplingMemArray.i b/src/MEDCoupling_Swig/MEDCouplingMemArray.i index a47188a90..7d583d4c8 100644 --- a/src/MEDCoupling_Swig/MEDCouplingMemArray.i +++ b/src/MEDCoupling_Swig/MEDCouplingMemArray.i @@ -2250,6 +2250,11 @@ namespace ParaMEDMEM return trueSelf; } + PyObject *__len__() throw(INTERP_KERNEL::Exception) + { + return PyInt_FromLong(self->getNumberOfCompo()); + } + PyObject *__getitem__(PyObject *obj) throw(INTERP_KERNEL::Exception) { const char msg2[]="DataArrayDoubleTuple::__getitem__ : Mismatch of slice values in 2nd parameter (components) !"; @@ -2269,7 +2274,8 @@ namespace ParaMEDMEM { std::ostringstream oss; oss << "Requesting for id " << singleVal << " having only " << nbc << " components !"; - throw INTERP_KERNEL::Exception(oss.str().c_str()); + PyErr_SetString(PyExc_StopIteration,oss.str().c_str()); + return 0; } if(singleVal>=0) return PyFloat_FromDouble(pt[singleVal]); @@ -4522,6 +4528,11 @@ namespace ParaMEDMEM Py_XINCREF(trueSelf); return trueSelf; } + + PyObject *__len__() throw(INTERP_KERNEL::Exception) + { + return PyInt_FromLong(self->getNumberOfCompo()); + } PyObject *__getitem__(PyObject *obj) throw(INTERP_KERNEL::Exception) { @@ -4542,7 +4553,8 @@ namespace ParaMEDMEM { std::ostringstream oss; oss << "Requesting for id " << singleVal << " having only " << nbc << " components !"; - throw INTERP_KERNEL::Exception(oss.str().c_str()); + PyErr_SetString(PyExc_StopIteration,oss.str().c_str()); + return 0; } if(singleVal>=0) return PyInt_FromLong(pt[singleVal]);