From 842274003f6f545b1ebf6cde7e11b08d8b424295 Mon Sep 17 00:00:00 2001 From: Olivier Parcollet Date: Thu, 12 Jun 2014 17:26:08 +0200 Subject: [PATCH] Fix import array in extensions. - import arrays in extensions (mako file). - put import_arrays in converter, along the lines of our own objects (numpy and triqs uses the same capsule technique, i.e. the standard technique from python doc.) --- pytriqs/wrap_generator/wrapper.mako.cpp | 3 +++ triqs/arrays/python/array_view_to_python.hpp | 2 +- triqs/arrays/storages/mem_block.hpp | 2 +- triqs/python_tools/wrapper_tools.hpp | 17 +++++++++++++++-- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/pytriqs/wrap_generator/wrapper.mako.cpp b/pytriqs/wrap_generator/wrapper.mako.cpp index ddf71ba6..105bafdf 100644 --- a/pytriqs/wrap_generator/wrapper.mako.cpp +++ b/pytriqs/wrap_generator/wrapper.mako.cpp @@ -1001,6 +1001,9 @@ ${f.code}; PyMODINIT_FUNC init${module.name}(void) { + // import numpy + import_array(); + PyObject* m; %for c in module.classes.values() : diff --git a/triqs/arrays/python/array_view_to_python.hpp b/triqs/arrays/python/array_view_to_python.hpp index 37f238f0..e44186f7 100644 --- a/triqs/arrays/python/array_view_to_python.hpp +++ b/triqs/arrays/python/array_view_to_python.hpp @@ -31,7 +31,7 @@ namespace triqs { namespace arrays { namespace numpy_interface { template PyObject * array_view_to_python ( ArrayViewType const & A, bool copy=false) { - _import_array(); + //_import_array(); typedef typename ArrayViewType::value_type value_type; static const int rank = ArrayViewType::rank; const int elementsType (numpy_to_C_type::type>::arraytype); diff --git a/triqs/arrays/storages/mem_block.hpp b/triqs/arrays/storages/mem_block.hpp index 10f6b4ff..d5b07de8 100644 --- a/triqs/arrays/storages/mem_block.hpp +++ b/triqs/arrays/storages/mem_block.hpp @@ -130,7 +130,7 @@ namespace triqs { namespace arrays { namespace storages { //namespace details { static_assert(!std::is_const::value, "internal error"); #ifdef TRIQS_WITH_PYTHON_SUPPORT - static void import_numpy_array() { if (_import_array()!=0) TRIQS_RUNTIME_ERROR <<"Internal Error in importing numpy";} + static void import_numpy_array() {}// if (_import_array()!=0) TRIQS_RUNTIME_ERROR <<"Internal Error in importing numpy";} #endif //Construct to state 0 diff --git a/triqs/python_tools/wrapper_tools.hpp b/triqs/python_tools/wrapper_tools.hpp index 6dfe7217..1f586f24 100644 --- a/triqs/python_tools/wrapper_tools.hpp +++ b/triqs/python_tools/wrapper_tools.hpp @@ -366,14 +366,27 @@ template struct py_converter struct py_converter_array { - static PyObject *c2py(ArrayType const &x) { return x.to_python(); } + static PyObject *c2py(ArrayType const &x) { + import_numpy(); + return x.to_python(); + } static ArrayType py2c(PyObject *ob) { return ArrayType (ob); } static bool is_convertible(PyObject *ob, bool raise_exception) { + import_numpy(); try { py2c(ob); return true;