diff options
author | Jisi Liu <jisi.liu@gmail.com> | 2017-07-25 11:52:33 -0700 |
---|---|---|
committer | Jisi Liu <jisi.liu@gmail.com> | 2017-07-25 11:52:33 -0700 |
commit | 759245a49a00315a41b28da8fe52a2894d4f57ea (patch) | |
tree | 5a0032a1a3619a448d5301da131907ca8449730f /conformance/conformance_python.py | |
parent | 4bff88e0fb2338657a781eeee0c5362a57b57bf3 (diff) | |
download | protobuf-759245a49a00315a41b28da8fe52a2894d4f57ea.tar.gz protobuf-759245a49a00315a41b28da8fe52a2894d4f57ea.tar.bz2 protobuf-759245a49a00315a41b28da8fe52a2894d4f57ea.zip |
Merge from master
Diffstat (limited to 'conformance/conformance_python.py')
-rwxr-xr-x | conformance/conformance_python.py | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/conformance/conformance_python.py b/conformance/conformance_python.py index 7ace9b16..c5ba2467 100755 --- a/conformance/conformance_python.py +++ b/conformance/conformance_python.py @@ -38,9 +38,12 @@ See conformance.proto for more information. import struct import sys import os +from google.protobuf import descriptor +from google.protobuf import descriptor_pool from google.protobuf import json_format from google.protobuf import message from google.protobuf import test_messages_proto3_pb2 +from google.protobuf import test_messages_proto2_pb2 import conformance_pb2 sys.stdout = os.fdopen(sys.stdout.fileno(), 'wb', 0) @@ -53,9 +56,17 @@ class ProtocolError(Exception): pass def do_test(request): - test_message = test_messages_proto3_pb2.TestAllTypes() + isProto3 = (request.message_type == "protobuf_test_messages.proto3.TestAllTypesProto3") + isJson = (request.WhichOneof('payload') == 'json_payload') + isProto2 = (request.message_type == "protobuf_test_messages.proto2.TestAllTypesProto2") + + if (not isProto3) and (not isJson) and (not isProto2): + 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() + response = conformance_pb2.ConformanceResponse() - test_message = test_messages_proto3_pb2.TestAllTypes() try: if request.WhichOneof('payload') == 'protobuf_payload': @@ -63,8 +74,8 @@ def do_test(request): test_message.ParseFromString(request.protobuf_payload) except message.DecodeError as e: response.parse_error = str(e) - return response - + return response + elif request.WhichOneof('payload') == 'json_payload': try: json_format.Parse(request.json_payload, test_message) @@ -82,7 +93,7 @@ def do_test(request): response.protobuf_payload = test_message.SerializeToString() elif request.requested_output_format == conformance_pb2.JSON: - try: + try: response.json_payload = json_format.MessageToJson(test_message) except Exception as e: response.serialize_error = str(e) |