Skip to content

Commit c1cd022

Browse files
rwgkhenryiiipre-commit-ci[bot]
authored
tests: Add test for boost::histogram::func_transform situation. (#5582)
* Add test for boost::histogram::func_transform situation. * Resolve clang-tidy errors ``` /__w/pybind11/pybind11/tests/test_callbacks.cpp:33:9: error: 'auto rec' can be declared as 'auto *rec' [readability-qualified-auto,-warnings-as-errors] 33 | auto rec = c.get_pointer<py::detail::function_record>(); | ^~~~ | auto * /__w/pybind11/pybind11/tests/test_callbacks.cpp:41:13: error: 'auto cap' can be declared as 'auto *cap' [readability-qualified-auto,-warnings-as-errors] 41 | auto cap = reinterpret_cast<capture *>(&rec->data); | ^~~~ | auto * ``` * Replace `apply_custom_transform()` implementation using `make_caster<std::function<raw_t>>` * Add func_is_stateless_with_exact_type feature in pybind11/functional.h * tests: use std::function::target Signed-off-by: Henry Schreiner <[email protected]> * style: pre-commit fixes * Resolve clang-tidy error ``` /__w/pybind11/pybind11/tests/test_callbacks.cpp:33:5: error: 'auto cfunc' can be declared as 'auto *cfunc' [readability-qualified-auto,-warnings-as-errors] 33 | auto cfunc = func.target<raw_t *>(); | ^~~~ | auto * ``` --------- Signed-off-by: Henry Schreiner <[email protected]> Co-authored-by: Henry Schreiner <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent f365314 commit c1cd022

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

tests/test_callbacks.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,32 @@
1414

1515
#include <thread>
1616

17+
namespace test_callbacks {
18+
namespace boost_histogram { // See PR #5580
19+
20+
double custom_transform_double(double value) { return value * 3; }
21+
int custom_transform_int(int value) { return value; }
22+
23+
// Originally derived from
24+
// https://github.com/scikit-hep/boost-histogram/blob/460ef90905d6a8a9e6dd3beddfe7b4b49b364579/include/bh_python/transform.hpp#L68-L85
25+
double apply_custom_transform(const py::object &src, double value) {
26+
using raw_t = double(double);
27+
28+
py::detail::make_caster<std::function<raw_t>> func_caster;
29+
if (!func_caster.load(src, /*convert*/ false)) {
30+
return -100;
31+
}
32+
auto func = static_cast<std::function<raw_t> &>(func_caster);
33+
auto *cfunc = func.target<raw_t *>();
34+
if (cfunc == nullptr) {
35+
return -200;
36+
}
37+
return (*cfunc)(value);
38+
}
39+
40+
} // namespace boost_histogram
41+
} // namespace test_callbacks
42+
1743
int dummy_function(int i) { return i + 1; }
1844

1945
TEST_SUBMODULE(callbacks, m) {
@@ -272,4 +298,11 @@ TEST_SUBMODULE(callbacks, m) {
272298
// rec_capsule with nullptr name
273299
py::capsule rec_capsule2(std::malloc(1), [](void *data) { std::free(data); });
274300
m.add_object("custom_function2", PyCFunction_New(custom_def, rec_capsule2.ptr()));
301+
302+
m.def("boost_histogram_custom_transform_double",
303+
test_callbacks::boost_histogram::custom_transform_double);
304+
m.def("boost_histogram_custom_transform_int",
305+
test_callbacks::boost_histogram::custom_transform_int);
306+
m.def("boost_histogram_apply_custom_transform",
307+
test_callbacks::boost_histogram::apply_custom_transform);
275308
}

tests/test_callbacks.py

+12
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,15 @@ def test_callback_docstring():
234234
m.test_tuple_unpacking.__doc__.strip()
235235
== "test_tuple_unpacking(arg0: Callable) -> object"
236236
)
237+
238+
239+
def test_boost_histogram_apply_custom_transform():
240+
ctd = m.boost_histogram_custom_transform_double
241+
cti = m.boost_histogram_custom_transform_int
242+
apply = m.boost_histogram_apply_custom_transform
243+
assert apply(ctd, 5) == 15
244+
assert apply(cti, 0) == -200
245+
assert apply(None, 0) == -100
246+
assert apply(lambda value: value, 9) == -200
247+
assert apply({}, 0) == -100
248+
assert apply("", 0) == -100

0 commit comments

Comments
 (0)