Skip to content

Commit a1b7094

Browse files
committed
Bring back .attr("value") in return_array_cpp_loop()
This was meant to further stress-test correctness of refcount handling. All modified test functions were manually leak-checked (`while True`, top command, Python 3.12.3, Ubuntu 24.01, gcc 13.2.0).
1 parent cfe6521 commit a1b7094

File tree

2 files changed

+27
-30
lines changed

2 files changed

+27
-30
lines changed

tests/test_numpy_array.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ py::array_t<PyObjectType> return_array_cpp_loop(const py::list &objs) {
195195
PyObjectType *data = arr_from_list.mutable_data();
196196
for (py::size_t i = 0; i < arr_size; i++) {
197197
assert(!data[i]);
198-
data[i] = convert_to_pyobjecttype<PyObjectType>(objs[i]);
198+
data[i] = convert_to_pyobjecttype<PyObjectType>(objs[i].attr("value"));
199199
}
200200
return arr_from_list;
201201
}

tests/test_numpy_array.py

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -629,64 +629,61 @@ def UnwrapPyValueHolder(vhs):
629629
return [vh.value for vh in vhs]
630630

631631

632+
PASS_ARRAY_PYOBJECT_RETURN_SUM_STR_VALUES_FUNCTIONS = [
633+
m.pass_array_pyobject_ptr_return_sum_str_values,
634+
m.pass_array_handle_return_sum_str_values,
635+
m.pass_array_object_return_sum_str_values,
636+
]
637+
638+
632639
@pytest.mark.parametrize(
633-
"func",
634-
[
635-
m.pass_array_pyobject_ptr_return_sum_str_values,
636-
m.pass_array_handle_return_sum_str_values,
637-
m.pass_array_object_return_sum_str_values,
638-
],
640+
"pass_array", PASS_ARRAY_PYOBJECT_RETURN_SUM_STR_VALUES_FUNCTIONS
639641
)
640-
def test_pass_array_object_return_sum_str_values_ndarray(func):
642+
def test_pass_array_object_return_sum_str_values_ndarray(pass_array):
641643
# Intentionally all temporaries, do not change.
642644
assert (
643-
func(np.array(WrapWithPyValueHolder(-3, "four", 5.0), dtype=object))
645+
pass_array(np.array(WrapWithPyValueHolder(-3, "four", 5.0), dtype=object))
644646
== "-3four5.0"
645647
)
646648

647649

648650
@pytest.mark.parametrize(
649-
"func",
650-
[
651-
m.pass_array_pyobject_ptr_return_sum_str_values,
652-
m.pass_array_handle_return_sum_str_values,
653-
m.pass_array_object_return_sum_str_values,
654-
],
651+
"pass_array", PASS_ARRAY_PYOBJECT_RETURN_SUM_STR_VALUES_FUNCTIONS
655652
)
656-
def test_pass_array_object_return_sum_str_values_list(func):
653+
def test_pass_array_object_return_sum_str_values_list(pass_array):
657654
# Intentionally all temporaries, do not change.
658-
assert func(WrapWithPyValueHolder(2, "three", -4.0)) == "2three-4.0"
655+
assert pass_array(WrapWithPyValueHolder(2, "three", -4.0)) == "2three-4.0"
659656

660657

661658
@pytest.mark.parametrize(
662-
"func",
659+
"pass_array",
663660
[
664661
m.pass_array_pyobject_ptr_return_as_list,
665662
m.pass_array_handle_return_as_list,
666663
m.pass_array_object_return_as_list,
667664
],
668665
)
669-
def test_pass_array_object_return_as_list(func):
666+
def test_pass_array_object_return_as_list(pass_array):
670667
# Intentionally all temporaries, do not change.
671668
assert UnwrapPyValueHolder(
672-
func(np.array(WrapWithPyValueHolder(-1, "two", 3.0), dtype=object))
669+
pass_array(np.array(WrapWithPyValueHolder(-1, "two", 3.0), dtype=object))
673670
) == [-1, "two", 3.0]
674671

675672

676673
@pytest.mark.parametrize(
677-
"func",
674+
("return_array", "unwrap"),
678675
[
679-
m.return_array_pyobject_ptr_cpp_loop,
680-
m.return_array_handle_cpp_loop,
681-
m.return_array_object_cpp_loop,
682-
m.return_array_pyobject_ptr_from_list,
683-
m.return_array_handle_from_list,
684-
m.return_array_object_from_list,
676+
(m.return_array_pyobject_ptr_cpp_loop, list),
677+
(m.return_array_handle_cpp_loop, list),
678+
(m.return_array_object_cpp_loop, list),
679+
(m.return_array_pyobject_ptr_from_list, UnwrapPyValueHolder),
680+
(m.return_array_handle_from_list, UnwrapPyValueHolder),
681+
(m.return_array_object_from_list, UnwrapPyValueHolder),
685682
],
686683
)
687-
def test_return_array_object_cpp_loop(func):
684+
def test_return_array_object_cpp_loop(return_array, unwrap):
688685
# Intentionally all temporaries, do not change.
689-
arr_from_list = func(WrapWithPyValueHolder(6, "seven", -8.0))
686+
arr_from_list = return_array(WrapWithPyValueHolder(6, "seven", -8.0))
690687
assert isinstance(arr_from_list, np.ndarray)
691688
assert arr_from_list.dtype == np.dtype("O")
692-
assert UnwrapPyValueHolder(arr_from_list) == [6, "seven", -8.0]
689+
assert unwrap(arr_from_list) == [6, "seven", -8.0]

0 commit comments

Comments
 (0)