mirror of
https://github.com/pybind/pybind11.git
synced 2025-02-07 17:32:00 +00:00
Move reference_wrapper test from test_issues to test_python_types
test_issues is deprecated, and the following commit adds other, related reference_wrapper tests.
This commit is contained in:
parent
005fde6a7f
commit
7cdf9f1a68
@ -117,19 +117,6 @@ void init_issues(py::module &m) {
|
|||||||
.def(py::init<int>())
|
.def(py::init<int>())
|
||||||
.def("__repr__", [](const Placeholder &p) { return "Placeholder[" + std::to_string(p.i) + "]"; });
|
.def("__repr__", [](const Placeholder &p) { return "Placeholder[" + std::to_string(p.i) + "]"; });
|
||||||
|
|
||||||
// #171: Can't return reference wrappers (or STL datastructures containing them)
|
|
||||||
m2.def("return_vec_of_reference_wrapper", [](std::reference_wrapper<Placeholder> p4) {
|
|
||||||
Placeholder *p1 = new Placeholder{1};
|
|
||||||
Placeholder *p2 = new Placeholder{2};
|
|
||||||
Placeholder *p3 = new Placeholder{3};
|
|
||||||
std::vector<std::reference_wrapper<Placeholder>> v;
|
|
||||||
v.push_back(std::ref(*p1));
|
|
||||||
v.push_back(std::ref(*p2));
|
|
||||||
v.push_back(std::ref(*p3));
|
|
||||||
v.push_back(p4);
|
|
||||||
return v;
|
|
||||||
});
|
|
||||||
|
|
||||||
// #181: iterator passthrough did not compile
|
// #181: iterator passthrough did not compile
|
||||||
m2.def("iterator_passthrough", [](py::iterator s) -> py::iterator {
|
m2.def("iterator_passthrough", [](py::iterator s) -> py::iterator {
|
||||||
return py::make_iterator(std::begin(s), std::end(s));
|
return py::make_iterator(std::begin(s), std::end(s));
|
||||||
|
@ -32,14 +32,6 @@ def test_dispatch_issue(msg):
|
|||||||
assert dispatch_issue_go(b) == "Yay.."
|
assert dispatch_issue_go(b) == "Yay.."
|
||||||
|
|
||||||
|
|
||||||
def test_reference_wrapper():
|
|
||||||
"""#171: Can't return reference wrappers (or STL data structures containing them)"""
|
|
||||||
from pybind11_tests.issues import Placeholder, return_vec_of_reference_wrapper
|
|
||||||
|
|
||||||
assert str(return_vec_of_reference_wrapper(Placeholder(4))) == \
|
|
||||||
"[Placeholder[1], Placeholder[2], Placeholder[3], Placeholder[4]]"
|
|
||||||
|
|
||||||
|
|
||||||
def test_iterator_passthrough():
|
def test_iterator_passthrough():
|
||||||
"""#181: iterator passthrough did not compile"""
|
"""#181: iterator passthrough did not compile"""
|
||||||
from pybind11_tests.issues import iterator_passthrough
|
from pybind11_tests.issues import iterator_passthrough
|
||||||
|
@ -560,6 +560,25 @@ test_initializer python_types([](py::module &m) {
|
|||||||
|
|
||||||
m.def("load_nullptr_t", [](std::nullptr_t) {}); // not useful, but it should still compile
|
m.def("load_nullptr_t", [](std::nullptr_t) {}); // not useful, but it should still compile
|
||||||
m.def("cast_nullptr_t", []() { return std::nullptr_t{}; });
|
m.def("cast_nullptr_t", []() { return std::nullptr_t{}; });
|
||||||
|
|
||||||
|
struct IntWrapper { int i; IntWrapper(int i) : i(i) { } };
|
||||||
|
py::class_<IntWrapper>(m, "IntWrapper")
|
||||||
|
.def(py::init<int>())
|
||||||
|
.def("__repr__", [](const IntWrapper &p) { return "IntWrapper[" + std::to_string(p.i) + "]"; });
|
||||||
|
|
||||||
|
// #171: Can't return reference wrappers (or STL datastructures containing them)
|
||||||
|
m.def("return_vec_of_reference_wrapper", [](std::reference_wrapper<IntWrapper> p4) {
|
||||||
|
IntWrapper *p1 = new IntWrapper{1};
|
||||||
|
IntWrapper *p2 = new IntWrapper{2};
|
||||||
|
IntWrapper *p3 = new IntWrapper{3};
|
||||||
|
std::vector<std::reference_wrapper<IntWrapper>> v;
|
||||||
|
v.push_back(std::ref(*p1));
|
||||||
|
v.push_back(std::ref(*p2));
|
||||||
|
v.push_back(std::ref(*p3));
|
||||||
|
v.push_back(p4);
|
||||||
|
return v;
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
#if defined(_MSC_VER)
|
#if defined(_MSC_VER)
|
||||||
|
@ -606,3 +606,15 @@ def test_void_caster():
|
|||||||
|
|
||||||
assert m.load_nullptr_t(None) is None
|
assert m.load_nullptr_t(None) is None
|
||||||
assert m.cast_nullptr_t() is None
|
assert m.cast_nullptr_t() is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_reference_wrapper():
|
||||||
|
"""std::reference_wrapper<T> tests.
|
||||||
|
|
||||||
|
#171: Can't return reference wrappers (or STL data structures containing them)
|
||||||
|
"""
|
||||||
|
from pybind11_tests import IntWrapper, return_vec_of_reference_wrapper
|
||||||
|
|
||||||
|
# 171:
|
||||||
|
assert str(return_vec_of_reference_wrapper(IntWrapper(4))) == \
|
||||||
|
"[IntWrapper[1], IntWrapper[2], IntWrapper[3], IntWrapper[4]]"
|
||||||
|
Loading…
Reference in New Issue
Block a user