mirror of
https://github.com/pybind/pybind11.git
synced 2026-05-19 12:30:31 +00:00
fix: avoid copy constructor instantiation in shared_ptr fallback cast (#6028)
* tests: add regressions for shared_ptr reference_internal fallback * fix: avoid copy constructor instantiation in shared_ptr fallback cast * Remove stray empty line * tests: rename PyTorch shared_ptr regression test files * refactor: add cast_non_owning helper for reference-like casts Name the non-owning generic cast path so callers do not have to rediscover that reference-like policies must pass null copy/move constructor callbacks. This keeps the shared_ptr reference_internal fallback self-documenting and points future maintainers toward the safe API. Made-with: Cursor * tests: guard deprecated-copy warning probes with __has_warning Use __has_warning for the Clang-only regression test so older compiler jobs skip unsupported warning groups instead of failing with -Wunknown-warning-option. A simple __clang_major__ >= 13 guard would be shorter, but it bakes in a version cutoff; __has_warning is slightly more verbose while being more robust to vendor builds, backports, and future packaging differences. Made-with: Cursor --------- Co-authored-by: Ralf W. Grosse-Kunstleve <rgrossekunst@nvidia.com>
This commit is contained in:
@@ -1027,7 +1027,7 @@ public:
|
||||
}
|
||||
|
||||
if (parent) {
|
||||
return type_caster_base<type>::cast(
|
||||
return type_caster_generic::cast_non_owning(
|
||||
srcs, return_value_policy::reference_internal, parent);
|
||||
}
|
||||
|
||||
|
||||
@@ -1004,6 +1004,18 @@ public:
|
||||
return cast(srcs, policy, parent, copy_constructor, move_constructor, existing_holder);
|
||||
}
|
||||
|
||||
static handle cast_non_owning(const cast_sources &srcs,
|
||||
return_value_policy policy,
|
||||
handle parent,
|
||||
const void *existing_holder = nullptr) {
|
||||
// Reference-like policies alias an existing C++ object instead of creating
|
||||
// a new one, so copy/move constructor callbacks must remain null here.
|
||||
assert(policy == return_value_policy::reference
|
||||
|| policy == return_value_policy::reference_internal
|
||||
|| policy == return_value_policy::automatic_reference);
|
||||
return cast(srcs, policy, parent, nullptr, nullptr, existing_holder);
|
||||
}
|
||||
|
||||
PYBIND11_NOINLINE static handle cast(const cast_sources &srcs,
|
||||
return_value_policy policy,
|
||||
handle parent,
|
||||
|
||||
Reference in New Issue
Block a user