From: Anthony Geay Date: Fri, 30 Aug 2019 08:54:17 +0000 (+0200) Subject: MEDCouplingFieldTemplate is pickable + fix of memory leak in python wrapping linked... X-Git-Tag: V9_4_0a1~2 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=5875934d995681f049d6c21880eb74fa3359305c;p=tools%2Fmedcoupling.git MEDCouplingFieldTemplate is pickable + fix of memory leak in python wrapping linked to pickelization of arrays --- diff --git a/src/MEDCoupling_Swig/MEDCouplingCommon.i b/src/MEDCoupling_Swig/MEDCouplingCommon.i index 9631f358f..830a4c888 100644 --- a/src/MEDCoupling_Swig/MEDCouplingCommon.i +++ b/src/MEDCoupling_Swig/MEDCouplingCommon.i @@ -5933,7 +5933,18 @@ def MEDCouplingFieldFloatReduce(self): self.checkConsistencyLight() d=(self.getTypeOfField(),self.getTimeDiscretization()) return MEDCouplingStdReduceFunct,(MEDCouplingFieldFloat,(d,(self.__getstate__()),)) - +def MEDCouplingFTReduceFunct(cls,params): + a,b=params + ret=object.__new__(cls) + ret.__init__(*a) + return ret + +def MEDCouplingFieldTemplateReduce(self): + ret = MEDCouplingFieldDouble(self) + nbTuples = self.getNumberOfTuplesExpected() + arr = DataArrayDouble(nbTuples) ; arr[:] = 0. + ret.setArray(arr) + return MEDCouplingFTReduceFunct,(MEDCouplingFieldTemplate,((ret,),())) # # Forwarding DataArrayInt functions to MEDCouplingUMesh: # diff --git a/src/MEDCoupling_Swig/MEDCouplingDataArrayTraits.hxx b/src/MEDCoupling_Swig/MEDCouplingDataArrayTraits.hxx index 1601400b5..ee04ca39a 100644 --- a/src/MEDCoupling_Swig/MEDCouplingDataArrayTraits.hxx +++ b/src/MEDCoupling_Swig/MEDCouplingDataArrayTraits.hxx @@ -43,6 +43,7 @@ void numarrdeal(void *pt, void *wron) void **wronc=(void **)wron; PyObject *weakRefOnOwner=reinterpret_cast(wronc[0]); PyObject *obj=PyWeakref_GetObject(weakRefOnOwner); + int64_t *offset=reinterpret_cast(wronc[2]); if(obj!=Py_None) { Py_XINCREF(obj); @@ -55,11 +56,10 @@ void numarrdeal(void *pt, void *wron) { typedef void (*MyDeallocator)(void *,void *); MyDeallocator deall=(MyDeallocator)wronc[1]; - int64_t *offset=reinterpret_cast(wronc[2]); deall(pt,offset); - delete offset; Py_XDECREF(weakRefOnOwner); } + delete offset; delete [] wronc; } #endif diff --git a/src/MEDCoupling_Swig/MEDCouplingFinalize.i b/src/MEDCoupling_Swig/MEDCouplingFinalize.i index c344981e1..1cc2bfab0 100644 --- a/src/MEDCoupling_Swig/MEDCouplingFinalize.i +++ b/src/MEDCoupling_Swig/MEDCouplingFinalize.i @@ -70,6 +70,8 @@ MEDCouplingFieldDouble.__floordiv__=MEDCouplingFieldDouble.__div__ MEDCouplingFieldDouble.__rfloordiv__=MEDCouplingFieldDouble.__rdiv__ MEDCouplingFieldDouble.__ifloordiv__=MEDCouplingFieldDoubleIdiv +MEDCouplingFieldTemplate.__reduce__=MEDCouplingFieldTemplateReduce + MEDCouplingFieldInt.__reduce__=MEDCouplingFieldIntReduce MEDCouplingFieldFloat.__reduce__=MEDCouplingFieldFloatReduce @@ -121,6 +123,7 @@ del MEDCoupling1DGTUMeshReduce del MEDCouplingFieldDoubleReduce del MEDCouplingFieldFloatReduce del MEDCouplingFieldIntReduce +del MEDCouplingFieldTemplateReduce del MEDCouplingDataArrayDoubleIadd del MEDCouplingDataArrayDoubleIsub del MEDCouplingDataArrayDoubleImul diff --git a/src/MEDCoupling_Swig/MEDCouplingPickleTest.py b/src/MEDCoupling_Swig/MEDCouplingPickleTest.py index 8ce564d43..e5f6bb732 100644 --- a/src/MEDCoupling_Swig/MEDCouplingPickleTest.py +++ b/src/MEDCoupling_Swig/MEDCouplingPickleTest.py @@ -209,6 +209,10 @@ class MEDCouplingPickleTest(unittest.TestCase): f2=pickle.loads(st) self.assertTrue(f2.isEqual(f,1e-16,1e-16)) self.assertTrue(f2.getMesh().isEqual(f.getMesh(),1e-16)) + ### + ft=MEDCouplingFieldTemplate(f) + ft2=pickle.loads(pickle.dumps(ft,pickle.HIGHEST_PROTOCOL)) + self.assertTrue(ft2.isEqual(ft,1e-16)) pass @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy") @@ -289,6 +293,10 @@ class MEDCouplingPickleTest(unittest.TestCase): f2=pickle.loads(st) self.assertTrue(f2.isEqual(f,1e-16,1e-16)) self.assertTrue(f2.getMesh().isEqual(f.getMesh(),1e-16)) + ### + ft=MEDCouplingFieldTemplate(f) + ft2=pickle.loads(pickle.dumps(ft,pickle.HIGHEST_PROTOCOL)) + self.assertTrue(ft2.isEqual(ft,1e-16)) pass def test13(self):