Skip to content

Commit 0f70076

Browse files
committed
Demonstrate some extra function_record leaks
1 parent f5ec1cc commit 0f70076

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

tests/test_constants_and_functions.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,19 @@ TEST_SUBMODULE(constants_and_functions, m) {
124124
m.def("f2", f2);
125125
m.def("f3", f3);
126126
m.def("f4", f4);
127+
128+
// test_function_record_leaks
129+
struct LargeCapture {
130+
// This should always be enough to trigger the alternative branch
131+
// where `sizeof(capture) > sizeof(rec->data)`
132+
uint64_t zeros[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
133+
};
134+
m.def("register_large_capture_with_invalid_arguments", [](py::module_ m) {
135+
LargeCapture capture; // VS 2015's MSVC is acting up if we create the array here
136+
m.def("should_raise", [capture](int) { return capture.zeros[9] + 33; }, py::kw_only(), py::arg());
137+
});
138+
m.def("register_with_raising_repr", [](py::module_ m, py::object default_value) {
139+
m.def("should_raise", [](int, int, py::object) { return 42; }, "some docstring",
140+
py::arg_v("x", 42), py::arg_v("y", 42, "<the answer>"), py::arg_v("z", default_value));
141+
});
127142
}

tests/test_constants_and_functions.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,14 @@ def test_exception_specifiers():
4040
assert m.f2(53) == 55
4141
assert m.f3(86) == 89
4242
assert m.f4(140) == 144
43+
44+
45+
def test_function_record_leaks():
46+
class RaisingRepr:
47+
def __repr__(self):
48+
raise RuntimeError("Surprise!")
49+
50+
with pytest.raises(RuntimeError):
51+
m.register_large_capture_with_invalid_arguments(m)
52+
with pytest.raises(RuntimeError):
53+
m.register_with_raising_repr(m, RaisingRepr())

0 commit comments

Comments
 (0)