]> SALOME platform Git repositories - tools/medcoupling.git/commitdiff
Salome HOME
Clean of swig on throw.
authorageay <ageay>
Thu, 21 Jun 2012 15:46:33 +0000 (15:46 +0000)
committerageay <ageay>
Thu, 21 Jun 2012 15:46:33 +0000 (15:46 +0000)
__getitem__ and __setitem__ on MEDFileMeshes and on MEDFileFields.

src/MEDLoader/Swig/MEDLoader.i
src/MEDLoader/Swig/MEDLoaderTest3.py
src/MEDLoader/Swig/MEDLoaderTypemaps.i

index b3dc04813d2c87e7139905cdfeedd49879480997..8fae00c9cd81d7c2a09fba300994c1d05e912231 100644 (file)
@@ -81,10 +81,12 @@ using namespace ParaMEDMEM;
 %newobject ParaMEDMEM::MEDFileMeshes::New;
 %newobject ParaMEDMEM::MEDFileMeshes::getMeshAtPos;
 %newobject ParaMEDMEM::MEDFileMeshes::getMeshWithName;
+%newobject ParaMEDMEM::MEDFileMeshes::__getitem__;
 
 %newobject ParaMEDMEM::MEDFileFields::New;
 %newobject ParaMEDMEM::MEDFileFields::getFieldWithName;
 %newobject ParaMEDMEM::MEDFileFields::getFieldAtPos;
+%newobject ParaMEDMEM::MEDFileFields::__getitem__;
 %newobject ParaMEDMEM::MEDFileFieldMultiTS::New;
 %newobject ParaMEDMEM::MEDFileFieldMultiTS::getFieldAtLevel;
 %newobject ParaMEDMEM::MEDFileFieldMultiTS::getFieldAtTopLevel;
@@ -615,6 +617,32 @@ namespace ParaMEDMEM
            {
              return self->simpleRepr();
            }
+
+         MEDFileMesh *__getitem__(PyObject *obj) throw(INTERP_KERNEL::Exception)
+         {
+           if(PyInt_Check(obj))
+             {
+               MEDFileMesh *ret=self->getMeshAtPos((int)PyInt_AS_LONG(obj));
+               if(ret)
+                 ret->incrRef();
+               return ret;
+             }
+           else if(PyString_Check(obj))
+             {
+               MEDFileMesh *ret=self->getMeshWithName(PyString_AsString(obj));
+               if(ret)
+                 ret->incrRef();
+               return ret;
+             }
+           else
+             throw INTERP_KERNEL::Exception("MEDFileMeshes::__getitem__ : only integer or string with meshname supported !");
+         }
+
+         MEDFileMeshes *__setitem__(int obj, MEDFileMesh *mesh) throw(INTERP_KERNEL::Exception)
+         {
+           self->setMeshAtPos(obj,mesh);
+           return self;
+         }
          
          MEDFileMesh *getMeshAtPos(int i) const throw(INTERP_KERNEL::Exception)
            {
@@ -1179,6 +1207,26 @@ namespace ParaMEDMEM
          {
            return self->simpleRepr();
          }
+         
+         MEDFileFieldMultiTS *__getitem__(PyObject *obj) throw(INTERP_KERNEL::Exception)
+         {
+           if(PyInt_Check(obj))
+             {
+               return self->getFieldAtPos((int)PyInt_AS_LONG(obj));
+             }
+           else if(PyString_Check(obj))
+             {
+               return self->getFieldWithName(PyString_AsString(obj));
+             }
+           else
+             throw INTERP_KERNEL::Exception("MEDFileFields::__getitem__ : only integer or string with fieldname supported !");
+         }
+
+         MEDFileFields *__setitem__(int obj, MEDFileFieldMultiTS *field) throw(INTERP_KERNEL::Exception)
+         {
+           self->setFieldAtPos(obj,field);
+           return self;
+         }
        }
   };
 
index 57f29217aa5665280ff8fa527ec07ef6e205649d..f1afe9e3303a6c3d3233add689ea76f56a35b9e4 100644 (file)
@@ -590,11 +590,15 @@ class MEDLoaderTest(unittest.TestCase):
         self.assertEqual(2,d2.getNumberOfMeshes())
         self.assertEqual(3,d2.getNumberOfFields())
         self.assertTrue(isinstance(d2.getMeshes().getMeshAtPos(0),MEDFileUMesh))
+        self.assertTrue(isinstance(d2.getMeshes()[0],MEDFileUMesh))
+        self.assertTrue(isinstance(d2.getMeshes()['2DCurveMesh_1'],MEDFileUMesh))
         m1bis=d2.getMeshes().getMeshAtPos(0).getMeshAtLevel(0)
         self.assertTrue(m1.isEqual(m1bis,1e-12))
         self.assertEqual(('f1', 'f21', 'f22'),d2.getFields().getFieldsNames())
         self.assertEqual([(-1, -1, 0.0)],d2.getFields().getFieldAtPos(2).getTimeSteps())
+        self.assertEqual([(-1, -1, 0.0)],d2.getFields()[2].getTimeSteps())
         self.assertEqual([(-1, -1, 0.0)],d2.getFields().getFieldWithName("f21").getTimeSteps())
+        self.assertEqual([(-1, -1, 0.0)],d2.getFields()["f21"].getTimeSteps())
         pass
     
     def testMEDField9(self):
index 493c0cda508bc9654d6ca2a048159918290c1467..d9c2717cd3fa71cf078cee055bae67579edbd19f 100644 (file)
@@ -19,7 +19,7 @@
 
 #include <vector>
 
-static PyObject* convertMEDFileMesh(ParaMEDMEM::MEDFileMesh* mesh, int owner)
+static PyObject* convertMEDFileMesh(ParaMEDMEM::MEDFileMesh* mesh, int owner) throw(INTERP_KERNEL::Exception)
 {
   PyObject *ret=0;
   if(dynamic_cast<ParaMEDMEM::MEDFileUMesh *>(mesh))
@@ -27,14 +27,11 @@ static PyObject* convertMEDFileMesh(ParaMEDMEM::MEDFileMesh* mesh, int owner)
   if(dynamic_cast<ParaMEDMEM::MEDFileCMesh *>(mesh))
     ret=SWIG_NewPointerObj((void*)mesh,SWIGTYPE_p_ParaMEDMEM__MEDFileCMesh,owner);
   if(!ret)
-    {
-      PyErr_SetString(PyExc_TypeError,"Not recognized type of MEDFileMesh on downcast !");
-      PyErr_Print();
-    }
+    throw INTERP_KERNEL::Exception("Not recognized type of MEDFileMesh on downcast !");
   return ret;
 }
 
-static std::vector<std::pair<int,int> > convertTimePairIdsFromPy(PyObject *pyLi)
+static std::vector<std::pair<int,int> > convertTimePairIdsFromPy(PyObject *pyLi) throw(INTERP_KERNEL::Exception)
 {
   std::vector<std::pair<int,int> > ret;
   if(PyList_Check(pyLi))
@@ -49,56 +46,25 @@ static std::vector<std::pair<int,int> > convertTimePairIdsFromPy(PyObject *pyLi)
               std::pair<int,int> p;
               int size2=PyTuple_Size(o);
               if(size2!=2)
-                {
-                  const char msg[]="tuples in list must be of size 2 (dt,it) !";
-                  ret.clear();
-                  PyErr_SetString(PyExc_TypeError,msg);
-                  PyErr_Print();
-                  throw INTERP_KERNEL::Exception(msg);
-                }
+                throw INTERP_KERNEL::Exception("tuples in list must be of size 2 (dt,it) !");
               PyObject *o0=PyTuple_GetItem(o,0);
               if(PyInt_Check(o0))
                 p.first=(int)PyInt_AS_LONG(o0);
               else
-                {
-                  const char msg[]="First elem of tuples in list must be integer : dt !";
-                  ret.clear();
-                  PyErr_SetString(PyExc_TypeError,msg);
-                  PyErr_Print();
-                  throw INTERP_KERNEL::Exception(msg);
-                }
+                throw INTERP_KERNEL::Exception("First elem of tuples in list must be integer : dt !");
               PyObject *o1=PyTuple_GetItem(o,1);
               if(PyInt_Check(o1))
                 p.second=(int)PyInt_AS_LONG(o1);
               else
-                {
-                  const char msg[]="Second elem of tuples in list must be integer : dt !";
-                  ret.clear();
-                  PyErr_SetString(PyExc_TypeError,msg);
-                  PyErr_Print();
-                  throw INTERP_KERNEL::Exception(msg);
-                }
+                throw INTERP_KERNEL::Exception("Second elem of tuples in list must be integer : dt !");
               ret[i]=p;
             }
           else
-            {
-              const char msg[]="list must contain tuples only";
-              ret.clear();
-              PyErr_SetString(PyExc_TypeError,msg);
-              PyErr_Print();
-              throw INTERP_KERNEL::Exception(msg);
-              return ret;
-            }
+            throw INTERP_KERNEL::Exception("list must contain tuples only");
         }
     }
   else
-    {
-      ret.clear();
-      const char msg[]="convertTimePairIdsFromPy : not a list";
-      PyErr_SetString(PyExc_TypeError,msg);
-      PyErr_Print();
-      throw INTERP_KERNEL::Exception(msg);
-    }
+    throw INTERP_KERNEL::Exception("convertTimePairIdsFromPy : not a list");
   return ret;
 }
 
@@ -153,7 +119,7 @@ static PyObject *convertFieldDoubleVecToPy(const std::vector<ParaMEDMEM::MEDCoup
   return ret;
 }
 
-static std::vector<const ParaMEDMEM::MEDCouplingUMesh *> convertUMeshVecFromPy(PyObject *pyLi)
+static std::vector<const ParaMEDMEM::MEDCouplingUMesh *> convertUMeshVecFromPy(PyObject *pyLi) throw(INTERP_KERNEL::Exception)
 {
   std::vector<const ParaMEDMEM::MEDCouplingUMesh *> ret;
   if(PyList_Check(pyLi))
@@ -166,24 +132,28 @@ static std::vector<const ParaMEDMEM::MEDCouplingUMesh *> convertUMeshVecFromPy(P
           void *argp;
           int status=SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_ParaMEDMEM__MEDCouplingUMesh,0|0);
           if(!SWIG_IsOK(status))
-            {
-              const char msg[]="list must contain only MEDCouplingUMesh";
-              PyErr_SetString(PyExc_TypeError,msg);
-              PyErr_Print();
-              throw INTERP_KERNEL::Exception(msg);
-            }
+            throw INTERP_KERNEL::Exception("list must contain only MEDCouplingUMesh");
           ParaMEDMEM::MEDCouplingUMesh *arg=reinterpret_cast< ParaMEDMEM::MEDCouplingUMesh * >(argp);
           ret[i]=arg;
         }
     }
-  else
+  else if(PyTuple_Check(pyLi))
     {
-      ret.clear();
-      const char msg[]="convertFieldDoubleVecFromPy : not a list";
-      PyErr_SetString(PyExc_TypeError,msg);
-      PyErr_Print();
-      throw INTERP_KERNEL::Exception(msg);
+      int size=PyTuple_Size(pyLi);
+      ret.resize(size);
+      for(int i=0;i<size;i++)
+        {
+          PyObject *obj=PyTuple_GetItem(pyLi,i);
+          void *argp;
+          int status=SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_ParaMEDMEM__MEDCouplingUMesh,0|0);
+          if(!SWIG_IsOK(status))
+            throw INTERP_KERNEL::Exception("tuple must contain only MEDCouplingUMesh");
+          ParaMEDMEM::MEDCouplingUMesh *arg=reinterpret_cast< ParaMEDMEM::MEDCouplingUMesh * >(argp);
+          ret[i]=arg;
+        }
     }
+  else
+    throw INTERP_KERNEL::Exception("convertFieldDoubleVecFromPy : not a list nor a tuple");
   return ret;
 }