Skip to content

Commit

Permalink
Minor formatting/include cleanups.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 678453559
  • Loading branch information
laramiel authored and copybara-github committed Oct 2, 2024
1 parent 55916e1 commit eea8124
Show file tree
Hide file tree
Showing 20 changed files with 86 additions and 98 deletions.
1 change: 0 additions & 1 deletion pybind11_protobuf/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ cc_library(
deps = [
"@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",
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 "google/protobuf/descriptor.h"
#include "google/protobuf/message.h"
#include "google/protobuf/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
3 changes: 2 additions & 1 deletion 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 "absl/strings/string_view.h"
#include "google/protobuf/message.h"
#include "python/google/protobuf/proto_api.h"
#include "absl/strings/string_view.h"

namespace pybind11_protobuf::check_unknown_fields {

Expand Down
9 changes: 3 additions & 6 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 "google/protobuf/descriptor.h"
#include "google/protobuf/generated_enum_reflection.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 "google/protobuf/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
10 changes: 3 additions & 7 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 "google/protobuf/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 @@ -184,10 +182,8 @@ GlobalState::GlobalState() {
// pybind11_protobuf casting needs a dependency on proto internals to work.
try {
ImportCached("google.protobuf.descriptor");
auto descriptor_pool =
ImportCached("google.protobuf.descriptor_pool");
auto message_factory =
ImportCached("google.protobuf.message_factory");
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 "google/protobuf/descriptor.h"
#include "google/protobuf/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
13 changes: 5 additions & 8 deletions pybind11_protobuf/proto_caster_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@
#include <pybind11/pybind11.h>
#include <pybind11/pytypes.h>

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

#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/message.h"
#include "pybind11_protobuf/proto_cast_util.h"

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
13 changes: 8 additions & 5 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 "google/protobuf/descriptor.pb.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"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.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
6 changes: 3 additions & 3 deletions pybind11_protobuf/proto_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion pybind11_protobuf/tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,9 @@ py_test(
srcs_version = "PY3",
deps = [
":test_py_pb2",
"@com_google_protobuf//:protobuf_python",
"@com_google_absl_py//absl/testing:absltest",
"@com_google_absl_py//absl/testing:parameterized",
"@com_google_protobuf//:protobuf_python",
requirement("absl_py"),
],
)
Expand All @@ -270,6 +270,7 @@ pybind_extension(
deps = [
":test_cc_proto",
"//pybind11_protobuf:wrapped_proto_caster",
"@com_google_absl//absl/functional:function_ref",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/types:optional",
"@com_google_protobuf//:protobuf",
Expand Down
6 changes: 4 additions & 2 deletions pybind11_protobuf/tests/dynamic_message_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@

#include <pybind11/pybind11.h>

#include <functional>
#include <cassert>
#include <cstdint>
#include <memory>
#include <stdexcept>
#include <string>

#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/dynamic_message.h"
#include "google/protobuf/message.h"
#include "google/protobuf/text_format.h"
Expand Down
2 changes: 1 addition & 1 deletion pybind11_protobuf/tests/dynamic_message_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

from absl.testing import absltest
from absl.testing import parameterized

from google.protobuf import descriptor_pb2
from google.protobuf import descriptor_pool
from google.protobuf import message_factory

from pybind11_protobuf.tests import compare
from pybind11_protobuf.tests import dynamic_message_module as m
from pybind11_protobuf.tests import test_pb2
Expand Down
8 changes: 3 additions & 5 deletions pybind11_protobuf/tests/message_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

#include <pybind11/pybind11.h>

#include <functional>
#include <memory>
#include <stdexcept>
#include <string>

#include "google/protobuf/message.h"
#include "google/protobuf/text_format.h"
Expand All @@ -18,8 +16,8 @@ namespace py = ::pybind11;

namespace {

using pybind11::test::IntMessage;
using pybind11::test::TestMessage;
using ::pybind11::test::IntMessage;
using ::pybind11::test::TestMessage;

PYBIND11_MODULE(message_module, m) {
pybind11_protobuf::ImportNativeProtoCasters();
Expand Down
2 changes: 1 addition & 1 deletion pybind11_protobuf/tests/message_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

from absl.testing import absltest
from absl.testing import parameterized
from google.protobuf import text_format

from google.protobuf import any_pb2
from pybind11_protobuf.tests import compare
from pybind11_protobuf.tests import message_module as m
from pybind11_protobuf.tests import test_pb2
from google.protobuf import text_format


def get_py_message():
Expand Down
7 changes: 4 additions & 3 deletions pybind11_protobuf/tests/pass_by_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include <cstdint>
#include <functional>
#include <memory>
#include <stdexcept>
#include <optional>
#include <variant>

#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/dynamic_message.h"
#include "google/protobuf/message.h"
#include "pybind11_protobuf/native_proto_caster.h"
Expand All @@ -22,7 +23,7 @@ namespace py = ::pybind11;

namespace {

using pybind11::test::IntMessage;
using ::pybind11::test::IntMessage;

bool CheckIntMessage(const IntMessage* message, int32_t value) {
return message ? message->value() == value : false;
Expand Down
9 changes: 5 additions & 4 deletions pybind11_protobuf/tests/pass_by_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

from absl.testing import absltest
from absl.testing import parameterized
from google.protobuf import descriptor_pool
from google.protobuf import message_factory

from pybind11_protobuf.tests import pass_by_module as m
from pybind11_protobuf.tests import test_pb2
from google.protobuf import descriptor_pool
from google.protobuf import message_factory


def get_abstract_pass_by_params():
Expand Down Expand Up @@ -60,7 +60,7 @@ def get_pass_by_params():
'concrete_uptr_ref',
'concrete_wref',
'std_variant',
'std_optional,'
'std_optional,',
]:
try:
p.append((x, getattr(m, x)))
Expand Down Expand Up @@ -115,7 +115,8 @@ def test_pool_proto_check(self, check_method):
pool = descriptor_pool.Default()
factory = message_factory.MessageFactory(pool)
prototype = factory.GetPrototype(
pool.FindMessageTypeByName('pybind11.test.IntMessage'))
pool.FindMessageTypeByName('pybind11.test.IntMessage')
)
message = prototype(value=9)
self.assertTrue(check_method(message, 9))

Expand Down
Loading

0 comments on commit eea8124

Please sign in to comment.