From 598d772be566cc080bf9d0538c847cd28591de42 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Sun, 4 Feb 2024 11:30:58 -0800 Subject: [PATCH] `LOG(WARNING)` `FALL BACK TO PROTOBUF SERIALIZE/PARSE` based on `ExtensionsWithUnknownFieldsPolicy`. PiperOrigin-RevId: 604127759 --- pybind11_protobuf/BUILD | 2 +- pybind11_protobuf/check_unknown_fields.cc | 5 +---- pybind11_protobuf/check_unknown_fields.h | 2 +- pybind11_protobuf/proto_cast_util.cc | 14 +++++++++----- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/pybind11_protobuf/BUILD b/pybind11_protobuf/BUILD index deba58a..bd63d58 100644 --- a/pybind11_protobuf/BUILD +++ b/pybind11_protobuf/BUILD @@ -88,7 +88,7 @@ cc_library( srcs = ["check_unknown_fields.cc"], hdrs = ["check_unknown_fields.h"], visibility = [ - "//visibility:private", + "//third_party/clif/python:__pkg__", ], deps = [ "@com_google_absl//absl/container:flat_hash_map", diff --git a/pybind11_protobuf/check_unknown_fields.cc b/pybind11_protobuf/check_unknown_fields.cc index bb67d69..0639d09 100644 --- a/pybind11_protobuf/check_unknown_fields.cc +++ b/pybind11_protobuf/check_unknown_fields.cc @@ -183,7 +183,7 @@ void AllowUnknownFieldsFor(absl::string_view top_message_descriptor_full_name, std::optional CheckRecursively( const ::google::protobuf::python::PyProto_API* py_proto_api, - const ::google::protobuf::Message* message, bool build_error_message_if_any) { + const ::google::protobuf::Message* message) { const auto* root_descriptor = message->GetDescriptor(); HasUnknownFields search{py_proto_api, root_descriptor}; if (!search.FindUnknownFieldsRecursive(message, 0u)) { @@ -193,9 +193,6 @@ std::optional CheckRecursively( search.FieldFQN())) != 0) { return std::nullopt; } - if (!build_error_message_if_any) { - return ""; // This indicates that an unknown field was found. - } return search.BuildErrorMessage(); } diff --git a/pybind11_protobuf/check_unknown_fields.h b/pybind11_protobuf/check_unknown_fields.h index e37adc7..79ac001 100644 --- a/pybind11_protobuf/check_unknown_fields.h +++ b/pybind11_protobuf/check_unknown_fields.h @@ -47,7 +47,7 @@ void AllowUnknownFieldsFor(absl::string_view top_message_descriptor_full_name, std::optional CheckRecursively( const ::google::protobuf::python::PyProto_API* py_proto_api, - const ::google::protobuf::Message* top_message, bool build_error_message_if_any); + const ::google::protobuf::Message* top_message); } // namespace pybind11_protobuf::check_unknown_fields diff --git a/pybind11_protobuf/proto_cast_util.cc b/pybind11_protobuf/proto_cast_util.cc index 0f7ba29..064797a 100644 --- a/pybind11_protobuf/proto_cast_util.cc +++ b/pybind11_protobuf/proto_cast_util.cc @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -828,14 +829,17 @@ py::handle GenericProtoCast(Message* src, py::return_value_policy policy, std::optional unknown_field_message = check_unknown_fields::CheckRecursively( - GlobalState::instance()->py_proto_api(), src, - check_unknown_fields::ExtensionsWithUnknownFieldsPolicy:: - UnknownFieldsAreDisallowed()); + GlobalState::instance()->py_proto_api(), src); if (unknown_field_message) { - if (!unknown_field_message->empty()) { + if (check_unknown_fields::ExtensionsWithUnknownFieldsPolicy:: + UnknownFieldsAreDisallowed()) { throw py::value_error(*unknown_field_message); } - // Fall back to serialize/parse. + static auto fall_back_log_shown = new std::unordered_set(); + if (fall_back_log_shown->insert(*unknown_field_message).second) { + LOG(WARNING) << "FALL BACK TO PROTOBUF SERIALIZE/PARSE: " + << *unknown_field_message; + } return GenericPyProtoCast(src, policy, parent, is_const); }