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 json_format |
41 from google.protobuf import message | 42 from google.protobuf import message |
42 from google.protobuf import json_format | 43 from google.protobuf import test_messages_proto3_pb2 |
43 import conformance_pb2 | 44 import conformance_pb2 |
44 | 45 |
45 sys.stdout = os.fdopen(sys.stdout.fileno(), 'wb', 0) | 46 sys.stdout = os.fdopen(sys.stdout.fileno(), 'wb', 0) |
46 sys.stdin = os.fdopen(sys.stdin.fileno(), 'rb', 0) | 47 sys.stdin = os.fdopen(sys.stdin.fileno(), 'rb', 0) |
47 | 48 |
48 test_count = 0 | 49 test_count = 0 |
49 verbose = False | 50 verbose = False |
50 | 51 |
51 class ProtocolError(Exception): | 52 class ProtocolError(Exception): |
52 pass | 53 pass |
53 | 54 |
54 def do_test(request): | 55 def do_test(request): |
55 test_message = conformance_pb2.TestAllTypes() | 56 test_message = test_messages_proto3_pb2.TestAllTypes() |
56 response = conformance_pb2.ConformanceResponse() | 57 response = conformance_pb2.ConformanceResponse() |
57 test_message = conformance_pb2.TestAllTypes() | 58 test_message = test_messages_proto3_pb2.TestAllTypes() |
58 | 59 |
59 try: | 60 try: |
60 if request.WhichOneof('payload') == 'protobuf_payload': | 61 if request.WhichOneof('payload') == 'protobuf_payload': |
61 try: | 62 try: |
62 test_message.ParseFromString(request.protobuf_payload) | 63 test_message.ParseFromString(request.protobuf_payload) |
63 except message.DecodeError as e: | 64 except message.DecodeError as e: |
64 response.parse_error = str(e) | 65 response.parse_error = str(e) |
65 return response | 66 return response |
66 | 67 |
67 elif request.WhichOneof('payload') == 'json_payload': | 68 elif request.WhichOneof('payload') == 'json_payload': |
68 try: | 69 try: |
69 json_format.Parse(request.json_payload, test_message) | 70 json_format.Parse(request.json_payload, test_message) |
70 except json_format.ParseError as e: | 71 except Exception as e: |
71 response.parse_error = str(e) | 72 response.parse_error = str(e) |
72 return response | 73 return response |
73 | 74 |
74 else: | 75 else: |
75 raise ProtocolError("Request didn't have payload.") | 76 raise ProtocolError("Request didn't have payload.") |
76 | 77 |
77 if request.requested_output_format == conformance_pb2.UNSPECIFIED: | 78 if request.requested_output_format == conformance_pb2.UNSPECIFIED: |
78 raise ProtocolError("Unspecified output format") | 79 raise ProtocolError("Unspecified output format") |
79 | 80 |
80 elif request.requested_output_format == conformance_pb2.PROTOBUF: | 81 elif request.requested_output_format == conformance_pb2.PROTOBUF: |
81 response.protobuf_payload = test_message.SerializeToString() | 82 response.protobuf_payload = test_message.SerializeToString() |
82 | 83 |
83 elif request.requested_output_format == conformance_pb2.JSON: | 84 elif request.requested_output_format == conformance_pb2.JSON: |
84 response.json_payload = json_format.MessageToJson(test_message) | 85 try: |
| 86 response.json_payload = json_format.MessageToJson(test_message) |
| 87 except Exception as e: |
| 88 response.serialize_error = str(e) |
| 89 return response |
85 | 90 |
86 except Exception as e: | 91 except Exception as e: |
87 response.runtime_error = str(e) | 92 response.runtime_error = str(e) |
88 | 93 |
89 return response | 94 return response |
90 | 95 |
91 def do_test_io(): | 96 def do_test_io(): |
92 length_bytes = sys.stdin.read(4) | 97 length_bytes = sys.stdin.read(4) |
93 if len(length_bytes) == 0: | 98 if len(length_bytes) == 0: |
94 return False # EOF | 99 return False # EOF |
(...skipping 26 matching lines...) Expand all Loading... |
121 global test_count | 126 global test_count |
122 test_count += 1 | 127 test_count += 1 |
123 | 128 |
124 return True | 129 return True |
125 | 130 |
126 while True: | 131 while True: |
127 if not do_test_io(): | 132 if not do_test_io(): |
128 sys.stderr.write("conformance_python: received EOF from test runner " + | 133 sys.stderr.write("conformance_python: received EOF from test runner " + |
129 "after %s tests, exiting\n" % (test_count)) | 134 "after %s tests, exiting\n" % (test_count)) |
130 sys.exit(0) | 135 sys.exit(0) |
OLD | NEW |