test_type_caster_pyobject_ptr.py
Go to the documentation of this file.
1 import pytest
2 
3 from pybind11_tests import type_caster_pyobject_ptr as m
4 
5 
6 # For use as a temporary user-defined object, to maximize sensitivity of the tests below.
7 class ValueHolder:
8  def __init__(self, value):
9  self.value = value
10 
11 
13  assert m.cast_from_pyobject_ptr() == 6758
14 
15 
17  assert m.cast_handle_to_pyobject_ptr(ValueHolder(24)) == 76
18 
19 
21  assert m.cast_object_to_pyobject_ptr(ValueHolder(43)) == 257
22 
23 
25  assert m.cast_list_to_pyobject_ptr([1, 2, 3, 4, 5]) == 395
26 
27 
29  assert m.return_pyobject_ptr() == 2314
30 
31 
33  assert m.pass_pyobject_ptr(ValueHolder(82)) == 118
34 
35 
36 @pytest.mark.parametrize(
37  "call_callback",
38  [
39  m.call_callback_with_object_return,
40  m.call_callback_with_pyobject_ptr_return,
41  ],
42 )
44  def cb(value):
45  if value < 0:
46  raise ValueError("Raised from cb")
47  return ValueHolder(1000 - value)
48 
49  assert call_callback(cb, 287).value == 713
50 
51  with pytest.raises(ValueError, match="^Raised from cb$"):
52  call_callback(cb, -1)
53 
54 
56  def cb(obj):
57  return 300 - obj.value
58 
59  assert m.call_callback_with_pyobject_ptr_arg(cb, ValueHolder(39)) == 261
60 
61 
62 @pytest.mark.parametrize("set_error", [True, False])
64  expected = {
65  True: r"^Reflective of healthy error handling\.$",
66  False: (
67  r"^Internal error: pybind11::error_already_set called "
68  r"while Python error indicator not set\.$"
69  ),
70  }[set_error]
71  with pytest.raises(RuntimeError, match=expected):
72  m.cast_to_pyobject_ptr_nullptr(set_error)
73 
74 
76  with pytest.raises(SystemError) as excinfo:
77  m.cast_to_pyobject_ptr_non_nullptr_with_error_set()
78  assert str(excinfo.value) == "src != nullptr but PyErr_Occurred()"
79  assert str(excinfo.value.__cause__) == "Reflective of unhealthy error handling."
80 
81 
83  acc = m.pass_list_pyobject_ptr([ValueHolder(842), ValueHolder(452)])
84  assert acc == 842452
85 
86 
88  vec_obj = m.return_list_pyobject_ptr_take_ownership(ValueHolder)
89  assert [e.value for e in vec_obj] == [93, 186]
90 
91 
93  vec_obj = m.return_list_pyobject_ptr_reference(ValueHolder)
94  assert [e.value for e in vec_obj] == [93, 186]
95  # Commenting out the next `assert` will leak the Python references.
96  # An easy way to see evidence of the leaks:
97  # Insert `while True:` as the first line of this function and monitor the
98  # process RES (Resident Memory Size) with the Unix top command.
99  assert m.dec_ref_each_pyobject_ptr(vec_obj) == 2
100 
101 
103  with pytest.raises(TypeError, match=r"1\. \(arg0: object, arg1: int\) -> None"):
104  m.pass_pyobject_ptr_and_int(ValueHolder(101), ValueHolder(202))
test_type_caster_pyobject_ptr.test_return_list_pyobject_ptr_take_ownership
def test_return_list_pyobject_ptr_take_ownership()
Definition: test_type_caster_pyobject_ptr.py:87
test_type_caster_pyobject_ptr.test_pass_list_pyobject_ptr
def test_pass_list_pyobject_ptr()
Definition: test_type_caster_pyobject_ptr.py:82
test_type_caster_pyobject_ptr.ValueHolder.__init__
def __init__(self, value)
Definition: test_type_caster_pyobject_ptr.py:8
test_type_caster_pyobject_ptr.test_type_caster_name_via_incompatible_function_arguments_type_error
def test_type_caster_name_via_incompatible_function_arguments_type_error()
Definition: test_type_caster_pyobject_ptr.py:102
test_type_caster_pyobject_ptr.test_cast_object_to_pyobject_ptr
def test_cast_object_to_pyobject_ptr()
Definition: test_type_caster_pyobject_ptr.py:20
test_type_caster_pyobject_ptr.test_cast_from_pyobject_ptr
def test_cast_from_pyobject_ptr()
Definition: test_type_caster_pyobject_ptr.py:12
test_type_caster_pyobject_ptr.test_cast_to_python_nullptr
def test_cast_to_python_nullptr(set_error)
Definition: test_type_caster_pyobject_ptr.py:63
test_type_caster_pyobject_ptr.test_call_callback_with_pyobject_ptr_arg
def test_call_callback_with_pyobject_ptr_arg()
Definition: test_type_caster_pyobject_ptr.py:55
test_type_caster_pyobject_ptr.test_cast_list_to_pyobject_ptr
def test_cast_list_to_pyobject_ptr()
Definition: test_type_caster_pyobject_ptr.py:24
test_type_caster_pyobject_ptr.test_return_pyobject_ptr
def test_return_pyobject_ptr()
Definition: test_type_caster_pyobject_ptr.py:28
test_type_caster_pyobject_ptr.ValueHolder
Definition: test_type_caster_pyobject_ptr.py:7
test_type_caster_pyobject_ptr.test_cast_to_python_non_nullptr_with_error_set
def test_cast_to_python_non_nullptr_with_error_set()
Definition: test_type_caster_pyobject_ptr.py:75
str
Definition: pytypes.h:1524
test_type_caster_pyobject_ptr.test_pass_pyobject_ptr
def test_pass_pyobject_ptr()
Definition: test_type_caster_pyobject_ptr.py:32
test_type_caster_pyobject_ptr.ValueHolder.value
value
Definition: test_type_caster_pyobject_ptr.py:9
test_type_caster_pyobject_ptr.test_return_list_pyobject_ptr_reference
def test_return_list_pyobject_ptr_reference()
Definition: test_type_caster_pyobject_ptr.py:92
test_type_caster_pyobject_ptr.test_cast_handle_to_pyobject_ptr
def test_cast_handle_to_pyobject_ptr()
Definition: test_type_caster_pyobject_ptr.py:16
test_type_caster_pyobject_ptr.test_call_callback_with_object_return
def test_call_callback_with_object_return(call_callback)
Definition: test_type_caster_pyobject_ptr.py:43


gtsam
Author(s):
autogenerated on Tue Jun 25 2024 03:05:31