Skip to content

Commit 7cc6879

Browse files
laramielcopybara-github
authored andcommitted
Minor formatting/include cleanups.
PiperOrigin-RevId: 678453559
1 parent 55916e1 commit 7cc6879

21 files changed

+300
-302
lines changed

pybind11_protobuf/BUILD

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pybind_library(
1111
"//visibility:public",
1212
],
1313
deps = [
14-
"@com_google_protobuf//:protobuf",
14+
"//third_party/protobuf:protobuf_lite",
1515
],
1616
)
1717

@@ -39,6 +39,7 @@ pybind_library(
3939
],
4040
deps = [
4141
":check_unknown_fields",
42+
"//third_party/protobuf:descriptor_pb",
4243
"@com_google_absl//absl/container:flat_hash_map",
4344
"@com_google_absl//absl/log",
4445
"@com_google_absl//absl/log:check",
@@ -71,7 +72,6 @@ cc_library(
7172
deps = [
7273
"@com_google_absl//absl/container:flat_hash_map",
7374
"@com_google_absl//absl/container:flat_hash_set",
74-
"@com_google_absl//absl/meta:type_traits",
7575
"@com_google_absl//absl/strings",
7676
"@com_google_absl//absl/synchronization",
7777
"@com_google_protobuf//:protobuf",

pybind11_protobuf/check_unknown_fields.cc

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,23 @@
66
#include <string>
77
#include <vector>
88

9-
#include "google/protobuf/descriptor.h"
10-
#include "google/protobuf/message.h"
11-
#include "google/protobuf/unknown_field_set.h"
129
#include "absl/container/flat_hash_map.h"
1310
#include "absl/container/flat_hash_set.h"
1411
#include "absl/strings/str_cat.h"
1512
#include "absl/strings/str_join.h"
1613
#include "absl/strings/string_view.h"
1714
#include "absl/synchronization/mutex.h"
15+
#include "google/protobuf/descriptor.h"
16+
#include "google/protobuf/message.h"
17+
#include "google/protobuf/unknown_field_set.h"
18+
#include "python/google/protobuf/proto_api.h"
1819

1920
namespace pybind11_protobuf::check_unknown_fields {
2021
namespace {
2122

2223
using AllowListSet = absl::flat_hash_set<std::string>;
2324
using MayContainExtensionsMap =
24-
absl::flat_hash_map<const ::google::protobuf::Descriptor*, bool>;
25+
absl::flat_hash_map<const google::protobuf::Descriptor*, bool>;
2526

2627
AllowListSet* GetAllowList() {
2728
static auto* allow_list = new AllowListSet();
@@ -37,7 +38,7 @@ std::string MakeAllowListKey(
3738

3839
/// Recurses through the message Descriptor class looking for valid extensions.
3940
/// Stores the result to `memoized`.
40-
bool MessageMayContainExtensionsRecursive(const ::google::protobuf::Descriptor* descriptor,
41+
bool MessageMayContainExtensionsRecursive(const google::protobuf::Descriptor* descriptor,
4142
MayContainExtensionsMap* memoized) {
4243
if (descriptor->extension_range_count() > 0) return true;
4344

@@ -48,7 +49,7 @@ bool MessageMayContainExtensionsRecursive(const ::google::protobuf::Descriptor*
4849

4950
for (int i = 0; i < descriptor->field_count(); i++) {
5051
auto* fd = descriptor->field(i);
51-
if (fd->cpp_type() != ::google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE) continue;
52+
if (fd->cpp_type() != google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE) continue;
5253
if (MessageMayContainExtensionsRecursive(fd->message_type(), memoized)) {
5354
(*memoized)[descriptor] = true;
5455
return true;
@@ -58,16 +59,16 @@ bool MessageMayContainExtensionsRecursive(const ::google::protobuf::Descriptor*
5859
return false;
5960
}
6061

61-
bool MessageMayContainExtensionsMemoized(const ::google::protobuf::Descriptor* descriptor) {
62+
bool MessageMayContainExtensionsMemoized(const google::protobuf::Descriptor* descriptor) {
6263
static auto* memoized = new MayContainExtensionsMap();
6364
static absl::Mutex lock;
6465
absl::MutexLock l(&lock);
6566
return MessageMayContainExtensionsRecursive(descriptor, memoized);
6667
}
6768

6869
struct HasUnknownFields {
69-
HasUnknownFields(const ::google::protobuf::python::PyProto_API* py_proto_api,
70-
const ::google::protobuf::Descriptor* root_descriptor)
70+
HasUnknownFields(const google::protobuf::python::PyProto_API* py_proto_api,
71+
const google::protobuf::Descriptor* root_descriptor)
7172
: py_proto_api(py_proto_api), root_descriptor(root_descriptor) {}
7273

7374
std::string FieldFQN() const { return absl::StrJoin(field_fqn_parts, "."); }
@@ -77,34 +78,33 @@ struct HasUnknownFields {
7778
: absl::StrCat(FieldFQN(), ".", unknown_field_number);
7879
}
7980

80-
bool FindUnknownFieldsRecursive(const ::google::protobuf::Message* sub_message,
81+
bool FindUnknownFieldsRecursive(const google::protobuf::Message* sub_message,
8182
uint32_t depth);
8283

8384
std::string BuildErrorMessage() const;
8485

85-
const ::google::protobuf::python::PyProto_API* py_proto_api;
86-
const ::google::protobuf::Descriptor* root_descriptor = nullptr;
87-
const ::google::protobuf::Descriptor* unknown_field_parent_descriptor = nullptr;
86+
const google::protobuf::python::PyProto_API* py_proto_api;
87+
const google::protobuf::Descriptor* root_descriptor = nullptr;
88+
const google::protobuf::Descriptor* unknown_field_parent_descriptor = nullptr;
8889
std::vector<std::string> field_fqn_parts;
8990
int unknown_field_number;
9091
};
9192

9293
/// Recurses through the message fields class looking for UnknownFields.
9394
bool HasUnknownFields::FindUnknownFieldsRecursive(
94-
const ::google::protobuf::Message* sub_message, uint32_t depth) {
95-
const ::google::protobuf::Reflection& reflection = *sub_message->GetReflection();
95+
const google::protobuf::Message* sub_message, uint32_t depth) {
96+
const google::protobuf::Reflection& reflection = *sub_message->GetReflection();
9697

9798
// If there are unknown fields, stop searching.
98-
const ::google::protobuf::UnknownFieldSet& unknown_field_set =
99+
const google::protobuf::UnknownFieldSet& unknown_field_set =
99100
reflection.GetUnknownFields(*sub_message);
100101
if (!unknown_field_set.empty()) {
101102
unknown_field_parent_descriptor = sub_message->GetDescriptor();
102103
unknown_field_number = unknown_field_set.field(0).number();
103104

104105
// Stop only if the extension is known by Python.
105106
if (py_proto_api->GetDefaultDescriptorPool()->FindExtensionByNumber(
106-
unknown_field_parent_descriptor,
107-
unknown_field_number)) {
107+
unknown_field_parent_descriptor, unknown_field_number)) {
108108
field_fqn_parts.resize(depth);
109109
return true;
110110
}
@@ -118,11 +118,11 @@ bool HasUnknownFields::FindUnknownFieldsRecursive(
118118

119119
// Otherwise the method has to check all present fields, including
120120
// extensions to determine if they include unknown fields.
121-
std::vector<const ::google::protobuf::FieldDescriptor*> present_fields;
121+
std::vector<const google::protobuf::FieldDescriptor*> present_fields;
122122
reflection.ListFields(*sub_message, &present_fields);
123123

124124
for (const auto* field : present_fields) {
125-
if (field->cpp_type() != ::google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE) {
125+
if (field->cpp_type() != google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE) {
126126
continue;
127127
}
128128
if (field->is_repeated()) {
@@ -182,8 +182,8 @@ void AllowUnknownFieldsFor(absl::string_view top_message_descriptor_full_name,
182182
}
183183

184184
std::optional<std::string> CheckRecursively(
185-
const ::google::protobuf::python::PyProto_API* py_proto_api,
186-
const ::google::protobuf::Message* message) {
185+
const google::protobuf::python::PyProto_API* py_proto_api,
186+
const google::protobuf::Message* message) {
187187
const auto* root_descriptor = message->GetDescriptor();
188188
HasUnknownFields search{py_proto_api, root_descriptor};
189189
if (!search.FindUnknownFieldsRecursive(message, 0u)) {

pybind11_protobuf/check_unknown_fields.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
#define PYBIND11_PROTOBUF_CHECK_UNKNOWN_FIELDS_H_
33

44
#include <optional>
5+
#include <string>
56

7+
#include "absl/strings/string_view.h"
68
#include "google/protobuf/message.h"
79
#include "python/google/protobuf/proto_api.h"
8-
#include "absl/strings/string_view.h"
910

1011
namespace pybind11_protobuf::check_unknown_fields {
1112

@@ -46,8 +47,8 @@ void AllowUnknownFieldsFor(absl::string_view top_message_descriptor_full_name,
4647
absl::string_view unknown_field_parent_message_fqn);
4748

4849
std::optional<std::string> CheckRecursively(
49-
const ::google::protobuf::python::PyProto_API* py_proto_api,
50-
const ::google::protobuf::Message* top_message);
50+
const google::protobuf::python::PyProto_API* py_proto_api,
51+
const google::protobuf::Message* top_message);
5152

5253
} // namespace pybind11_protobuf::check_unknown_fields
5354

pybind11_protobuf/enum_type_caster.h

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,19 @@
1111
#include <pybind11/pybind11.h>
1212
#include <pybind11/pytypes.h>
1313

14-
#include <string>
1514
#include <type_traits>
1615

17-
#include "google/protobuf/descriptor.h"
18-
#include "google/protobuf/generated_enum_reflection.h"
1916
#include "google/protobuf/generated_enum_util.h"
2017

2118
// pybind11 type_caster specialization which translates Proto::Enum types
2219
// to/from ints. This will have ODR conflicts when users specify wrappers for
2320
// enums using py::enum_<T>.
2421
//
25-
// ::google::protobuf::is_proto_enum and ::google::protobuf::GetEnumDescriptor are require
22+
// google::protobuf::is_proto_enum and google::protobuf::GetEnumDescriptor
23+
// are require
2624
//
27-
// NOTE: The protobuf compiler does not generate ::google::protobuf::is_proto_enum traits
28-
// for enumerations of oneof fields.
25+
// NOTE: The protobuf compiler does not generate
26+
// google::protobuf::is_proto_enum traits for enumerations of oneof fields.
2927
//
3028
// Example:
3129
// #include <pybind11/pybind11.h>
@@ -100,17 +98,17 @@ constexpr bool pybind11_protobuf_enable_enum_type_caster(...) { return true; }
10098
#if defined(PYBIND11_HAS_NATIVE_ENUM)
10199
template <typename EnumType>
102100
struct type_caster_enum_type_enabled<
103-
EnumType, std::enable_if_t<(::google::protobuf::is_proto_enum<EnumType>::value &&
101+
EnumType, std::enable_if_t<(google::protobuf::is_proto_enum<EnumType>::value &&
104102
pybind11_protobuf_enable_enum_type_caster(
105103
static_cast<EnumType*>(nullptr)))>>
106104
: std::false_type {};
107105
#endif
108106

109107
// Specialization of pybind11::detail::type_caster<T> for types satisfying
110-
// ::google::protobuf::is_proto_enum.
108+
// google::protobuf::is_proto_enum.
111109
template <typename EnumType>
112110
struct type_caster<EnumType,
113-
std::enable_if_t<(::google::protobuf::is_proto_enum<EnumType>::value &&
111+
std::enable_if_t<(google::protobuf::is_proto_enum<EnumType>::value &&
114112
pybind11_protobuf_enable_enum_type_caster(
115113
static_cast<EnumType*>(nullptr)))>>
116114
: public pybind11_protobuf::enum_type_caster<EnumType> {};

pybind11_protobuf/native_proto_caster.h

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,17 @@
1111
// IWYU
1212
#include <Python.h>
1313

14-
#include <functional>
15-
#include <memory>
16-
#include <optional>
17-
#include <string>
1814
#include <type_traits>
19-
#include <utility>
2015

21-
#include "google/protobuf/message.h"
2216
#include "absl/strings/string_view.h"
17+
#include "google/protobuf/message.h"
2318
#include "pybind11_protobuf/enum_type_caster.h"
2419
#include "pybind11_protobuf/proto_caster_impl.h"
2520

26-
// pybind11::type_caster<> specialization for ::google::protobuf::Message types that
27-
// that converts protocol buffer objects between C++ and python representations.
28-
// This binder supports binaries linked with both native python protos
29-
// and fast cpp python protos.
21+
// pybind11::type_caster<> specialization for google::protobuf::Message types
22+
// that that converts protocol buffer objects between C++ and python
23+
// representations. This binder supports binaries linked with both native python
24+
// protos and fast cpp python protos.
3025
//
3126
// When passing protos between python and C++, if possible, an underlying C++
3227
// object may have ownership transferred, or may be copied if both instances
@@ -85,7 +80,7 @@ constexpr bool pybind11_protobuf_enable_type_caster(...) { return true; }
8580
template <typename ProtoType>
8681
struct type_caster<
8782
ProtoType,
88-
std::enable_if_t<(std::is_base_of<::google::protobuf::Message, ProtoType>::value &&
83+
std::enable_if_t<(std::is_base_of<google::protobuf::Message, ProtoType>::value &&
8984
pybind11_protobuf_enable_type_caster(
9085
static_cast<ProtoType *>(nullptr)))>>
9186
: public pybind11_protobuf::proto_caster<
@@ -95,12 +90,12 @@ struct type_caster<
9590

9691
template <typename ProtoType>
9792
struct copyable_holder_caster_shared_ptr_with_smart_holder_support_enabled<
98-
ProtoType, enable_if_t<std::is_base_of<::google::protobuf::Message, ProtoType>::value>>
93+
ProtoType, enable_if_t<std::is_base_of<google::protobuf::Message, ProtoType>::value>>
9994
: std::false_type {};
10095

10196
template <typename ProtoType>
10297
struct move_only_holder_caster_unique_ptr_with_smart_holder_support_enabled<
103-
ProtoType, enable_if_t<std::is_base_of<::google::protobuf::Message, ProtoType>::value>>
98+
ProtoType, enable_if_t<std::is_base_of<google::protobuf::Message, ProtoType>::value>>
10499
: std::false_type {};
105100

106101
#endif // PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT
@@ -118,7 +113,7 @@ struct move_only_holder_caster_unique_ptr_with_smart_holder_support_enabled<
118113
template <typename ProtoType, typename HolderType>
119114
struct move_only_holder_caster<
120115
ProtoType, HolderType,
121-
std::enable_if_t<(std::is_base_of<::google::protobuf::Message, ProtoType>::value &&
116+
std::enable_if_t<(std::is_base_of<google::protobuf::Message, ProtoType>::value &&
122117
pybind11_protobuf_enable_type_caster(
123118
static_cast<ProtoType *>(nullptr)))>>
124119
: public pybind11_protobuf::move_only_holder_caster_impl<ProtoType,
@@ -136,18 +131,18 @@ struct move_only_holder_caster<
136131
template <typename ProtoType, typename HolderType>
137132
struct copyable_holder_caster<
138133
ProtoType, HolderType,
139-
std::enable_if_t<(std::is_base_of<::google::protobuf::Message, ProtoType>::value &&
134+
std::enable_if_t<(std::is_base_of<google::protobuf::Message, ProtoType>::value &&
140135
pybind11_protobuf_enable_type_caster(
141136
static_cast<ProtoType *>(nullptr)))>>
142137
: public pybind11_protobuf::copyable_holder_caster_impl<ProtoType,
143138
HolderType> {};
144139

145140
// NOTE: We also need to add support and/or test classes:
146141
//
147-
// ::google::protobuf::Descriptor
148-
// ::google::protobuf::EnumDescriptor
149-
// ::google::protobuf::EnumValueDescriptor
150-
// ::google::protobuf::FieldDescriptor
142+
// google::protobuf::Descriptor
143+
// google::protobuf::EnumDescriptor
144+
// google::protobuf::EnumValueDescriptor
145+
// google::protobuf::FieldDescriptor
151146
//
152147

153148
} // namespace detail

pybind11_protobuf/proto_cast_util.cc

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
#include <string>
1212
#include <unordered_set>
1313
#include <utility>
14-
#include <vector>
1514

16-
#include "google/protobuf/descriptor.pb.h"
1715
#include "absl/container/flat_hash_map.h"
1816
#include "absl/log/check.h"
1917
#include "absl/log/log.h"
@@ -25,6 +23,7 @@
2523
#include "absl/strings/strip.h"
2624
#include "absl/types/optional.h"
2725
#include "google/protobuf/descriptor.h"
26+
#include "google/protobuf/descriptor.pb.h"
2827
#include "google/protobuf/descriptor_database.h"
2928
#include "google/protobuf/dynamic_message.h"
3029

@@ -34,7 +33,6 @@ using ::google::protobuf::Descriptor;
3433
using ::google::protobuf::DescriptorDatabase;
3534
using ::google::protobuf::DescriptorPool;
3635
using ::google::protobuf::DynamicMessageFactory;
37-
using ::google::protobuf::FileDescriptor;
3836
using ::google::protobuf::FileDescriptorProto;
3937
using ::google::protobuf::Message;
4038
using ::google::protobuf::MessageFactory;
@@ -183,11 +181,9 @@ GlobalState::GlobalState() {
183181

184182
// pybind11_protobuf casting needs a dependency on proto internals to work.
185183
try {
186-
ImportCached("google.protobuf.descriptor");
187-
auto descriptor_pool =
188-
ImportCached("google.protobuf.descriptor_pool");
189-
auto message_factory =
190-
ImportCached("google.protobuf.message_factory");
184+
ImportCached("google.protobuf");
185+
auto descriptor_pool = ImportCached("google.protobuf.descriptor_pool");
186+
auto message_factory = ImportCached("google.protobuf.message_factory");
191187
global_pool_ = descriptor_pool.attr("Default")();
192188
find_message_type_by_name_ = global_pool_.attr("FindMessageTypeByName");
193189
if (hasattr(message_factory, "GetMessageClass")) {
@@ -221,6 +217,7 @@ py::module_ GlobalState::ImportCached(const std::string& module_name) {
221217
if (cached != import_cache_.end()) {
222218
return cached->second;
223219
}
220+
LOG(INFO) << "ImportCached " << module_name;
224221
auto module = py::module_::import(module_name.c_str());
225222
import_cache_[module_name] = module;
226223
return module;

0 commit comments

Comments
 (0)