Henry Schreiner | d8c7ee0 | 2020-07-20 13:35:21 -0400 | [diff] [blame^] | 1 | # -*- coding: utf-8 -*- |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 2 | import pytest |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 3 | |
| 4 | from pybind11_tests import virtual_functions as m |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 5 | from pybind11_tests import ConstructorStats |
| 6 | |
| 7 | |
| 8 | def test_override(capture, msg): |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 9 | class ExtendedExampleVirt(m.ExampleVirt): |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 10 | def __init__(self, state): |
| 11 | super(ExtendedExampleVirt, self).__init__(state + 1) |
| 12 | self.data = "Hello world" |
| 13 | |
| 14 | def run(self, value): |
| 15 | print('ExtendedExampleVirt::run(%i), calling parent..' % value) |
| 16 | return super(ExtendedExampleVirt, self).run(value + 1) |
| 17 | |
| 18 | def run_bool(self): |
| 19 | print('ExtendedExampleVirt::run_bool()') |
| 20 | return False |
| 21 | |
Jason Rhinelander | 7dfb932 | 2016-09-08 14:49:43 -0400 | [diff] [blame] | 22 | def get_string1(self): |
| 23 | return "override1" |
| 24 | |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 25 | def pure_virtual(self): |
| 26 | print('ExtendedExampleVirt::pure_virtual(): %s' % self.data) |
| 27 | |
Jason Rhinelander | 7dfb932 | 2016-09-08 14:49:43 -0400 | [diff] [blame] | 28 | class ExtendedExampleVirt2(ExtendedExampleVirt): |
| 29 | def __init__(self, state): |
| 30 | super(ExtendedExampleVirt2, self).__init__(state + 1) |
| 31 | |
| 32 | def get_string2(self): |
| 33 | return "override2" |
| 34 | |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 35 | ex12 = m.ExampleVirt(10) |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 36 | with capture: |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 37 | assert m.runExampleVirt(ex12, 20) == 30 |
Dean Moldovan | 76e993a | 2016-12-13 00:59:28 +0100 | [diff] [blame] | 38 | assert capture == """ |
| 39 | Original implementation of ExampleVirt::run(state=10, value=20, str1=default1, str2=default2) |
| 40 | """ # noqa: E501 line too long |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 41 | |
| 42 | with pytest.raises(RuntimeError) as excinfo: |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 43 | m.runExampleVirtVirtual(ex12) |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 44 | assert msg(excinfo.value) == 'Tried to call pure virtual function "ExampleVirt::pure_virtual"' |
| 45 | |
| 46 | ex12p = ExtendedExampleVirt(10) |
| 47 | with capture: |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 48 | assert m.runExampleVirt(ex12p, 20) == 32 |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 49 | assert capture == """ |
| 50 | ExtendedExampleVirt::run(20), calling parent.. |
Jason Rhinelander | 7dfb932 | 2016-09-08 14:49:43 -0400 | [diff] [blame] | 51 | Original implementation of ExampleVirt::run(state=11, value=21, str1=override1, str2=default2) |
Dean Moldovan | 76e993a | 2016-12-13 00:59:28 +0100 | [diff] [blame] | 52 | """ # noqa: E501 line too long |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 53 | with capture: |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 54 | assert m.runExampleVirtBool(ex12p) is False |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 55 | assert capture == "ExtendedExampleVirt::run_bool()" |
| 56 | with capture: |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 57 | m.runExampleVirtVirtual(ex12p) |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 58 | assert capture == "ExtendedExampleVirt::pure_virtual(): Hello world" |
| 59 | |
Jason Rhinelander | 7dfb932 | 2016-09-08 14:49:43 -0400 | [diff] [blame] | 60 | ex12p2 = ExtendedExampleVirt2(15) |
| 61 | with capture: |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 62 | assert m.runExampleVirt(ex12p2, 50) == 68 |
Jason Rhinelander | 7dfb932 | 2016-09-08 14:49:43 -0400 | [diff] [blame] | 63 | assert capture == """ |
| 64 | ExtendedExampleVirt::run(50), calling parent.. |
| 65 | Original implementation of ExampleVirt::run(state=17, value=51, str1=override1, str2=override2) |
Dean Moldovan | 76e993a | 2016-12-13 00:59:28 +0100 | [diff] [blame] | 66 | """ # noqa: E501 line too long |
Jason Rhinelander | 7dfb932 | 2016-09-08 14:49:43 -0400 | [diff] [blame] | 67 | |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 68 | cstats = ConstructorStats.get(m.ExampleVirt) |
Jason Rhinelander | 7dfb932 | 2016-09-08 14:49:43 -0400 | [diff] [blame] | 69 | assert cstats.alive() == 3 |
| 70 | del ex12, ex12p, ex12p2 |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 71 | assert cstats.alive() == 0 |
Jason Rhinelander | 7dfb932 | 2016-09-08 14:49:43 -0400 | [diff] [blame] | 72 | assert cstats.values() == ['10', '11', '17'] |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 73 | assert cstats.copy_constructions == 0 |
| 74 | assert cstats.move_constructions >= 0 |
| 75 | |
| 76 | |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 77 | def test_alias_delay_initialization1(capture): |
| 78 | """`A` only initializes its trampoline class when we inherit from it |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 79 | |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 80 | If we just create and use an A instance directly, the trampoline initialization is |
| 81 | bypassed and we only initialize an A() instead (for performance reasons). |
| 82 | """ |
| 83 | class B(m.A): |
| 84 | def __init__(self): |
| 85 | super(B, self).__init__() |
| 86 | |
| 87 | def f(self): |
| 88 | print("In python f()") |
| 89 | |
| 90 | # C++ version |
| 91 | with capture: |
| 92 | a = m.A() |
| 93 | m.call_f(a) |
| 94 | del a |
| 95 | pytest.gc_collect() |
| 96 | assert capture == "A.f()" |
| 97 | |
| 98 | # Python version |
| 99 | with capture: |
| 100 | b = B() |
| 101 | m.call_f(b) |
| 102 | del b |
| 103 | pytest.gc_collect() |
| 104 | assert capture == """ |
| 105 | PyA.PyA() |
| 106 | PyA.f() |
| 107 | In python f() |
| 108 | PyA.~PyA() |
| 109 | """ |
| 110 | |
| 111 | |
| 112 | def test_alias_delay_initialization2(capture): |
| 113 | """`A2`, unlike the above, is configured to always initialize the alias |
| 114 | |
| 115 | While the extra initialization and extra class layer has small virtual dispatch |
| 116 | performance penalty, it also allows us to do more things with the trampoline |
| 117 | class such as defining local variables and performing construction/destruction. |
| 118 | """ |
| 119 | class B2(m.A2): |
| 120 | def __init__(self): |
| 121 | super(B2, self).__init__() |
| 122 | |
| 123 | def f(self): |
| 124 | print("In python B2.f()") |
| 125 | |
| 126 | # No python subclass version |
| 127 | with capture: |
| 128 | a2 = m.A2() |
| 129 | m.call_f(a2) |
| 130 | del a2 |
| 131 | pytest.gc_collect() |
Jason Rhinelander | 464d989 | 2017-06-12 21:52:48 -0400 | [diff] [blame] | 132 | a3 = m.A2(1) |
| 133 | m.call_f(a3) |
| 134 | del a3 |
| 135 | pytest.gc_collect() |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 136 | assert capture == """ |
| 137 | PyA2.PyA2() |
| 138 | PyA2.f() |
| 139 | A2.f() |
| 140 | PyA2.~PyA2() |
Jason Rhinelander | 464d989 | 2017-06-12 21:52:48 -0400 | [diff] [blame] | 141 | PyA2.PyA2() |
| 142 | PyA2.f() |
| 143 | A2.f() |
| 144 | PyA2.~PyA2() |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 145 | """ |
| 146 | |
| 147 | # Python subclass version |
| 148 | with capture: |
| 149 | b2 = B2() |
| 150 | m.call_f(b2) |
| 151 | del b2 |
| 152 | pytest.gc_collect() |
| 153 | assert capture == """ |
| 154 | PyA2.PyA2() |
| 155 | PyA2.f() |
| 156 | In python B2.f() |
| 157 | PyA2.~PyA2() |
| 158 | """ |
| 159 | |
| 160 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 161 | # PyPy: Reference count > 1 causes call with noncopyable instance |
| 162 | # to fail in ncv1.print_nc() |
| 163 | @pytest.unsupported_on_pypy |
| 164 | @pytest.mark.skipif(not hasattr(m, "NCVirt"), reason="NCVirt test broken on ICPC") |
| 165 | def test_move_support(): |
| 166 | class NCVirtExt(m.NCVirt): |
| 167 | def get_noncopyable(self, a, b): |
| 168 | # Constructs and returns a new instance: |
| 169 | nc = m.NonCopyable(a * a, b * b) |
| 170 | return nc |
| 171 | |
| 172 | def get_movable(self, a, b): |
| 173 | # Return a referenced copy |
| 174 | self.movable = m.Movable(a, b) |
| 175 | return self.movable |
| 176 | |
| 177 | class NCVirtExt2(m.NCVirt): |
| 178 | def get_noncopyable(self, a, b): |
| 179 | # Keep a reference: this is going to throw an exception |
| 180 | self.nc = m.NonCopyable(a, b) |
| 181 | return self.nc |
| 182 | |
| 183 | def get_movable(self, a, b): |
| 184 | # Return a new instance without storing it |
| 185 | return m.Movable(a, b) |
| 186 | |
| 187 | ncv1 = NCVirtExt() |
| 188 | assert ncv1.print_nc(2, 3) == "36" |
| 189 | assert ncv1.print_movable(4, 5) == "9" |
| 190 | ncv2 = NCVirtExt2() |
| 191 | assert ncv2.print_movable(7, 7) == "14" |
| 192 | # Don't check the exception message here because it differs under debug/non-debug mode |
| 193 | with pytest.raises(RuntimeError): |
| 194 | ncv2.print_nc(9, 9) |
| 195 | |
| 196 | nc_stats = ConstructorStats.get(m.NonCopyable) |
| 197 | mv_stats = ConstructorStats.get(m.Movable) |
| 198 | assert nc_stats.alive() == 1 |
| 199 | assert mv_stats.alive() == 1 |
| 200 | del ncv1, ncv2 |
| 201 | assert nc_stats.alive() == 0 |
| 202 | assert mv_stats.alive() == 0 |
| 203 | assert nc_stats.values() == ['4', '9', '9', '9'] |
| 204 | assert mv_stats.values() == ['4', '5', '7', '7'] |
| 205 | assert nc_stats.copy_constructions == 0 |
| 206 | assert mv_stats.copy_constructions == 1 |
| 207 | assert nc_stats.move_constructions >= 0 |
| 208 | assert mv_stats.move_constructions >= 0 |
| 209 | |
| 210 | |
| 211 | def test_dispatch_issue(msg): |
| 212 | """#159: virtual function dispatch has problems with similar-named functions""" |
| 213 | class PyClass1(m.DispatchIssue): |
| 214 | def dispatch(self): |
| 215 | return "Yay.." |
| 216 | |
| 217 | class PyClass2(m.DispatchIssue): |
| 218 | def dispatch(self): |
| 219 | with pytest.raises(RuntimeError) as excinfo: |
| 220 | super(PyClass2, self).dispatch() |
| 221 | assert msg(excinfo.value) == 'Tried to call pure virtual function "Base::dispatch"' |
| 222 | |
| 223 | p = PyClass1() |
| 224 | return m.dispatch_issue_go(p) |
| 225 | |
| 226 | b = PyClass2() |
| 227 | assert m.dispatch_issue_go(b) == "Yay.." |
| 228 | |
| 229 | |
| 230 | def test_override_ref(): |
Unknown | 0b3f44e | 2017-11-01 21:08:06 -0400 | [diff] [blame] | 231 | """#392/397: overriding reference-returning functions""" |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 232 | o = m.OverrideTest("asdf") |
| 233 | |
| 234 | # Not allowed (see associated .cpp comment) |
| 235 | # i = o.str_ref() |
| 236 | # assert o.str_ref() == "asdf" |
| 237 | assert o.str_value() == "asdf" |
| 238 | |
| 239 | assert o.A_value().value == "hi" |
| 240 | a = o.A_ref() |
| 241 | assert a.value == "hi" |
| 242 | a.value = "bye" |
| 243 | assert a.value == "bye" |
| 244 | |
| 245 | |
| 246 | def test_inherited_virtuals(): |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 247 | class AR(m.A_Repeat): |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 248 | def unlucky_number(self): |
| 249 | return 99 |
| 250 | |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 251 | class AT(m.A_Tpl): |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 252 | def unlucky_number(self): |
| 253 | return 999 |
| 254 | |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 255 | obj = AR() |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame] | 256 | assert obj.say_something(3) == "hihihi" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 257 | assert obj.unlucky_number() == 99 |
Jason Rhinelander | 2097826 | 2016-08-29 18:16:46 -0400 | [diff] [blame] | 258 | assert obj.say_everything() == "hi 99" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 259 | |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 260 | obj = AT() |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame] | 261 | assert obj.say_something(3) == "hihihi" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 262 | assert obj.unlucky_number() == 999 |
Jason Rhinelander | 2097826 | 2016-08-29 18:16:46 -0400 | [diff] [blame] | 263 | assert obj.say_everything() == "hi 999" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 264 | |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 265 | for obj in [m.B_Repeat(), m.B_Tpl()]: |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame] | 266 | assert obj.say_something(3) == "B says hi 3 times" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 267 | assert obj.unlucky_number() == 13 |
| 268 | assert obj.lucky_number() == 7.0 |
Jason Rhinelander | 2097826 | 2016-08-29 18:16:46 -0400 | [diff] [blame] | 269 | assert obj.say_everything() == "B says hi 1 times 13" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 270 | |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 271 | for obj in [m.C_Repeat(), m.C_Tpl()]: |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame] | 272 | assert obj.say_something(3) == "B says hi 3 times" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 273 | assert obj.unlucky_number() == 4444 |
| 274 | assert obj.lucky_number() == 888.0 |
Jason Rhinelander | 2097826 | 2016-08-29 18:16:46 -0400 | [diff] [blame] | 275 | assert obj.say_everything() == "B says hi 1 times 4444" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 276 | |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 277 | class CR(m.C_Repeat): |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 278 | def lucky_number(self): |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 279 | return m.C_Repeat.lucky_number(self) + 1.25 |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 280 | |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 281 | obj = CR() |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame] | 282 | assert obj.say_something(3) == "B says hi 3 times" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 283 | assert obj.unlucky_number() == 4444 |
| 284 | assert obj.lucky_number() == 889.25 |
Jason Rhinelander | 2097826 | 2016-08-29 18:16:46 -0400 | [diff] [blame] | 285 | assert obj.say_everything() == "B says hi 1 times 4444" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 286 | |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 287 | class CT(m.C_Tpl): |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 288 | pass |
| 289 | |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 290 | obj = CT() |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame] | 291 | assert obj.say_something(3) == "B says hi 3 times" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 292 | assert obj.unlucky_number() == 4444 |
| 293 | assert obj.lucky_number() == 888.0 |
Jason Rhinelander | 2097826 | 2016-08-29 18:16:46 -0400 | [diff] [blame] | 294 | assert obj.say_everything() == "B says hi 1 times 4444" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 295 | |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 296 | class CCR(CR): |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 297 | def lucky_number(self): |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 298 | return CR.lucky_number(self) * 10 |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 299 | |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 300 | obj = CCR() |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame] | 301 | assert obj.say_something(3) == "B says hi 3 times" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 302 | assert obj.unlucky_number() == 4444 |
| 303 | assert obj.lucky_number() == 8892.5 |
Jason Rhinelander | 2097826 | 2016-08-29 18:16:46 -0400 | [diff] [blame] | 304 | assert obj.say_everything() == "B says hi 1 times 4444" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 305 | |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 306 | class CCT(CT): |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 307 | def lucky_number(self): |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 308 | return CT.lucky_number(self) * 1000 |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 309 | |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 310 | obj = CCT() |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame] | 311 | assert obj.say_something(3) == "B says hi 3 times" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 312 | assert obj.unlucky_number() == 4444 |
| 313 | assert obj.lucky_number() == 888000.0 |
Jason Rhinelander | 2097826 | 2016-08-29 18:16:46 -0400 | [diff] [blame] | 314 | assert obj.say_everything() == "B says hi 1 times 4444" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 315 | |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 316 | class DR(m.D_Repeat): |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 317 | def unlucky_number(self): |
| 318 | return 123 |
| 319 | |
| 320 | def lucky_number(self): |
| 321 | return 42.0 |
| 322 | |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 323 | for obj in [m.D_Repeat(), m.D_Tpl()]: |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame] | 324 | assert obj.say_something(3) == "B says hi 3 times" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 325 | assert obj.unlucky_number() == 4444 |
| 326 | assert obj.lucky_number() == 888.0 |
Jason Rhinelander | 2097826 | 2016-08-29 18:16:46 -0400 | [diff] [blame] | 327 | assert obj.say_everything() == "B says hi 1 times 4444" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 328 | |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 329 | obj = DR() |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame] | 330 | assert obj.say_something(3) == "B says hi 3 times" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 331 | assert obj.unlucky_number() == 123 |
| 332 | assert obj.lucky_number() == 42.0 |
Jason Rhinelander | 2097826 | 2016-08-29 18:16:46 -0400 | [diff] [blame] | 333 | assert obj.say_everything() == "B says hi 1 times 123" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 334 | |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 335 | class DT(m.D_Tpl): |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 336 | def say_something(self, times): |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 337 | return "DT says:" + (' quack' * times) |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 338 | |
| 339 | def unlucky_number(self): |
| 340 | return 1234 |
| 341 | |
| 342 | def lucky_number(self): |
| 343 | return -4.25 |
| 344 | |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 345 | obj = DT() |
| 346 | assert obj.say_something(3) == "DT says: quack quack quack" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 347 | assert obj.unlucky_number() == 1234 |
| 348 | assert obj.lucky_number() == -4.25 |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 349 | assert obj.say_everything() == "DT says: quack 1234" |
Jason Rhinelander | 2097826 | 2016-08-29 18:16:46 -0400 | [diff] [blame] | 350 | |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 351 | class DT2(DT): |
Jason Rhinelander | 2097826 | 2016-08-29 18:16:46 -0400 | [diff] [blame] | 352 | def say_something(self, times): |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 353 | return "DT2: " + ('QUACK' * times) |
Jason Rhinelander | 2097826 | 2016-08-29 18:16:46 -0400 | [diff] [blame] | 354 | |
| 355 | def unlucky_number(self): |
| 356 | return -3 |
| 357 | |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 358 | class BT(m.B_Tpl): |
Jason Rhinelander | 2097826 | 2016-08-29 18:16:46 -0400 | [diff] [blame] | 359 | def say_something(self, times): |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 360 | return "BT" * times |
| 361 | |
Jason Rhinelander | 2097826 | 2016-08-29 18:16:46 -0400 | [diff] [blame] | 362 | def unlucky_number(self): |
| 363 | return -7 |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 364 | |
Jason Rhinelander | 2097826 | 2016-08-29 18:16:46 -0400 | [diff] [blame] | 365 | def lucky_number(self): |
| 366 | return -1.375 |
| 367 | |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 368 | obj = BT() |
| 369 | assert obj.say_something(3) == "BTBTBT" |
Jason Rhinelander | 2097826 | 2016-08-29 18:16:46 -0400 | [diff] [blame] | 370 | assert obj.unlucky_number() == -7 |
| 371 | assert obj.lucky_number() == -1.375 |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 372 | assert obj.say_everything() == "BT -7" |
Wenzel Jakob | 44e39e0 | 2018-09-11 09:32:45 +0200 | [diff] [blame] | 373 | |
| 374 | |
| 375 | def test_issue_1454(): |
| 376 | # Fix issue #1454 (crash when acquiring/releasing GIL on another thread in Python 2.7) |
| 377 | m.test_gil() |
| 378 | m.test_gil_from_thread() |