adding test_get_static_pointee (works as desired for shared_ptr holder)

parent 09db57a3
...@@ -44,6 +44,11 @@ inline int pass_shared_pointee(std::shared_ptr<pointee> ptr) { ...@@ -44,6 +44,11 @@ inline int pass_shared_pointee(std::shared_ptr<pointee> ptr) {
return 5000 + ptr->get_int(); return 5000 + ptr->get_int();
} }
inline pointee* get_static_pointee() {
static pointee cpp_instance;
return &cpp_instance;
}
TEST_SUBMODULE(holder_shared_ptr, m) { TEST_SUBMODULE(holder_shared_ptr, m) {
m.def("to_cout", to_cout); m.def("to_cout", to_cout);
...@@ -55,6 +60,9 @@ TEST_SUBMODULE(holder_shared_ptr, m) { ...@@ -55,6 +60,9 @@ TEST_SUBMODULE(holder_shared_ptr, m) {
m.def("make_shared_pointee", make_shared_pointee); m.def("make_shared_pointee", make_shared_pointee);
// m.def("pass_unique_pointee", pass_unique_pointee); // m.def("pass_unique_pointee", pass_unique_pointee);
m.def("pass_shared_pointee", pass_shared_pointee); m.def("pass_shared_pointee", pass_shared_pointee);
m.def("get_static_pointee",
get_static_pointee, py::return_value_policy::reference);
} }
} // namespace holder_shared_ptr } // namespace holder_shared_ptr
......
# KEEP IN SYNC WITH test_holder_unique_ptr.py
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# KEEP IN SYNC WITH test_holder_unique_ptr.py
import pytest
from pybind11_tests import holder_shared_ptr as m from pybind11_tests import holder_shared_ptr as m
...@@ -41,3 +43,14 @@ def test_pass_shared_pointee(): ...@@ -41,3 +43,14 @@ def test_pass_shared_pointee():
i = m.pass_shared_pointee(obj) i = m.pass_shared_pointee(obj)
assert i == 5213 assert i == 5213
m.to_cout("") m.to_cout("")
def test_get_static_pointee():
m.to_cout("")
m.to_cout("")
m.to_cout("get_static_pointee")
obj = m.get_static_pointee()
assert obj.get_int() == 213
with pytest.raises(RuntimeError) as excinfo:
m.pass_shared_pointee(obj)
assert "Unable to cast from non-held to held instance" in str(excinfo.value)
...@@ -44,6 +44,11 @@ inline int pass_shared_pointee(std::shared_ptr<pointee> ptr) { ...@@ -44,6 +44,11 @@ inline int pass_shared_pointee(std::shared_ptr<pointee> ptr) {
return 5000 + ptr->get_int(); return 5000 + ptr->get_int();
} }
inline pointee* get_static_pointee() {
static pointee cpp_instance;
return &cpp_instance;
}
TEST_SUBMODULE(holder_unique_ptr, m) { TEST_SUBMODULE(holder_unique_ptr, m) {
m.def("to_cout", to_cout); m.def("to_cout", to_cout);
...@@ -55,6 +60,9 @@ TEST_SUBMODULE(holder_unique_ptr, m) { ...@@ -55,6 +60,9 @@ TEST_SUBMODULE(holder_unique_ptr, m) {
m.def("make_shared_pointee", make_shared_pointee); m.def("make_shared_pointee", make_shared_pointee);
// m.def("pass_unique_pointee", pass_unique_pointee); // m.def("pass_unique_pointee", pass_unique_pointee);
m.def("pass_shared_pointee", pass_shared_pointee); m.def("pass_shared_pointee", pass_shared_pointee);
m.def("get_static_pointee",
get_static_pointee, py::return_value_policy::reference);
} }
} // namespace holder_unique_ptr } // namespace holder_unique_ptr
......
# KEEP IN SYNC WITH test_holder_shared_ptr.py
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# KEEP IN SYNC WITH test_holder_shared_ptr.py
import pytest
from pybind11_tests import holder_unique_ptr as m from pybind11_tests import holder_unique_ptr as m
...@@ -41,3 +43,14 @@ def test_pass_shared_pointee(): ...@@ -41,3 +43,14 @@ def test_pass_shared_pointee():
i = m.pass_shared_pointee(obj) i = m.pass_shared_pointee(obj)
assert i == 5213 assert i == 5213
m.to_cout("") m.to_cout("")
def test_get_static_pointee():
m.to_cout("")
m.to_cout("")
m.to_cout("get_static_pointee")
obj = m.get_static_pointee()
assert obj.get_int() == 213
with pytest.raises(RuntimeError) as excinfo:
m.pass_unique_pointee(obj)
assert "Unable to cast from non-held to held instance" in str(excinfo.value)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment