Salome HOME
Merge branch 'V8_0_BR'
[tools/medcoupling.git] / src / MEDCoupling_Swig / MEDCouplingDataArrayTypemaps.i
index 6755deb234844809a582aa9a0f2a7eb5858f658f..eba6b204ef63f1fb44d9d71a40276940ffb72c10 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2014  CEA/DEN, EDF R&D
+// Copyright (C) 2007-2015  CEA/DEN, EDF R&D
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -69,6 +69,11 @@ int InterpreteNegativeInt(int val, int nbelem)
 
 #ifdef WITH_NUMPY
 #include <numpy/arrayobject.h>
+#if NPY_API_VERSION <= 0x00000006
+#  define MED_NUMPY_OWNDATA NPY_OWNDATA
+#else
+#  define MED_NUMPY_OWNDATA NPY_ARRAY_OWNDATA
+#endif
 
 // specific DataArray deallocator callback. This deallocator is used both in the constructor of DataArray and in the toNumPyArr
 // method. This dellocator uses weakref to determine if the linked numArr is still alive or not. If alive the ownership is given to it.
@@ -82,7 +87,7 @@ void numarrdeal(void *pt, void *wron)
     {
       Py_XINCREF(obj);
       PyArrayObject *objC=reinterpret_cast<PyArrayObject *>(obj);
-      objC->flags|=NPY_OWNDATA;
+      objC->flags|=MED_NUMPY_OWNDATA;
       Py_XDECREF(weakRefOnOwner);
       Py_XDECREF(obj);
     }
@@ -282,24 +287,36 @@ MCData *BuildNewInstance(PyObject *elt0, int npyObjectType, PyTypeObject *pytype
   if(PyArray_ISBEHAVED(elt0))//aligned and writeable and in machine byte-order
     {
       PyArrayObject *elt0C=reinterpret_cast<PyArrayObject *>(elt0);
-      PyArrayObject *eltOwning=(PyArray_FLAGS(elt0C) & NPY_OWNDATA)?elt0C:NULL;
-      int mask=NPY_OWNDATA; mask=~mask;
+      PyArrayObject *eltOwning=(PyArray_FLAGS(elt0C) & MED_NUMPY_OWNDATA)?elt0C:NULL;
+      int mask=MED_NUMPY_OWNDATA; mask=~mask;
       elt0C->flags&=mask;
       PyObject *deepestObj=elt0;
       PyObject *base=elt0C->base;
       if(base) deepestObj=base;
+      bool isSpetialCase(false);
       while(base)
         {
           if(PyArray_Check(base))
             {
               PyArrayObject *baseC=reinterpret_cast<PyArrayObject *>(base);
-              eltOwning=(PyArray_FLAGS(baseC) & NPY_OWNDATA)?baseC:eltOwning;
+              eltOwning=(PyArray_FLAGS(baseC) & MED_NUMPY_OWNDATA)?baseC:eltOwning;
               baseC->flags&=mask;
               base=baseC->base;
               if(base) deepestObj=base;
             }
           else
-            break;
+            {
+              isSpetialCase=true;
+              break;
+            }
+        }
+      if(isSpetialCase)
+        {// this case is present for numpy arrayint coming from load of pickelized string. The owner of elt0 is not an array -> A copy is requested.
+          std::size_t nbOfElems(sz0*sz1);
+          T *dataCpy=(T*)malloc(sizeof(T)*nbOfElems);
+          std::copy(reinterpret_cast<const T*>(data),reinterpret_cast<const T*>(data)+nbOfElems,dataCpy);
+          ret->useArray(dataCpy,true,ParaMEDMEM::C_DEALLOC,sz0,sz1);
+          return ret.retn();
         }
       typename ParaMEDMEM::MemArray<T>& mma=ret->accessToMemArray();
       if(eltOwning==NULL)
@@ -363,7 +380,7 @@ int NumpyArrSetBaseObjectExt(PyArrayObject *arr, PyObject *obj)
  
 
         /* If this array owns its own data, stop collapsing */
-        if (PyArray_CHKFLAGS(obj_arr, NPY_OWNDATA)) {
+        if (PyArray_CHKFLAGS(obj_arr, MED_NUMPY_OWNDATA )) { 
             break;
         }