From dd6b996e5d2e2f9f57b7d1cf2371e5b2868a00e5 Mon Sep 17 00:00:00 2001 From: Olivier Parcollet Date: Tue, 4 Feb 2014 15:03:33 +0100 Subject: [PATCH] arrays: to_python now raise exception when array is empty - otherwise it was simply crashing . - now raise a C++ exception - adapted cython wrapper to catch it as a python exception --- pytriqs/pxd/arrays.pxd | 12 ++++++------ triqs/arrays/impl/indexmap_storage_pair.hpp | 5 ++++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/pytriqs/pxd/arrays.pxd b/pytriqs/pxd/arrays.pxd index 25d379d3..847ac306 100644 --- a/pytriqs/pxd/arrays.pxd +++ b/pytriqs/pxd/arrays.pxd @@ -8,7 +8,7 @@ cdef extern from "" namespace "triqs::ar array operator -( array_view &) array operator *( array_view &) array operator /( array_view &) - object to_python() + object to_python() except + cdef cppclass array_view "triqs::arrays::array_c" [T,R] : array_view() @@ -18,7 +18,7 @@ cdef extern from "" namespace "triqs::ar array_view operator *( array_view &) array_view operator /( array_view &) void operator << (array_view &) - object to_python() + object to_python() except + int shape(int) # bug in cython #_lazy_expr operator()(_lazy_expr&) @@ -36,7 +36,7 @@ cdef extern from "" namespace "triqs::ar matrix_view(matrix) matrix_view(object) except + void operator << (matrix_view &) - object to_python() + object to_python() except + cdef cppclass matrix "triqs::arrays::matrix" [T] : matrix() @@ -44,7 +44,7 @@ cdef extern from "" namespace "triqs::ar matrix operator +( matrix_view &) matrix operator -( matrix_view &) matrix operator /( matrix_view &) - object to_python() + object to_python() except + cdef matrix_view operator +( matrix_view & , matrix_view &) cdef matrix_view operator -( matrix_view & , matrix_view &) @@ -58,7 +58,7 @@ cdef extern from "" namespace "triqs::ar tqa_vector operator -( tqa_vector_view &) tqa_vector operator *( tqa_vector_view &) tqa_vector operator /( tqa_vector_view &) - object to_python() + object to_python() except + cdef cppclass tqa_vector_view "triqs::arrays::vector_c" [T] : tqa_vector_view() @@ -68,5 +68,5 @@ cdef extern from "" namespace "triqs::ar tqa_vector_view operator *(tqa_vector_view &) tqa_vector_view operator /(tqa_vector_view &) void operator << (tqa_vector_view &) - object to_python() + object to_python() except + diff --git a/triqs/arrays/impl/indexmap_storage_pair.hpp b/triqs/arrays/impl/indexmap_storage_pair.hpp index 1c355e17..0cc80e98 100644 --- a/triqs/arrays/impl/indexmap_storage_pair.hpp +++ b/triqs/arrays/impl/indexmap_storage_pair.hpp @@ -147,7 +147,10 @@ namespace triqs { namespace arrays { storage_type & storage() {return storage_;} #ifdef TRIQS_WITH_PYTHON_SUPPORT - PyObject * to_python() const { return numpy_interface::array_view_to_python(*this);} + PyObject *to_python() const { + if (is_empty()) TRIQS_RUNTIME_ERROR << "Error : trying to return an empty array/matrix/vector to python"; + return numpy_interface::array_view_to_python(*this); + } #endif /// data_start is the starting point of the data of the object