Skip to content

Commit f9327bd

Browse files
authored
fix: regression in #662 causing segfaults (#708)
1 parent cbdfaa2 commit f9327bd

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

include/bh_python/register_histogram.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,16 +190,17 @@ auto register_histogram(py::module& m, const char* name, const char* desc) {
190190

191191
.def("reduce",
192192
[](const histogram_t& self, py::args args) {
193+
auto commands
194+
= py::cast<std::vector<bh::algorithm::reduce_command>>(args);
193195
py::gil_scoped_release release;
194-
return bh::algorithm::reduce(
195-
self, py::cast<std::vector<bh::algorithm::reduce_command>>(args));
196+
return bh::algorithm::reduce(self, commands);
196197
})
197198

198199
.def("project",
199200
[](const histogram_t& self, py::args values) {
201+
auto cpp_values = py::cast<std::vector<unsigned>>(values);
200202
py::gil_scoped_release release;
201-
return bh::algorithm::project(self,
202-
py::cast<std::vector<unsigned>>(values));
203+
return bh::algorithm::project(self, cpp_values);
203204
})
204205

205206
.def("fill", &fill<histogram_t>)

src/boost_histogram/_internal/hist.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,11 @@ def project(self: H, *args: int) -> Union[H, float, Accumulator]:
997997
Provided a list of axis numbers, this will produce the histogram over
998998
those axes only. Flow bins are used if available.
999999
"""
1000+
for arg in args:
1001+
if arg < 0 or arg >= self.ndim:
1002+
raise ValueError(
1003+
f"Projection axis must be a valid axis number 0 to {self.ndim-1}, not {arg}"
1004+
)
10001005

10011006
return self._new_hist(self._hist.project(*args))
10021007

tests/test_histogram.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,12 @@ def test_project():
590590
with pytest.raises(ValueError):
591591
h.project(2, 1)
592592

593+
with pytest.raises(ValueError):
594+
h.project(9)
595+
596+
with pytest.raises(ValueError):
597+
h.project(-1)
598+
593599

594600
def test_shrink_1d():
595601
h = bh.Histogram(bh.axis.Regular(20, 1, 5))

0 commit comments

Comments
 (0)