From f9a8ff211e7b522fab78d1c43e6b00fdd849b6f9 Mon Sep 17 00:00:00 2001 From: geay Date: Tue, 22 Apr 2014 17:28:23 +0200 Subject: [PATCH] Enable unpacking of tuples in DataArrays. --- src/MEDCoupling_Swig/MEDCouplingBasicsTest.py | 36 +++++++++++++++++++ src/MEDCoupling_Swig/MEDCouplingMemArray.i | 16 +++++++-- 2 files changed, 50 insertions(+), 2 deletions(-) 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]); -- 2.39.2