Exemple #1
1
 inline static void import_numpy() {
  static bool init = false;
  if (!init) {
   _import_array();
   // std::cerr << "importing array"<<std::endl;
   init = true;
  }
 }
Exemple #2
0
PyObject* ICoinPackedMatrix::np_getIndices(){

	npy_intp dims = this->getNumElements();
	_import_array();
	PyObject *Arr = PyArray_SimpleNewFromData( 1, &dims, PyArray_INT32, this->IgetIndices() );
	return Arr;
}
Exemple #3
0
static PyObject *
initaubio (void)
{
  PyObject *m = NULL;
  int err;

  // fvec is defined in __init__.py
  if (   (PyType_Ready (&Py_cvecType) < 0)
      || (PyType_Ready (&Py_filterType) < 0)
      || (PyType_Ready (&Py_filterbankType) < 0)
      || (PyType_Ready (&Py_fftType) < 0)
      || (PyType_Ready (&Py_pvocType) < 0)
      || (PyType_Ready (&Py_sourceType) < 0)
      || (PyType_Ready (&Py_sinkType) < 0)
      // generated objects
      || (generated_types_ready() < 0 )
  ) {
    return m;
  }

#if PY_MAJOR_VERSION >= 3
  m = PyModule_Create(&moduledef);
#else
  m = Py_InitModule3 ("_aubio", aubio_methods, aubio_module_doc);
#endif

  if (m == NULL) {
    return m;
  }

  err = _import_array ();
  if (err != 0) {
    fprintf (stderr,
        "Unable to import Numpy array from aubio module (error %d)\n", err);
  }

  Py_INCREF (&Py_cvecType);
  PyModule_AddObject (m, "cvec", (PyObject *) & Py_cvecType);
  Py_INCREF (&Py_filterType);
  PyModule_AddObject (m, "digital_filter", (PyObject *) & Py_filterType);
  Py_INCREF (&Py_filterbankType);
  PyModule_AddObject (m, "filterbank", (PyObject *) & Py_filterbankType);
  Py_INCREF (&Py_fftType);
  PyModule_AddObject (m, "fft", (PyObject *) & Py_fftType);
  Py_INCREF (&Py_pvocType);
  PyModule_AddObject (m, "pvoc", (PyObject *) & Py_pvocType);
  Py_INCREF (&Py_sourceType);
  PyModule_AddObject (m, "source", (PyObject *) & Py_sourceType);
  Py_INCREF (&Py_sinkType);
  PyModule_AddObject (m, "sink", (PyObject *) & Py_sinkType);

  // add generated objects
  add_generated_objects(m);

  // add ufunc
  add_ufuncs(m);

  return m;
}
void initializeNumpy() {
  // Use _import_array() because import_array() is a macro that contains a
  // return statement.
  if (_import_array() != 0) {
    throw std::runtime_error(
        "initializeNumpy: numpy.core.multiarray failed to import.");
  }
}
Exemple #5
0
PYBIND11_MODULE(superutils, m) {
    _import_array();

    m.doc() = "fast utils";

    vaex::init_hash_primitives(m);
    vaex::init_hash_string(m);
    vaex::init_hash_object(m);
}
Exemple #6
0
void Invocation::numpy_init()
{
    // this macro is defined be NumPy and must be included
    if (_import_array() < 0)
    {
        std::ostringstream oss;
        oss << "unable to initialize NumPy with error '" << getPythonTraceback() << "'";
        throw python_error(oss.str());
    }
}
Exemple #7
0
int initialize_numpy(void)
{
    // lock here so only one thread can initialize NumPy
    boost::mutex::scoped_lock lock(numpy_initialize_mtx);

    if (!numpy_initialized) {
        _import_array();    // import NumPy
        numpy_initialized = true; // numpy successfully initialized
    }
    return GADGET_OK;
}
Exemple #8
0
PyMODINIT_FUNC
init_aubio (void)
{
  PyObject *m;
  int err;

  // fvec is defined in __init__.py
  if (   (PyType_Ready (&Py_cvecType) < 0)
      || (PyType_Ready (&Py_filterType) < 0)
      || (PyType_Ready (&Py_filterbankType) < 0)
      || (PyType_Ready (&Py_fftType) < 0)
      || (PyType_Ready (&Py_pvocType) < 0)
      // generated objects
      || (generated_types_ready() < 0 )
  ) {
    return;
  }

  m = Py_InitModule3 ("_aubio", aubio_methods, aubio_module_doc);

  if (m == NULL) {
    return;
  }

  err = _import_array ();
  if (err != 0) {
    fprintf (stderr,
        "Unable to import Numpy array from aubio module (error %d)\n", err);
  }

  Py_INCREF (&Py_cvecType);
  PyModule_AddObject (m, "cvec", (PyObject *) & Py_cvecType);
  Py_INCREF (&Py_filterType);
  PyModule_AddObject (m, "digital_filter", (PyObject *) & Py_filterType);
  Py_INCREF (&Py_filterbankType);
  PyModule_AddObject (m, "filterbank", (PyObject *) & Py_filterbankType);
  Py_INCREF (&Py_fftType);
  PyModule_AddObject (m, "fft", (PyObject *) & Py_fftType);
  Py_INCREF (&Py_pvocType);
  PyModule_AddObject (m, "pvoc", (PyObject *) & Py_pvocType);

  // add generated objects
  add_generated_objects(m);

  // add ufunc
  add_ufuncs(m);
}
Exemple #9
0
    /**
     * Try and initialize numpy module, setting the useNumPy flag appropriately
     * Note that this can only be called once and must be called from the init section of the Python module
     */
    void MantidVecHelper::initializeDependencies()
    {
      if( MantidVecHelper::g_isInitialized ) return;
      // Import numpy and check it succeeded
      int result = _import_array();
      if( result < 0 )
      {
        PyErr_Clear();
        MantidVecHelper::g_useNumPy = false;
      }
      else
      {
        MantidVecHelper::g_useNumPy = true;
        boost::python::numeric::array::set_module_and_type("numpy", "ndarray");
      }

      MantidVecHelper::g_isInitialized = true;
    }
Exemple #10
0
PyMODINIT_FUNC
init_chebyfit(void)
#endif
{
    PyObject *module;

    char *doc = (char *)PyMem_Malloc(sizeof(module_doc) + sizeof(_VERSION_));
    PyOS_snprintf(doc, sizeof(module_doc) + sizeof(_VERSION_),
                  module_doc, _VERSION_);

#if PY_MAJOR_VERSION >= 3
    moduledef.m_doc = doc;
    module = PyModule_Create(&moduledef);
#else
    module = Py_InitModule3("_chebyfit", module_methods, doc);
#endif

    PyMem_Free(doc);

    if (module == NULL)
        INITERROR;

    if (_import_array() < 0) {
        Py_DECREF(module);
        INITERROR;
    }

    {
#if PY_MAJOR_VERSION < 3
    PyObject *s = PyString_FromString(_VERSION_);
#else
    PyObject *s = PyUnicode_FromString(_VERSION_);
#endif
    PyObject *dict = PyModule_GetDict(module);
    PyDict_SetItemString(dict, "__version__", s);
    Py_DECREF(s);
    }

#if PY_MAJOR_VERSION >= 3
    return module;
#endif
}
Exemple #11
0
 Kernel::DblMatrix MantidVecHelper::getMatrixFromArray(PyObject *p)
 {       
   _import_array();
   if(PyArray_Check(p)==1)
   {
     boost::python::numeric::array a=boost::python::extract<boost::python::numeric::array>(p);
     a=(boost::python::numeric::array) a.astype('d');//force the array to be of double type (in case it was int)
     boost::python::tuple shape( a.attr("shape") );                  
     if( boost::python::len( shape ) != 2 ) throw std::invalid_argument( "numeric::array must have 2 dimensions" );
     size_t nx,ny,i,j;
     nx=(size_t)(boost::python::extract< unsigned >( shape[0] ));
     ny=(size_t)(boost::python::extract< unsigned >( shape[1] ));
     Kernel::Matrix<double> m(nx,ny);
     for( i = 0; i < nx; i++ )
     {
       for( j = 0; j < ny; j++ )
       {
         m[i][j] = boost::python::extract< double >( a[ boost::python::make_tuple( i, j ) ] );
       }
     }
     return m; 
   }      
   else throw std::invalid_argument("Not a numpy array"); 
 } 
Exemple #12
0
void _formatinput_init(void) { _import_array(); }
Exemple #13
0
  numpy_extractor ( PyObject * X, bool allow_copy) {
   if (X==NULL) TRIQS_RUNTIME_ERROR<<"numpy interface : the python object is NULL !";
   if (_import_array()!=0) TRIQS_RUNTIME_ERROR <<"Internal Error in importing numpy";

   // make sure IndexMap is cuboid ?s
   static const char Order = IndexMapType::index_order_type::C_or_F;
   static_assert( ((Order=='F')||(Order=='C')), "Ordering must be C or Fortran");

   if ((!PyArray_Check(X)) && (Order=='D'))
    TRIQS_RUNTIME_ERROR<<"numpy interface : the python object is not a numpy and you ask me to deduce the ordering in memory !";

   const int elementsType (numpy_to_C_type<typename boost::remove_const<ValueType>::type>::arraytype);
   int rank = IndexMapType::rank;
   static const char * error_msg = "   A deep copy of the object would be necessary while views are supposed to guarantee to present a *view* of the python data.\n";

   if (!allow_copy) {
    // in case of a view, we decide ourselves if we can do it...
    // a previous uses PyArray_FromAny, but behaviour changes between different version of numpy, so it is not portable...
    if (!PyArray_Check(X)) 
     throw copy_exception () << error_msg<<"   Indeed the object was not even an array !\n";  

    if ( elementsType != PyArray_TYPE((PyArrayObject*)X)) 
     throw copy_exception () << error_msg<<"   The deep copy is caused by a type mismatch of the elements. \n";

    PyArrayObject *arr = (PyArrayObject *)X;    
    if ( arr->nd != rank)
     throw copy_exception () << error_msg<<"   Rank mismatch . numpy array is of rank "<< arr->nd << "while you ask for rank "<< rank<<". \n";

    if ((Order == 'C') && (PyArray_ISFORTRAN(arr))) 
     throw copy_exception () << error_msg<<"     The numpy is in Fortran order while it is expected in C order. \n";

    if ((Order == 'F') && (!PyArray_ISFORTRAN(arr))) 
     throw copy_exception () << error_msg<<"     The numpy is not in Fortran order as it is expected. \n";

    numpy_obj = X; Py_INCREF(X); 
   }
   else { 
    // From X, we ask the numpy library to make a numpy, and of the correct type.
    // This handles automatically the cases where : 
    //   - we have list, or list of list/tuple
    //   - the numpy type is not the one we want.
    //   - adjust the dimension if needed
    // If X is an array : 
    //   - if Order is same, don't change it
    //   - else impose it (may provoque a copy).
    // if X is not array : 
    //   - Order = FortranOrder or SameOrder - > Fortran order otherwise C
    bool ForceCast = false;// Unless FORCECAST is present in flags, this call will generate an error if the data type cannot be safely obtained from the object.
    int flags = (ForceCast ? NPY_FORCECAST : 0) ;// do NOT force a copy | (make_copy ?  NPY_ENSURECOPY : 0);
    if (!(PyArray_Check(X) && (Order=='D'))) flags |= (Order =='F' ? NPY_F_CONTIGUOUS : NPY_C_CONTIGUOUS); //impose mem order
    //if (!(PyArray_Check(X) && (Order=='D'))) flags |= (Order =='F' ? NPY_FARRAY : NPY_CARRAY); //impose mem order
    numpy_obj= PyArray_FromAny(X,PyArray_DescrFromType(elementsType), rank,rank, flags , NULL );

    // do several checks
    if (!numpy_obj) {// The convertion of X to a numpy has failed !
     if (PyErr_Occurred()) {PyErr_Print();PyErr_Clear();}
     TRIQS_RUNTIME_ERROR<<"numpy interface : the python object  is not convertible to a numpy. ";
    }
    assert (PyArray_Check(numpy_obj)); assert((numpy_obj->ob_refcnt==1) || ((numpy_obj ==X)));

    arr_obj = (PyArrayObject *)numpy_obj;
    try {
     if (arr_obj->nd!=rank)  TRIQS_RUNTIME_ERROR<<"numpy interface : internal error : dimensions do not match";
     if (arr_obj->descr->type_num != elementsType) 
      TRIQS_RUNTIME_ERROR<<"numpy interface : internal error : incorrect type of element :" <<arr_obj->descr->type_num <<" vs "<<elementsType;
     if (Order == 'F') { if (!PyArray_ISFORTRAN(numpy_obj)) TRIQS_RUNTIME_ERROR<<"numpy interface : internal error : should be Fortran array";}
     else {if (!PyArray_ISCONTIGUOUS(numpy_obj)) TRIQS_RUNTIME_ERROR<<"numpy interface : internal error : should be contiguous";}
    }
    catch(...) { Py_DECREF(numpy_obj); throw;} // make sure that in case of problem, the reference counting of python is still ok...
   }

   arr_obj = (PyArrayObject *)numpy_obj;
  } 
Exemple #14
0
PyMODINIT_FUNC
initflow_mod(void)
{
  _import_array();
  Py_InitModule3("flow_mod", flow_mod_methods, flow_mod_doc);
}
Exemple #15
0
PyMODINIT_FUNC init5numpy4core5umath(void)
#endif
{
    PyObject *m, *d, *s, *s2, *c_api;
    int UFUNC_FLOATING_POINT_SUPPORT = 1;

#ifdef NO_UFUNC_FLOATING_POINT_SUPPORT
    UFUNC_FLOATING_POINT_SUPPORT = 0;
#endif
    /* Create the module and add the functions */
#if defined(NPY_PY3K)
    m = PyModule_Create(&moduledef);
#else
    m = Py_InitModule("numpy.core.umath", methods);
#endif
    if (!m) {
        return RETVAL;
    }

    /* Import the array */
    if (_import_array() < 0) {
        if (!PyErr_Occurred()) {
            PyErr_SetString(PyExc_ImportError,
                            "umath failed: Could not import array core.");
        }
        return RETVAL;
    }

    /* Initialize the types */
    if (PyType_Ready(&PyUFunc_Type) < 0)
        return RETVAL;

    /* Add some symbolic constants to the module */
    d = PyModule_GetDict(m);

    c_api = NpyCapsule_FromVoidPtr((void *)PyUFunc_API, NULL);
    if (PyErr_Occurred()) {
        goto err;
    }
    PyDict_SetItemString(d, "_UFUNC_API", c_api);
    Py_DECREF(c_api);
    if (PyErr_Occurred()) {
        goto err;
    }

    s = PyString_FromString("0.4.0");
    PyDict_SetItemString(d, "__version__", s);
    Py_DECREF(s);

    /* Load the ufunc operators into the array module's namespace */
    InitOperators(d);

    PyDict_SetItemString(d, "pi", s = PyFloat_FromDouble(NPY_PI));
    Py_DECREF(s);
    PyDict_SetItemString(d, "e", s = PyFloat_FromDouble(NPY_E));
    Py_DECREF(s);
    PyDict_SetItemString(d, "euler_gamma", s = PyFloat_FromDouble(NPY_EULER));
    Py_DECREF(s);

#define ADDCONST(str) PyModule_AddIntConstant(m, #str, UFUNC_##str)
#define ADDSCONST(str) PyModule_AddStringConstant(m, "UFUNC_" #str, UFUNC_##str)

    ADDCONST(ERR_IGNORE);
    ADDCONST(ERR_WARN);
    ADDCONST(ERR_CALL);
    ADDCONST(ERR_RAISE);
    ADDCONST(ERR_PRINT);
    ADDCONST(ERR_LOG);
    ADDCONST(ERR_DEFAULT);

    ADDCONST(SHIFT_DIVIDEBYZERO);
    ADDCONST(SHIFT_OVERFLOW);
    ADDCONST(SHIFT_UNDERFLOW);
    ADDCONST(SHIFT_INVALID);

    ADDCONST(FPE_DIVIDEBYZERO);
    ADDCONST(FPE_OVERFLOW);
    ADDCONST(FPE_UNDERFLOW);
    ADDCONST(FPE_INVALID);

    ADDCONST(FLOATING_POINT_SUPPORT);

    ADDSCONST(PYVALS_NAME);

#undef ADDCONST
#undef ADDSCONST
    PyModule_AddIntConstant(m, "UFUNC_BUFSIZE_DEFAULT", (long)NPY_BUFSIZE);

    PyModule_AddObject(m, "PINF", PyFloat_FromDouble(NPY_INFINITY));
    PyModule_AddObject(m, "NINF", PyFloat_FromDouble(-NPY_INFINITY));
    PyModule_AddObject(m, "PZERO", PyFloat_FromDouble(NPY_PZERO));
    PyModule_AddObject(m, "NZERO", PyFloat_FromDouble(NPY_NZERO));
    PyModule_AddObject(m, "NAN", PyFloat_FromDouble(NPY_NAN));

#if defined(NPY_PY3K)
    s = PyDict_GetItemString(d, "true_divide");
    PyDict_SetItemString(d, "divide", s);
#endif

    s = PyDict_GetItemString(d, "conjugate");
    s2 = PyDict_GetItemString(d, "remainder");
    /* Setup the array object's numerical structures with appropriate
       ufuncs in d*/
    PyArray_SetNumericOps(d);

    PyDict_SetItemString(d, "conj", s);
    PyDict_SetItemString(d, "mod", s2);

    initscalarmath(m);

    if (!intern_strings()) {
        goto err;
    }

    return RETVAL;

 err:
    /* Check for errors */
    if (!PyErr_Occurred()) {
        PyErr_SetString(PyExc_RuntimeError,
                        "cannot load umath module.");
    }
    return RETVAL;
}
Exemple #16
0
static PyObject *
initaubio (void)
{
    PyObject *m = NULL;
    int err;

    // fvec is defined in __init__.py
    if (   (PyType_Ready (&Py_cvecType) < 0)
            || (PyType_Ready (&Py_filterType) < 0)
            || (PyType_Ready (&Py_filterbankType) < 0)
            || (PyType_Ready (&Py_fftType) < 0)
            || (PyType_Ready (&Py_pvocType) < 0)
            || (PyType_Ready (&Py_sourceType) < 0)
            || (PyType_Ready (&Py_sinkType) < 0)
            // generated objects
            || (generated_types_ready() < 0 )
       ) {
        return m;
    }

#if PY_MAJOR_VERSION >= 3
    m = PyModule_Create(&moduledef);
#else
    m = Py_InitModule3 ("_aubio", aubio_methods, aubio_module_doc);
#endif

    if (m == NULL) {
        return m;
    }

    err = _import_array ();
    if (err != 0) {
        fprintf (stderr,
                 "Unable to import Numpy array from aubio module (error %d)\n", err);
    }

    Py_INCREF (&Py_cvecType);
    PyModule_AddObject (m, "cvec", (PyObject *) & Py_cvecType);
    Py_INCREF (&Py_filterType);
    PyModule_AddObject (m, "digital_filter", (PyObject *) & Py_filterType);
    Py_INCREF (&Py_filterbankType);
    PyModule_AddObject (m, "filterbank", (PyObject *) & Py_filterbankType);
    Py_INCREF (&Py_fftType);
    PyModule_AddObject (m, "fft", (PyObject *) & Py_fftType);
    Py_INCREF (&Py_pvocType);
    PyModule_AddObject (m, "pvoc", (PyObject *) & Py_pvocType);
    Py_INCREF (&Py_sourceType);
    PyModule_AddObject (m, "source", (PyObject *) & Py_sourceType);
    Py_INCREF (&Py_sinkType);
    PyModule_AddObject (m, "sink", (PyObject *) & Py_sinkType);

    PyModule_AddStringConstant(m, "float_type", AUBIO_NPY_SMPL_STR);
    PyModule_AddStringConstant(m, "__version__", DEFINEDSTRING(AUBIO_VERSION));

    // add generated objects
    add_generated_objects(m);

    // add ufunc
    add_ufuncs(m);

    aubio_log_set_level_function(AUBIO_LOG_ERR, aubio_log_function, NULL);
    aubio_log_set_level_function(AUBIO_LOG_WRN, aubio_log_function, NULL);
    return m;
}
Exemple #17
0
PyObject* ICoinPackedMatrix::np_getVectorStarts(){
	npy_intp dims = this->getMajorDim() + 1;
	_import_array();
	PyObject *Arr = PyArray_SimpleNewFromData( 1, &dims, PyArray_INT32, this->IgetVectorStarts() );
	return Arr;
}
Exemple #18
0
 // return a NEW (owned) reference
 //
 inline PyObject * numpy_extractor_impl ( PyObject * X, bool allow_copy, std::string type_name, 
   int elementsType, int rank, size_t * lengths, std::ptrdiff_t * strides, size_t size_of_ValueType) {

  PyObject * numpy_obj;

  if (X==NULL) TRIQS_RUNTIME_ERROR<<"numpy interface : the python object is NULL !";
  if (_import_array()!=0) TRIQS_RUNTIME_ERROR <<"Internal Error in importing numpy";

  static const char * error_msg = "   A deep copy of the object would be necessary while views are supposed to guarantee to present a *view* of the python data.\n";

  if (!allow_copy) {
   if (!PyArray_Check(X)) throw copy_exception () << error_msg<<"   Indeed the object was not even an array !\n";
   if ( elementsType != PyArray_TYPE((PyArrayObject*)X))
    throw copy_exception () << error_msg<<"   The deep copy is caused by a type mismatch of the elements. Expected "<< type_name<< " and found XXX \n";
   PyArrayObject *arr = (PyArrayObject *)X;
#ifdef TRIQS_NUMPY_VERSION_LT_17
   if ( arr->nd != rank) throw copy_exception () << error_msg<<"   Rank mismatch . numpy array is of rank "<< arr->nd << "while you ask for rank "<< rank<<". \n";
#else
   if ( PyArray_NDIM(arr) != rank) throw copy_exception () << error_msg<<"   Rank mismatch . numpy array is of rank "<<  PyArray_NDIM(arr) << "while you ask for rank "<< rank<<". \n";
#endif
   numpy_obj = X; Py_INCREF(X);
  }
  else {
   // From X, we ask the numpy library to make a numpy, and of the correct type.
   // This handles automatically the cases where :
   //   - we have list, or list of list/tuple
   //   - the numpy type is not the one we want.
   //   - adjust the dimension if needed
   // If X is an array :
   //   - if Order is same, don't change it
   //   - else impose it (may provoque a copy).
   // if X is not array :
   //   - Order = FortranOrder or SameOrder - > Fortran order otherwise C

   //bool ForceCast = false;// Unless FORCECAST is present in flags, this call will generate an error if the data type cannot be safely obtained from the object.
   int flags = 0; //(ForceCast ? NPY_FORCECAST : 0) ;// do NOT force a copy | (make_copy ?  NPY_ENSURECOPY : 0);
   if (!(PyArray_Check(X) ))
    //flags |= ( IndexMapType::traversal_order == indexmaps::mem_layout::c_order(rank) ? NPY_C_CONTIGUOUS : NPY_F_CONTIGUOUS); //impose mem order
#ifdef TRIQS_NUMPY_VERSION_LT_17
    flags |= (NPY_C_CONTIGUOUS); //impose mem order
#else
    flags |= (NPY_ARRAY_C_CONTIGUOUS); //impose mem order
#endif
   numpy_obj= PyArray_FromAny(X,PyArray_DescrFromType(elementsType), rank,rank, flags , NULL );

   // do several checks
   if (!numpy_obj) {// The convertion of X to a numpy has failed !
    if (PyErr_Occurred()) {PyErr_Print();PyErr_Clear();}
    TRIQS_RUNTIME_ERROR<<"numpy interface : the python object  is not convertible to a numpy. ";
   }
   assert (PyArray_Check(numpy_obj)); assert((numpy_obj->ob_refcnt==1) || ((numpy_obj ==X)));

   PyArrayObject *arr_obj;
   arr_obj = (PyArrayObject *)numpy_obj;
   try {
#ifdef TRIQS_NUMPY_VERSION_LT_17
    if (arr_obj->nd!=rank)  TRIQS_RUNTIME_ERROR<<"numpy interface : internal error : dimensions do not match";
    if (arr_obj->descr->type_num != elementsType)
     TRIQS_RUNTIME_ERROR<<"numpy interface : internal error : incorrect type of element :" <<arr_obj->descr->type_num <<" vs "<<elementsType;
#else
    if ( PyArray_NDIM(arr_obj) !=rank)  TRIQS_RUNTIME_ERROR<<"numpy interface : internal error : dimensions do not match";
    if ( PyArray_DESCR(arr_obj)->type_num != elementsType)
     TRIQS_RUNTIME_ERROR<<"numpy interface : internal error : incorrect type of element :" <<PyArray_DESCR(arr_obj)->type_num <<" vs "<<elementsType;
#endif
   }
   catch(...) { Py_DECREF(numpy_obj); throw;} // make sure that in case of problem, the reference counting of python is still ok...
  }

  // extract strides and lengths
  PyArrayObject *arr_obj;
  arr_obj = (PyArrayObject *)numpy_obj;
#ifdef TRIQS_NUMPY_VERSION_LT_17
  const size_t dim =arr_obj->nd; // we know that dim == rank
  for (size_t i=0; i< dim ; ++i) {
   lengths[i] = size_t(arr_obj-> dimensions[i]);
   strides[i] = std::ptrdiff_t(arr_obj-> strides[i])/ size_of_ValueType;
  }
#else
  const size_t dim = PyArray_NDIM(arr_obj); // we know that dim == rank
  for (size_t i=0; i< dim ; ++i) {
   lengths[i] = size_t( PyArray_DIMS(arr_obj)[i]);
   strides[i] = std::ptrdiff_t( PyArray_STRIDES(arr_obj)[i])/ size_of_ValueType;
  }
#endif

  return numpy_obj;
 }