Skip to content

Commit e198e25

Browse files
committed
Fix #195, unlimited and atomic single access
1 parent d61118a commit e198e25

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

include/boost/histogram/python/register_histogram.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ register_histogram(py::module &m, const char *name, const char *desc) {
172172
py::return_value_policy::move)
173173

174174
.def("at",
175-
[](const histogram_t &self, py::args &args) {
175+
[](const histogram_t &self, py::args &args) -> value_type {
176176
auto int_args = py::cast<std::vector<int>>(args);
177177
return self.at(int_args);
178178
})

src/register_histograms.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,33 @@ inline void copy_in<>(bh::histogram<vector_axis_variant, bh::dense_storage<doubl
6767
}
6868
}
6969

70+
/// Allow a Python int to implicitly convert to an atomic int in C++
71+
namespace pybind11 {
72+
namespace detail {
73+
template <>
74+
struct type_caster<storage::atomic_int::value_type> {
75+
public:
76+
PYBIND11_TYPE_CASTER(storage::atomic_int::value_type, _("atomic_int"));
77+
78+
bool load(handle src, bool) {
79+
PyObject *source = src.ptr();
80+
PyObject *tmp = PyNumber_Long(source);
81+
if(!tmp)
82+
return false;
83+
value.store(PyLong_AsUnsignedLongLong(tmp));
84+
Py_DECREF(tmp);
85+
return !PyErr_Occurred();
86+
}
87+
88+
static handle cast(storage::atomic_int::value_type src,
89+
return_value_policy /* policy */,
90+
handle /* parent */) {
91+
return PyLong_FromUnsignedLongLong(src.load());
92+
}
93+
};
94+
} // namespace detail
95+
} // namespace pybind11
96+
7097
void register_histograms(py::module &hist) {
7198
hist.attr("_axes_limit") = BOOST_HISTOGRAM_DETAIL_AXES_LIMIT;
7299

0 commit comments

Comments
 (0)