From 8670fe471ebbb065d050fe760cf7d80741108b00 Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Tue, 10 Mar 2015 09:04:51 +0100 Subject: [PATCH] On the road for pickelization of MEDCouplingMesh. --- src/MEDCoupling_Swig/MEDCoupling.i | 3 + src/MEDCoupling_Swig/MEDCouplingCommon.i | 93 +++++++++++++++++++++- src/MEDCoupling_Swig/MEDCouplingFinalize.i | 2 + src/MEDCoupling_Swig/MEDCouplingMemArray.i | 4 +- 4 files changed, 99 insertions(+), 3 deletions(-) diff --git a/src/MEDCoupling_Swig/MEDCoupling.i b/src/MEDCoupling_Swig/MEDCoupling.i index 02eb0c768..42c77253b 100644 --- a/src/MEDCoupling_Swig/MEDCoupling.i +++ b/src/MEDCoupling_Swig/MEDCoupling.i @@ -107,6 +107,9 @@ def ParaMEDMEMDenseMatrixIadd(self,*args): def ParaMEDMEMDenseMatrixIsub(self,*args): import _MEDCoupling return _MEDCoupling.DenseMatrix____isub___(self, self, *args) +def ParaMEDMEMMEDCouplingUMeshnew(cls,*args): + import _MEDCoupling + return _MEDCoupling.MEDCouplingUMesh____new___(cls,args) %} %include "MEDCouplingFinalize.i" diff --git a/src/MEDCoupling_Swig/MEDCouplingCommon.i b/src/MEDCoupling_Swig/MEDCouplingCommon.i index 6c161e3ec..08654c59d 100644 --- a/src/MEDCoupling_Swig/MEDCouplingCommon.i +++ b/src/MEDCoupling_Swig/MEDCouplingCommon.i @@ -611,7 +611,7 @@ namespace ParaMEDMEM PyList_SetItem(res,2,SWIG_From_int(tmp2)); return res; } - + int getCellContainingPoint(PyObject *p, double eps) const throw(INTERP_KERNEL::Exception) { double val; @@ -1013,6 +1013,60 @@ namespace ParaMEDMEM std::vector littleStrings; self->resizeForUnserialization(tinyInfo,a1,a2,littleStrings); } + + PyObject *__getstate__() const throw(INTERP_KERNEL::Exception) + { + PyObject *ret0(ParaMEDMEM_MEDCouplingMesh_getTinySerializationInformation(self)); + PyObject *ret1(ParaMEDMEM_MEDCouplingMesh_serialize(self)); + PyObject *ret(PyTuple_New(2)); + PyTuple_SetItem(ret,0,ret0); + PyTuple_SetItem(ret,1,ret1); + return ret; + } + + void __setstate__(PyObject *inp) throw(INTERP_KERNEL::Exception) + { + static const char MSG[]="MEDCouplingMesh.__setstate__ : expected input is a tuple of size 2 !"; + if(!PyTuple_Check(inp)) + throw INTERP_KERNEL::Exception(MSG); + int sz(PyTuple_Size(inp)); + if(sz!=2) + throw INTERP_KERNEL::Exception(MSG); + PyObject *elt0(PyTuple_GetItem(inp,0)); + PyObject *elt1(PyTuple_GetItem(inp,1)); + std::vector a0; + std::vector a1; + std::vector a2; + DataArrayInt *b0(0); + DataArrayDouble *b1(0); + { + if(!PyTuple_Check(elt0) && PyTuple_Size(elt0)!=3) + throw INTERP_KERNEL::Exception(MSG); + PyObject *a0py(PyTuple_GetItem(elt0,0)),*a1py(PyTuple_GetItem(elt0,1)),*a2py(PyTuple_GetItem(elt0,2)); + int tmp(-1); + fillArrayWithPyListDbl3(a0py,tmp,a0); + convertPyToNewIntArr3(a1py,a1); + fillStringVector(a2py,a2); + } + { + if(!PyTuple_Check(elt1) && PyTuple_Size(elt1)!=2) + throw INTERP_KERNEL::Exception(MSG); + PyObject *b0py(PyTuple_GetItem(elt1,0)),*b1py(PyTuple_GetItem(elt1,1)); + void *argp(0); + int status(SWIG_ConvertPtr(b0py,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayInt,0|0)); + if(!SWIG_IsOK(status)) + throw INTERP_KERNEL::Exception(MSG); + b0=reinterpret_cast(argp); + status=SWIG_ConvertPtr(b0py,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayDouble,0|0); + if(!SWIG_IsOK(status)) + throw INTERP_KERNEL::Exception(MSG); + b1=reinterpret_cast(argp); + } + // + std::vector uselessVector; + self->resizeForUnserialization(a1,b0,b1,uselessVector); + self->unserialization(a0,a1,b0,b1,a2); + } static MEDCouplingMesh *MergeMeshes(PyObject *li) throw(INTERP_KERNEL::Exception) { @@ -1741,6 +1795,43 @@ namespace ParaMEDMEM { return MEDCouplingUMesh::New(meshName,meshDim); } + + // serialization + static PyObject *___new___(PyObject *cls, PyObject *args) throw(INTERP_KERNEL::Exception) + { + static const char MSG[]="MEDCouplingUMesh.__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); + PyObject *initMeth(PyObject_GetAttrString(instance,"__init__")); + int sz(PyTuple_Size(args)); + + if(PyTuple_Size(args)==2 && PyDict_Size(PyTuple_GetItem(args,1))==0 ) + {// NOT general case. only true if in unpickeling context ! call __init__. Because for all other cases, __init__ is called right after __new__ ! + PyObject *tmp3(PyTuple_New(0)); + PyObject *tmp2(PyObject_CallObject(initMeth,tmp3)); + Py_XDECREF(tmp2); + Py_DECREF(tmp3); + } + Py_DECREF(initMeth); + return instance; + } + + PyObject *__getnewargs__() throw(INTERP_KERNEL::Exception) + {// put an empty dict in input to say to __new__ to call __init__... + PyObject *ret(PyTuple_New(1)); + PyObject *ret0(PyDict_New()); + PyTuple_SetItem(ret,0,ret0); + return ret; + } std::string __str__() const throw(INTERP_KERNEL::Exception) { diff --git a/src/MEDCoupling_Swig/MEDCouplingFinalize.i b/src/MEDCoupling_Swig/MEDCouplingFinalize.i index 12502f150..89a6769b9 100644 --- a/src/MEDCoupling_Swig/MEDCouplingFinalize.i +++ b/src/MEDCoupling_Swig/MEDCouplingFinalize.i @@ -53,6 +53,8 @@ DataArrayIntTuple.__imod__=ParaMEDMEMDataArrayIntTupleImod DenseMatrix.__iadd__=ParaMEDMEMDenseMatrixIadd DenseMatrix.__isub__=ParaMEDMEMDenseMatrixIsub +MEDCouplingUMesh.__new__=classmethod(ParaMEDMEMMEDCouplingUMeshnew) + del ParaMEDMEMDataArrayDoublenew del ParaMEDMEMDataArrayDoubleIadd del ParaMEDMEMDataArrayDoubleIsub diff --git a/src/MEDCoupling_Swig/MEDCouplingMemArray.i b/src/MEDCoupling_Swig/MEDCouplingMemArray.i index ef598e156..cbf759e94 100644 --- a/src/MEDCoupling_Swig/MEDCouplingMemArray.i +++ b/src/MEDCoupling_Swig/MEDCouplingMemArray.i @@ -2213,7 +2213,7 @@ namespace ParaMEDMEM // serialization static PyObject *___new___(PyObject *cls, PyObject *args) throw(INTERP_KERNEL::Exception) { - static const char MSG[]="DataArrayDouble.__new__ : the args in input is expected to be a tuple of size 2 !"; + static const char MSG[]="DataArrayDouble.__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 @@ -4570,7 +4570,7 @@ namespace ParaMEDMEM // serialization static PyObject *___new___(PyObject *cls, PyObject *args) throw(INTERP_KERNEL::Exception) { - static const char MSG[]="DataArrayInt.__new__ : the args in input is expected to be a tuple of size 2 !"; + static const char MSG[]="DataArrayInt.__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 -- 2.39.2