[BUG]: keep_alive does not applied to properties
jnooree opened this issue · comments
Required prerequisites
- Make sure you've read the documentation. Your issue may be addressed there.
- Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
- Consider asking first in the Gitter chat room or in a Discussion.
What version (or hash if on master) of pybind11 are you using?
2.10.4, but 8b48ff8 (the current master) still has the same problem.
Problem description
The problem
Passing keep_alive
directly to def_property*
family is ignored. The flow:
- The getter/setters are created without "extra" attributes. Related source locations:
pybind11/include/pybind11/pybind11.h
Line 1750 in 8b48ff8
pybind11/include/pybind11/pybind11.h
Line 1766 in 8b48ff8
pybind11/include/pybind11/pybind11.h
Lines 1782 to 1783 in 8b48ff8
pybind11/include/pybind11/pybind11.h
Lines 1790 to 1794 in 8b48ff8
pybind11/include/pybind11/pybind11.h
Lines 1812 to 1813 in 8b48ff8
- All the calls to
def_properties*
eventually forwarded todef_property_static
, and "extra" attributes are processed there, withprocess_attributes<Extra...>::init
:
pybind11/include/pybind11/pybind11.h
Lines 1816 to 1848 in 8b48ff8
- ... which in turn calls
process_attribute<keep_alive<>>::init
:
pybind11/include/pybind11/attr.h
Lines 644 to 650 in 8b48ff8
- Unfortunately, the
process_attribute<keep_alive<>>::init
is empty.
pybind11/include/pybind11/attr.h
Lines 619 to 639 in 8b48ff8
Possible solution
Just blindly pass "extra" attributes to the cpp_function
constructor. I haven't yet analyzed possible side effects of this solution.
Workaround
keep_alive
works when it is passed to the cpp_function
constructor directly. If the maintainers choose not to update implementations, this (confusing) behavior should be documented IMO.
Related issues
Reproducible example code
No response
Is this a regression? Put the last known working version here if it is.
Not a regression