From aabdb26e2b496c2ca2dae4b536be1c1f29afd822 Mon Sep 17 00:00:00 2001 From: ageay Date: Thu, 21 Jun 2012 15:46:33 +0000 Subject: [PATCH] Clean of swig on throw. __getitem__ and __setitem__ on MEDFileMeshes and on MEDFileFields. --- src/MEDLoader/Swig/MEDLoader.i | 48 ++++++++++++++++ src/MEDLoader/Swig/MEDLoaderTest3.py | 4 ++ src/MEDLoader/Swig/MEDLoaderTypemaps.i | 80 ++++++++------------------ 3 files changed, 77 insertions(+), 55 deletions(-) diff --git a/src/MEDLoader/Swig/MEDLoader.i b/src/MEDLoader/Swig/MEDLoader.i index b3dc04813..8fae00c9c 100644 --- a/src/MEDLoader/Swig/MEDLoader.i +++ b/src/MEDLoader/Swig/MEDLoader.i @@ -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; + } } }; diff --git a/src/MEDLoader/Swig/MEDLoaderTest3.py b/src/MEDLoader/Swig/MEDLoaderTest3.py index 57f29217a..f1afe9e33 100644 --- a/src/MEDLoader/Swig/MEDLoaderTest3.py +++ b/src/MEDLoader/Swig/MEDLoaderTest3.py @@ -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): diff --git a/src/MEDLoader/Swig/MEDLoaderTypemaps.i b/src/MEDLoader/Swig/MEDLoaderTypemaps.i index 493c0cda5..d9c2717cd 100644 --- a/src/MEDLoader/Swig/MEDLoaderTypemaps.i +++ b/src/MEDLoader/Swig/MEDLoaderTypemaps.i @@ -19,7 +19,7 @@ #include -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(mesh)) @@ -27,14 +27,11 @@ static PyObject* convertMEDFileMesh(ParaMEDMEM::MEDFileMesh* mesh, int owner) if(dynamic_cast(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 > convertTimePairIdsFromPy(PyObject *pyLi) +static std::vector > convertTimePairIdsFromPy(PyObject *pyLi) throw(INTERP_KERNEL::Exception) { std::vector > ret; if(PyList_Check(pyLi)) @@ -49,56 +46,25 @@ static std::vector > convertTimePairIdsFromPy(PyObject *pyLi) std::pair 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 convertUMeshVecFromPy(PyObject *pyLi) +static std::vector convertUMeshVecFromPy(PyObject *pyLi) throw(INTERP_KERNEL::Exception) { std::vector ret; if(PyList_Check(pyLi)) @@ -166,24 +132,28 @@ static std::vector 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(argp); + ret[i]=arg; + } } + else + throw INTERP_KERNEL::Exception("convertFieldDoubleVecFromPy : not a list nor a tuple"); return ret; } -- 2.39.2