2024-06-22 06:00:13 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-03-04 01:58:42 +00:00
|
|
|
import pytest
|
|
|
|
|
2021-04-10 06:08:44 +00:00
|
|
|
from pybind11_tests import class_sh_trampoline_basic as m
|
2021-03-04 01:58:42 +00:00
|
|
|
|
|
|
|
|
2021-03-16 13:31:24 +00:00
|
|
|
class PyDrvd0(m.Abase0):
|
2021-03-04 01:58:42 +00:00
|
|
|
def __init__(self, val):
|
2022-02-15 03:00:40 +00:00
|
|
|
super().__init__(val)
|
2021-03-04 01:58:42 +00:00
|
|
|
|
2023-02-22 20:46:12 +00:00
|
|
|
def Add(self, other_val):
|
2021-03-04 01:58:42 +00:00
|
|
|
return self.Get() * 100 + other_val
|
|
|
|
|
|
|
|
|
2021-03-16 13:31:24 +00:00
|
|
|
class PyDrvd1(m.Abase1):
|
|
|
|
def __init__(self, val):
|
2022-02-15 03:00:40 +00:00
|
|
|
super().__init__(val)
|
2021-03-16 13:31:24 +00:00
|
|
|
|
2023-02-22 20:46:12 +00:00
|
|
|
def Add(self, other_val):
|
2021-03-16 13:31:24 +00:00
|
|
|
return self.Get() * 200 + other_val
|
|
|
|
|
|
|
|
|
|
|
|
def test_drvd0_add():
|
|
|
|
drvd = PyDrvd0(74)
|
2021-03-04 01:58:42 +00:00
|
|
|
assert drvd.Add(38) == (74 * 10 + 3) * 100 + 38
|
|
|
|
|
|
|
|
|
2021-03-16 13:31:24 +00:00
|
|
|
def test_drvd0_add_in_cpp_raw_ptr():
|
|
|
|
drvd = PyDrvd0(52)
|
2021-03-04 01:58:42 +00:00
|
|
|
assert m.AddInCppRawPtr(drvd, 27) == ((52 * 10 + 3) * 100 + 27) * 10 + 7
|
|
|
|
|
|
|
|
|
2021-03-16 13:31:24 +00:00
|
|
|
def test_drvd0_add_in_cpp_shared_ptr():
|
|
|
|
while True:
|
|
|
|
drvd = PyDrvd0(36)
|
|
|
|
assert m.AddInCppSharedPtr(drvd, 56) == ((36 * 10 + 3) * 100 + 56) * 100 + 11
|
|
|
|
return # Comment out for manual leak checking (use `top` command).
|
|
|
|
|
|
|
|
|
|
|
|
def test_drvd0_add_in_cpp_unique_ptr():
|
|
|
|
while True:
|
|
|
|
drvd = PyDrvd0(0)
|
|
|
|
with pytest.raises(ValueError) as exc_info:
|
|
|
|
m.AddInCppUniquePtr(drvd, 0)
|
|
|
|
assert (
|
|
|
|
str(exc_info.value)
|
2021-03-19 19:18:39 +00:00
|
|
|
== "Alias class (also known as trampoline) does not inherit from"
|
2021-04-10 06:08:44 +00:00
|
|
|
" py::trampoline_self_life_support, therefore the ownership of this"
|
2021-03-19 19:18:39 +00:00
|
|
|
" instance cannot safely be transferred to C++."
|
2021-03-16 13:31:24 +00:00
|
|
|
)
|
|
|
|
return # Comment out for manual leak checking (use `top` command).
|
2021-03-04 01:58:42 +00:00
|
|
|
|
|
|
|
|
2021-03-16 13:31:24 +00:00
|
|
|
def test_drvd1_add_in_cpp_unique_ptr():
|
|
|
|
while True:
|
|
|
|
drvd = PyDrvd1(25)
|
|
|
|
assert m.AddInCppUniquePtr(drvd, 83) == ((25 * 10 + 3) * 200 + 83) * 100 + 13
|
|
|
|
return # Comment out for manual leak checking (use `top` command).
|