1 from __future__
import annotations
5 from pybind11_tests
import type_caster_pyobject_ptr
as m
15 assert m.cast_from_pyobject_ptr() == 6758
19 assert m.cast_handle_to_pyobject_ptr(
ValueHolder(24)) == 76
23 assert m.cast_object_to_pyobject_ptr(
ValueHolder(43)) == 257
27 assert m.cast_list_to_pyobject_ptr([1, 2, 3, 4, 5]) == 395
31 assert m.return_pyobject_ptr() == 2314
38 @pytest.mark.parametrize(
41 m.call_callback_with_object_return,
42 m.call_callback_with_pyobject_ptr_return,
48 raise ValueError(
"Raised from cb")
51 assert call_callback(cb, 287).value == 713
53 with pytest.raises(ValueError, match=
"^Raised from cb$"):
59 return 300 - obj.value
61 assert m.call_callback_with_pyobject_ptr_arg(cb,
ValueHolder(39)) == 261
64 @pytest.mark.parametrize(
"set_error", [
True,
False])
67 True:
r"^Reflective of healthy error handling\.$",
69 r"^Internal error: pybind11::error_already_set called "
70 r"while Python error indicator not set\.$"
73 with pytest.raises(RuntimeError, match=expected):
74 m.cast_to_pyobject_ptr_nullptr(set_error)
78 with pytest.raises(SystemError)
as excinfo:
79 m.cast_to_pyobject_ptr_non_nullptr_with_error_set()
80 assert str(excinfo.value) ==
"src != nullptr but PyErr_Occurred()"
81 assert str(excinfo.value.__cause__) ==
"Reflective of unhealthy error handling."
90 vec_obj = m.return_list_pyobject_ptr_take_ownership(ValueHolder)
91 assert [e.value
for e
in vec_obj] == [93, 186]
95 vec_obj = m.return_list_pyobject_ptr_reference(ValueHolder)
96 assert [e.value
for e
in vec_obj] == [93, 186]
101 assert m.dec_ref_each_pyobject_ptr(vec_obj) == 2
105 with pytest.raises(TypeError, match=
r"1\. \(arg0: object, arg1: int\) -> None"):
110 class Drvd(m.WithPyObjectPtrReturn):
111 def return_pyobject_ptr(self):
112 return [
"11",
"22",
"33"]
116 assert d.return_pyobject_ptr() == [
"11",
"22",
"33"]
120 d_repr = m.call_return_pyobject_ptr(d)
121 assert d_repr ==
repr([
"11",
"22",
"33"])