protobuf 6.33.0__cp39-abi3-manylinux2014_s390x.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of protobuf might be problematic. Click here for more details.
- google/_upb/_message.abi3.so +0 -0
- google/protobuf/__init__.py +10 -0
- google/protobuf/any.py +53 -0
- google/protobuf/any_pb2.py +37 -0
- google/protobuf/api_pb2.py +47 -0
- google/protobuf/compiler/__init__.py +0 -0
- google/protobuf/compiler/plugin_pb2.py +46 -0
- google/protobuf/descriptor.py +1676 -0
- google/protobuf/descriptor_database.py +172 -0
- google/protobuf/descriptor_pb2.py +3363 -0
- google/protobuf/descriptor_pool.py +1370 -0
- google/protobuf/duration.py +100 -0
- google/protobuf/duration_pb2.py +37 -0
- google/protobuf/empty_pb2.py +37 -0
- google/protobuf/field_mask_pb2.py +37 -0
- google/protobuf/internal/__init__.py +7 -0
- google/protobuf/internal/api_implementation.py +136 -0
- google/protobuf/internal/builder.py +118 -0
- google/protobuf/internal/containers.py +690 -0
- google/protobuf/internal/decoder.py +1066 -0
- google/protobuf/internal/encoder.py +806 -0
- google/protobuf/internal/enum_type_wrapper.py +112 -0
- google/protobuf/internal/extension_dict.py +194 -0
- google/protobuf/internal/field_mask.py +312 -0
- google/protobuf/internal/message_listener.py +55 -0
- google/protobuf/internal/python_edition_defaults.py +5 -0
- google/protobuf/internal/python_message.py +1599 -0
- google/protobuf/internal/testing_refleaks.py +128 -0
- google/protobuf/internal/type_checkers.py +455 -0
- google/protobuf/internal/well_known_types.py +695 -0
- google/protobuf/internal/wire_format.py +245 -0
- google/protobuf/json_format.py +1111 -0
- google/protobuf/message.py +448 -0
- google/protobuf/message_factory.py +190 -0
- google/protobuf/proto.py +153 -0
- google/protobuf/proto_builder.py +111 -0
- google/protobuf/proto_json.py +83 -0
- google/protobuf/proto_text.py +129 -0
- google/protobuf/pyext/__init__.py +0 -0
- google/protobuf/pyext/cpp_message.py +49 -0
- google/protobuf/reflection.py +36 -0
- google/protobuf/runtime_version.py +104 -0
- google/protobuf/service_reflection.py +272 -0
- google/protobuf/source_context_pb2.py +37 -0
- google/protobuf/struct_pb2.py +47 -0
- google/protobuf/symbol_database.py +179 -0
- google/protobuf/testdata/__init__.py +0 -0
- google/protobuf/text_encoding.py +106 -0
- google/protobuf/text_format.py +1884 -0
- google/protobuf/timestamp.py +112 -0
- google/protobuf/timestamp_pb2.py +37 -0
- google/protobuf/type_pb2.py +53 -0
- google/protobuf/unknown_fields.py +96 -0
- google/protobuf/util/__init__.py +0 -0
- google/protobuf/wrappers_pb2.py +53 -0
- protobuf-6.33.0.dist-info/LICENSE +32 -0
- protobuf-6.33.0.dist-info/METADATA +17 -0
- protobuf-6.33.0.dist-info/RECORD +59 -0
- protobuf-6.33.0.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# Protocol Buffers - Google's data interchange format
|
|
2
|
+
# Copyright 2008 Google Inc. All rights reserved.
|
|
3
|
+
#
|
|
4
|
+
# Use of this source code is governed by a BSD-style
|
|
5
|
+
# license that can be found in the LICENSE file or at
|
|
6
|
+
# https://developers.google.com/open-source/licenses/bsd
|
|
7
|
+
|
|
8
|
+
"""A subclass of unittest.TestCase which checks for reference leaks.
|
|
9
|
+
|
|
10
|
+
To use:
|
|
11
|
+
- Use testing_refleak.BaseTestCase instead of unittest.TestCase
|
|
12
|
+
- Configure and compile Python with --with-pydebug
|
|
13
|
+
|
|
14
|
+
If sys.gettotalrefcount() is not available (because Python was built without
|
|
15
|
+
the Py_DEBUG option), then this module is a no-op and tests will run normally.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
import copyreg
|
|
19
|
+
import gc
|
|
20
|
+
import sys
|
|
21
|
+
import unittest
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class LocalTestResult(unittest.TestResult):
|
|
25
|
+
"""A TestResult which forwards events to a parent object, except for Skips."""
|
|
26
|
+
|
|
27
|
+
def __init__(self, parent_result):
|
|
28
|
+
unittest.TestResult.__init__(self)
|
|
29
|
+
self.parent_result = parent_result
|
|
30
|
+
|
|
31
|
+
def addError(self, test, error):
|
|
32
|
+
self.parent_result.addError(test, error)
|
|
33
|
+
|
|
34
|
+
def addFailure(self, test, error):
|
|
35
|
+
self.parent_result.addFailure(test, error)
|
|
36
|
+
|
|
37
|
+
def addSkip(self, test, reason):
|
|
38
|
+
pass
|
|
39
|
+
|
|
40
|
+
def addDuration(self, test, duration):
|
|
41
|
+
pass
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class ReferenceLeakCheckerMixin(object):
|
|
45
|
+
"""A mixin class for TestCase, which checks reference counts."""
|
|
46
|
+
|
|
47
|
+
NB_RUNS = 3
|
|
48
|
+
|
|
49
|
+
def run(self, result=None):
|
|
50
|
+
testMethod = getattr(self, self._testMethodName)
|
|
51
|
+
expecting_failure_method = getattr(testMethod, "__unittest_expecting_failure__", False)
|
|
52
|
+
expecting_failure_class = getattr(self, "__unittest_expecting_failure__", False)
|
|
53
|
+
if expecting_failure_class or expecting_failure_method:
|
|
54
|
+
return
|
|
55
|
+
|
|
56
|
+
# python_message.py registers all Message classes to some pickle global
|
|
57
|
+
# registry, which makes the classes immortal.
|
|
58
|
+
# We save a copy of this registry, and reset it before we could references.
|
|
59
|
+
self._saved_pickle_registry = copyreg.dispatch_table.copy()
|
|
60
|
+
|
|
61
|
+
# Run the test twice, to warm up the instance attributes.
|
|
62
|
+
super(ReferenceLeakCheckerMixin, self).run(result=result)
|
|
63
|
+
super(ReferenceLeakCheckerMixin, self).run(result=result)
|
|
64
|
+
|
|
65
|
+
local_result = LocalTestResult(result)
|
|
66
|
+
num_flakes = 0
|
|
67
|
+
refcount_deltas = []
|
|
68
|
+
|
|
69
|
+
# Observe the refcount, then create oldrefcount which actually makes the
|
|
70
|
+
# refcount 1 higher than the recorded value immediately
|
|
71
|
+
oldrefcount = self._getRefcounts()
|
|
72
|
+
while len(refcount_deltas) < self.NB_RUNS:
|
|
73
|
+
oldrefcount = self._getRefcounts()
|
|
74
|
+
super(ReferenceLeakCheckerMixin, self).run(result=local_result)
|
|
75
|
+
newrefcount = self._getRefcounts()
|
|
76
|
+
# If the GC was able to collect some objects after the call to run() that
|
|
77
|
+
# it could not collect before the call, then the counts won't match.
|
|
78
|
+
if newrefcount < oldrefcount and num_flakes < 2:
|
|
79
|
+
# This result is (probably) a flake -- garbage collectors aren't very
|
|
80
|
+
# predictable, but a lower ending refcount is the opposite of the
|
|
81
|
+
# failure we are testing for. If the result is repeatable, then we will
|
|
82
|
+
# eventually report it, but not after trying to eliminate it.
|
|
83
|
+
num_flakes += 1
|
|
84
|
+
continue
|
|
85
|
+
num_flakes = 0
|
|
86
|
+
refcount_deltas.append(newrefcount - oldrefcount)
|
|
87
|
+
print(refcount_deltas, self)
|
|
88
|
+
|
|
89
|
+
try:
|
|
90
|
+
self.assertEqual(refcount_deltas, [0] * self.NB_RUNS)
|
|
91
|
+
except Exception: # pylint: disable=broad-except
|
|
92
|
+
result.addError(self, sys.exc_info())
|
|
93
|
+
|
|
94
|
+
def _getRefcounts(self):
|
|
95
|
+
if hasattr(sys, "_clear_internal_caches"): # Since 3.13
|
|
96
|
+
sys._clear_internal_caches() # pylint: disable=protected-access
|
|
97
|
+
else:
|
|
98
|
+
sys._clear_type_cache() # pylint: disable=protected-access
|
|
99
|
+
copyreg.dispatch_table.clear()
|
|
100
|
+
copyreg.dispatch_table.update(self._saved_pickle_registry)
|
|
101
|
+
# It is sometimes necessary to gc.collect() multiple times, to ensure
|
|
102
|
+
# that all objects can be collected.
|
|
103
|
+
gc.collect()
|
|
104
|
+
gc.collect()
|
|
105
|
+
gc.collect()
|
|
106
|
+
return sys.gettotalrefcount()
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
if hasattr(sys, 'gettotalrefcount'):
|
|
110
|
+
|
|
111
|
+
def TestCase(test_class):
|
|
112
|
+
new_bases = (ReferenceLeakCheckerMixin,) + test_class.__bases__
|
|
113
|
+
new_class = type(test_class)(
|
|
114
|
+
test_class.__name__, new_bases, dict(test_class.__dict__))
|
|
115
|
+
return new_class
|
|
116
|
+
SkipReferenceLeakChecker = unittest.skip
|
|
117
|
+
|
|
118
|
+
else:
|
|
119
|
+
# When PyDEBUG is not enabled, run the tests normally.
|
|
120
|
+
|
|
121
|
+
def TestCase(test_class):
|
|
122
|
+
return test_class
|
|
123
|
+
|
|
124
|
+
def SkipReferenceLeakChecker(reason):
|
|
125
|
+
del reason # Don't skip, so don't need a reason.
|
|
126
|
+
def Same(func):
|
|
127
|
+
return func
|
|
128
|
+
return Same
|
|
@@ -0,0 +1,455 @@
|
|
|
1
|
+
# Protocol Buffers - Google's data interchange format
|
|
2
|
+
# Copyright 2008 Google Inc. All rights reserved.
|
|
3
|
+
#
|
|
4
|
+
# Use of this source code is governed by a BSD-style
|
|
5
|
+
# license that can be found in the LICENSE file or at
|
|
6
|
+
# https://developers.google.com/open-source/licenses/bsd
|
|
7
|
+
|
|
8
|
+
"""Provides type checking routines.
|
|
9
|
+
|
|
10
|
+
This module defines type checking utilities in the forms of dictionaries:
|
|
11
|
+
|
|
12
|
+
VALUE_CHECKERS: A dictionary of field types and a value validation object.
|
|
13
|
+
TYPE_TO_BYTE_SIZE_FN: A dictionary with field types and a size computing
|
|
14
|
+
function.
|
|
15
|
+
TYPE_TO_SERIALIZE_METHOD: A dictionary with field types and serialization
|
|
16
|
+
function.
|
|
17
|
+
FIELD_TYPE_TO_WIRE_TYPE: A dictionary with field typed and their
|
|
18
|
+
corresponding wire types.
|
|
19
|
+
TYPE_TO_DESERIALIZE_METHOD: A dictionary with field types and deserialization
|
|
20
|
+
function.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
__author__ = 'robinson@google.com (Will Robinson)'
|
|
24
|
+
|
|
25
|
+
import numbers
|
|
26
|
+
import struct
|
|
27
|
+
import warnings
|
|
28
|
+
|
|
29
|
+
from google.protobuf import descriptor
|
|
30
|
+
from google.protobuf.internal import decoder
|
|
31
|
+
from google.protobuf.internal import encoder
|
|
32
|
+
from google.protobuf.internal import wire_format
|
|
33
|
+
|
|
34
|
+
_FieldDescriptor = descriptor.FieldDescriptor
|
|
35
|
+
# TODO: Remove this warning count after 34.0
|
|
36
|
+
# Assign bool to int/enum warnings will print 100 times at most which should
|
|
37
|
+
# be enough for users to notice and do not cause timeout.
|
|
38
|
+
_BoolWarningCount = 100
|
|
39
|
+
|
|
40
|
+
def TruncateToFourByteFloat(original):
|
|
41
|
+
return struct.unpack('<f', struct.pack('<f', original))[0]
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def ToShortestFloat(original):
|
|
45
|
+
"""Returns the shortest float that has same value in wire."""
|
|
46
|
+
# All 4 byte floats have between 6 and 9 significant digits, so we
|
|
47
|
+
# start with 6 as the lower bound.
|
|
48
|
+
# It has to be iterative because use '.9g' directly can not get rid
|
|
49
|
+
# of the noises for most values. For example if set a float_field=0.9
|
|
50
|
+
# use '.9g' will print 0.899999976.
|
|
51
|
+
precision = 6
|
|
52
|
+
rounded = float('{0:.{1}g}'.format(original, precision))
|
|
53
|
+
while TruncateToFourByteFloat(rounded) != original:
|
|
54
|
+
precision += 1
|
|
55
|
+
rounded = float('{0:.{1}g}'.format(original, precision))
|
|
56
|
+
return rounded
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def GetTypeChecker(field):
|
|
60
|
+
"""Returns a type checker for a message field of the specified types.
|
|
61
|
+
|
|
62
|
+
Args:
|
|
63
|
+
field: FieldDescriptor object for this field.
|
|
64
|
+
|
|
65
|
+
Returns:
|
|
66
|
+
An instance of TypeChecker which can be used to verify the types
|
|
67
|
+
of values assigned to a field of the specified type.
|
|
68
|
+
"""
|
|
69
|
+
if (field.cpp_type == _FieldDescriptor.CPPTYPE_STRING and
|
|
70
|
+
field.type == _FieldDescriptor.TYPE_STRING):
|
|
71
|
+
return UnicodeValueChecker()
|
|
72
|
+
if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
|
|
73
|
+
if field.enum_type.is_closed:
|
|
74
|
+
return EnumValueChecker(field.enum_type)
|
|
75
|
+
else:
|
|
76
|
+
# When open enums are supported, any int32 can be assigned.
|
|
77
|
+
return _VALUE_CHECKERS[_FieldDescriptor.CPPTYPE_INT32]
|
|
78
|
+
return _VALUE_CHECKERS[field.cpp_type]
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
# None of the typecheckers below make any attempt to guard against people
|
|
82
|
+
# subclassing builtin types and doing weird things. We're not trying to
|
|
83
|
+
# protect against malicious clients here, just people accidentally shooting
|
|
84
|
+
# themselves in the foot in obvious ways.
|
|
85
|
+
class TypeChecker(object):
|
|
86
|
+
|
|
87
|
+
"""Type checker used to catch type errors as early as possible
|
|
88
|
+
when the client is setting scalar fields in protocol messages.
|
|
89
|
+
"""
|
|
90
|
+
|
|
91
|
+
def __init__(self, *acceptable_types):
|
|
92
|
+
self._acceptable_types = acceptable_types
|
|
93
|
+
|
|
94
|
+
def CheckValue(self, proposed_value):
|
|
95
|
+
"""Type check the provided value and return it.
|
|
96
|
+
|
|
97
|
+
The returned value might have been normalized to another type.
|
|
98
|
+
"""
|
|
99
|
+
if not isinstance(proposed_value, self._acceptable_types):
|
|
100
|
+
message = ('%.1024r has type %s, but expected one of: %s' %
|
|
101
|
+
(proposed_value, type(proposed_value), self._acceptable_types))
|
|
102
|
+
raise TypeError(message)
|
|
103
|
+
return proposed_value
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
class TypeCheckerWithDefault(TypeChecker):
|
|
107
|
+
|
|
108
|
+
def __init__(self, default_value, *acceptable_types):
|
|
109
|
+
TypeChecker.__init__(self, *acceptable_types)
|
|
110
|
+
self._default_value = default_value
|
|
111
|
+
|
|
112
|
+
def DefaultValue(self):
|
|
113
|
+
return self._default_value
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
class BoolValueChecker(object):
|
|
117
|
+
"""Type checker used for bool fields."""
|
|
118
|
+
|
|
119
|
+
def CheckValue(self, proposed_value):
|
|
120
|
+
if not hasattr(proposed_value, '__index__'):
|
|
121
|
+
# Under NumPy 2.3, numpy.bool does not have an __index__ method.
|
|
122
|
+
if (type(proposed_value).__module__ == 'numpy' and
|
|
123
|
+
type(proposed_value).__name__ == 'bool'):
|
|
124
|
+
return bool(proposed_value)
|
|
125
|
+
message = ('%.1024r has type %s, but expected one of: %s' %
|
|
126
|
+
(proposed_value, type(proposed_value), (bool, int)))
|
|
127
|
+
raise TypeError(message)
|
|
128
|
+
|
|
129
|
+
if (type(proposed_value).__module__ == 'numpy' and
|
|
130
|
+
type(proposed_value).__name__ == 'ndarray'):
|
|
131
|
+
message = ('%.1024r has type %s, but expected one of: %s' %
|
|
132
|
+
(proposed_value, type(proposed_value), (bool, int)))
|
|
133
|
+
raise TypeError(message)
|
|
134
|
+
|
|
135
|
+
return bool(proposed_value)
|
|
136
|
+
|
|
137
|
+
def DefaultValue(self):
|
|
138
|
+
return False
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
# IntValueChecker and its subclasses perform integer type-checks
|
|
142
|
+
# and bounds-checks.
|
|
143
|
+
class IntValueChecker(object):
|
|
144
|
+
|
|
145
|
+
"""Checker used for integer fields. Performs type-check and range check."""
|
|
146
|
+
|
|
147
|
+
def CheckValue(self, proposed_value):
|
|
148
|
+
global _BoolWarningCount
|
|
149
|
+
if type(proposed_value) == bool and _BoolWarningCount > 0:
|
|
150
|
+
_BoolWarningCount -= 1
|
|
151
|
+
message = (
|
|
152
|
+
'%.1024r has type %s, but expected one of: %s. This warning '
|
|
153
|
+
'will turn into error in 7.34.0, please fix it before that.'
|
|
154
|
+
% (
|
|
155
|
+
proposed_value,
|
|
156
|
+
type(proposed_value),
|
|
157
|
+
(int,),
|
|
158
|
+
)
|
|
159
|
+
)
|
|
160
|
+
# TODO: Raise errors in 2026 Q1 release
|
|
161
|
+
warnings.warn(message)
|
|
162
|
+
|
|
163
|
+
if not hasattr(proposed_value, '__index__') or (
|
|
164
|
+
type(proposed_value).__module__ == 'numpy' and
|
|
165
|
+
type(proposed_value).__name__ == 'ndarray'):
|
|
166
|
+
message = ('%.1024r has type %s, but expected one of: %s' %
|
|
167
|
+
(proposed_value, type(proposed_value), (int,)))
|
|
168
|
+
raise TypeError(message)
|
|
169
|
+
|
|
170
|
+
if not self._MIN <= int(proposed_value) <= self._MAX:
|
|
171
|
+
raise ValueError('Value out of range: %d' % proposed_value)
|
|
172
|
+
# We force all values to int to make alternate implementations where the
|
|
173
|
+
# distinction is more significant (e.g. the C++ implementation) simpler.
|
|
174
|
+
proposed_value = int(proposed_value)
|
|
175
|
+
return proposed_value
|
|
176
|
+
|
|
177
|
+
def DefaultValue(self):
|
|
178
|
+
return 0
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
class EnumValueChecker(object):
|
|
182
|
+
|
|
183
|
+
"""Checker used for enum fields. Performs type-check and range check."""
|
|
184
|
+
|
|
185
|
+
def __init__(self, enum_type):
|
|
186
|
+
self._enum_type = enum_type
|
|
187
|
+
|
|
188
|
+
def CheckValue(self, proposed_value):
|
|
189
|
+
global _BoolWarningCount
|
|
190
|
+
if type(proposed_value) == bool and _BoolWarningCount > 0:
|
|
191
|
+
_BoolWarningCount -= 1
|
|
192
|
+
message = (
|
|
193
|
+
'%.1024r has type %s, but expected one of: %s. This warning '
|
|
194
|
+
'will turn into error in 7.34.0, please fix it before that.'
|
|
195
|
+
% (
|
|
196
|
+
proposed_value,
|
|
197
|
+
type(proposed_value),
|
|
198
|
+
(int,),
|
|
199
|
+
)
|
|
200
|
+
)
|
|
201
|
+
# TODO: Raise errors in 2026 Q1 release
|
|
202
|
+
warnings.warn(message)
|
|
203
|
+
if not isinstance(proposed_value, numbers.Integral):
|
|
204
|
+
message = ('%.1024r has type %s, but expected one of: %s' %
|
|
205
|
+
(proposed_value, type(proposed_value), (int,)))
|
|
206
|
+
raise TypeError(message)
|
|
207
|
+
if int(proposed_value) not in self._enum_type.values_by_number:
|
|
208
|
+
raise ValueError('Unknown enum value: %d' % proposed_value)
|
|
209
|
+
return proposed_value
|
|
210
|
+
|
|
211
|
+
def DefaultValue(self):
|
|
212
|
+
return self._enum_type.values[0].number
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
class UnicodeValueChecker(object):
|
|
216
|
+
|
|
217
|
+
"""Checker used for string fields.
|
|
218
|
+
|
|
219
|
+
Always returns a unicode value, even if the input is of type str.
|
|
220
|
+
"""
|
|
221
|
+
|
|
222
|
+
def CheckValue(self, proposed_value):
|
|
223
|
+
if not isinstance(proposed_value, (bytes, str)):
|
|
224
|
+
message = ('%.1024r has type %s, but expected one of: %s' %
|
|
225
|
+
(proposed_value, type(proposed_value), (bytes, str)))
|
|
226
|
+
raise TypeError(message)
|
|
227
|
+
|
|
228
|
+
# If the value is of type 'bytes' make sure that it is valid UTF-8 data.
|
|
229
|
+
if isinstance(proposed_value, bytes):
|
|
230
|
+
try:
|
|
231
|
+
proposed_value = proposed_value.decode('utf-8')
|
|
232
|
+
except UnicodeDecodeError:
|
|
233
|
+
raise ValueError('%.1024r has type bytes, but isn\'t valid UTF-8 '
|
|
234
|
+
'encoding. Non-UTF-8 strings must be converted to '
|
|
235
|
+
'unicode objects before being added.' %
|
|
236
|
+
(proposed_value))
|
|
237
|
+
else:
|
|
238
|
+
try:
|
|
239
|
+
proposed_value.encode('utf8')
|
|
240
|
+
except UnicodeEncodeError:
|
|
241
|
+
raise ValueError('%.1024r isn\'t a valid unicode string and '
|
|
242
|
+
'can\'t be encoded in UTF-8.'%
|
|
243
|
+
(proposed_value))
|
|
244
|
+
|
|
245
|
+
return proposed_value
|
|
246
|
+
|
|
247
|
+
def DefaultValue(self):
|
|
248
|
+
return u""
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
class Int32ValueChecker(IntValueChecker):
|
|
252
|
+
# We're sure to use ints instead of longs here since comparison may be more
|
|
253
|
+
# efficient.
|
|
254
|
+
_MIN = -2147483648
|
|
255
|
+
_MAX = 2147483647
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
class Uint32ValueChecker(IntValueChecker):
|
|
259
|
+
_MIN = 0
|
|
260
|
+
_MAX = (1 << 32) - 1
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
class Int64ValueChecker(IntValueChecker):
|
|
264
|
+
_MIN = -(1 << 63)
|
|
265
|
+
_MAX = (1 << 63) - 1
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
class Uint64ValueChecker(IntValueChecker):
|
|
269
|
+
_MIN = 0
|
|
270
|
+
_MAX = (1 << 64) - 1
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
# The max 4 bytes float is about 3.4028234663852886e+38
|
|
274
|
+
_FLOAT_MAX = float.fromhex('0x1.fffffep+127')
|
|
275
|
+
_FLOAT_MIN = -_FLOAT_MAX
|
|
276
|
+
_MAX_FLOAT_AS_DOUBLE_ROUNDED = 3.4028235677973366e38
|
|
277
|
+
_INF = float('inf')
|
|
278
|
+
_NEG_INF = float('-inf')
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
class DoubleValueChecker(object):
|
|
282
|
+
"""Checker used for double fields.
|
|
283
|
+
|
|
284
|
+
Performs type-check and range check.
|
|
285
|
+
"""
|
|
286
|
+
|
|
287
|
+
def CheckValue(self, proposed_value):
|
|
288
|
+
"""Check and convert proposed_value to float."""
|
|
289
|
+
if (not hasattr(proposed_value, '__float__') and
|
|
290
|
+
not hasattr(proposed_value, '__index__')) or (
|
|
291
|
+
type(proposed_value).__module__ == 'numpy' and
|
|
292
|
+
type(proposed_value).__name__ == 'ndarray'):
|
|
293
|
+
message = ('%.1024r has type %s, but expected one of: int, float' %
|
|
294
|
+
(proposed_value, type(proposed_value)))
|
|
295
|
+
raise TypeError(message)
|
|
296
|
+
return float(proposed_value)
|
|
297
|
+
|
|
298
|
+
def DefaultValue(self):
|
|
299
|
+
return 0.0
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
class FloatValueChecker(DoubleValueChecker):
|
|
303
|
+
"""Checker used for float fields.
|
|
304
|
+
|
|
305
|
+
Performs type-check and range check.
|
|
306
|
+
|
|
307
|
+
Values exceeding a 32-bit float will be converted to inf/-inf.
|
|
308
|
+
"""
|
|
309
|
+
|
|
310
|
+
def CheckValue(self, proposed_value):
|
|
311
|
+
"""Check and convert proposed_value to float."""
|
|
312
|
+
converted_value = super().CheckValue(proposed_value)
|
|
313
|
+
# This inf rounding matches the C++ proto SafeDoubleToFloat logic.
|
|
314
|
+
if converted_value > _FLOAT_MAX:
|
|
315
|
+
if converted_value <= _MAX_FLOAT_AS_DOUBLE_ROUNDED:
|
|
316
|
+
return _FLOAT_MAX
|
|
317
|
+
return _INF
|
|
318
|
+
if converted_value < _FLOAT_MIN:
|
|
319
|
+
if converted_value >= -_MAX_FLOAT_AS_DOUBLE_ROUNDED:
|
|
320
|
+
return _FLOAT_MIN
|
|
321
|
+
return _NEG_INF
|
|
322
|
+
|
|
323
|
+
return TruncateToFourByteFloat(converted_value)
|
|
324
|
+
|
|
325
|
+
# Type-checkers for all scalar CPPTYPEs.
|
|
326
|
+
_VALUE_CHECKERS = {
|
|
327
|
+
_FieldDescriptor.CPPTYPE_INT32: Int32ValueChecker(),
|
|
328
|
+
_FieldDescriptor.CPPTYPE_INT64: Int64ValueChecker(),
|
|
329
|
+
_FieldDescriptor.CPPTYPE_UINT32: Uint32ValueChecker(),
|
|
330
|
+
_FieldDescriptor.CPPTYPE_UINT64: Uint64ValueChecker(),
|
|
331
|
+
_FieldDescriptor.CPPTYPE_DOUBLE: DoubleValueChecker(),
|
|
332
|
+
_FieldDescriptor.CPPTYPE_FLOAT: FloatValueChecker(),
|
|
333
|
+
_FieldDescriptor.CPPTYPE_BOOL: BoolValueChecker(),
|
|
334
|
+
_FieldDescriptor.CPPTYPE_STRING: TypeCheckerWithDefault(b'', bytes),
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
# Map from field type to a function F, such that F(field_num, value)
|
|
339
|
+
# gives the total byte size for a value of the given type. This
|
|
340
|
+
# byte size includes tag information and any other additional space
|
|
341
|
+
# associated with serializing "value".
|
|
342
|
+
TYPE_TO_BYTE_SIZE_FN = {
|
|
343
|
+
_FieldDescriptor.TYPE_DOUBLE: wire_format.DoubleByteSize,
|
|
344
|
+
_FieldDescriptor.TYPE_FLOAT: wire_format.FloatByteSize,
|
|
345
|
+
_FieldDescriptor.TYPE_INT64: wire_format.Int64ByteSize,
|
|
346
|
+
_FieldDescriptor.TYPE_UINT64: wire_format.UInt64ByteSize,
|
|
347
|
+
_FieldDescriptor.TYPE_INT32: wire_format.Int32ByteSize,
|
|
348
|
+
_FieldDescriptor.TYPE_FIXED64: wire_format.Fixed64ByteSize,
|
|
349
|
+
_FieldDescriptor.TYPE_FIXED32: wire_format.Fixed32ByteSize,
|
|
350
|
+
_FieldDescriptor.TYPE_BOOL: wire_format.BoolByteSize,
|
|
351
|
+
_FieldDescriptor.TYPE_STRING: wire_format.StringByteSize,
|
|
352
|
+
_FieldDescriptor.TYPE_GROUP: wire_format.GroupByteSize,
|
|
353
|
+
_FieldDescriptor.TYPE_MESSAGE: wire_format.MessageByteSize,
|
|
354
|
+
_FieldDescriptor.TYPE_BYTES: wire_format.BytesByteSize,
|
|
355
|
+
_FieldDescriptor.TYPE_UINT32: wire_format.UInt32ByteSize,
|
|
356
|
+
_FieldDescriptor.TYPE_ENUM: wire_format.EnumByteSize,
|
|
357
|
+
_FieldDescriptor.TYPE_SFIXED32: wire_format.SFixed32ByteSize,
|
|
358
|
+
_FieldDescriptor.TYPE_SFIXED64: wire_format.SFixed64ByteSize,
|
|
359
|
+
_FieldDescriptor.TYPE_SINT32: wire_format.SInt32ByteSize,
|
|
360
|
+
_FieldDescriptor.TYPE_SINT64: wire_format.SInt64ByteSize
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
# Maps from field types to encoder constructors.
|
|
365
|
+
TYPE_TO_ENCODER = {
|
|
366
|
+
_FieldDescriptor.TYPE_DOUBLE: encoder.DoubleEncoder,
|
|
367
|
+
_FieldDescriptor.TYPE_FLOAT: encoder.FloatEncoder,
|
|
368
|
+
_FieldDescriptor.TYPE_INT64: encoder.Int64Encoder,
|
|
369
|
+
_FieldDescriptor.TYPE_UINT64: encoder.UInt64Encoder,
|
|
370
|
+
_FieldDescriptor.TYPE_INT32: encoder.Int32Encoder,
|
|
371
|
+
_FieldDescriptor.TYPE_FIXED64: encoder.Fixed64Encoder,
|
|
372
|
+
_FieldDescriptor.TYPE_FIXED32: encoder.Fixed32Encoder,
|
|
373
|
+
_FieldDescriptor.TYPE_BOOL: encoder.BoolEncoder,
|
|
374
|
+
_FieldDescriptor.TYPE_STRING: encoder.StringEncoder,
|
|
375
|
+
_FieldDescriptor.TYPE_GROUP: encoder.GroupEncoder,
|
|
376
|
+
_FieldDescriptor.TYPE_MESSAGE: encoder.MessageEncoder,
|
|
377
|
+
_FieldDescriptor.TYPE_BYTES: encoder.BytesEncoder,
|
|
378
|
+
_FieldDescriptor.TYPE_UINT32: encoder.UInt32Encoder,
|
|
379
|
+
_FieldDescriptor.TYPE_ENUM: encoder.EnumEncoder,
|
|
380
|
+
_FieldDescriptor.TYPE_SFIXED32: encoder.SFixed32Encoder,
|
|
381
|
+
_FieldDescriptor.TYPE_SFIXED64: encoder.SFixed64Encoder,
|
|
382
|
+
_FieldDescriptor.TYPE_SINT32: encoder.SInt32Encoder,
|
|
383
|
+
_FieldDescriptor.TYPE_SINT64: encoder.SInt64Encoder,
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
# Maps from field types to sizer constructors.
|
|
388
|
+
TYPE_TO_SIZER = {
|
|
389
|
+
_FieldDescriptor.TYPE_DOUBLE: encoder.DoubleSizer,
|
|
390
|
+
_FieldDescriptor.TYPE_FLOAT: encoder.FloatSizer,
|
|
391
|
+
_FieldDescriptor.TYPE_INT64: encoder.Int64Sizer,
|
|
392
|
+
_FieldDescriptor.TYPE_UINT64: encoder.UInt64Sizer,
|
|
393
|
+
_FieldDescriptor.TYPE_INT32: encoder.Int32Sizer,
|
|
394
|
+
_FieldDescriptor.TYPE_FIXED64: encoder.Fixed64Sizer,
|
|
395
|
+
_FieldDescriptor.TYPE_FIXED32: encoder.Fixed32Sizer,
|
|
396
|
+
_FieldDescriptor.TYPE_BOOL: encoder.BoolSizer,
|
|
397
|
+
_FieldDescriptor.TYPE_STRING: encoder.StringSizer,
|
|
398
|
+
_FieldDescriptor.TYPE_GROUP: encoder.GroupSizer,
|
|
399
|
+
_FieldDescriptor.TYPE_MESSAGE: encoder.MessageSizer,
|
|
400
|
+
_FieldDescriptor.TYPE_BYTES: encoder.BytesSizer,
|
|
401
|
+
_FieldDescriptor.TYPE_UINT32: encoder.UInt32Sizer,
|
|
402
|
+
_FieldDescriptor.TYPE_ENUM: encoder.EnumSizer,
|
|
403
|
+
_FieldDescriptor.TYPE_SFIXED32: encoder.SFixed32Sizer,
|
|
404
|
+
_FieldDescriptor.TYPE_SFIXED64: encoder.SFixed64Sizer,
|
|
405
|
+
_FieldDescriptor.TYPE_SINT32: encoder.SInt32Sizer,
|
|
406
|
+
_FieldDescriptor.TYPE_SINT64: encoder.SInt64Sizer,
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
# Maps from field type to a decoder constructor.
|
|
411
|
+
TYPE_TO_DECODER = {
|
|
412
|
+
_FieldDescriptor.TYPE_DOUBLE: decoder.DoubleDecoder,
|
|
413
|
+
_FieldDescriptor.TYPE_FLOAT: decoder.FloatDecoder,
|
|
414
|
+
_FieldDescriptor.TYPE_INT64: decoder.Int64Decoder,
|
|
415
|
+
_FieldDescriptor.TYPE_UINT64: decoder.UInt64Decoder,
|
|
416
|
+
_FieldDescriptor.TYPE_INT32: decoder.Int32Decoder,
|
|
417
|
+
_FieldDescriptor.TYPE_FIXED64: decoder.Fixed64Decoder,
|
|
418
|
+
_FieldDescriptor.TYPE_FIXED32: decoder.Fixed32Decoder,
|
|
419
|
+
_FieldDescriptor.TYPE_BOOL: decoder.BoolDecoder,
|
|
420
|
+
_FieldDescriptor.TYPE_STRING: decoder.StringDecoder,
|
|
421
|
+
_FieldDescriptor.TYPE_GROUP: decoder.GroupDecoder,
|
|
422
|
+
_FieldDescriptor.TYPE_MESSAGE: decoder.MessageDecoder,
|
|
423
|
+
_FieldDescriptor.TYPE_BYTES: decoder.BytesDecoder,
|
|
424
|
+
_FieldDescriptor.TYPE_UINT32: decoder.UInt32Decoder,
|
|
425
|
+
_FieldDescriptor.TYPE_ENUM: decoder.EnumDecoder,
|
|
426
|
+
_FieldDescriptor.TYPE_SFIXED32: decoder.SFixed32Decoder,
|
|
427
|
+
_FieldDescriptor.TYPE_SFIXED64: decoder.SFixed64Decoder,
|
|
428
|
+
_FieldDescriptor.TYPE_SINT32: decoder.SInt32Decoder,
|
|
429
|
+
_FieldDescriptor.TYPE_SINT64: decoder.SInt64Decoder,
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
# Maps from field type to expected wiretype.
|
|
433
|
+
FIELD_TYPE_TO_WIRE_TYPE = {
|
|
434
|
+
_FieldDescriptor.TYPE_DOUBLE: wire_format.WIRETYPE_FIXED64,
|
|
435
|
+
_FieldDescriptor.TYPE_FLOAT: wire_format.WIRETYPE_FIXED32,
|
|
436
|
+
_FieldDescriptor.TYPE_INT64: wire_format.WIRETYPE_VARINT,
|
|
437
|
+
_FieldDescriptor.TYPE_UINT64: wire_format.WIRETYPE_VARINT,
|
|
438
|
+
_FieldDescriptor.TYPE_INT32: wire_format.WIRETYPE_VARINT,
|
|
439
|
+
_FieldDescriptor.TYPE_FIXED64: wire_format.WIRETYPE_FIXED64,
|
|
440
|
+
_FieldDescriptor.TYPE_FIXED32: wire_format.WIRETYPE_FIXED32,
|
|
441
|
+
_FieldDescriptor.TYPE_BOOL: wire_format.WIRETYPE_VARINT,
|
|
442
|
+
_FieldDescriptor.TYPE_STRING:
|
|
443
|
+
wire_format.WIRETYPE_LENGTH_DELIMITED,
|
|
444
|
+
_FieldDescriptor.TYPE_GROUP: wire_format.WIRETYPE_START_GROUP,
|
|
445
|
+
_FieldDescriptor.TYPE_MESSAGE:
|
|
446
|
+
wire_format.WIRETYPE_LENGTH_DELIMITED,
|
|
447
|
+
_FieldDescriptor.TYPE_BYTES:
|
|
448
|
+
wire_format.WIRETYPE_LENGTH_DELIMITED,
|
|
449
|
+
_FieldDescriptor.TYPE_UINT32: wire_format.WIRETYPE_VARINT,
|
|
450
|
+
_FieldDescriptor.TYPE_ENUM: wire_format.WIRETYPE_VARINT,
|
|
451
|
+
_FieldDescriptor.TYPE_SFIXED32: wire_format.WIRETYPE_FIXED32,
|
|
452
|
+
_FieldDescriptor.TYPE_SFIXED64: wire_format.WIRETYPE_FIXED64,
|
|
453
|
+
_FieldDescriptor.TYPE_SINT32: wire_format.WIRETYPE_VARINT,
|
|
454
|
+
_FieldDescriptor.TYPE_SINT64: wire_format.WIRETYPE_VARINT,
|
|
455
|
+
}
|