Skip to content

Commit af796d0

Browse files
Don't allow keep_alive or call_guard on properties (#5533)
* Don't allow keep_alive or call_guard on properties The def_property family blindly ignore the keep_alive and call_guard arguments passed to them making them confusing to use. This adds a static_assert if either is passed to make it clear it doesn't work. I would prefer this to be a compiler warning but I can't find a way to do that. Is that even possible? * style: pre-commit fixes * Re-run tests --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 42cda75 commit af796d0

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

include/pybind11/attr.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,12 @@ struct process_attributes {
702702
}
703703
};
704704

705+
template <typename T>
706+
struct is_keep_alive : std::false_type {};
707+
708+
template <size_t Nurse, size_t Patient>
709+
struct is_keep_alive<keep_alive<Nurse, Patient>> : std::true_type {};
710+
705711
template <typename T>
706712
using is_call_guard = is_instantiation<call_guard, T>;
707713

include/pybind11/pybind11.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2430,6 +2430,12 @@ class class_ : public detail::generic_type {
24302430
const Extra &...extra) {
24312431
static_assert(0 == detail::constexpr_sum(std::is_base_of<arg, Extra>::value...),
24322432
"Argument annotations are not allowed for properties");
2433+
static_assert(0 == detail::constexpr_sum(detail::is_call_guard<Extra>::value...),
2434+
"def_property family does not currently support call_guard. Use a "
2435+
"py::cpp_function instead.");
2436+
static_assert(0 == detail::constexpr_sum(detail::is_keep_alive<Extra>::value...),
2437+
"def_property family does not currently support keep_alive. Use a "
2438+
"py::cpp_function instead.");
24332439
auto rec_fget = get_function_record(fget), rec_fset = get_function_record(fset);
24342440
auto *rec_active = rec_fget;
24352441
if (rec_fget) {

0 commit comments

Comments
 (0)