|
1 | 1 | import pytest
|
| 2 | +import sys |
| 3 | +import weakref |
2 | 4 |
|
3 | 5 | from pybind11_tests import class_ as m
|
4 | 6 | from pybind11_tests import UserType, ConstructorStats
|
@@ -289,3 +291,40 @@ def test_aligned():
|
289 | 291 | if hasattr(m, "Aligned"):
|
290 | 292 | p = m.Aligned().ptr()
|
291 | 293 | assert p % 1024 == 0
|
| 294 | + |
| 295 | + |
| 296 | +@pytest.unsupported_on_pypy |
| 297 | +@pytest.mark.skipif(sys.platform != "linux", reason="only runs on Linux") |
| 298 | +@pytest.mark.skipif(sys.version_info < (3, 0) or "d" in sys.abiflags, |
| 299 | + reason="requires python3 in release mode") |
| 300 | +def test_1922(): |
| 301 | + # Test #1922 (drake#11424). |
| 302 | + # Define a derived class which *does not* overload the method. |
| 303 | + # WARNING: The reproduction of this failure may be platform-specific, and |
| 304 | + # seems to depend on the order of definition and/or the name of the classes |
| 305 | + # defined. For example, trying to place this and the C++ code in |
| 306 | + # `test_virtual_functions` makes `assert id_1 == id_2` below fail. |
| 307 | + class Child1(m.ExampleVirt2): |
| 308 | + pass |
| 309 | + |
| 310 | + id_1 = id(Child1) |
| 311 | + assert m.example_virt2_get_name(m.ExampleVirt2()) == "ExampleVirt2" |
| 312 | + assert m.example_virt2_get_name(Child1()) == "ExampleVirt2" |
| 313 | + |
| 314 | + # Now delete everything (and ensure it's deleted). |
| 315 | + wref = weakref.ref(Child1) |
| 316 | + del Child1 |
| 317 | + pytest.gc_collect() |
| 318 | + assert wref() is None |
| 319 | + |
| 320 | + # Define a derived class which *does* define an overload. |
| 321 | + class Child2(m.ExampleVirt2): |
| 322 | + def get_name(self): |
| 323 | + return "Child2" |
| 324 | + |
| 325 | + id_2 = id(Child2) |
| 326 | + assert id_1 == id_2 # This happens in CPython; not sure about PyPy. |
| 327 | + assert m.example_virt2_get_name(m.ExampleVirt2()) == "ExampleVirt2" |
| 328 | + # THIS WILL FAIL: This is using the cached `ExampleVirt2.get_name`, rather |
| 329 | + # than re-inspect the Python dictionary. |
| 330 | + assert m.example_virt2_get_name(Child2()) == "Child2" |
0 commit comments