A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/protocolbuffers/protobuf/commit/308aaf47686229b7c6f5f9534e254910cebd28c2 below:

Fix python codegen crash when C++ features are used. · protocolbuffers/protobuf@308aaf4 · GitHub

File tree Expand file treeCollapse file tree 7 files changed

+125

-6

lines changed

Filter options

Expand file treeCollapse file tree 7 files changed

+125

-6

lines changed Original file line number Diff line number Diff line change

@@ -107,6 +107,7 @@ cc_library(

107 107

visibility = [

108 108

"//pkg:__pkg__",

109 109

"//src/google/protobuf/compiler:__pkg__",

110 +

"//src/google/protobuf/compiler/python:__pkg__", # For testing only.

110 111

"@io_kythe//kythe/cxx/tools:__subpackages__",

111 112

],

112 113

deps = [

Original file line number Diff line number Diff line change

@@ -51,7 +51,11 @@ cc_test(

51 51

copts = COPTS,

52 52

deps = [

53 53

":python",

54 +

"//src/google/protobuf",

55 +

"//src/google/protobuf/compiler:code_generator",

54 56

"//src/google/protobuf/compiler:command_line_interface",

57 +

"//src/google/protobuf/compiler:command_line_interface_tester",

58 +

"//src/google/protobuf/compiler/cpp",

55 59

"//src/google/protobuf/io",

56 60

"//src/google/protobuf/io:printer",

57 61

"//src/google/protobuf/testing",

Original file line number Diff line number Diff line change

@@ -479,8 +479,17 @@ std::string Generator::GetResolvedFeatures(

479 479

// Assume these are all enums. If we add non-enum global features or any

480 480

// python-specific features, we will need to come back and improve this

481 481

// logic.

482 -

ABSL_CHECK(field->enum_type() != nullptr)

483 -

<< "Unexpected non-enum field found!";

482 +

if (field->type() != FieldDescriptor::TYPE_ENUM) {

483 +

ABSL_CHECK(field->is_extension())

484 +

<< "Unsupported non-enum global feature found: "

485 +

<< field->full_name();

486 +

// Placeholder for python-specific features.

487 +

ABSL_CHECK(field->number() != 1003)

488 +

<< "Unsupported python-specific feature found: "

489 +

<< field->full_name();

490 +

// Skip any non-python language-specific features.

491 +

continue;

492 +

}

484 493

if (field->options().retention() == FieldOptions::RETENTION_SOURCE) {

485 494

// Skip any source-retention features.

486 495

continue;

Original file line number Diff line number Diff line change

@@ -9,15 +9,20 @@

9 9 10 10

#include <memory>

11 11

#include <string>

12 +

#include <utility>

12 13

#include <vector>

13 14 14 15

#include "google/protobuf/testing/file.h"

15 -

#include "google/protobuf/testing/file.h"

16 -

#include "google/protobuf/compiler/command_line_interface.h"

17 -

#include "google/protobuf/compiler/python/generator.h"

18 16

#include <gtest/gtest.h>

19 17

#include "absl/log/absl_check.h"

18 +

#include "absl/strings/str_cat.h"

20 19

#include "absl/strings/str_split.h"

20 +

#include "absl/strings/substitute.h"

21 +

#include "google/protobuf/compiler/code_generator.h"

22 +

#include "google/protobuf/compiler/command_line_interface_tester.h"

23 +

#include "google/protobuf/compiler/cpp/generator.h"

24 +

#include "google/protobuf/compiler/python/generator.h"

25 +

#include "google/protobuf/cpp_features.pb.h"

21 26

#include "google/protobuf/io/printer.h"

22 27

#include "google/protobuf/io/zero_copy_stream.h"

23 28

@@ -100,6 +105,58 @@ TEST(PythonPluginTest, ImportTest) {

100 105

EXPECT_TRUE(found_expected_import);

101 106

}

102 107 108 +

class PythonGeneratorTest : public CommandLineInterfaceTester,

109 +

public testing::WithParamInterface<bool> {

110 +

protected:

111 +

PythonGeneratorTest() {

112 +

auto generator = std::make_unique<Generator>();

113 +

generator->set_opensource_runtime(GetParam());

114 +

RegisterGenerator("--python_out", "--python_opt", std::move(generator),

115 +

"Python test generator");

116 + 117 +

// Generate built-in protos.

118 +

CreateTempFile(

119 +

google::protobuf::DescriptorProto::descriptor()->file()->name(),

120 +

google::protobuf::DescriptorProto::descriptor()->file()->DebugString());

121 +

}

122 +

};

123 + 124 +

TEST_P(PythonGeneratorTest, PythonWithCppFeatures) {

125 +

// Test that the presence of C++ features does not break Python generation.

126 +

RegisterGenerator("--cpp_out", "--cpp_opt",

127 +

std::make_unique<cpp::CppGenerator>(),

128 +

"C++ test generator");

129 +

CreateTempFile("google/protobuf/cpp_features.proto",

130 +

pb::CppFeatures::descriptor()->file()->DebugString());

131 +

CreateTempFile("foo.proto",

132 +

R"schema(

133 +

edition = "2023";

134 + 135 +

import "google/protobuf/cpp_features.proto";

136 + 137 +

package foo;

138 + 139 +

enum Bar {

140 +

AAA = 0;

141 +

BBB = 1;

142 +

}

143 + 144 +

message Foo {

145 +

Bar bar_enum = 1 [features.(pb.cpp).legacy_closed_enum = true];

146 +

})schema");

147 + 148 +

RunProtoc(absl::Substitute(

149 +

"protocol_compiler --proto_path=$$tmpdir --cpp_out=$$tmpdir "

150 +

"--python_out=$$tmpdir foo.proto $0 "

151 +

"google/protobuf/cpp_features.proto",

152 +

google::protobuf::DescriptorProto::descriptor()->file()->name()));

153 + 154 +

ExpectNoErrors();

155 +

}

156 + 157 +

INSTANTIATE_TEST_SUITE_P(PythonGeneratorTest, PythonGeneratorTest,

158 +

testing::Bool());

159 + 103 160

} // namespace

104 161

} // namespace python

105 162

} // namespace compiler

Original file line number Diff line number Diff line change

@@ -5610,7 +5610,7 @@ static void InferLegacyProtoFeatures(const ProtoT& proto,

5610 5610

static void InferLegacyProtoFeatures(const FieldDescriptorProto& proto,

5611 5611

const FieldOptions& options,

5612 5612

Edition edition, FeatureSet& features) {

5613 -

if (!features.MutableExtension(pb::cpp)->has_string_type()) {

5613 +

if (!features.GetExtension(pb::cpp).has_string_type()) {

5614 5614

if (options.ctype() == FieldOptions::CORD) {

5615 5615

features.MutableExtension(pb::cpp)->set_string_type(

5616 5616

pb::CppFeatures::CORD);

Original file line number Diff line number Diff line change

@@ -1135,6 +1135,11 @@ message FeatureSet {

1135 1135

type: ".pb.JavaFeatures"

1136 1136

},

1137 1137

declaration = { number: 1002, full_name: ".pb.go", type: ".pb.GoFeatures" },

1138 +

declaration = {

1139 +

number: 1003,

1140 +

full_name: ".pb.python",

1141 +

type: ".pb.PythonFeatures"

1142 +

},

1138 1143

declaration = {

1139 1144

number: 9990,

1140 1145

full_name: ".pb.proto1",

Original file line number Diff line number Diff line change

@@ -12053,6 +12053,49 @@ TEST_F(DescriptorPoolFeaturesTest, OverrideDefaults) {

12053 12053

)pb"));

12054 12054

}

12055 12055 12056 +

TEST_F(DescriptorPoolFeaturesTest, OverrideFieldDefaults) {

12057 +

FeatureSetDefaults defaults = ParseTextOrDie(R"pb(

12058 +

defaults {

12059 +

edition: EDITION_PROTO2

12060 +

overridable_features {

12061 +

field_presence: EXPLICIT

12062 +

enum_type: CLOSED

12063 +

repeated_field_encoding: EXPANDED

12064 +

utf8_validation: VERIFY

12065 +

message_encoding: LENGTH_PREFIXED

12066 +

json_format: ALLOW

12067 +

enforce_naming_style: STYLE_LEGACY

12068 +

}

12069 +

}

12070 +

minimum_edition: EDITION_PROTO2

12071 +

maximum_edition: EDITION_2023

12072 +

)pb");

12073 +

EXPECT_OK(pool_.SetFeatureSetDefaults(std::move(defaults)));

12074 + 12075 +

FileDescriptorProto file_proto = ParseTextOrDie(R"pb(

12076 +

name: "foo.proto"

12077 +

syntax: "editions"

12078 +

edition: EDITION_PROTO3

12079 +

message_type {

12080 +

name: "Foo"

12081 +

field { name: "bar" number: 1 label: LABEL_OPTIONAL type: TYPE_INT64 }

12082 +

}

12083 +

)pb");

12084 + 12085 +

BuildDescriptorMessagesInTestPool();

12086 +

const FileDescriptor* file = ABSL_DIE_IF_NULL(pool_.BuildFile(file_proto));

12087 +

const FieldDescriptor* field = file->message_type(0)->field(0);

12088 +

EXPECT_THAT(GetFeatures(field), EqualsProto(R"pb(

12089 +

field_presence: EXPLICIT

12090 +

enum_type: CLOSED

12091 +

repeated_field_encoding: EXPANDED

12092 +

utf8_validation: VERIFY

12093 +

message_encoding: LENGTH_PREFIXED

12094 +

json_format: ALLOW

12095 +

enforce_naming_style: STYLE_LEGACY

12096 +

)pb"));

12097 +

}

12098 + 12056 12099

TEST_F(DescriptorPoolFeaturesTest, ResolvesFeaturesForCppDefault) {

12057 12100

EXPECT_FALSE(pool_.ResolvesFeaturesFor(pb::test));

12058 12101

EXPECT_FALSE(pool_.ResolvesFeaturesFor(pb::TestMessage::test_message));

You can’t perform that action at this time.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4