Skip to content

Commit 15eccf3

Browse files
mkruskal-googlecopybara-github
authored andcommitted
Implement Editions in Pure Python.
This change only covers pure python, and follow-up changes will handle C++/upb variants and actually enable editions support. The C++ one works (as evident from the conformance tests), but needs some APIs added to allow for testing. PiperOrigin-RevId: 580304039
1 parent fc456ae commit 15eccf3

17 files changed

+1242
-283
lines changed

conformance/BUILD.bazel

+1-2
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,7 @@ py_binary(
261261
deps = [
262262
":conformance_py_proto",
263263
"//:protobuf_python",
264-
"//python:test_messages_proto2_py_proto",
265-
"//python:test_messages_proto3_py_proto",
264+
"//python:conformance_test_py_proto",
266265
],
267266
)
268267

conformance/conformance_python.py

+16-6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
from google.protobuf import test_messages_proto2_pb2
2020
from google.protobuf import test_messages_proto3_pb2
2121
from conformance import conformance_pb2
22+
from google.protobuf.editions.golden import test_messages_proto2_editions_pb2
23+
from google.protobuf.editions.golden import test_messages_proto3_editions_pb2
2224

2325
test_count = 0
2426
verbose = False
@@ -28,6 +30,18 @@ class ProtocolError(Exception):
2830
pass
2931

3032

33+
def _create_test_message(type):
34+
if type == "protobuf_test_messages.proto2.TestAllTypesProto2":
35+
return test_messages_proto2_pb2.TestAllTypesProto2()
36+
if type == "protobuf_test_messages.proto3.TestAllTypesProto3":
37+
return test_messages_proto3_pb2.TestAllTypesProto3()
38+
if type == "protobuf_test_messages.editions.proto2.TestAllTypesProto2":
39+
return test_messages_proto2_editions_pb2.TestAllTypesProto2()
40+
if type == "protobuf_test_messages.editions.proto3.TestAllTypesProto3":
41+
return test_messages_proto3_editions_pb2.TestAllTypesProto3()
42+
return None
43+
44+
3145
def do_test(request):
3246
response = conformance_pb2.ConformanceResponse()
3347

@@ -85,16 +99,12 @@ def do_test(request):
8599
response.protobuf_payload = failure_set.SerializeToString()
86100
return response
87101

88-
isProto3 = (request.message_type == "protobuf_test_messages.proto3.TestAllTypesProto3")
89102
isJson = (request.WhichOneof('payload') == 'json_payload')
90-
isProto2 = (request.message_type == "protobuf_test_messages.proto2.TestAllTypesProto2")
103+
test_message = _create_test_message(request.message_type)
91104

92-
if (not isProto3) and (not isJson) and (not isProto2):
105+
if (not isJson) and (test_message is None):
93106
raise ProtocolError("Protobuf request doesn't have specific payload type")
94107

95-
test_message = test_messages_proto2_pb2.TestAllTypesProto2() if isProto2 else \
96-
test_messages_proto3_pb2.TestAllTypesProto3()
97-
98108
try:
99109
if request.WhichOneof('payload') == 'protobuf_payload':
100110
try:

conformance/failure_list_python.txt

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
Recommended.Proto3.JsonInput.IgnoreUnknownEnumStringValueInMapValue.ProtobufOutput
22
Recommended.Proto3.JsonInput.IgnoreUnknownEnumStringValueInOptionalField.ProtobufOutput
33
Recommended.Proto3.JsonInput.IgnoreUnknownEnumStringValueInRepeatedField.ProtobufOutput
4+
Recommended.Editions_Proto3.JsonInput.IgnoreUnknownEnumStringValueInMapValue.ProtobufOutput
5+
Recommended.Editions_Proto3.JsonInput.IgnoreUnknownEnumStringValueInOptionalField.ProtobufOutput
6+
Recommended.Editions_Proto3.JsonInput.IgnoreUnknownEnumStringValueInRepeatedField.ProtobufOutput

conformance/failure_list_python_cpp.txt

+3
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@
99
Recommended.Proto3.JsonInput.IgnoreUnknownEnumStringValueInMapValue.ProtobufOutput
1010
Recommended.Proto3.JsonInput.IgnoreUnknownEnumStringValueInOptionalField.ProtobufOutput
1111
Recommended.Proto3.JsonInput.IgnoreUnknownEnumStringValueInRepeatedField.ProtobufOutput
12+
Recommended.Editions_Proto3.JsonInput.IgnoreUnknownEnumStringValueInMapValue.ProtobufOutput
13+
Recommended.Editions_Proto3.JsonInput.IgnoreUnknownEnumStringValueInOptionalField.ProtobufOutput
14+
Recommended.Editions_Proto3.JsonInput.IgnoreUnknownEnumStringValueInRepeatedField.ProtobufOutput

conformance/text_format_failure_list_python.txt

+6
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@ Required.Proto3.TextFormatInput.StringLiteralBasicEscapesBytes.ProtobufOutput
77
Required.Proto3.TextFormatInput.StringLiteralBasicEscapesBytes.TextFormatOutput
88
Required.Proto3.TextFormatInput.StringLiteralBasicEscapesString.ProtobufOutput
99
Required.Proto3.TextFormatInput.StringLiteralBasicEscapesString.TextFormatOutput
10+
Required.Editions_Proto3.TextFormatInput.FloatFieldMaxValue.ProtobufOutput
11+
Required.Editions_Proto3.TextFormatInput.FloatFieldMaxValue.TextFormatOutput
12+
Required.Editions_Proto3.TextFormatInput.StringLiteralBasicEscapesBytes.ProtobufOutput
13+
Required.Editions_Proto3.TextFormatInput.StringLiteralBasicEscapesBytes.TextFormatOutput
14+
Required.Editions_Proto3.TextFormatInput.StringLiteralBasicEscapesString.ProtobufOutput
15+
Required.Editions_Proto3.TextFormatInput.StringLiteralBasicEscapesString.TextFormatOutput

conformance/text_format_failure_list_python_cpp.txt

+4
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@ Required.Proto3.TextFormatInput.StringLiteralBasicEscapesBytes.ProtobufOutput
22
Required.Proto3.TextFormatInput.StringLiteralBasicEscapesBytes.TextFormatOutput
33
Required.Proto3.TextFormatInput.StringLiteralBasicEscapesString.ProtobufOutput
44
Required.Proto3.TextFormatInput.StringLiteralBasicEscapesString.TextFormatOutput
5+
Required.Editions_Proto3.TextFormatInput.StringLiteralBasicEscapesBytes.ProtobufOutput
6+
Required.Editions_Proto3.TextFormatInput.StringLiteralBasicEscapesBytes.TextFormatOutput
7+
Required.Editions_Proto3.TextFormatInput.StringLiteralBasicEscapesString.ProtobufOutput
8+
Required.Editions_Proto3.TextFormatInput.StringLiteralBasicEscapesString.TextFormatOutput

python/build_targets.bzl

+33-37
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ load("//:protobuf.bzl", "internal_py_proto_library")
1212
load("//build_defs:arch_tests.bzl", "aarch64_test", "x86_64_test")
1313
load("//build_defs:cpp_opts.bzl", "COPTS")
1414
load("//conformance:defs.bzl", "conformance_test")
15+
load("//src/google/protobuf/editions:defaults.bzl", "compile_edition_defaults", "embed_edition_defaults")
1516
load(":internal.bzl", "internal_copy_files", "internal_py_test")
1617

1718
def build_targets(name):
@@ -143,8 +144,23 @@ def build_targets(name):
143144
],
144145
)
145146

146-
py_library(
147-
name = "python_srcs",
147+
compile_edition_defaults(
148+
name = "python_edition_defaults",
149+
srcs = ["//:descriptor_proto"],
150+
maximum_edition = "2023",
151+
minimum_edition = "PROTO2",
152+
)
153+
154+
embed_edition_defaults(
155+
name = "embedded_python_edition_defaults_generate",
156+
defaults = "python_edition_defaults",
157+
output = "google/protobuf/internal/python_edition_defaults.py",
158+
placeholder = "DEFAULTS_VALUE",
159+
template = "google/protobuf/internal/python_edition_defaults.py.template",
160+
)
161+
162+
native.filegroup(
163+
name = "python_src_files",
148164
srcs = native.glob(
149165
[
150166
"google/protobuf/**/*.py",
@@ -154,7 +170,12 @@ def build_targets(name):
154170
"google/protobuf/internal/test_util.py",
155171
"google/protobuf/internal/import_test_package/__init__.py",
156172
],
157-
),
173+
) + ["google/protobuf/internal/python_edition_defaults.py"],
174+
)
175+
176+
py_library(
177+
name = "python_srcs",
178+
srcs = [":python_src_files"],
158179
imports = ["python"],
159180
srcs_version = "PY2AND3",
160181
visibility = [
@@ -196,19 +217,13 @@ def build_targets(name):
196217
)
197218

198219
internal_copy_files(
199-
name = "copied_test_messages_proto2_files",
220+
name = "copied_conformance_test_files",
200221
testonly = 1,
201222
srcs = [
202223
"//src/google/protobuf:test_messages_proto2.proto",
203-
],
204-
strip_prefix = "src",
205-
)
206-
207-
internal_copy_files(
208-
name = "copied_test_messages_proto3_files",
209-
testonly = 1,
210-
srcs = [
211224
"//src/google/protobuf:test_messages_proto3.proto",
225+
"//src/google/protobuf/editions:golden/test_messages_proto2_editions.proto",
226+
"//src/google/protobuf/editions:golden/test_messages_proto3_editions.proto",
212227
],
213228
strip_prefix = "src",
214229
)
@@ -241,22 +256,9 @@ def build_targets(name):
241256
)
242257

243258
internal_py_proto_library(
244-
name = "test_messages_proto2_py_proto",
245-
testonly = 1,
246-
srcs = [":copied_test_messages_proto2_files"],
247-
include = ".",
248-
default_runtime = "//:protobuf_python",
249-
protoc = "//:protoc",
250-
visibility = [
251-
"//conformance:__pkg__",
252-
"//python:__subpackages__",
253-
],
254-
)
255-
256-
internal_py_proto_library(
257-
name = "test_messages_proto3_py_proto",
259+
name = "conformance_test_py_proto",
258260
testonly = 1,
259-
srcs = [":copied_test_messages_proto3_files"],
261+
srcs = [":copied_conformance_test_files"],
260262
include = ".",
261263
default_runtime = "//:protobuf_python",
262264
protoc = "//:protoc",
@@ -404,6 +406,7 @@ def build_targets(name):
404406
":use_fast_cpp_protos": ["@platforms//:incompatible"],
405407
"//conditions:default": [],
406408
}),
409+
maximum_edition = "2023",
407410
testee = "//conformance:conformance_python",
408411
text_format_failure_list = "//conformance:text_format_failure_list_python.txt",
409412
)
@@ -418,6 +421,7 @@ def build_targets(name):
418421
":use_fast_cpp_protos": [],
419422
"//conditions:default": ["@platforms//:incompatible"],
420423
}),
424+
maximum_edition = "2023",
421425
testee = "//conformance:conformance_python",
422426
text_format_failure_list = "//conformance:text_format_failure_list_python_cpp.txt",
423427
)
@@ -428,16 +432,8 @@ def build_targets(name):
428432

429433
pkg_files(
430434
name = "python_source_files",
431-
srcs = native.glob(
432-
[
433-
"google/protobuf/**/*.py",
434-
],
435-
exclude = [
436-
"google/protobuf/internal/*_test.py",
437-
"google/protobuf/internal/test_util.py",
438-
"google/protobuf/internal/import_test_package/__init__.py",
439-
],
440-
) + [
435+
srcs = [
436+
":python_src_files",
441437
"README.md",
442438
"google/__init__.py",
443439
"setup.cfg",

0 commit comments

Comments
 (0)