Skip to content

Commit 1d48c78

Browse files
Ensure that we do not get a false-positive on the lowest type...
1 parent e1af185 commit 1d48c78

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

include/pybind11/cast.h

+13-5
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,8 @@ inline LoadType determine_load_type(handle src, const type_info* typeinfo,
526526
// (This is essentially the same as case 2b, but because not using multiple inheritance
527527
// is extremely common, we handle it specially to avoid the loop iterator and type
528528
// pointer lookup overhead)
529+
// TODO(eric.cousineau): This seems to also capture C++-registered classes as well, not just Python-derived
530+
// classes.
529531
if (bases.size() == 1 && (no_cpp_mi || bases.front()->type == typeinfo->type)) {
530532
return LoadType::DerivedCppSinglePySingle;
531533
}
@@ -1552,11 +1554,17 @@ struct copyable_holder_caster : public type_caster_base<type> {
15521554
// Go ahead and release ownership to C++, if able.
15531555
auto* py_type = (PyTypeObject*)src.get_type().ptr();
15541556
lowest_type = detail::get_type_info(py_type);
1555-
if (lowest_type->release_info.can_derive_from_wrapper) {
1556-
do_release_to_cpp = true;
1557-
} else {
1558-
std::cerr << "WARNING! Python-derived C++ instance will soon lose Python portion. "
1559-
"Consider having your base class extend from pybind11::wrapper<>." << std::endl;
1557+
// Double-check that we did not get along C++ inheritance.
1558+
// TODO(eric.cousineau): Generalize this to unique_ptr<> case as well.
1559+
const bool is_actually_pure_cpp = lowest_type->type == py_type;
1560+
if (!is_actually_pure_cpp) {
1561+
if (lowest_type->release_info.can_derive_from_wrapper) {
1562+
do_release_to_cpp = true;
1563+
} else {
1564+
std::cerr << "WARNING! Casting to std::shared_ptr<> will cause Python subclass of pybind11 C++ instance to lose its Python portion. "
1565+
"Make your base class extend from pybind11::wrapper<> to prevent aliasing."
1566+
<< std::endl;
1567+
}
15601568
}
15611569
}
15621570

0 commit comments

Comments
 (0)