--- a/pybind11_protobuf/BUILD +++ b/pybind11_protobuf/BUILD @@ -46,7 +46,6 @@ "@com_google_absl//absl/strings", "@com_google_absl//absl/types:optional", "@com_google_protobuf//:protobuf", - "@com_google_protobuf//python:proto_api", ], ) @@ -75,7 +74,6 @@ "@com_google_absl//absl/synchronization", "@com_google_absl//absl/types:optional", "@com_google_protobuf//:protobuf", - "@com_google_protobuf//python:proto_api", ], ) --- a/pybind11_protobuf/check_unknown_fields.h +++ b/pybind11_protobuf/check_unknown_fields.h @@ -6,7 +6,11 @@ #include "absl/strings/string_view.h" #include "absl/types/optional.h" #include "google/protobuf/message.h" +#if __has_include("python/google/protobuf/proto_api.h") #include "python/google/protobuf/proto_api.h" +#else +namespace google::protobuf::python { struct PyProto_API; } +#endif namespace pybind11_protobuf::check_unknown_fields { --- a/pybind11_protobuf/check_unknown_fields.cc +++ b/pybind11_protobuf/check_unknown_fields.cc @@ -15,7 +15,9 @@ #include "google/protobuf/descriptor.h" #include "google/protobuf/message.h" #include "google/protobuf/unknown_field_set.h" +#if __has_include("python/google/protobuf/proto_api.h") #include "python/google/protobuf/proto_api.h" +#endif namespace pybind11_protobuf::check_unknown_fields { namespace { @@ -103,11 +105,16 @@ unknown_field_number = unknown_field_set.field(0).number(); // Stop only if the extension is known by Python. +#if __has_include("python/google/protobuf/proto_api.h") if (py_proto_api->GetDefaultDescriptorPool()->FindExtensionByNumber( unknown_field_parent_descriptor, unknown_field_number)) { field_fqn_parts.resize(depth); return true; } +#else + field_fqn_parts.resize(depth); + return true; +#endif } // If this message does not include submessages which allow extensions, @@ -184,6 +191,9 @@ absl::optional CheckRecursively( const ::google::protobuf::python::PyProto_API* py_proto_api, const ::google::protobuf::Message* message) { +#if !__has_include("python/google/protobuf/proto_api.h") + return absl::nullopt; +#else const auto* root_descriptor = message->GetDescriptor(); HasUnknownFields search{py_proto_api, root_descriptor}; if (!search.FindUnknownFieldsRecursive(message, 0u)) { @@ -194,6 +204,7 @@ return absl::nullopt; } return search.BuildErrorMessage(); +#endif } } // namespace pybind11_protobuf::check_unknown_fields