Skip to content

Commit

Permalink
Demonstrate some extra function_record leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
YannickJadoul committed Jan 4, 2021
1 parent f9d3554 commit 1d56f73
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
15 changes: 15 additions & 0 deletions tests/test_constants_and_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,19 @@ TEST_SUBMODULE(constants_and_functions, m) {
m.def("f2", f2);
m.def("f3", f3);
m.def("f4", f4);

// test_function_record_leaks
struct LargeCapture {
// This should always be enough to trigger the alternative branch
// where `sizeof(capture) > sizeof(rec->data)`
uint64_t zeros[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
};
m.def("register_large_capture_with_invalid_arguments", [](py::module_ m) {
LargeCapture capture; // VS 2015's MSVC is acting up if we create the array here
m.def("should_raise", [capture](int) { return capture.zeros[9] + 33; }, py::kw_only(), py::arg());
});
m.def("register_with_raising_repr", [](py::module_ m, py::object default_value) {
m.def("should_raise", [](int, int, py::object) { return 42; }, "some docstring",
py::arg_v("x", 42), py::arg_v("y", 42, "<the answer>"), py::arg_v("z", default_value));
});
}
11 changes: 11 additions & 0 deletions tests/test_constants_and_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,14 @@ def test_exception_specifiers():
assert m.f2(53) == 55
assert m.f3(86) == 89
assert m.f4(140) == 144


def test_function_record_leaks():
class RaisingRepr:
def __repr__(self):
raise RuntimeError("Surprise!")

with pytest.raises(RuntimeError):
m.register_large_capture_with_invalid_arguments(m)
with pytest.raises(RuntimeError):
m.register_with_raising_repr(m, RaisingRepr())

0 comments on commit 1d56f73

Please sign in to comment.