]> SALOME platform Git repositories - tools/medcoupling.git/commitdiff
Salome HOME
DataArrayFloat pickelized
authorAnthony Geay <anthony.geay@edf.fr>
Thu, 4 May 2017 12:25:02 +0000 (14:25 +0200)
committerAnthony Geay <anthony.geay@edf.fr>
Thu, 4 May 2017 12:25:02 +0000 (14:25 +0200)
src/MEDCoupling_Swig/MEDCouplingCommon.i
src/MEDCoupling_Swig/MEDCouplingDataArrayTypemaps.i
src/MEDCoupling_Swig/MEDCouplingFinalize.i
src/MEDCoupling_Swig/MEDCouplingPickleTest.py

index 7d31929f3a9b94c6bc0adeae821e7fa4c4d56ec5..a19239b5dc108e38dd885c12cac9ba086b5bac01 100644 (file)
@@ -5040,35 +5040,7 @@ namespace MEDCoupling
 
       static PyObject *___new___(PyObject *cls, PyObject *args) throw(INTERP_KERNEL::Exception)
       {
-        static const char MSG[]="MEDCouplingFieldDouble.__new__ : the args in input is expected to be a tuple !";
-        if(!PyTuple_Check(args))
-          throw INTERP_KERNEL::Exception(MSG);
-        PyObject *builtinsd(PyEval_GetBuiltins());//borrowed
-        PyObject *obj(PyDict_GetItemString(builtinsd,"object"));//borrowed
-        PyObject *selfMeth(PyObject_GetAttrString(obj,"__new__"));
-        //
-        PyObject *tmp0(PyTuple_New(1));
-        PyTuple_SetItem(tmp0,0,cls); Py_XINCREF(cls);
-        PyObject *instance(PyObject_CallObject(selfMeth,tmp0));
-        Py_DECREF(tmp0);
-        Py_DECREF(selfMeth);
-        if(PyTuple_Size(args)==2 && PyDict_Check(PyTuple_GetItem(args,1)) && PyDict_Size(PyTuple_GetItem(args,1))==1 )
-          {// NOT general case. only true if in unpickeling context ! call __init__. Because for all other cases, __init__ is called right after __new__ !
-            PyObject *initMeth(PyObject_GetAttrString(instance,"__init__"));
-            ////
-            PyObject *a(PyInt_FromLong(0));
-            PyObject *uniqueElt(PyDict_GetItem(PyTuple_GetItem(args,1),a));
-            Py_DECREF(a);
-            if(!uniqueElt)
-              throw INTERP_KERNEL::Exception(MSG);
-            if(!PyTuple_Check(uniqueElt) || PyTuple_Size(uniqueElt)!=2)
-              throw INTERP_KERNEL::Exception(MSG);
-            PyObject *tmp2(PyObject_CallObject(initMeth,uniqueElt));
-            Py_XDECREF(tmp2);
-            ////
-            Py_DECREF(initMeth);
-          }
-        return instance;
+        return NewMethWrapCallInitOnlyIfDictWithSingleEltInInputGeneral<SinglePyObjExpectToBeAListOfSz2>(cls,args,"MEDCouplingFieldDouble");
       }
 
       PyObject *__getnewargs__() throw(INTERP_KERNEL::Exception)
index cc56b47750ad0e2bd8e48ca891c9ce043b304a24..564c3a5e412e12eb53d65e459492165c00cb4023 100644 (file)
@@ -2396,7 +2396,8 @@ static PyObject *NewMethWrapCallInitOnlyIfEmptyDictInInput(PyObject *cls, PyObje
   return instance;
 }
 
-static PyObject *NewMethWrapCallInitOnlyIfDictWithSingleEltInInput(PyObject *cls, PyObject *args, const char *clsName)
+template<class T>
+static PyObject *NewMethWrapCallInitOnlyIfDictWithSingleEltInInputGeneral(PyObject *cls, PyObject *args, const char *clsName)
 {
   if(!PyTuple_Check(args))
     {
@@ -2421,11 +2422,27 @@ static PyObject *NewMethWrapCallInitOnlyIfDictWithSingleEltInInput(PyObject *cls
        zeNumpyRepr=PyDict_GetItem(PyTuple_GetItem(args,1),tmp1);//borrowed
         Py_DECREF(tmp1);
       }
+      if(!zeNumpyRepr)
+        {
+          std::ostringstream oss; oss << clsName << ".__new__ : the args in input is expected to be a tuple !";
+          throw INTERP_KERNEL::Exception(oss.str().c_str());
+        }
+      T tt;
       {
-        PyObject *tmp3(PyTuple_New(1));
-        PyTuple_SetItem(tmp3,0,zeNumpyRepr); Py_XINCREF(zeNumpyRepr);
-        PyObject *tmp2(PyObject_CallObject(initMeth,tmp3));
-        Py_XDECREF(tmp2);
+        PyObject *tmp3(0);
+        try
+          {
+            tmp3=tt(zeNumpyRepr);
+          }
+        catch(INTERP_KERNEL::Exception& e)
+          {
+            std::ostringstream oss; oss << clsName << ".__new__ : Invalid type in input " << " : " << e.what();
+            throw INTERP_KERNEL::Exception(oss.str());
+          }
+        {
+          PyObject *tmp2(PyObject_CallObject(initMeth,tmp3));
+          Py_XDECREF(tmp2);
+        }
         Py_DECREF(tmp3);
       }
       Py_DECREF(initMeth);
@@ -2433,6 +2450,32 @@ static PyObject *NewMethWrapCallInitOnlyIfDictWithSingleEltInInput(PyObject *cls
   return instance;
 }
 
+struct SinglePyObjToBePutInATuple
+{
+  PyObject *operator()(PyObject *zeNumpyRepr)
+  {
+    PyObject *tmp3(PyTuple_New(1));
+    PyTuple_SetItem(tmp3,0,zeNumpyRepr); Py_XINCREF(zeNumpyRepr);
+    return tmp3;
+  }
+};
+
+struct SinglePyObjExpectToBeAListOfSz2
+{
+  PyObject *operator()(PyObject *uniqueElt)
+  {
+    if(!PyTuple_Check(uniqueElt) || PyTuple_Size(uniqueElt)!=2)
+      throw INTERP_KERNEL::Exception("Not a tuple of size 2 !");
+    Py_XINCREF(uniqueElt);
+    return uniqueElt;
+  }
+};
+
+static PyObject *NewMethWrapCallInitOnlyIfDictWithSingleEltInInput(PyObject *cls, PyObject *args, const char *clsName)
+{
+  return NewMethWrapCallInitOnlyIfDictWithSingleEltInInputGeneral<SinglePyObjToBePutInATuple>(cls,args,clsName);
+}
+
 static PyObject *convertPartDefinition(MEDCoupling::PartDefinition *pd, int owner) throw(INTERP_KERNEL::Exception)
 {
   PyObject *ret=0;
index 384c448e170d77880621630532334bc1b89eeb76..a297355d218f3ca1a0e949bd8be840a5d1b8cedc 100644 (file)
@@ -36,7 +36,7 @@ DataArrayInt.__ipow__=MEDCouplingDataArrayIntIpow
 
 DataArrayByte.__new__=classmethod(MEDCouplingDataArrayBytenew)
 
-DataArrayFloat.__new__=MEDCouplingDataArrayFloatnew
+DataArrayFloat.__new__=classmethod(MEDCouplingDataArrayFloatnew)
 DataArrayFloat.__iadd__=MEDCouplingDataArrayFloatIadd
 DataArrayFloat.__isub__=MEDCouplingDataArrayFloatIsub
 DataArrayFloat.__imul__=MEDCouplingDataArrayFloatImul
index a34420d8a2580c073fe36a85ef08efefc77d9f96..bbb2a64cb0a8dcd0a858710343799ef82f5f66fb 100644 (file)
@@ -306,6 +306,17 @@ class MEDCouplingPickleTest(unittest.TestCase):
         self.assertTrue(x2.isEqual(x))
         pass
 
+    @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy")
+    def test15(self):
+        """Pickelization of DataArrayFloat"""
+        x=DataArrayFloat(256) ; x.iota()
+        x.rearrange(2) ; x.setInfoOnComponents(["aa","bbb"])
+        x.setName("toto")
+        st=cPickle.dumps(x,cPickle.HIGHEST_PROTOCOL)
+        x2=cPickle.loads(st)
+        self.assertTrue(x2.isEqual(x,1e-7))
+        pass
+
     def setUp(self):
         pass
     pass