aboutsummaryrefslogtreecommitdiff
path: root/conformance/conformance_python.py
diff options
context:
space:
mode:
authorYilun Chong <yilunchong@google.com>2017-06-27 18:24:15 -0700
committerYilun Chong <yilunchong@google.com>2017-06-27 18:24:15 -0700
commit18a0c2c4d2894e820f96494f33d8ca3ea33dec3a (patch)
treeba6297a500f23a17ba2844f3daeae0f02561ac80 /conformance/conformance_python.py
parent2ad74e1606728564cc777aa4917d7e2299317eda (diff)
downloadprotobuf-18a0c2c4d2894e820f96494f33d8ca3ea33dec3a.tar.gz
protobuf-18a0c2c4d2894e820f96494f33d8ca3ea33dec3a.tar.bz2
protobuf-18a0c2c4d2894e820f96494f33d8ca3ea33dec3a.zip
add proto2 supported for cpp,python,nodejs,ruby,php
Diffstat (limited to 'conformance/conformance_python.py')
-rwxr-xr-xconformance/conformance_python.py33
1 files changed, 26 insertions, 7 deletions
diff --git a/conformance/conformance_python.py b/conformance/conformance_python.py
index 7ace9b16..846ccbc6 100755
--- a/conformance/conformance_python.py
+++ b/conformance/conformance_python.py
@@ -41,6 +41,7 @@ import os
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)
@@ -56,14 +57,26 @@ def do_test(request):
test_message = test_messages_proto3_pb2.TestAllTypes()
response = conformance_pb2.ConformanceResponse()
test_message = test_messages_proto3_pb2.TestAllTypes()
+ test_message_proto2 = test_messages_proto2_pb2.TestAllTypesProto2()
+ isProto3 = (request.message_type == "proto3")
+ isJson = (request.WhichOneof('payload') == 'json_payload')
try:
if request.WhichOneof('payload') == 'protobuf_payload':
- try:
- test_message.ParseFromString(request.protobuf_payload)
- except message.DecodeError as e:
- response.parse_error = str(e)
- return response
+ if isProto3:
+ try:
+ test_message.ParseFromString(request.protobuf_payload)
+ except message.DecodeError as e:
+ response.parse_error = str(e)
+ return response
+ elif request.message_type == "proto2":
+ try:
+ test_message_proto2.ParseFromString(request.protobuf_payload)
+ except message.DecodeError as e:
+ response.parse_error = str(e)
+ return response
+ else:
+ raise ProtocolError("Protobuf request doesn't have specific payload type")
elif request.WhichOneof('payload') == 'json_payload':
try:
@@ -79,11 +92,17 @@ def do_test(request):
raise ProtocolError("Unspecified output format")
elif request.requested_output_format == conformance_pb2.PROTOBUF:
- response.protobuf_payload = test_message.SerializeToString()
+ if isProto3 or isJson:
+ response.protobuf_payload = test_message.SerializeToString()
+ else:
+ response.protobuf_payload = test_message_proto2.SerializeToString()
elif request.requested_output_format == conformance_pb2.JSON:
try:
- response.json_payload = json_format.MessageToJson(test_message)
+ if isProto3 or isJson:
+ response.json_payload = json_format.MessageToJson(test_message)
+ else:
+ response.json_payload = json_format.MessageToJson(test_message_proto2)
except Exception as e:
response.serialize_error = str(e)
return response