| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # | 2 # |
| 3 # Protocol Buffers - Google's data interchange format | 3 # Protocol Buffers - Google's data interchange format |
| 4 # Copyright 2008 Google Inc. All rights reserved. | 4 # Copyright 2008 Google Inc. All rights reserved. |
| 5 # https://developers.google.com/protocol-buffers/ | 5 # https://developers.google.com/protocol-buffers/ |
| 6 # | 6 # |
| 7 # Redistribution and use in source and binary forms, with or without | 7 # Redistribution and use in source and binary forms, with or without |
| 8 # modification, are permitted provided that the following conditions are | 8 # modification, are permitted provided that the following conditions are |
| 9 # met: | 9 # met: |
| 10 # | 10 # |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 31 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | 32 |
| 33 """A conformance test implementation for the Python protobuf library. | 33 """A conformance test implementation for the Python protobuf library. |
| 34 | 34 |
| 35 See conformance.proto for more information. | 35 See conformance.proto for more information. |
| 36 """ | 36 """ |
| 37 | 37 |
| 38 import struct | 38 import struct |
| 39 import sys | 39 import sys |
| 40 import os | 40 import os |
| 41 from google.protobuf import message |
| 41 from google.protobuf import json_format | 42 from google.protobuf import json_format |
| 42 from google.protobuf import message | |
| 43 from google.protobuf import test_messages_proto3_pb2 | |
| 44 import conformance_pb2 | 43 import conformance_pb2 |
| 45 | 44 |
| 46 sys.stdout = os.fdopen(sys.stdout.fileno(), 'wb', 0) | 45 sys.stdout = os.fdopen(sys.stdout.fileno(), 'wb', 0) |
| 47 sys.stdin = os.fdopen(sys.stdin.fileno(), 'rb', 0) | 46 sys.stdin = os.fdopen(sys.stdin.fileno(), 'rb', 0) |
| 48 | 47 |
| 49 test_count = 0 | 48 test_count = 0 |
| 50 verbose = False | 49 verbose = False |
| 51 | 50 |
| 52 class ProtocolError(Exception): | 51 class ProtocolError(Exception): |
| 53 pass | 52 pass |
| 54 | 53 |
| 55 def do_test(request): | 54 def do_test(request): |
| 56 test_message = test_messages_proto3_pb2.TestAllTypes() | 55 test_message = conformance_pb2.TestAllTypes() |
| 57 response = conformance_pb2.ConformanceResponse() | 56 response = conformance_pb2.ConformanceResponse() |
| 58 test_message = test_messages_proto3_pb2.TestAllTypes() | 57 test_message = conformance_pb2.TestAllTypes() |
| 59 | 58 |
| 60 try: | 59 try: |
| 61 if request.WhichOneof('payload') == 'protobuf_payload': | 60 if request.WhichOneof('payload') == 'protobuf_payload': |
| 62 try: | 61 try: |
| 63 test_message.ParseFromString(request.protobuf_payload) | 62 test_message.ParseFromString(request.protobuf_payload) |
| 64 except message.DecodeError as e: | 63 except message.DecodeError as e: |
| 65 response.parse_error = str(e) | 64 response.parse_error = str(e) |
| 66 return response | 65 return response |
| 67 | 66 |
| 68 elif request.WhichOneof('payload') == 'json_payload': | 67 elif request.WhichOneof('payload') == 'json_payload': |
| 69 try: | 68 try: |
| 70 json_format.Parse(request.json_payload, test_message) | 69 json_format.Parse(request.json_payload, test_message) |
| 71 except Exception as e: | 70 except json_format.ParseError as e: |
| 72 response.parse_error = str(e) | 71 response.parse_error = str(e) |
| 73 return response | 72 return response |
| 74 | 73 |
| 75 else: | 74 else: |
| 76 raise ProtocolError("Request didn't have payload.") | 75 raise ProtocolError("Request didn't have payload.") |
| 77 | 76 |
| 78 if request.requested_output_format == conformance_pb2.UNSPECIFIED: | 77 if request.requested_output_format == conformance_pb2.UNSPECIFIED: |
| 79 raise ProtocolError("Unspecified output format") | 78 raise ProtocolError("Unspecified output format") |
| 80 | 79 |
| 81 elif request.requested_output_format == conformance_pb2.PROTOBUF: | 80 elif request.requested_output_format == conformance_pb2.PROTOBUF: |
| 82 response.protobuf_payload = test_message.SerializeToString() | 81 response.protobuf_payload = test_message.SerializeToString() |
| 83 | 82 |
| 84 elif request.requested_output_format == conformance_pb2.JSON: | 83 elif request.requested_output_format == conformance_pb2.JSON: |
| 85 try: | 84 response.json_payload = json_format.MessageToJson(test_message) |
| 86 response.json_payload = json_format.MessageToJson(test_message) | |
| 87 except Exception as e: | |
| 88 response.serialize_error = str(e) | |
| 89 return response | |
| 90 | 85 |
| 91 except Exception as e: | 86 except Exception as e: |
| 92 response.runtime_error = str(e) | 87 response.runtime_error = str(e) |
| 93 | 88 |
| 94 return response | 89 return response |
| 95 | 90 |
| 96 def do_test_io(): | 91 def do_test_io(): |
| 97 length_bytes = sys.stdin.read(4) | 92 length_bytes = sys.stdin.read(4) |
| 98 if len(length_bytes) == 0: | 93 if len(length_bytes) == 0: |
| 99 return False # EOF | 94 return False # EOF |
| (...skipping 26 matching lines...) Expand all Loading... |
| 126 global test_count | 121 global test_count |
| 127 test_count += 1 | 122 test_count += 1 |
| 128 | 123 |
| 129 return True | 124 return True |
| 130 | 125 |
| 131 while True: | 126 while True: |
| 132 if not do_test_io(): | 127 if not do_test_io(): |
| 133 sys.stderr.write("conformance_python: received EOF from test runner " + | 128 sys.stderr.write("conformance_python: received EOF from test runner " + |
| 134 "after %s tests, exiting\n" % (test_count)) | 129 "after %s tests, exiting\n" % (test_count)) |
| 135 sys.exit(0) | 130 sys.exit(0) |
| OLD | NEW |