blob: 1b29ceb31b611bbff54dd8a0189408af1b8868bc [file] [log] [blame]
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02001import pytest
2import gc
Wenzel Jakobbd560ac2016-11-03 11:53:35 +01003from pybind11_tests import ConstructorStats
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02004
5
Dean Moldovan665e8802016-08-12 22:28:31 +02006def test_regressions():
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02007 from pybind11_tests.issues import print_cchar, print_char
8
Dean Moldovan665e8802016-08-12 22:28:31 +02009 # #137: const char* isn't handled properly
10 assert print_cchar("const char *") == "const char *"
11 # #150: char bindings broken
12 assert print_char("c") == "c"
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020013
14
Dean Moldovan665e8802016-08-12 22:28:31 +020015def test_dispatch_issue(msg):
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020016 """#159: virtual function dispatch has problems with similar-named functions"""
17 from pybind11_tests.issues import DispatchIssue, dispatch_issue_go
18
19 class PyClass1(DispatchIssue):
20 def dispatch(self):
Dean Moldovan665e8802016-08-12 22:28:31 +020021 return "Yay.."
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020022
23 class PyClass2(DispatchIssue):
24 def dispatch(self):
25 with pytest.raises(RuntimeError) as excinfo:
26 super(PyClass2, self).dispatch()
27 assert msg(excinfo.value) == 'Tried to call pure virtual function "Base::dispatch"'
28
29 p = PyClass1()
Dean Moldovan665e8802016-08-12 22:28:31 +020030 return dispatch_issue_go(p)
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020031
32 b = PyClass2()
Dean Moldovan665e8802016-08-12 22:28:31 +020033 assert dispatch_issue_go(b) == "Yay.."
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020034
35
36def test_reference_wrapper():
37 """#171: Can't return reference wrappers (or STL data structures containing them)"""
38 from pybind11_tests.issues import Placeholder, return_vec_of_reference_wrapper
39
40 assert str(return_vec_of_reference_wrapper(Placeholder(4))) == \
41 "[Placeholder[1], Placeholder[2], Placeholder[3], Placeholder[4]]"
42
43
44def test_iterator_passthrough():
45 """#181: iterator passthrough did not compile"""
46 from pybind11_tests.issues import iterator_passthrough
47
48 assert list(iterator_passthrough(iter([3, 5, 7, 9, 11, 13, 15]))) == [3, 5, 7, 9, 11, 13, 15]
49
50
51def test_shared_ptr_gc():
52 """// #187: issue involving std::shared_ptr<> return value policy & garbage collection"""
53 from pybind11_tests.issues import ElementList, ElementA
54
55 el = ElementList()
56 for i in range(10):
57 el.add(ElementA(i))
58 gc.collect()
59 for i, v in enumerate(el.get()):
60 assert i == v.value()
61
62
Wenzel Jakobfe40dfe2016-11-07 15:59:01 +010063def test_no_id(msg):
Dean Moldovan99dbdc12016-08-19 13:45:36 +020064 from pybind11_tests.issues import get_element, expect_float, expect_int
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020065
66 with pytest.raises(TypeError) as excinfo:
Dean Moldovan99dbdc12016-08-19 13:45:36 +020067 get_element(None)
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020068 assert msg(excinfo.value) == """
Wenzel Jakobe99ebae2016-09-12 11:44:37 +090069 get_element(): incompatible function arguments. The following argument types are supported:
Dean Moldovan99dbdc12016-08-19 13:45:36 +020070 1. (arg0: m.issues.ElementA) -> int
Wenzel Jakobe99ebae2016-09-12 11:44:37 +090071
72 Invoked with: None
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020073 """
74
75 with pytest.raises(TypeError) as excinfo:
76 expect_int(5.2)
77 assert msg(excinfo.value) == """
Wenzel Jakobe99ebae2016-09-12 11:44:37 +090078 expect_int(): incompatible function arguments. The following argument types are supported:
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020079 1. (arg0: int) -> int
Wenzel Jakobe99ebae2016-09-12 11:44:37 +090080
81 Invoked with: 5.2
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020082 """
83 assert expect_float(12) == 12
84
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020085
86
Dean Moldovan99dbdc12016-08-19 13:45:36 +020087def test_str_issue(msg):
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020088 """Issue #283: __str__ called on uninitialized instance when constructor arguments invalid"""
89 from pybind11_tests.issues import StrIssue
90
Dean Moldovan99dbdc12016-08-19 13:45:36 +020091 assert str(StrIssue(3)) == "StrIssue[3]"
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020092
93 with pytest.raises(TypeError) as excinfo:
94 str(StrIssue("no", "such", "constructor"))
95 assert msg(excinfo.value) == """
Wenzel Jakobe99ebae2016-09-12 11:44:37 +090096 __init__(): incompatible constructor arguments. The following argument types are supported:
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020097 1. m.issues.StrIssue(arg0: int)
98 2. m.issues.StrIssue()
Wenzel Jakobe99ebae2016-09-12 11:44:37 +090099
100 Invoked with: no, such, constructor
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200101 """
102
103
Dean Moldovan665e8802016-08-12 22:28:31 +0200104def test_nested():
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200105 """ #328: first member in a class can't be used in operators"""
Dean Moldovan665e8802016-08-12 22:28:31 +0200106 from pybind11_tests.issues import NestA, NestB, NestC, get_NestA, get_NestB, get_NestC
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200107
108 a = NestA()
109 b = NestB()
110 c = NestC()
111
112 a += 10
Dean Moldovan665e8802016-08-12 22:28:31 +0200113 assert get_NestA(a) == 13
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200114 b.a += 100
Dean Moldovan665e8802016-08-12 22:28:31 +0200115 assert get_NestA(b.a) == 103
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200116 c.b.a += 1000
Dean Moldovan665e8802016-08-12 22:28:31 +0200117 assert get_NestA(c.b.a) == 1003
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200118 b -= 1
Dean Moldovan665e8802016-08-12 22:28:31 +0200119 assert get_NestB(b) == 3
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200120 c.b -= 3
Dean Moldovan665e8802016-08-12 22:28:31 +0200121 assert get_NestB(c.b) == 1
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200122 c *= 7
Dean Moldovan665e8802016-08-12 22:28:31 +0200123 assert get_NestC(c) == 35
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200124
125 abase = a.as_base()
126 assert abase.value == -2
127 a.as_base().value += 44
128 assert abase.value == 42
129 assert c.b.a.as_base().value == -2
130 c.b.a.as_base().value += 44
131 assert c.b.a.as_base().value == 42
132
133 del c
134 gc.collect()
135 del a # Should't delete while abase is still alive
136 gc.collect()
137
138 assert abase.value == 42
139 del abase, b
140 gc.collect()
Wenzel Jakobc84b37b2016-09-07 00:47:17 +0900141
142
143def test_move_fallback():
144 from pybind11_tests.issues import get_moveissue1, get_moveissue2
145 m2 = get_moveissue2(2)
146 assert m2.value == 2
147 m1 = get_moveissue1(1)
148 assert m1.value == 1
Jason Rhinelander56f71772016-09-07 13:32:49 -0400149
Wenzel Jakobfe40dfe2016-11-07 15:59:01 +0100150
Jason Rhinelander56f71772016-09-07 13:32:49 -0400151def test_override_ref():
152 from pybind11_tests.issues import OverrideTest
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400153 o = OverrideTest("asdf")
Jason Rhinelander56f71772016-09-07 13:32:49 -0400154
Jason Rhinelanderc03db9b2016-09-07 13:38:32 -0400155 # Not allowed (see associated .cpp comment)
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400156 #i = o.str_ref()
157 #assert o.str_ref() == "asdf"
158 assert o.str_value() == "asdf"
Jason Rhinelander56f71772016-09-07 13:32:49 -0400159
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400160 assert o.A_value().value == "hi"
Jason Rhinelander56f71772016-09-07 13:32:49 -0400161 a = o.A_ref()
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400162 assert a.value == "hi"
163 a.value = "bye"
164 assert a.value == "bye"
Wenzel Jakob382484a2016-09-10 15:28:37 +0900165
Wenzel Jakobfe40dfe2016-11-07 15:59:01 +0100166
Wenzel Jakob382484a2016-09-10 15:28:37 +0900167def test_operators_notimplemented(capture):
168 from pybind11_tests.issues import OpTest1, OpTest2
169 with capture:
170 C1, C2 = OpTest1(), OpTest2()
171 C1 + C1
172 C2 + C2
173 C2 + C1
174 C1 + C2
175 assert capture == """Add OpTest1 with OpTest1
176Add OpTest2 with OpTest2
177Add OpTest2 with OpTest1
178Add OpTest2 with OpTest1"""
Wenzel Jakobb212f6c2016-09-10 16:00:50 +0900179
Wenzel Jakobfe40dfe2016-11-07 15:59:01 +0100180
Wenzel Jakobb212f6c2016-09-10 16:00:50 +0900181def test_iterator_rvpolicy():
182 """ Issue 388: Can't make iterators via make_iterator() with different r/v policies """
183 from pybind11_tests.issues import make_iterator_1
184 from pybind11_tests.issues import make_iterator_2
185
186 assert list(make_iterator_1()) == [1, 2, 3]
187 assert list(make_iterator_2()) == [1, 2, 3]
188 assert(type(make_iterator_1()) != type(make_iterator_2()))
Jason Rhinelander6873c202016-10-24 21:58:22 -0400189
Wenzel Jakobfe40dfe2016-11-07 15:59:01 +0100190
Jason Rhinelander6873c202016-10-24 21:58:22 -0400191def test_dupe_assignment():
192 """ Issue 461: overwriting a class with a function """
193 from pybind11_tests.issues import dupe_exception_failures
194 assert dupe_exception_failures() == []
Wenzel Jakobbd560ac2016-11-03 11:53:35 +0100195
196
197def test_enable_shared_from_this_with_reference_rvp():
198 """ Issue #471: shared pointer instance not dellocated """
199 from pybind11_tests import SharedParent, SharedChild
200
201 parent = SharedParent()
202 child = parent.get_child()
203
204 cstats = ConstructorStats.get(SharedChild)
205 assert cstats.alive() == 1
206 del child, parent
207 assert cstats.alive() == 0
Jason Rhinelanderc07ec312016-11-06 13:12:48 -0500208
Wenzel Jakobfe40dfe2016-11-07 15:59:01 +0100209
Jason Rhinelanderc07ec312016-11-06 13:12:48 -0500210def test_non_destructed_holders():
211 """ Issue #478: unique ptrs constructed and freed without destruction """
212 from pybind11_tests import SpecialHolderObj
213
214 a = SpecialHolderObj(123)
215 b = a.child()
216
217 assert a.val == 123
218 assert b.val == 124
219
220 cstats = SpecialHolderObj.holder_cstats()
221 assert cstats.alive() == 1
222 del b
223 assert cstats.alive() == 1
224 del a
225 assert cstats.alive() == 0
Wenzel Jakobfe40dfe2016-11-07 15:59:01 +0100226
227
228def test_complex_cast(capture):
229 """ Issue #484: number conversion generates unhandled exceptions """
230 from pybind11_tests.issues import test_complex
231
232 with capture:
233 test_complex(1)
234 test_complex(2j)
235
236 assert capture == """
2371.0
238(0.0, 2.0)
239"""
Wenzel Jakob7c2461e2016-11-20 05:26:02 +0100240
241
242def test_inheritance_override_def_static():
243 from pybind11_tests.issues import MyBase, MyDerived
244
245 b = MyBase.make()
246 d1 = MyDerived.make2()
247 d2 = MyDerived.make()
248
249 assert isinstance(b, MyBase)
250 assert isinstance(d1, MyDerived)
251 assert isinstance(d2, MyDerived)