Skip to content

Commit

Permalink
Minor formatting/include cleanups.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 681493832
  • Loading branch information
laramiel authored and copybara-github committed Oct 2, 2024
1 parent f346779 commit ed430af
Show file tree
Hide file tree
Showing 23 changed files with 123 additions and 139 deletions.
13 changes: 5 additions & 8 deletions pybind11_protobuf/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pybind_library(
"//visibility:public",
],
deps = [
"//net/proto2/public",
"@com_google_protobuf//:protobuf",
],
)

Expand All @@ -25,8 +25,8 @@ pybind_library(
":check_unknown_fields",
":enum_type_caster",
":proto_cast_util",
"//net/proto2/public",
"@com_google_absl//absl/strings:string_view",
"@com_google_protobuf//:protobuf",
],
)

Expand All @@ -39,8 +39,6 @@ pybind_library(
],
deps = [
":check_unknown_fields",
"//net/proto2/proto:descriptor_cc_proto",
"//net/proto2/public",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/log",
"@com_google_absl//absl/log:check",
Expand All @@ -60,9 +58,9 @@ pybind_library(
],
deps = [
":proto_cast_util",
"//net/proto2/public",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/types:optional",
"@com_google_protobuf//:protobuf",
],
)

Expand All @@ -71,13 +69,12 @@ cc_library(
srcs = ["check_unknown_fields.cc"],
hdrs = ["check_unknown_fields.h"],
deps = [
"//net/proto2/public",
"//net/proto2/python/public:proto_api",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/meta:type_traits",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/synchronization",
"@com_google_protobuf//:protobuf",
"@com_google_protobuf//python:proto_api",
],
)

Expand Down
10 changes: 5 additions & 5 deletions pybind11_protobuf/check_unknown_fields.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
#include <string>
#include <vector>

#include "net/proto2/public/descriptor.h"
#include "net/proto2/public/message.h"
#include "net/proto2/public/unknown_field_set.h"
#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_join.h"
#include "absl/strings/string_view.h"
#include "absl/synchronization/mutex.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/message.h"
#include "google/protobuf/unknown_field_set.h"
#include "python/google/protobuf/proto_api.h"

namespace pybind11_protobuf::check_unknown_fields {
namespace {
Expand Down Expand Up @@ -103,8 +104,7 @@ bool HasUnknownFields::FindUnknownFieldsRecursive(

// Stop only if the extension is known by Python.
if (py_proto_api->GetDefaultDescriptorPool()->FindExtensionByNumber(
unknown_field_parent_descriptor,
unknown_field_number)) {
unknown_field_parent_descriptor, unknown_field_number)) {
field_fqn_parts.resize(depth);
return true;
}
Expand Down
5 changes: 3 additions & 2 deletions pybind11_protobuf/check_unknown_fields.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
#define PYBIND11_PROTOBUF_CHECK_UNKNOWN_FIELDS_H_

#include <optional>
#include <string>

#include "net/proto2/public/message.h"
#include "net/proto2/python/public/proto_api.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/message.h"
#include "python/google/protobuf/proto_api.h"

namespace pybind11_protobuf::check_unknown_fields {

Expand Down
11 changes: 4 additions & 7 deletions pybind11_protobuf/enum_type_caster.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,18 @@
#include <pybind11/pybind11.h>
#include <pybind11/pytypes.h>

#include <string>
#include <type_traits>

#include "net/proto2/public/descriptor.h"
#include "net/proto2/public/generated_enum_reflection.h"
#include "net/proto2/public/generated_enum_util.h"
#include "google/protobuf/generated_enum_util.h"

// pybind11 type_caster specialization which translates Proto::Enum types
// to/from ints. This will have ODR conflicts when users specify wrappers for
// enums using py::enum_<T>.
//
// ::google::protobuf::is_proto_enum and ::google::protobuf::GetEnumDescriptor are require
// ::google::protobuf::is_proto_enum and ::google::protobuf::GetEnumDescriptor are required.
//
// NOTE: The protobuf compiler does not generate ::google::protobuf::is_proto_enum traits
// for enumerations of oneof fields.
// NOTE: The protobuf compiler does not generate ::google::protobuf::is_proto_enum
// traits for enumerations of oneof fields.
//
// Example:
// #include <pybind11/pybind11.h>
Expand Down
15 changes: 5 additions & 10 deletions pybind11_protobuf/native_proto_caster.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,17 @@
// IWYU
#include <Python.h>

#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <type_traits>
#include <utility>

#include "net/proto2/public/message.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/message.h"
#include "pybind11_protobuf/enum_type_caster.h"
#include "pybind11_protobuf/proto_caster_impl.h"

// pybind11::type_caster<> specialization for ::google::protobuf::Message types that
// that converts protocol buffer objects between C++ and python representations.
// This binder supports binaries linked with both native python protos
// and fast cpp python protos.
// pybind11::type_caster<> specialization for ::google::protobuf::Message types
// that that converts protocol buffer objects between C++ and python
// representations. This binder supports binaries linked with both native python
// protos and fast cpp python protos.
//
// When passing protos between python and C++, if possible, an underlying C++
// object may have ownership transferred, or may be copied if both instances
Expand Down
12 changes: 4 additions & 8 deletions pybind11_protobuf/proto_cast_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>

#include "net/proto2/proto/descriptor.pb.h"
#include "absl/container/flat_hash_map.h"
#include "absl/log/check.h"
#include "absl/log/log.h"
Expand All @@ -25,6 +23,7 @@
#include "absl/strings/strip.h"
#include "absl/types/optional.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/descriptor_database.h"
#include "google/protobuf/dynamic_message.h"

Expand All @@ -34,7 +33,6 @@ using ::google::protobuf::Descriptor;
using ::google::protobuf::DescriptorDatabase;
using ::google::protobuf::DescriptorPool;
using ::google::protobuf::DynamicMessageFactory;
using ::google::protobuf::FileDescriptor;
using ::google::protobuf::FileDescriptorProto;
using ::google::protobuf::Message;
using ::google::protobuf::MessageFactory;
Expand Down Expand Up @@ -183,11 +181,9 @@ GlobalState::GlobalState() {

// pybind11_protobuf casting needs a dependency on proto internals to work.
try {
ImportCached("google3.net.proto2.python.public.descriptor");
auto descriptor_pool =
ImportCached("google3.net.proto2.python.public.descriptor_pool");
auto message_factory =
ImportCached("google3.net.proto2.python.public.message_factory");
ImportCached("google.protobuf.descriptor");
auto descriptor_pool = ImportCached("google.protobuf.descriptor_pool");
auto message_factory = ImportCached("google.protobuf.message_factory");
global_pool_ = descriptor_pool.attr("Default")();
find_message_type_by_name_ = global_pool_.attr("FindMessageTypeByName");
if (hasattr(message_factory, "GetMessageClass")) {
Expand Down
7 changes: 2 additions & 5 deletions pybind11_protobuf/proto_cast_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@
#include <pybind11/pybind11.h>
#include <pybind11/pytypes.h>

#include <functional>
#include <memory>
#include <string>
#include <type_traits>
#include <utility>

#include "net/proto2/public/descriptor.h"
#include "net/proto2/public/message.h"
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/message.h"

// PYBIND11_PROTOBUF_ASSUME_FULL_ABI_COMPATIBILITY can be defined by users
// certain about ABI compatibility between all Python extensions in their
Expand Down
17 changes: 7 additions & 10 deletions pybind11_protobuf/proto_caster_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@
#include <pybind11/pybind11.h>
#include <pybind11/pytypes.h>

#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <type_traits>
#include <utility>

#include "net/proto2/proto/descriptor.pb.h"
#include "net/proto2/public/descriptor.h"
#include "net/proto2/public/message.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/message.h"
#include "pybind11_protobuf/proto_cast_util.h"

// Enables unsafe conversions; currently these are a work in progress.
Expand Down Expand Up @@ -74,8 +72,7 @@ struct proto_caster_load_impl {

owned = std::unique_ptr<ProtoType>(new ProtoType());
value = owned.get();
return owned.get()->ParsePartialFromString(
PyBytesAsStringView(serialized_bytes));
return owned->ParsePartialFromString(PyBytesAsStringView(serialized_bytes));
}

// ensure_owned ensures that the owned member contains a copy of the
Expand Down Expand Up @@ -126,8 +123,7 @@ struct proto_caster_load_impl<::google::protobuf::Message> {
src, *descriptor_name)
.release()));
value = owned.get();
return owned.get()->ParsePartialFromString(
PyBytesAsStringView(serialized_bytes));
return owned->ParsePartialFromString(PyBytesAsStringView(serialized_bytes));
}

// ensure_owned ensures that the owned member contains a copy of the
Expand Down Expand Up @@ -156,7 +152,8 @@ struct fast_cpp_cast_impl {
(policy == pybind11::return_value_policy::reference ||
policy == pybind11::return_value_policy::reference_internal)) {
throw pybind11::type_error(
"Cannot return a const reference to a ::google::protobuf::Message derived "
"Cannot return a const reference to a ::google::protobuf::Message "
"derived "
"type. Consider setting return_value_policy::copy in the "
"pybind11 def().");
}
Expand Down
15 changes: 9 additions & 6 deletions pybind11_protobuf/proto_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@

#include <pybind11/functional.h>

#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <functional>
#include <memory>
#include <stdexcept>
#include <string>
#include <utility>

#include "net/proto2/proto/descriptor.pb.h"
#include "net/proto2/public/descriptor.h"
#include "net/proto2/public/message.h"
#include "net/proto2/public/reflection.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/message.h"
#include "google/protobuf/reflection.h"

namespace pybind11 {
namespace google {
Expand Down Expand Up @@ -516,7 +519,7 @@ struct FindMapPair {
handle key, bool add_key = true) {
// When using the proto reflection API, maps are represented as repeated
// message fields (messages with 2 elements: 'key' and 'value'). If protocol
// buffers guarrantee a consistent (and useful) ordering of elements,
// buffers guarantee a consistent (and useful) ordering of elements,
// it should be possible to do this search in O(log(n)) time. However, that
// requires more knowledge of protobuf internals than I have, so for now
// assume a random ordering of elements, in which case a O(n) search is
Expand Down
8 changes: 4 additions & 4 deletions pybind11_protobuf/proto_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#include <memory>

#include "net/proto2/public/message.h"
#include "google/protobuf/message.h"

namespace pybind11 {
namespace google {
Expand All @@ -19,9 +19,9 @@ namespace google {
// Unlike ProtoSetField, this allows setting message, map and repeated fields.
void ProtoInitFields(::google::protobuf::Message* message, kwargs kwargs_in);

// Wrapper around ::google::protobuf::Message::CopyFrom which can efficiently copy from
// either a wrapped C++ or native python proto. Throws an error if `other`
// is not a proto of the correct type.
// Wrapper around ::google::protobuf::Message::CopyFrom which can efficiently
// copy from either a wrapped C++ or native python proto. Throws an error if
// `other` is not a proto of the correct type.
void ProtoCopyFrom(::google::protobuf::Message* msg, handle other);

// Allocate and return the ProtoType given by the template argument.
Expand Down
Loading

0 comments on commit ed430af

Please sign in to comment.