Skip to content

Commit

Permalink
LOG(WARNING) FALL BACK TO PROTOBUF SERIALIZE/PARSE based on `Exte…
Browse files Browse the repository at this point in the history
…nsionsWithUnknownFieldsPolicy`.

PiperOrigin-RevId: 604127759
  • Loading branch information
Ralf W. Grosse-Kunstleve authored and copybara-github committed Feb 4, 2024
1 parent 3b11990 commit 598d772
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pybind11_protobuf/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 1 addition & 4 deletions pybind11_protobuf/check_unknown_fields.cc
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ void AllowUnknownFieldsFor(absl::string_view top_message_descriptor_full_name,

std::optional<std::string> 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)) {
Expand All @@ -193,9 +193,6 @@ std::optional<std::string> 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();
}

Expand Down
2 changes: 1 addition & 1 deletion pybind11_protobuf/check_unknown_fields.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void AllowUnknownFieldsFor(absl::string_view top_message_descriptor_full_name,

std::optional<std::string> 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

Expand Down
14 changes: 9 additions & 5 deletions pybind11_protobuf/proto_cast_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <iostream>
#include <memory>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -828,14 +829,17 @@ py::handle GenericProtoCast(Message* src, py::return_value_policy policy,

std::optional<std::string> 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<std::string>();
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);
}

Expand Down

0 comments on commit 598d772

Please sign in to comment.