Skip to content

Commit 25c60cc

Browse files
committed
Condense new unit tests via a simple local helper macro.
1 parent 6fce637 commit 25c60cc

File tree

1 file changed

+30
-63
lines changed

1 file changed

+30
-63
lines changed

tests/test_pytypes.cpp

Lines changed: 30 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ TEST_SUBMODULE(pytypes, m) {
297297
return d;
298298
});
299299

300-
m.def("accessor_moves", []() {
300+
m.def("accessor_moves", []() { // See PR #3970
301301
py::list return_list;
302302
#ifdef PYBIND11_HANDLE_REF_DEBUG
303303
# ifdef PYBIND11_PR3970
@@ -316,70 +316,37 @@ TEST_SUBMODULE(pytypes, m) {
316316
py::list lst;
317317
lst.append(0);
318318

319-
std::size_t inc_refs = py::handle::inc_ref_counter();
320-
tup[py_int_0]; // l-value (to have a control)
321-
inc_refs = py::handle::inc_ref_counter() - inc_refs;
322-
return_list.append(inc_refs);
323-
inc_refs = py::handle::inc_ref_counter();
324-
tup[py::int_(0)]; // r-value
325-
inc_refs = py::handle::inc_ref_counter() - inc_refs;
326-
return_list.append(inc_refs);
327-
328-
inc_refs = py::handle::inc_ref_counter();
329-
tup.attr(py_str_count); // l-value (to have a control)
330-
inc_refs = py::handle::inc_ref_counter() - inc_refs;
331-
return_list.append(inc_refs);
332-
inc_refs = py::handle::inc_ref_counter();
333-
tup.attr(py::str("count")); // r-value
334-
inc_refs = py::handle::inc_ref_counter() - inc_refs;
335-
return_list.append(inc_refs);
336-
337-
inc_refs = py::handle::inc_ref_counter();
338-
seq[py_int_0]; // l-value (to have a control)
339-
inc_refs = py::handle::inc_ref_counter() - inc_refs;
340-
return_list.append(inc_refs);
341-
inc_refs = py::handle::inc_ref_counter();
342-
seq[py::int_(0)]; // r-value
343-
inc_refs = py::handle::inc_ref_counter() - inc_refs;
344-
return_list.append(inc_refs);
345-
346-
inc_refs = py::handle::inc_ref_counter();
347-
seq.attr(py_str_count); // l-value (to have a control)
348-
inc_refs = py::handle::inc_ref_counter() - inc_refs;
349-
return_list.append(inc_refs);
350-
inc_refs = py::handle::inc_ref_counter();
351-
seq.attr(py::str("count")); // r-value
352-
inc_refs = py::handle::inc_ref_counter() - inc_refs;
353-
return_list.append(inc_refs);
354-
355-
inc_refs = py::handle::inc_ref_counter();
356-
lst[py_int_0]; // l-value (to have a control)
357-
inc_refs = py::handle::inc_ref_counter() - inc_refs;
358-
return_list.append(inc_refs);
359-
inc_refs = py::handle::inc_ref_counter();
360-
lst[py::int_(0)]; // r-value
361-
inc_refs = py::handle::inc_ref_counter() - inc_refs;
362-
return_list.append(inc_refs);
363-
364-
inc_refs = py::handle::inc_ref_counter();
365-
lst.attr(py_str_count); // l-value (to have a control)
366-
inc_refs = py::handle::inc_ref_counter() - inc_refs;
367-
return_list.append(inc_refs);
368-
inc_refs = py::handle::inc_ref_counter();
369-
lst.attr(py::str("count")); // r-value
370-
inc_refs = py::handle::inc_ref_counter() - inc_refs;
371-
return_list.append(inc_refs);
319+
# define PYBIND11_LOCAL_DEF(...) \
320+
{ \
321+
std::size_t inc_refs = py::handle::inc_ref_counter(); \
322+
__VA_ARGS__; \
323+
inc_refs = py::handle::inc_ref_counter() - inc_refs; \
324+
return_list.append(inc_refs); \
325+
}
326+
327+
PYBIND11_LOCAL_DEF(tup[py_int_0]) // l-value (to have a control)
328+
PYBIND11_LOCAL_DEF(tup[py::int_(0)]) // r-value
329+
330+
PYBIND11_LOCAL_DEF(tup.attr(py_str_count)) // l-value
331+
PYBIND11_LOCAL_DEF(tup.attr(py::str("count"))) // r-value
332+
333+
PYBIND11_LOCAL_DEF(seq[py_int_0]) // l-value
334+
PYBIND11_LOCAL_DEF(seq[py::int_(0)]) // r-value
335+
336+
PYBIND11_LOCAL_DEF(seq.attr(py_str_count)) // l-value
337+
PYBIND11_LOCAL_DEF(seq.attr(py::str("count"))) // r-value
338+
339+
PYBIND11_LOCAL_DEF(lst[py_int_0]) // l-value
340+
PYBIND11_LOCAL_DEF(lst[py::int_(0)]) // r-value
341+
342+
PYBIND11_LOCAL_DEF(lst.attr(py_str_count)) // l-value
343+
PYBIND11_LOCAL_DEF(lst.attr(py::str("count"))) // r-value
372344

373345
auto lst_acc = lst[py::int_(0)];
374-
lst_acc = py::int_(42); // Detaches lst_acc from lst.
375-
inc_refs = py::handle::inc_ref_counter();
376-
lst_acc = py_int_42; // l-value (to have a control)
377-
inc_refs = py::handle::inc_ref_counter() - inc_refs;
378-
return_list.append(inc_refs);
379-
inc_refs = py::handle::inc_ref_counter();
380-
lst_acc = py::int_(42); // r-value
381-
inc_refs = py::handle::inc_ref_counter() - inc_refs;
382-
return_list.append(inc_refs);
346+
lst_acc = py::int_(42); // Detaches lst_acc from lst.
347+
PYBIND11_LOCAL_DEF(lst_acc = py_int_42) // l-value
348+
PYBIND11_LOCAL_DEF(lst_acc = py::int_(42)) // r-value
349+
# undef PYBIND11_LOCAL_DEF
383350
#endif
384351
return return_list;
385352
});

0 commit comments

Comments
 (0)