]> SALOME platform Git repositories - tools/medcoupling.git/commitdiff
Salome HOME
MEDCouplingFieldTemplate is pickable + fix of memory leak in python wrapping linked...
authorAnthony Geay <anthony.geay@edf.fr>
Fri, 30 Aug 2019 08:54:17 +0000 (10:54 +0200)
committerAnthony Geay <anthony.geay@edf.fr>
Fri, 30 Aug 2019 08:54:17 +0000 (10:54 +0200)
src/MEDCoupling_Swig/MEDCouplingCommon.i
src/MEDCoupling_Swig/MEDCouplingDataArrayTraits.hxx
src/MEDCoupling_Swig/MEDCouplingFinalize.i
src/MEDCoupling_Swig/MEDCouplingPickleTest.py

index 9631f358f2203b3061b70b828b120f4c9c5fd09f..830a4c88822f5c2925e0dc7d03c810f462f302a7 100644 (file)
@@ -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:
 #
index 1601400b5de4413151c0f8d81645c7d406cbc07b..ee04ca39a261df1c143aa37a157f2a882e215288 100644 (file)
@@ -43,6 +43,7 @@ void numarrdeal(void *pt, void *wron)
   void **wronc=(void **)wron;
   PyObject *weakRefOnOwner=reinterpret_cast<PyObject *>(wronc[0]);
   PyObject *obj=PyWeakref_GetObject(weakRefOnOwner);
+  int64_t *offset=reinterpret_cast<int64_t*>(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<int64_t*>(wronc[2]);
       deall(pt,offset);
-      delete offset;
       Py_XDECREF(weakRefOnOwner);
     }
+  delete offset;
   delete [] wronc;
 }
 #endif
index c344981e1fcdc6a8bbe2c0d281c6c4c7a2747f83..1cc2bfab0dfaa0dba9b792038b2376c164f50d90 100644 (file)
@@ -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
index 8ce564d4367686007a356b73ff1863f556b3aee6..e5f6bb732227071af260d0bf08a921b354555900 100644 (file)
@@ -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):