diff --git a/include/bh_python/register_histogram.hpp b/include/bh_python/register_histogram.hpp index bf488ae24..eb36bc01f 100644 --- a/include/bh_python/register_histogram.hpp +++ b/include/bh_python/register_histogram.hpp @@ -190,16 +190,17 @@ auto register_histogram(py::module& m, const char* name, const char* desc) { .def("reduce", [](const histogram_t& self, py::args args) { + auto commands + = py::cast>(args); py::gil_scoped_release release; - return bh::algorithm::reduce( - self, py::cast>(args)); + return bh::algorithm::reduce(self, commands); }) .def("project", [](const histogram_t& self, py::args values) { + auto cpp_values = py::cast>(values); py::gil_scoped_release release; - return bh::algorithm::project(self, - py::cast>(values)); + return bh::algorithm::project(self, cpp_values); }) .def("fill", &fill) diff --git a/src/boost_histogram/_internal/hist.py b/src/boost_histogram/_internal/hist.py index ba8d0d5eb..ab5e6fb0b 100644 --- a/src/boost_histogram/_internal/hist.py +++ b/src/boost_histogram/_internal/hist.py @@ -998,6 +998,11 @@ def project(self: H, *args: int) -> Union[H, float, Accumulator]: Provided a list of axis numbers, this will produce the histogram over those axes only. Flow bins are used if available. """ + for arg in args: + if arg < 0 or arg >= self.ndim: + raise ValueError( + f"Projection axis must be a valid axis number 0 to {self.ndim-1}, not {arg}" + ) return self._new_hist(self._hist.project(*args)) diff --git a/tests/test_histogram.py b/tests/test_histogram.py index 9ccf8f8b9..dfd52dfae 100644 --- a/tests/test_histogram.py +++ b/tests/test_histogram.py @@ -590,6 +590,12 @@ def test_project(): with pytest.raises(ValueError): h.project(2, 1) + with pytest.raises(ValueError): + h.project(9) + + with pytest.raises(ValueError): + h.project(-1) + def test_shrink_1d(): h = bh.Histogram(bh.axis.Regular(20, 1, 5))