Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(281)

Side by Side Diff: third_party/protobuf/conformance/conformance_python.py

Issue 1842653006: Update //third_party/protobuf to version 3. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: merge Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #!/usr/bin/env python
2 #
3 # Protocol Buffers - Google's data interchange format
4 # Copyright 2008 Google Inc. All rights reserved.
5 # https://developers.google.com/protocol-buffers/
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are
9 # met:
10 #
11 # * Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following disclaimer
15 # in the documentation and/or other materials provided with the
16 # distribution.
17 # * Neither the name of Google Inc. nor the names of its
18 # contributors may be used to endorse or promote products derived from
19 # this software without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33 """A conformance test implementation for the Python protobuf library.
34
35 See conformance.proto for more information.
36 """
37
38 import struct
39 import sys
40 import os
41 from google.protobuf import message
42 from google.protobuf import json_format
43 import conformance_pb2
44
45 sys.stdout = os.fdopen(sys.stdout.fileno(), 'wb', 0)
46 sys.stdin = os.fdopen(sys.stdin.fileno(), 'rb', 0)
47
48 test_count = 0
49 verbose = False
50
51 class ProtocolError(Exception):
52 pass
53
54 def do_test(request):
55 test_message = conformance_pb2.TestAllTypes()
56 response = conformance_pb2.ConformanceResponse()
57 test_message = conformance_pb2.TestAllTypes()
58
59 try:
60 if request.WhichOneof('payload') == 'protobuf_payload':
61 try:
62 test_message.ParseFromString(request.protobuf_payload)
63 except message.DecodeError as e:
64 response.parse_error = str(e)
65 return response
66
67 elif request.WhichOneof('payload') == 'json_payload':
68 try:
69 json_format.Parse(request.json_payload, test_message)
70 except json_format.ParseError as e:
71 response.parse_error = str(e)
72 return response
73
74 else:
75 raise ProtocolError("Request didn't have payload.")
76
77 if request.requested_output_format == conformance_pb2.UNSPECIFIED:
78 raise ProtocolError("Unspecified output format")
79
80 elif request.requested_output_format == conformance_pb2.PROTOBUF:
81 response.protobuf_payload = test_message.SerializeToString()
82
83 elif request.requested_output_format == conformance_pb2.JSON:
84 response.json_payload = json_format.MessageToJson(test_message)
85
86 except Exception as e:
87 response.runtime_error = str(e)
88
89 return response
90
91 def do_test_io():
92 length_bytes = sys.stdin.read(4)
93 if len(length_bytes) == 0:
94 return False # EOF
95 elif len(length_bytes) != 4:
96 raise IOError("I/O error")
97
98 # "I" is "unsigned int", so this depends on running on a platform with
99 # 32-bit "unsigned int" type. The Python struct module unfortunately
100 # has no format specifier for uint32_t.
101 length = struct.unpack("<I", length_bytes)[0]
102 serialized_request = sys.stdin.read(length)
103 if len(serialized_request) != length:
104 raise IOError("I/O error")
105
106 request = conformance_pb2.ConformanceRequest()
107 request.ParseFromString(serialized_request)
108
109 response = do_test(request)
110
111 serialized_response = response.SerializeToString()
112 sys.stdout.write(struct.pack("<I", len(serialized_response)))
113 sys.stdout.write(serialized_response)
114 sys.stdout.flush()
115
116 if verbose:
117 sys.stderr.write("conformance_python: request=%s, response=%s\n" % (
118 request.ShortDebugString().c_str(),
119 response.ShortDebugString().c_str()))
120
121 global test_count
122 test_count += 1
123
124 return True
125
126 while True:
127 if not do_test_io():
128 sys.stderr.write("conformance_python: received EOF from test runner " +
129 "after %s tests, exiting\n" % (test_count))
130 sys.exit(0)
OLDNEW
« no previous file with comments | « third_party/protobuf/conformance/conformance_objc.m ('k') | third_party/protobuf/conformance/conformance_ruby.rb » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698