Skip to content

Commit

Permalink
Implement Editions in Pure Python
Browse files Browse the repository at this point in the history
COPYBARA_INTEGRATE_REVIEW=#14505 from achingbrain:patch-1 1a80ee9
FUTURE_COPYBARA_INTEGRATE_REVIEW=#14505 from achingbrain:patch-1 1a80ee9
PiperOrigin-RevId: 574304610
  • Loading branch information
mkruskal-google authored and copybara-github committed Oct 31, 2023
1 parent 3667102 commit a5b835c
Show file tree
Hide file tree
Showing 16 changed files with 633 additions and 238 deletions.
1 change: 1 addition & 0 deletions conformance/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ py_binary(
"//:protobuf_python",
"//python:test_messages_proto2_py_proto",
"//python:test_messages_proto3_py_proto",
"//src/google/protobuf/editions:test_messages_proto2_editions_py_pb2",
],
)

Expand Down
23 changes: 17 additions & 6 deletions conformance/conformance_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from google.protobuf import test_messages_proto2_pb2
from google.protobuf import test_messages_proto3_pb2
from conformance import conformance_pb2
from google.protobuf.editions.golden import test_messages_proto2_editions_pb2
from google.protobuf.editions.golden import test_messages_proto3_editions_pb2

test_count = 0
verbose = False
Expand All @@ -28,6 +30,19 @@ class ProtocolError(Exception):
pass


def _create_test_message(type):
match type:
case "protobuf_test_messages.proto2.TestAllTypesProto2":
return test_messages_proto2_pb2.TestAllTypesProto2()
case "protobuf_test_messages.proto3.TestAllTypesProto3":
return test_messages_proto3_pb2.TestAllTypesProto3()
case "protobuf_test_messages.editions.proto2.TestAllTypesProto2":
return test_messages_proto2_editions_pb2.TestAllTypesProto2()
case "protobuf_test_messages.editions.proto3.TestAllTypesProto3":
return test_messages_proto3_editions_pb2.TestAllTypesProto3()
return None


def do_test(request):
response = conformance_pb2.ConformanceResponse()

Expand Down Expand Up @@ -85,16 +100,12 @@ def do_test(request):
response.protobuf_payload = failure_set.SerializeToString()
return response

isProto3 = (request.message_type == "protobuf_test_messages.proto3.TestAllTypesProto3")
isJson = (request.WhichOneof('payload') == 'json_payload')
isProto2 = (request.message_type == "protobuf_test_messages.proto2.TestAllTypesProto2")
test_message = _create_test_message(request.message_type)

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

test_message = test_messages_proto2_pb2.TestAllTypesProto2() if isProto2 else \
test_messages_proto3_pb2.TestAllTypesProto3()

try:
if request.WhichOneof('payload') == 'protobuf_payload':
try:
Expand Down
3 changes: 3 additions & 0 deletions conformance/failure_list_python.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
Recommended.Proto3.JsonInput.IgnoreUnknownEnumStringValueInMapValue.ProtobufOutput
Recommended.Proto3.JsonInput.IgnoreUnknownEnumStringValueInOptionalField.ProtobufOutput
Recommended.Proto3.JsonInput.IgnoreUnknownEnumStringValueInRepeatedField.ProtobufOutput
Recommended.Editions_Proto3.JsonInput.IgnoreUnknownEnumStringValueInMapValue.ProtobufOutput
Recommended.Editions_Proto3.JsonInput.IgnoreUnknownEnumStringValueInOptionalField.ProtobufOutput
Recommended.Editions_Proto3.JsonInput.IgnoreUnknownEnumStringValueInRepeatedField.ProtobufOutput
3 changes: 3 additions & 0 deletions conformance/failure_list_python_cpp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@
Recommended.Proto3.JsonInput.IgnoreUnknownEnumStringValueInMapValue.ProtobufOutput
Recommended.Proto3.JsonInput.IgnoreUnknownEnumStringValueInOptionalField.ProtobufOutput
Recommended.Proto3.JsonInput.IgnoreUnknownEnumStringValueInRepeatedField.ProtobufOutput
Recommended.Editions_Proto3.JsonInput.IgnoreUnknownEnumStringValueInMapValue.ProtobufOutput
Recommended.Editions_Proto3.JsonInput.IgnoreUnknownEnumStringValueInOptionalField.ProtobufOutput
Recommended.Editions_Proto3.JsonInput.IgnoreUnknownEnumStringValueInRepeatedField.ProtobufOutput
6 changes: 6 additions & 0 deletions conformance/text_format_failure_list_python.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ Required.Proto3.TextFormatInput.StringLiteralBasicEscapesBytes.ProtobufOutput
Required.Proto3.TextFormatInput.StringLiteralBasicEscapesBytes.TextFormatOutput
Required.Proto3.TextFormatInput.StringLiteralBasicEscapesString.ProtobufOutput
Required.Proto3.TextFormatInput.StringLiteralBasicEscapesString.TextFormatOutput
Required.Editions_Proto3.TextFormatInput.FloatFieldMaxValue.ProtobufOutput
Required.Editions_Proto3.TextFormatInput.FloatFieldMaxValue.TextFormatOutput
Required.Editions_Proto3.TextFormatInput.StringLiteralBasicEscapesBytes.ProtobufOutput
Required.Editions_Proto3.TextFormatInput.StringLiteralBasicEscapesBytes.TextFormatOutput
Required.Editions_Proto3.TextFormatInput.StringLiteralBasicEscapesString.ProtobufOutput
Required.Editions_Proto3.TextFormatInput.StringLiteralBasicEscapesString.TextFormatOutput
4 changes: 4 additions & 0 deletions conformance/text_format_failure_list_python_cpp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ Required.Proto3.TextFormatInput.StringLiteralBasicEscapesBytes.ProtobufOutput
Required.Proto3.TextFormatInput.StringLiteralBasicEscapesBytes.TextFormatOutput
Required.Proto3.TextFormatInput.StringLiteralBasicEscapesString.ProtobufOutput
Required.Proto3.TextFormatInput.StringLiteralBasicEscapesString.TextFormatOutput
Required.Editions_Proto3.TextFormatInput.StringLiteralBasicEscapesBytes.ProtobufOutput
Required.Editions_Proto3.TextFormatInput.StringLiteralBasicEscapesBytes.TextFormatOutput
Required.Editions_Proto3.TextFormatInput.StringLiteralBasicEscapesString.ProtobufOutput
Required.Editions_Proto3.TextFormatInput.StringLiteralBasicEscapesString.TextFormatOutput
5 changes: 5 additions & 0 deletions docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -486,3 +486,8 @@ with info about your project (name and website) so we can add an entry for you.

* Website: https://square.github.io/wire/
* Extensions: 1185

1. Protons

* Website: https://github.com/ipfs/protons
* Extensions: 1186
20 changes: 19 additions & 1 deletion python/build_targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ load("//:protobuf.bzl", "internal_py_proto_library")
load("//build_defs:arch_tests.bzl", "aarch64_test", "x86_64_test")
load("//build_defs:cpp_opts.bzl", "COPTS")
load("//conformance:defs.bzl", "conformance_test")
load("//src/google/protobuf/editions:defaults.bzl", "compile_edition_defaults", "embed_edition_defaults")
load(":internal.bzl", "internal_copy_files", "internal_py_test")

def build_targets(name):
Expand Down Expand Up @@ -143,6 +144,21 @@ def build_targets(name):
],
)

compile_edition_defaults(
name = "python_edition_defaults",
srcs = ["//:descriptor_proto"],
maximum_edition = "2023",
minimum_edition = "PROTO2",
)

embed_edition_defaults(
name = "embedded_python_edition_defaults_generate",
defaults = "python_edition_defaults",
output = "google/protobuf/internal/python_edition_defaults.py",
placeholder = "DEFAULTS_VALUE",
template = "google/protobuf/internal/python_edition_defaults.py.template",
)

py_library(
name = "python_srcs",
srcs = native.glob(
Expand All @@ -154,7 +170,7 @@ def build_targets(name):
"google/protobuf/internal/test_util.py",
"google/protobuf/internal/import_test_package/__init__.py",
],
),
) + ["google/protobuf/internal/python_edition_defaults.py"],
imports = ["python"],
srcs_version = "PY2AND3",
visibility = [
Expand Down Expand Up @@ -404,6 +420,7 @@ def build_targets(name):
":use_fast_cpp_protos": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
maximum_edition = "2023",
testee = "//conformance:conformance_python",
text_format_failure_list = "//conformance:text_format_failure_list_python.txt",
)
Expand All @@ -418,6 +435,7 @@ def build_targets(name):
":use_fast_cpp_protos": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
maximum_edition = "2023",
testee = "//conformance:conformance_python",
text_format_failure_list = "//conformance:text_format_failure_list_python_cpp.txt",
)
Expand Down
Loading

0 comments on commit a5b835c

Please sign in to comment.