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) 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( 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