16
16
#include " google/protobuf/descriptor.h"
17
17
#include " google/protobuf/message.h"
18
18
#include " google/protobuf/reflection.h"
19
+ #include " absl/strings/str_cat.h"
19
20
#include " absl/strings/string_view.h"
20
21
21
22
namespace pybind11 {
@@ -45,9 +46,9 @@ void ProtoSetField(::google::protobuf::Message* message,
45
46
bool PyProtoFullName (handle py_proto, std::string* name);
46
47
47
48
// Returns whether py_proto is a proto and matches the expected_type.
48
- bool PyProtoCheckType (handle py_proto, const std::string& expected_type);
49
+ bool PyProtoCheckType (handle py_proto, absl::string_view expected_type);
49
50
// Throws a type error if py_proto is not a proto or the wrong message type.
50
- void PyProtoCheckTypeOrThrow (handle py_proto, const std::string& expected_type);
51
+ void PyProtoCheckTypeOrThrow (handle py_proto, absl::string_view expected_type);
51
52
52
53
// Returns whether py_proto is a proto and matches the ProtoType.
53
54
template <typename ProtoType>
@@ -419,7 +420,9 @@ class ProtoFieldContainer<GenericEnum> : public ProtoFieldContainerBase {
419
420
void Append (handle value) {
420
421
reflection_->AddEnumValue (proto_, field_desc_, CastOrTypeError<int >(value));
421
422
}
422
- std::string ElementRepr (int idx) const { return GetDesc (idx)->name (); }
423
+ std::string ElementRepr (int idx) const {
424
+ return std::string (GetDesc (idx)->name ());
425
+ }
423
426
};
424
427
425
428
// A container for a repeated field.
@@ -677,10 +680,8 @@ const ::google::protobuf::FieldDescriptor* GetFieldDescriptor(
677
680
message->GetDescriptor ()->FindFieldByName (std::string (name));
678
681
679
682
if (!field_desc) {
680
- std::string error_str =
681
- " '" + message->GetTypeName () + " ' object has no attribute '" ;
682
- error_str.append (std::string (name));
683
- error_str.append (" '" );
683
+ std::string error_str = absl::StrCat (
684
+ " '" , message->GetTypeName (), " ' object has no attribute '" , name, " '" );
684
685
PyErr_SetString (error_type, error_str.c_str ());
685
686
throw error_already_set ();
686
687
}
@@ -736,23 +737,22 @@ bool PyProtoFullName(handle py_proto, std::string* name) {
736
737
return false ;
737
738
}
738
739
739
- bool PyProtoCheckType (handle py_proto, const std::string& expected_type) {
740
+ bool PyProtoCheckType (handle py_proto, absl::string_view expected_type) {
740
741
std::string name;
741
742
if (PyProtoFullName (py_proto, &name)) return name == expected_type;
742
743
return false ;
743
744
}
744
745
745
- void PyProtoCheckTypeOrThrow (handle py_proto,
746
- const std::string& expected_type) {
746
+ void PyProtoCheckTypeOrThrow (handle py_proto, absl::string_view expected_type) {
747
747
std::string name;
748
748
if (!PyProtoFullName (py_proto, &name)) {
749
749
auto builtins = module::import (PYBIND11_BUILTINS_MODULE);
750
750
std::string type_str =
751
751
str (builtins.attr (" repr" )(builtins.attr (" type" )(py_proto)));
752
752
throw type_error (" Expected a proto, got a " + type_str + " ." );
753
753
} else if (name != expected_type) {
754
- throw type_error (" Passed proto is the wrong type. Expected " +
755
- expected_type + " but got " + name + " ." );
754
+ throw type_error (absl::StrCat ( " Passed proto is the wrong type. Expected " ,
755
+ expected_type, " but got " , name, " ." ) );
756
756
}
757
757
}
758
758
@@ -798,8 +798,8 @@ std::unique_ptr<::google::protobuf::Message> PyProtoAllocateMessage(
798
798
::google::protobuf::MessageFactory::generated_factory ()->GetPrototype(descriptor);
799
799
if (!prototype) {
800
800
throw std::runtime_error (
801
- " Not able to generate prototype for descriptor of: " +
802
- descriptor->full_name ());
801
+ absl::StrCat ( " Not able to generate prototype for descriptor of: " ,
802
+ descriptor->full_name () ));
803
803
}
804
804
auto message = std::unique_ptr<::google::protobuf::Message>(prototype->New ());
805
805
ProtoInitFields (message.get (), kwargs_in);
@@ -824,8 +824,9 @@ void ProtoSetField(::google::protobuf::Message* message,
824
824
const ::google::protobuf::FieldDescriptor* field_desc, handle value) {
825
825
if (field_desc->is_map () || field_desc->is_repeated () ||
826
826
field_desc->type () == ::google::protobuf::FieldDescriptor::TYPE_MESSAGE) {
827
- std::string error = " Assignment not allowed to field \" " +
828
- field_desc->name () + " \" in protocol message object." ;
827
+ std::string error =
828
+ absl::StrCat (" Assignment not allowed to field \" " , field_desc->name (),
829
+ " \" in protocol message object." );
829
830
PyErr_SetString (PyExc_AttributeError, error.c_str ());
830
831
throw error_already_set ();
831
832
}
0 commit comments