From f81cbe9fec1a1edb526d48a8fbe41aa88b2de85d Mon Sep 17 00:00:00 2001 From: Hans Dembinski Date: Fri, 24 Mar 2017 21:33:01 +0100 Subject: [PATCH] fix bug for conversion of uninitialized histogram --- src/python/histogram.cpp | 10 ++++------ test/python_suite_test.py.in | 11 +++++++++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/python/histogram.cpp b/src/python/histogram.cpp index 34c66824..19226a64 100644 --- a/src/python/histogram.cpp +++ b/src/python/histogram.cpp @@ -272,7 +272,7 @@ struct storage_access { static python::object array_interface(dynamic_histogram<>& self) { - const auto& b = self.storage_.buffer_; + auto& b = self.storage_.buffer_; if (b.type_.id_ == 5) { // PyErr_SetString(PyExc_KeyError, "cannot convert multiprecision storage to numpy array"); // python::throw_error_already_set(); @@ -280,11 +280,9 @@ struct storage_access { return python::object(); } - if (!b.ptr_) { - // PyErr_SetString(PyExc_KeyError, "cannot convert empty histogram"); - // python::throw_error_already_set(); - // workaround: for some reason, exception is ignored here - return python::object(); + if (b.type_.id_ == 0) { + // buffer not created yet, do that now + b.template initialize(); } python::dict d; diff --git a/test/python_suite_test.py.in b/test/python_suite_test.py.in index 80cd37bd..396a96b8 100755 --- a/test/python_suite_test.py.in +++ b/test/python_suite_test.py.in @@ -645,6 +645,17 @@ class histogramtest(unittest.TestCase): self.assertTrue(numpy.all(c == r)) self.assertTrue(numpy.all(v == r)) + @unittest.skipUnless(have_numpy, "requires build with numpy-support") + def test_numpy_conversion_4(self): + a = histogram(integer_axis(0, 1, uoflow=False)) + a1 = numpy.asarray(a) + self.assertEqual(a1.dtype, numpy.uint8) + self.assertEqual(a1.shape, (2,)) + + b = histogram() + b1 = numpy.asarray(b) + self.assertEqual(b1, 0) + @unittest.skipUnless(have_numpy, "requires build with numpy-support") def test_bad_numpy_conversion(self): a = histogram(integer_axis(0, 0))