test_type_caster_pyobject_ptr.py
Go to the documentation of this file.
1 from __future__ import annotations
2 
3 import pytest
4 
5 from pybind11_tests import type_caster_pyobject_ptr as m
6 
7 
8 # For use as a temporary user-defined object, to maximize sensitivity of the tests below.
9 class ValueHolder:
10  def __init__(self, value):
11  self.value = value
12 
13 
15  assert m.cast_from_pyobject_ptr() == 6758
16 
17 
19  assert m.cast_handle_to_pyobject_ptr(ValueHolder(24)) == 76
20 
21 
23  assert m.cast_object_to_pyobject_ptr(ValueHolder(43)) == 257
24 
25 
27  assert m.cast_list_to_pyobject_ptr([1, 2, 3, 4, 5]) == 395
28 
29 
31  assert m.return_pyobject_ptr() == 2314
32 
33 
35  assert m.pass_pyobject_ptr(ValueHolder(82)) == 118
36 
37 
38 @pytest.mark.parametrize(
39  "call_callback",
40  [
41  m.call_callback_with_object_return,
42  m.call_callback_with_pyobject_ptr_return,
43  ],
44 )
46  def cb(value):
47  if value < 0:
48  raise ValueError("Raised from cb")
49  return ValueHolder(1000 - value)
50 
51  assert call_callback(cb, 287).value == 713
52 
53  with pytest.raises(ValueError, match="^Raised from cb$"):
54  call_callback(cb, -1)
55 
56 
58  def cb(obj):
59  return 300 - obj.value
60 
61  assert m.call_callback_with_pyobject_ptr_arg(cb, ValueHolder(39)) == 261
62 
63 
64 @pytest.mark.parametrize("set_error", [True, False])
66  expected = {
67  True: r"^Reflective of healthy error handling\.$",
68  False: (
69  r"^Internal error: pybind11::error_already_set called "
70  r"while Python error indicator not set\.$"
71  ),
72  }[set_error]
73  with pytest.raises(RuntimeError, match=expected):
74  m.cast_to_pyobject_ptr_nullptr(set_error)
75 
76 
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."
82 
83 
85  acc = m.pass_list_pyobject_ptr([ValueHolder(842), ValueHolder(452)])
86  assert acc == 842452
87 
88 
90  vec_obj = m.return_list_pyobject_ptr_take_ownership(ValueHolder)
91  assert [e.value for e in vec_obj] == [93, 186]
92 
93 
95  vec_obj = m.return_list_pyobject_ptr_reference(ValueHolder)
96  assert [e.value for e in vec_obj] == [93, 186]
97  # Commenting out the next `assert` will leak the Python references.
98  # An easy way to see evidence of the leaks:
99  # Insert `while True:` as the first line of this function and monitor the
100  # process RES (Resident Memory Size) with the Unix top command.
101  assert m.dec_ref_each_pyobject_ptr(vec_obj) == 2
102 
103 
105  with pytest.raises(TypeError, match=r"1\. \(arg0: object, arg1: int\) -> None"):
106  m.pass_pyobject_ptr_and_int(ValueHolder(101), ValueHolder(202))
107 
108 
110  class Drvd(m.WithPyObjectPtrReturn):
111  def return_pyobject_ptr(self):
112  return ["11", "22", "33"]
113 
114  # Basic health check: First make sure this works as expected.
115  d = Drvd()
116  assert d.return_pyobject_ptr() == ["11", "22", "33"]
117 
118  while True:
119  # This failed before PR #5156: AddressSanitizer: heap-use-after-free ... in Py_DECREF
120  d_repr = m.call_return_pyobject_ptr(d)
121  assert d_repr == repr(["11", "22", "33"])
122  break # Comment out for manual leak checking.
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:89
test_type_caster_pyobject_ptr.test_pass_list_pyobject_ptr
def test_pass_list_pyobject_ptr()
Definition: test_type_caster_pyobject_ptr.py:84
test_type_caster_pyobject_ptr.ValueHolder.__init__
def __init__(self, value)
Definition: test_type_caster_pyobject_ptr.py:10
test_type_caster_pyobject_ptr.test_trampoline_with_pyobject_ptr_return
def test_trampoline_with_pyobject_ptr_return()
Definition: test_type_caster_pyobject_ptr.py:109
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:104
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:22
test_type_caster_pyobject_ptr.test_cast_from_pyobject_ptr
def test_cast_from_pyobject_ptr()
Definition: test_type_caster_pyobject_ptr.py:14
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:65
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:57
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:26
test_type_caster_pyobject_ptr.test_return_pyobject_ptr
def test_return_pyobject_ptr()
Definition: test_type_caster_pyobject_ptr.py:30
test_type_caster_pyobject_ptr.ValueHolder
Definition: test_type_caster_pyobject_ptr.py:9
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:77
str
Definition: pytypes.h:1558
test_type_caster_pyobject_ptr.test_pass_pyobject_ptr
def test_pass_pyobject_ptr()
Definition: test_type_caster_pyobject_ptr.py:34
test_type_caster_pyobject_ptr.ValueHolder.value
value
Definition: test_type_caster_pyobject_ptr.py:11
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:94
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:18
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:45
repr
str repr(handle h)
Definition: pytypes.h:2467


gtsam
Author(s):
autogenerated on Thu Jul 4 2024 03:06:05