3 from pybind11_tests
import virtual_functions
as m
4 from pybind11_tests
import ConstructorStats
8 class ExtendedExampleVirt(m.ExampleVirt):
9 def __init__(self, state):
10 super(ExtendedExampleVirt, self).__init__(state + 1)
11 self.data =
"Hello world" 14 print(
'ExtendedExampleVirt::run(%i), calling parent..' % value)
15 return super(ExtendedExampleVirt, self).run(value + 1)
18 print(
'ExtendedExampleVirt::run_bool()')
21 def get_string1(self):
24 def pure_virtual(self):
25 print(
'ExtendedExampleVirt::pure_virtual(): %s' % self.data)
27 class ExtendedExampleVirt2(ExtendedExampleVirt):
28 def __init__(self, state):
29 super(ExtendedExampleVirt2, self).__init__(state + 1)
31 def get_string2(self):
34 ex12 = m.ExampleVirt(10)
36 assert m.runExampleVirt(ex12, 20) == 30
38 Original implementation of ExampleVirt::run(state=10, value=20, str1=default1, str2=default2) 41 with pytest.raises(RuntimeError)
as excinfo:
42 m.runExampleVirtVirtual(ex12)
43 assert msg(excinfo.value) ==
'Tried to call pure virtual function "ExampleVirt::pure_virtual"' 45 ex12p = ExtendedExampleVirt(10)
47 assert m.runExampleVirt(ex12p, 20) == 32
49 ExtendedExampleVirt::run(20), calling parent.. 50 Original implementation of ExampleVirt::run(state=11, value=21, str1=override1, str2=default2) 53 assert m.runExampleVirtBool(ex12p)
is False 54 assert capture ==
"ExtendedExampleVirt::run_bool()" 56 m.runExampleVirtVirtual(ex12p)
57 assert capture ==
"ExtendedExampleVirt::pure_virtual(): Hello world" 59 ex12p2 = ExtendedExampleVirt2(15)
61 assert m.runExampleVirt(ex12p2, 50) == 68
63 ExtendedExampleVirt::run(50), calling parent.. 64 Original implementation of ExampleVirt::run(state=17, value=51, str1=override1, str2=override2) 68 assert cstats.alive() == 3
69 del ex12, ex12p, ex12p2
70 assert cstats.alive() == 0
71 assert cstats.values() == [
'10',
'11',
'17']
72 assert cstats.copy_constructions == 0
73 assert cstats.move_constructions >= 0
77 """`A` only initializes its trampoline class when we inherit from it 79 If we just create and use an A instance directly, the trampoline initialization is 80 bypassed and we only initialize an A() instead (for performance reasons). 84 super(B, self).__init__()
87 print(
"In python f()")
95 assert capture ==
"A.f()" 103 assert capture ==
""" 112 """`A2`, unlike the above, is configured to always initialize the alias 114 While the extra initialization and extra class layer has small virtual dispatch 115 performance penalty, it also allows us to do more things with the trampoline 116 class such as defining local variables and performing construction/destruction. 120 super(B2, self).__init__()
123 print(
"In python B2.f()")
135 assert capture ==
""" 152 assert capture ==
""" 162 @pytest.unsupported_on_pypy
163 @pytest.mark.skipif(
not hasattr(m,
"NCVirt"), reason=
"NCVirt test broken on ICPC")
165 class NCVirtExt(m.NCVirt):
166 def get_noncopyable(self, a, b):
168 nc = m.NonCopyable(a * a, b * b)
171 def get_movable(self, a, b):
173 self.movable = m.Movable(a, b)
176 class NCVirtExt2(m.NCVirt):
177 def get_noncopyable(self, a, b):
179 self.nc = m.NonCopyable(a, b)
182 def get_movable(self, a, b):
184 return m.Movable(a, b)
187 assert ncv1.print_nc(2, 3) ==
"36" 188 assert ncv1.print_movable(4, 5) ==
"9" 190 assert ncv2.print_movable(7, 7) ==
"14" 192 with pytest.raises(RuntimeError):
197 assert nc_stats.alive() == 1
198 assert mv_stats.alive() == 1
200 assert nc_stats.alive() == 0
201 assert mv_stats.alive() == 0
202 assert nc_stats.values() == [
'4',
'9',
'9',
'9']
203 assert mv_stats.values() == [
'4',
'5',
'7',
'7']
204 assert nc_stats.copy_constructions == 0
205 assert mv_stats.copy_constructions == 1
206 assert nc_stats.move_constructions >= 0
207 assert mv_stats.move_constructions >= 0
211 """#159: virtual function dispatch has problems with similar-named functions""" 212 class PyClass1(m.DispatchIssue):
216 class PyClass2(m.DispatchIssue):
218 with pytest.raises(RuntimeError)
as excinfo:
219 super(PyClass2, self).dispatch()
220 assert msg(excinfo.value) ==
'Tried to call pure virtual function "Base::dispatch"' 223 return m.dispatch_issue_go(p)
226 assert m.dispatch_issue_go(b) ==
"Yay.." 230 """#392/397: overriding reference-returning functions""" 231 o = m.OverrideTest(
"asdf")
236 assert o.str_value() ==
"asdf" 238 assert o.A_value().value ==
"hi" 240 assert a.value ==
"hi" 242 assert a.value ==
"bye" 246 class AR(m.A_Repeat):
247 def unlucky_number(self):
251 def unlucky_number(self):
255 assert obj.say_something(3) ==
"hihihi" 256 assert obj.unlucky_number() == 99
257 assert obj.say_everything() ==
"hi 99" 260 assert obj.say_something(3) ==
"hihihi" 261 assert obj.unlucky_number() == 999
262 assert obj.say_everything() ==
"hi 999" 264 for obj
in [m.B_Repeat(), m.B_Tpl()]:
265 assert obj.say_something(3) ==
"B says hi 3 times" 266 assert obj.unlucky_number() == 13
267 assert obj.lucky_number() == 7.0
268 assert obj.say_everything() ==
"B says hi 1 times 13" 270 for obj
in [m.C_Repeat(), m.C_Tpl()]:
271 assert obj.say_something(3) ==
"B says hi 3 times" 272 assert obj.unlucky_number() == 4444
273 assert obj.lucky_number() == 888.0
274 assert obj.say_everything() ==
"B says hi 1 times 4444" 276 class CR(m.C_Repeat):
277 def lucky_number(self):
278 return m.C_Repeat.lucky_number(self) + 1.25
281 assert obj.say_something(3) ==
"B says hi 3 times" 282 assert obj.unlucky_number() == 4444
283 assert obj.lucky_number() == 889.25
284 assert obj.say_everything() ==
"B says hi 1 times 4444" 290 assert obj.say_something(3) ==
"B says hi 3 times" 291 assert obj.unlucky_number() == 4444
292 assert obj.lucky_number() == 888.0
293 assert obj.say_everything() ==
"B says hi 1 times 4444" 296 def lucky_number(self):
297 return CR.lucky_number(self) * 10
300 assert obj.say_something(3) ==
"B says hi 3 times" 301 assert obj.unlucky_number() == 4444
302 assert obj.lucky_number() == 8892.5
303 assert obj.say_everything() ==
"B says hi 1 times 4444" 306 def lucky_number(self):
307 return CT.lucky_number(self) * 1000
310 assert obj.say_something(3) ==
"B says hi 3 times" 311 assert obj.unlucky_number() == 4444
312 assert obj.lucky_number() == 888000.0
313 assert obj.say_everything() ==
"B says hi 1 times 4444" 315 class DR(m.D_Repeat):
316 def unlucky_number(self):
319 def lucky_number(self):
322 for obj
in [m.D_Repeat(), m.D_Tpl()]:
323 assert obj.say_something(3) ==
"B says hi 3 times" 324 assert obj.unlucky_number() == 4444
325 assert obj.lucky_number() == 888.0
326 assert obj.say_everything() ==
"B says hi 1 times 4444" 329 assert obj.say_something(3) ==
"B says hi 3 times" 330 assert obj.unlucky_number() == 123
331 assert obj.lucky_number() == 42.0
332 assert obj.say_everything() ==
"B says hi 1 times 123" 335 def say_something(self, times):
336 return "DT says:" + (
' quack' * times)
338 def unlucky_number(self):
341 def lucky_number(self):
345 assert obj.say_something(3) ==
"DT says: quack quack quack" 346 assert obj.unlucky_number() == 1234
347 assert obj.lucky_number() == -4.25
348 assert obj.say_everything() ==
"DT says: quack 1234" 351 def say_something(self, times):
352 return "DT2: " + (
'QUACK' * times)
354 def unlucky_number(self):
358 def say_something(self, times):
361 def unlucky_number(self):
364 def lucky_number(self):
368 assert obj.say_something(3) ==
"BTBTBT" 369 assert obj.unlucky_number() == -7
370 assert obj.lucky_number() == -1.375
371 assert obj.say_everything() ==
"BT -7" 377 m.test_gil_from_thread()
def test_alias_delay_initialization1(capture)
static ConstructorStats & get(std::type_index type)
def test_alias_delay_initialization2(capture)
def test_dispatch_issue(msg)
def test_inherited_virtuals()
def test_override(capture, msg)
void print(Args &&...args)
bool hasattr(handle obj, handle name)