Skip to content

Commit 59113a1

Browse files
Add unittest checking drake#11424
Also disable boost
1 parent c50f90e commit 59113a1

File tree

3 files changed

+64
-15
lines changed

3 files changed

+64
-15
lines changed

tests/CMakeLists.txt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -222,21 +222,21 @@ if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1)
222222
endif()
223223

224224
# Optional dependency for some tests (boost::variant is only supported with version >= 1.56)
225-
find_package(Boost 1.56)
226-
227-
if(Boost_FOUND)
228-
if(NOT TARGET Boost::headers)
229-
if(TARGET Boost::boost)
230-
# Classic FindBoost
231-
add_library(Boost::headers ALIAS Boost::boost)
232-
else()
233-
# Very old FindBoost, or newer Boost than CMake in older CMakes
234-
add_library(Boost::headers IMPORTED INTERFACE)
235-
set_property(TARGET Boost::headers PROPERTY INTERFACE_INCLUDE_DIRECTORIES
236-
${Boost_INCLUDE_DIRS})
237-
endif()
238-
endif()
239-
endif()
225+
# find_package(Boost 1.56)
226+
227+
# if(Boost_FOUND)
228+
# if(NOT TARGET Boost::headers)
229+
# if(TARGET Boost::boost)
230+
# # Classic FindBoost
231+
# add_library(Boost::headers ALIAS Boost::boost)
232+
# else()
233+
# # Very old FindBoost, or newer Boost than CMake in older CMakes
234+
# add_library(Boost::headers IMPORTED INTERFACE)
235+
# set_property(TARGET Boost::headers PROPERTY INTERFACE_INCLUDE_DIRECTORIES
236+
# ${Boost_INCLUDE_DIRS})
237+
# endif()
238+
# endif()
239+
# endif()
240240

241241
# Compile with compiler warnings turned on
242242
function(pybind11_enable_warnings target_name)

tests/test_class.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,24 @@ TEST_SUBMODULE(class_, m) {
466466
py::class_<OtherDuplicateNested>(gt, "OtherDuplicateNested");
467467
py::class_<OtherDuplicateNested>(gt, "YetAnotherDuplicateNested");
468468
});
469+
470+
// Test drake#11424.
471+
class ExampleVirt2 {
472+
public:
473+
virtual ~ExampleVirt2() {}
474+
virtual std::string get_name() const { return "ExampleVirt2"; }
475+
};
476+
class PyExampleVirt2 : public ExampleVirt2 {
477+
public:
478+
std::string get_name() const override {
479+
PYBIND11_OVERLOAD(std::string, ExampleVirt2, get_name, );
480+
}
481+
};
482+
py::class_<ExampleVirt2, PyExampleVirt2>(m, "ExampleVirt2")
483+
.def(py::init())
484+
.def("get_name", &ExampleVirt2::get_name);
485+
m.def("example_virt2_get_name",
486+
[](const ExampleVirt2& obj) { return obj.get_name(); });
469487
}
470488

471489
template <int N> class BreaksBase { public:

tests/test_class.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22
import pytest
3+
import weakref
34

45
import env # noqa: F401
56

@@ -465,3 +466,33 @@ class ClassScope:
465466
m.register_duplicate_nested_class_type(ClassScope)
466467
expected = 'generic_type: type "YetAnotherDuplicateNested" is already registered!'
467468
assert str(exc_info.value) == expected
469+
470+
471+
def test_drake_11424():
472+
# Define a derived class which *does not* overload the method.
473+
# WARNING: The reproduction of this failure may be platform-specific, and
474+
# seems to depend on the order of definition and/or the name of the classes
475+
# defined. For example, trying to place this and the C++ code in
476+
# `test_virtual_functions` makes `assert id_1 == id_2` below fail.
477+
class Child1(m.ExampleVirt2): pass
478+
479+
id_1 = id(Child1)
480+
assert m.example_virt2_get_name(m.ExampleVirt2()) == "ExampleVirt2"
481+
assert m.example_virt2_get_name(Child1()) == "ExampleVirt2"
482+
483+
# Now delete everything (and ensure it's deleted).
484+
wref = weakref.ref(Child1)
485+
del Child1
486+
pytest.gc_collect()
487+
assert wref() == None
488+
489+
# Define a derived class which *does* define an overload.
490+
class Child2(m.ExampleVirt2):
491+
def get_name(self): return "Child2"
492+
493+
id_2 = id(Child2)
494+
assert id_1 == id_2 # This happens in CPython; not sure about PyPy.
495+
assert m.example_virt2_get_name(m.ExampleVirt2()) == "ExampleVirt2"
496+
# THIS WILL FAIL: This is using the cached `ExampleVirt2.get_name`, rather
497+
# than re-inspect the Python dictionary.
498+
assert m.example_virt2_get_name(Child2()) == "Child2"

0 commit comments

Comments
 (0)