Skip to content

Commit 2085199

Browse files
committed
Adding test_unique_ptr_member (for desired PyCLIF behavior).
See also: pybind#2583 Does not build with upstream master or pybind#2047, but builds with https://github.com/RobotLocomotion/pybind11 and almost runs: ``` Running tests in directory "/usr/local/google/home/rwgk/forked/EricCousineau-TRI/pybind11/tests": ================================================================================= test session starts ================================================================================= platform linux -- Python 3.8.5, pytest-5.4.3, py-1.9.0, pluggy-0.13.1 rootdir: /usr/local/google/home/rwgk/forked/EricCousineau-TRI/pybind11/tests, inifile: pytest.ini collected 2 items test_unique_ptr_member.py .F [100%] ====================================================================================== FAILURES ======================================================================================= _____________________________________________________________________________ test_pointee_and_ptr_owner ______________________________________________________________________________ def test_pointee_and_ptr_owner(): obj = m.pointee() assert obj.get_int() == 213 m.ptr_owner(obj) with pytest.raises(ValueError) as exc_info: > obj.get_int() E Failed: DID NOT RAISE <class 'ValueError'> test_unique_ptr_member.py:17: Failed ============================================================================= 1 failed, 1 passed in 0.06s ============================================================================= ```
1 parent 42e7380 commit 2085199

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

tests/test_unique_ptr_member.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include "pybind11_tests.h"
2+
3+
#include <memory>
4+
5+
namespace pybind11_tests {
6+
namespace unique_ptr_member {
7+
8+
class pointee { // NOT copyable.
9+
public:
10+
pointee() = default;
11+
12+
int get_int() const { return 213; }
13+
14+
private:
15+
pointee(const pointee &) = delete;
16+
pointee(pointee &&) = delete;
17+
pointee &operator=(const pointee &) = delete;
18+
pointee &operator=(pointee &&) = delete;
19+
};
20+
21+
class ptr_owner {
22+
public:
23+
explicit ptr_owner(std::unique_ptr<pointee> ptr) : ptr_(std::move(ptr)) {}
24+
25+
private:
26+
std::unique_ptr<pointee> ptr_;
27+
};
28+
29+
// Just to have a minimal example of a typical C++ pattern.
30+
inline int cpp_pattern() {
31+
auto obj = std::unique_ptr<pointee>(new pointee);
32+
int result = (obj ? 10 : 0);
33+
ptr_owner owner(std::move(obj));
34+
result += (obj ? 1 : 0);
35+
return result;
36+
}
37+
38+
TEST_SUBMODULE(unique_ptr_member, m) {
39+
m.def("cpp_pattern", cpp_pattern);
40+
41+
py::class_<pointee>(m, "pointee")
42+
.def(py::init<>())
43+
.def("get_int", &pointee::get_int);
44+
45+
py::class_<ptr_owner>(m, "ptr_owner")
46+
#ifdef FEAT_UNIQUE_PTR_ARG
47+
.def(py::init<std::unique_ptr<pointee>>(), py::arg("ptr"))
48+
#endif
49+
;
50+
}
51+
52+
} // namespace unique_ptr_member
53+
} // namespace pybind11_tests

tests/test_unique_ptr_member.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# -*- coding: utf-8 -*-
2+
import pytest
3+
4+
from pybind11_tests import unique_ptr_member as m
5+
6+
7+
def test_cpp_pattern():
8+
res = m.cpp_pattern()
9+
assert res == 10
10+
11+
12+
def test_pointee_and_ptr_owner():
13+
obj = m.pointee()
14+
assert obj.get_int() == 213
15+
m.ptr_owner(obj)
16+
with pytest.raises(ValueError) as exc_info:
17+
obj.get_int()
18+
assert str(exc_info.value).startswith("Missing value for wrapped C++ type ")

0 commit comments

Comments
 (0)