Repository navigation
Conversation
A container caster destroyed the type caster of each element before the bound function ran. Elements that refer to data in their caster, such as the pointers in `std::vector<const char *>`, were then dangling. Add the `cast_refers_to_caster` trait. The container casters keep the element casters for the duration of the call only if the trait is true. A custom caster can opt in with a `refers_to_caster` member. Fixes pybind#2527, fixes pybind#2245. Assisted-by: ClaudeCode:claude-fable-5-2
The variant caster value-initialized the caster of each alternative before. With default initialization, GCC 16 reports -Wmaybe-uninitialized for a `std::nullptr_t` alternative. Assisted-by: ClaudeCode:claude-fable-5-2
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤖 AI text below 🤖
Description
Fixes #2527 and fixes #2245. Alternative to and closes #6280, and replaces -- closes #2303 and closes #2532.
A container caster destroyed the type caster of each element before the bound function ran. Elements that refer to data in their caster were then dangling. Examples are the pointers in
std::vector<const char *>andstd::vector<std::string *>.Changes:
detail::cast_refers_to_caster<T>. It is true for thecast_is_temporary_value_reference<T>cases, and for a caster that has astatic constexpr bool refers_to_caster = truemember.stl.h(list, array, set, map, optional, variant) keep their element casters in astd::listfor the duration of the call, but only if the trait is true. For other element types, the element caster stays a loop local, as before.tuple_casterand thereference_wrappercaster pass the flag up. Thus nested types such asstd::vector<std::vector<const char *>>work.Compared with #6280:
char *.const char *argument has no added cost.load()loop is not fixed unless it useselement_casters. Casters that derive from thestl.hcasters are fixed.Trade-offs:
py::cast<std::vector<const char *>>(obj)still dangles, because the caster is a temporary there.docs/limitations.rstnow says this. Astatic_assertis not added, because it would also rejectpy::cast<std::vector<PyObject *>>(), which works today.The tests include the cases from #6280. Tested locally with Apple Clang (C++17, full suite) and GCC 16 (C++11,
test_stland three related modules). The new test fails without the fix.Suggested changelog entry:
std::vector<const char *>orstd::vector<std::string *>) is an argument of a bound function. A custom type caster can opt in with arefers_to_castermember.📚 Documentation preview 📚: https://pybind11--6283.org.readthedocs.build/