Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions include/bh_python/register_histogram.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::vector<bh::algorithm::reduce_command>>(args);
py::gil_scoped_release release;
return bh::algorithm::reduce(
self, py::cast<std::vector<bh::algorithm::reduce_command>>(args));
return bh::algorithm::reduce(self, commands);
})

.def("project",
[](const histogram_t& self, py::args values) {
auto cpp_values = py::cast<std::vector<unsigned>>(values);
py::gil_scoped_release release;
return bh::algorithm::project(self,
py::cast<std::vector<unsigned>>(values));
return bh::algorithm::project(self, cpp_values);
Comment on lines +201 to +203
Copy link
Member Author

@henryiii henryiii Feb 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the actual fix, I was running Python API via py::cast inside a GIL-less section! I'm also now catching this error before it happens in Python, which should make limited EH environments like pyodide happier. Plus we get a nicer error message & error class.

})

.def("fill", &fill<histogram_t>)
Expand Down
5 changes: 5 additions & 0 deletions src/boost_histogram/_internal/hist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
6 changes: 6 additions & 0 deletions tests/test_histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down