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
|
— codex on behalf of Ralf — Release the caster for a rejected variant alternative before using a fallback. When an alternative declares Generated by Codex. Ralf has yet to review these findings. |
Assisted-by: ClaudeCode:claude-fable-5-2
|
🤖 AI text below 🤖 Rejected variant alternative keeps its caster: confirmed and fixed in 41fb2c6.
The other container casters do not have this problem. If an element fails to load there, the full argument load fails and the container caster is destroyed. |
🤖 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/