64
64
#include " absl/strings/substitute.h"
65
65
#include " google/protobuf/compiler/python/helpers.h"
66
66
#include " google/protobuf/compiler/python/pyi_generator.h"
67
+ #include " google/protobuf/compiler/retention.h"
67
68
#include " google/protobuf/descriptor.h"
68
69
#include " google/protobuf/descriptor.pb.h"
69
70
#include " google/protobuf/io/printer.h"
@@ -249,8 +250,7 @@ bool Generator::Generate(const FileDescriptor* file,
249
250
250
251
std::string filename = GetFileName (file, " .py" );
251
252
252
- FileDescriptorProto fdp;
253
- file_->CopyTo (&fdp);
253
+ FileDescriptorProto fdp = StripSourceRetentionOptions (*file_);
254
254
fdp.SerializeToString (&file_descriptor_serialized_);
255
255
256
256
if (!opensource_runtime_ && GeneratingDescriptorProto ()) {
@@ -342,7 +342,7 @@ bool Generator::Generate(const FileDescriptor* file,
342
342
FixAllDescriptorOptions ();
343
343
344
344
// Set serialized_start and serialized_end.
345
- SetSerializedPbInterval ();
345
+ SetSerializedPbInterval (fdp );
346
346
347
347
printer_->Outdent ();
348
348
if (HasGenericServices (file)) {
@@ -442,7 +442,8 @@ void Generator::PrintFileDescriptor() const {
442
442
m[" name" ] = file_->name ();
443
443
m[" package" ] = file_->package ();
444
444
m[" syntax" ] = StringifySyntax (file_->syntax ());
445
- m[" options" ] = OptionsValue (file_->options ().SerializeAsString ());
445
+ m[" options" ] = OptionsValue (
446
+ StripLocalSourceRetentionOptions (*file_).SerializeAsString ());
446
447
m[" serialized_descriptor" ] = absl::CHexEscape (file_descriptor_serialized_);
447
448
if (GeneratingDescriptorProto ()) {
448
449
printer_->Print (" if _descriptor._USE_C_DESCRIPTORS == False:\n " );
@@ -528,7 +529,8 @@ void Generator::PrintEnum(const EnumDescriptor& enum_descriptor) const {
528
529
" create_key=_descriptor._internal_create_key,\n "
529
530
" values=[\n " ;
530
531
std::string options_string;
531
- enum_descriptor.options ().SerializeToString (&options_string);
532
+ StripLocalSourceRetentionOptions (enum_descriptor)
533
+ .SerializeToString (&options_string);
532
534
printer_->Print (m, enum_descriptor_template);
533
535
printer_->Indent ();
534
536
printer_->Indent ();
@@ -681,7 +683,8 @@ void Generator::PrintDescriptor(const Descriptor& message_descriptor) const {
681
683
printer_->Outdent ();
682
684
printer_->Print (" ],\n " );
683
685
std::string options_string;
684
- message_descriptor.options ().SerializeToString (&options_string);
686
+ StripLocalSourceRetentionOptions (message_descriptor)
687
+ .SerializeToString (&options_string);
685
688
printer_->Print (
686
689
" serialized_options=$options_value$,\n "
687
690
" is_extendable=$extendable$,\n "
@@ -708,7 +711,8 @@ void Generator::PrintDescriptor(const Descriptor& message_descriptor) const {
708
711
m[" name" ] = desc->name ();
709
712
m[" full_name" ] = desc->full_name ();
710
713
m[" index" ] = absl::StrCat (desc->index ());
711
- options_string = OptionsValue (desc->options ().SerializeAsString ());
714
+ options_string = OptionsValue (
715
+ StripLocalSourceRetentionOptions (*desc).SerializeAsString ());
712
716
if (options_string == " None" ) {
713
717
m[" serialized_options" ] = " " ;
714
718
} else {
@@ -1050,7 +1054,8 @@ void Generator::PrintEnumValueDescriptor(
1050
1054
// TODO(robinson): Fix up EnumValueDescriptor "type" fields.
1051
1055
// More circular references. ::sigh::
1052
1056
std::string options_string;
1053
- descriptor.options ().SerializeToString (&options_string);
1057
+ StripLocalSourceRetentionOptions (descriptor)
1058
+ .SerializeToString (&options_string);
1054
1059
absl::flat_hash_map<absl::string_view, std::string> m;
1055
1060
m[" name" ] = descriptor.name ();
1056
1061
m[" index" ] = absl::StrCat (descriptor.index ());
@@ -1078,7 +1083,7 @@ std::string Generator::OptionsValue(
1078
1083
void Generator::PrintFieldDescriptor (const FieldDescriptor& field,
1079
1084
bool is_extension) const {
1080
1085
std::string options_string;
1081
- field. options ( ).SerializeToString (&options_string);
1086
+ StripLocalSourceRetentionOptions (field ).SerializeToString (&options_string);
1082
1087
absl::flat_hash_map<absl::string_view, std::string> m;
1083
1088
m[" name" ] = field.name ();
1084
1089
m[" full_name" ] = field.full_name ();
@@ -1216,21 +1221,17 @@ std::string Generator::InternalPackage() const {
1216
1221
: " google3.net.google.protobuf.python.internal" ;
1217
1222
}
1218
1223
1219
- // Prints standard constructor arguments serialized_start and serialized_end .
1224
+ // Prints descriptor offsets _serialized_start and _serialized_end .
1220
1225
// Args:
1221
- // descriptor: The cpp descriptor to have a serialized reference.
1222
- // proto: A proto
1226
+ // descriptor_proto: The descriptor proto to have a serialized reference.
1223
1227
// Example printer output:
1224
- // serialized_start=41,
1225
- // serialized_end=43,
1226
- //
1227
- template <typename DescriptorT, typename DescriptorProtoT>
1228
- void Generator::PrintSerializedPbInterval (const DescriptorT& descriptor,
1229
- DescriptorProtoT& proto,
1230
- absl::string_view name) const {
1231
- descriptor.CopyTo (&proto);
1228
+ // _globals['_MYMESSAGE']._serialized_start=47
1229
+ // _globals['_MYMESSAGE']._serialized_end=76
1230
+ template <typename DescriptorProtoT>
1231
+ void Generator::PrintSerializedPbInterval (
1232
+ const DescriptorProtoT& descriptor_proto, absl::string_view name) const {
1232
1233
std::string sp;
1233
- proto .SerializeToString (&sp);
1234
+ descriptor_proto .SerializeToString (&sp);
1234
1235
int offset = file_descriptor_serialized_.find (sp);
1235
1236
ABSL_CHECK_GE (offset, 0 );
1236
1237
@@ -1254,51 +1255,56 @@ void PrintDescriptorOptionsFixingCode(absl::string_view descriptor,
1254
1255
}
1255
1256
} // namespace
1256
1257
1257
- void Generator::SetSerializedPbInterval () const {
1258
+ // Generates the start and end offsets for each entity in the serialized file
1259
+ // descriptor. The file argument must exactly match what was serialized into
1260
+ // file_descriptor_serialized_, and should already have had any
1261
+ // source-retention options stripped out. This is important because we need an
1262
+ // exact byte-for-byte match so that we can successfully find the correct
1263
+ // offsets in the serialized descriptors.
1264
+ void Generator::SetSerializedPbInterval (const FileDescriptorProto& file) const {
1258
1265
// Top level enums.
1259
1266
for (int i = 0 ; i < file_->enum_type_count (); ++i) {
1260
- EnumDescriptorProto proto;
1261
1267
const EnumDescriptor& descriptor = *file_->enum_type (i);
1262
- PrintSerializedPbInterval (descriptor, proto ,
1268
+ PrintSerializedPbInterval (file. enum_type (i) ,
1263
1269
ModuleLevelDescriptorName (descriptor));
1264
1270
}
1265
1271
1266
1272
// Messages.
1267
1273
for (int i = 0 ; i < file_->message_type_count (); ++i) {
1268
- SetMessagePbInterval (*file_->message_type (i));
1274
+ SetMessagePbInterval (file. message_type (i), *file_->message_type (i));
1269
1275
}
1270
1276
1271
1277
// Services.
1272
1278
for (int i = 0 ; i < file_->service_count (); ++i) {
1273
- ServiceDescriptorProto proto;
1274
1279
const ServiceDescriptor& service = *file_->service (i);
1275
- PrintSerializedPbInterval (service, proto ,
1280
+ PrintSerializedPbInterval (file. service (i) ,
1276
1281
ModuleLevelServiceDescriptorName (service));
1277
1282
}
1278
1283
}
1279
1284
1280
- void Generator::SetMessagePbInterval (const Descriptor& descriptor) const {
1281
- DescriptorProto message_proto;
1282
- PrintSerializedPbInterval (descriptor, message_proto,
1285
+ void Generator::SetMessagePbInterval (const DescriptorProto& message_proto,
1286
+ const Descriptor& descriptor) const {
1287
+ PrintSerializedPbInterval (message_proto,
1283
1288
ModuleLevelDescriptorName (descriptor));
1284
1289
1285
1290
// Nested messages.
1286
1291
for (int i = 0 ; i < descriptor.nested_type_count (); ++i) {
1287
- SetMessagePbInterval (*descriptor.nested_type (i));
1292
+ SetMessagePbInterval (message_proto.nested_type (i),
1293
+ *descriptor.nested_type (i));
1288
1294
}
1289
1295
1290
1296
for (int i = 0 ; i < descriptor.enum_type_count (); ++i) {
1291
- EnumDescriptorProto proto;
1292
1297
const EnumDescriptor& enum_des = *descriptor.enum_type (i);
1293
- PrintSerializedPbInterval (enum_des, proto ,
1298
+ PrintSerializedPbInterval (message_proto. enum_type (i) ,
1294
1299
ModuleLevelDescriptorName (enum_des));
1295
1300
}
1296
1301
}
1297
1302
1298
1303
// Prints expressions that set the options field of all descriptors.
1299
1304
void Generator::FixAllDescriptorOptions () const {
1300
1305
// Prints an expression that sets the file descriptor's options.
1301
- std::string file_options = OptionsValue (file_->options ().SerializeAsString ());
1306
+ std::string file_options = OptionsValue (
1307
+ StripLocalSourceRetentionOptions (*file_).SerializeAsString ());
1302
1308
if (file_options != " None" ) {
1303
1309
PrintDescriptorOptionsFixingCode (kDescriptorKey , file_options, printer_);
1304
1310
} else {
@@ -1326,7 +1332,8 @@ void Generator::FixAllDescriptorOptions() const {
1326
1332
}
1327
1333
1328
1334
void Generator::FixOptionsForOneof (const OneofDescriptor& oneof) const {
1329
- std::string oneof_options = OptionsValue (oneof.options ().SerializeAsString ());
1335
+ std::string oneof_options =
1336
+ OptionsValue (StripLocalSourceRetentionOptions (oneof).SerializeAsString ());
1330
1337
if (oneof_options != " None" ) {
1331
1338
std::string oneof_name = absl::Substitute (
1332
1339
" $0.$1['$2']" , ModuleLevelDescriptorName (*oneof.containing_type ()),
@@ -1339,15 +1346,15 @@ void Generator::FixOptionsForOneof(const OneofDescriptor& oneof) const {
1339
1346
// value descriptors.
1340
1347
void Generator::FixOptionsForEnum (const EnumDescriptor& enum_descriptor) const {
1341
1348
std::string descriptor_name = ModuleLevelDescriptorName (enum_descriptor);
1342
- std::string enum_options =
1343
- OptionsValue (enum_descriptor. options ( ).SerializeAsString ());
1349
+ std::string enum_options = OptionsValue (
1350
+ StripLocalSourceRetentionOptions (enum_descriptor).SerializeAsString ());
1344
1351
if (enum_options != " None" ) {
1345
1352
PrintDescriptorOptionsFixingCode (descriptor_name, enum_options, printer_);
1346
1353
}
1347
1354
for (int i = 0 ; i < enum_descriptor.value_count (); ++i) {
1348
1355
const EnumValueDescriptor& value_descriptor = *enum_descriptor.value (i);
1349
- std::string value_options =
1350
- OptionsValue (value_descriptor. options ( ).SerializeAsString ());
1356
+ std::string value_options = OptionsValue (
1357
+ StripLocalSourceRetentionOptions (value_descriptor).SerializeAsString ());
1351
1358
if (value_options != " None" ) {
1352
1359
PrintDescriptorOptionsFixingCode (
1353
1360
absl::StrFormat (" %s.values_by_name[\" %s\" ]" , descriptor_name.c_str (),
@@ -1363,17 +1370,17 @@ void Generator::FixOptionsForService(
1363
1370
const ServiceDescriptor& service_descriptor) const {
1364
1371
std::string descriptor_name =
1365
1372
ModuleLevelServiceDescriptorName (service_descriptor);
1366
- std::string service_options =
1367
- OptionsValue (service_descriptor. options ( ).SerializeAsString ());
1373
+ std::string service_options = OptionsValue (
1374
+ StripLocalSourceRetentionOptions (service_descriptor).SerializeAsString ());
1368
1375
if (service_options != " None" ) {
1369
1376
PrintDescriptorOptionsFixingCode (descriptor_name, service_options,
1370
1377
printer_);
1371
1378
}
1372
1379
1373
1380
for (int i = 0 ; i < service_descriptor.method_count (); ++i) {
1374
1381
const MethodDescriptor* method = service_descriptor.method (i);
1375
- std::string method_options =
1376
- OptionsValue ( method-> options ( ).SerializeAsString ());
1382
+ std::string method_options = OptionsValue (
1383
+ StripLocalSourceRetentionOptions (* method).SerializeAsString ());
1377
1384
if (method_options != " None" ) {
1378
1385
std::string method_name = absl::StrCat (
1379
1386
descriptor_name, " .methods_by_name['" , method->name (), " ']" );
@@ -1385,7 +1392,8 @@ void Generator::FixOptionsForService(
1385
1392
// Prints expressions that set the options for field descriptors (including
1386
1393
// extensions).
1387
1394
void Generator::FixOptionsForField (const FieldDescriptor& field) const {
1388
- std::string field_options = OptionsValue (field.options ().SerializeAsString ());
1395
+ std::string field_options =
1396
+ OptionsValue (StripLocalSourceRetentionOptions (field).SerializeAsString ());
1389
1397
if (field_options != " None" ) {
1390
1398
std::string field_name;
1391
1399
if (field.is_extension ()) {
@@ -1430,8 +1438,8 @@ void Generator::FixOptionsForMessage(const Descriptor& descriptor) const {
1430
1438
FixOptionsForField (field);
1431
1439
}
1432
1440
// Message option for this message.
1433
- std::string message_options =
1434
- OptionsValue (descriptor. options ( ).SerializeAsString ());
1441
+ std::string message_options = OptionsValue (
1442
+ StripLocalSourceRetentionOptions (descriptor).SerializeAsString ());
1435
1443
if (message_options != " None" ) {
1436
1444
std::string descriptor_name = ModuleLevelDescriptorName (descriptor);
1437
1445
PrintDescriptorOptionsFixingCode (descriptor_name, message_options,
0 commit comments