From 132cb30b2a05cb3927c9dc132c74fd0f7cdac0e0 Mon Sep 17 00:00:00 2001 From: Corentin Le Molgat Date: Thu, 19 Mar 2026 10:22:42 +0100 Subject: [PATCH 1/3] util: fix python wrappers build --- ortools/sat/python/wrappers.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ortools/sat/python/wrappers.cc b/ortools/sat/python/wrappers.cc index 4e2acfbf417..351dc289187 100644 --- a/ortools/sat/python/wrappers.cc +++ b/ortools/sat/python/wrappers.cc @@ -344,7 +344,7 @@ class Generator { .def_property_readonly( "$0", []($1 self) { return self->mutable_$2(); }, - py::return_value_policy::reference, py::keep_alive<0, 1>()))", + py::return_value_policy::reference_internal))", field.name(), current_context_.self_mutable_name, field.name()); // We'll need to generate the wrapping for // `google::protobuf::RepeatedPtrField<$3>`. @@ -357,7 +357,7 @@ class Generator { .def_property_readonly( "$0", []($1 self) { return self->mutable_$0(); }, - py::return_value_policy::reference, py::keep_alive<0, 1>()))", + py::return_value_policy::reference_internal))", field.name(), current_context_.self_mutable_name); // We'll need to generate the wrapping for From 9a54d26d7c57aef380f41993a851e4ae2f351e9c Mon Sep 17 00:00:00 2001 From: cielavenir Date: Mon, 25 May 2026 19:48:50 +0900 Subject: [PATCH 2/3] Make sure proto_builder_pybind11.h is regenerated after gen_proto_builder_pybind11 is rebuilt --- ortools/sat/python/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/ortools/sat/python/CMakeLists.txt b/ortools/sat/python/CMakeLists.txt index c42f3e58446..e0d889a4785 100644 --- a/ortools/sat/python/CMakeLists.txt +++ b/ortools/sat/python/CMakeLists.txt @@ -59,6 +59,7 @@ add_custom_command( OUTPUT proto_builder_pybind11.h COMMAND gen_proto_builder_pybind11 > proto_builder_pybind11.h COMMENT "Generate C++ proto_builder_pybind11.h" + DEPENDS ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/gen_proto_builder_pybind11 VERBATIM) pybind11_add_module(cp_model_helper_pybind11 MODULE cp_model_helper.cc proto_builder_pybind11.h) From 6d1ece7ad19ec8d82dc30bb9180638b65ae8f96a Mon Sep 17 00:00:00 2001 From: cielavenir Date: Mon, 25 May 2026 19:49:29 +0900 Subject: [PATCH 3/3] Wrap py::keep_alive<1, 0> with py::cpp_function --- ortools/sat/python/cp_model_helper.cc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ortools/sat/python/cp_model_helper.cc b/ortools/sat/python/cp_model_helper.cc index 822d6f7392d..b442ebb9a27 100644 --- a/ortools/sat/python/cp_model_helper.cc +++ b/ortools/sat/python/cp_model_helper.cc @@ -1754,8 +1754,8 @@ PYBIND11_MODULE(cp_model_helper, m) { .def(py::init, int>()) .def(py::init>()) // new variable. .def_property_readonly( - "proto", &IntVar::proto, py::return_value_policy::reference, - py::keep_alive<1, 0>(), + "proto", py::cpp_function(&IntVar::proto, py::return_value_policy::reference, + py::keep_alive<1, 0>()), "Returns the IntegerVariableProto of this variable.") .def_property_readonly("model_proto", &IntVar::model_proto, "Returns the CP model protobuf") @@ -2000,9 +2000,9 @@ PYBIND11_MODULE(cp_model_helper, m) { "Returns the index of the constraint in the model protobuf.") .def_property_readonly("model_proto", &Constraint::model_proto, "Returns the model protobuf.") - .def_property_readonly("proto", &Constraint::proto, + .def_property_readonly("proto", py::cpp_function(&Constraint::proto, py::return_value_policy::reference, - py::keep_alive<1, 0>(), + py::keep_alive<1, 0>()), "Returns the ConstraintProto of this constraint.") .def_property("name", &Constraint::name, &Constraint::SetName, "The name of the constraint.") @@ -2118,8 +2118,8 @@ intervals into the schedule. .def_property_readonly("model_proto", &IntervalVar::model_proto, "Returns the model protobuf.") .def_property_readonly( - "proto", &IntervalVar::proto, py::return_value_policy::reference, - py::keep_alive<1, 0>(), "Returns the interval constraint protobuf.") + "proto", py::cpp_function(&IntervalVar::proto, py::return_value_policy::reference, + py::keep_alive<1, 0>()), "Returns the interval constraint protobuf.") .def_property("name", &IntervalVar::name, &IntervalVar::SetName, "The name of the interval variable.") .def(