]> SALOME platform Git repositories - tools/medcoupling.git/commitdiff
Salome HOME
Enable unpacking of tuples in DataArrays.
authorgeay <anthony.geay@cea.fr>
Tue, 22 Apr 2014 15:28:23 +0000 (17:28 +0200)
committergeay <anthony.geay@cea.fr>
Tue, 22 Apr 2014 15:28:23 +0000 (17:28 +0200)
src/MEDCoupling_Swig/MEDCouplingBasicsTest.py
src/MEDCoupling_Swig/MEDCouplingMemArray.i

index efb1273674fd5615c23efcd5398a5092fb46cc0c..d21ab4ffdcd45b93afd60c0635489f984d3056d1 100644 (file)
@@ -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
index a47188a900aa66526a91dd48aa924df121465d46..7d583d4c8599038e25bab3262e4e12f06be9a464 100644 (file)
@@ -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]);