| OLD | NEW |
| 1 # Protocol Buffers - Google's data interchange format | 1 # Protocol Buffers - Google's data interchange format |
| 2 # Copyright 2008 Google Inc. All rights reserved. | 2 # Copyright 2008 Google Inc. All rights reserved. |
| 3 # https://developers.google.com/protocol-buffers/ | 3 # https://developers.google.com/protocol-buffers/ |
| 4 # | 4 # |
| 5 # Redistribution and use in source and binary forms, with or without | 5 # Redistribution and use in source and binary forms, with or without |
| 6 # modification, are permitted provided that the following conditions are | 6 # modification, are permitted provided that the following conditions are |
| 7 # met: | 7 # met: |
| 8 # | 8 # |
| 9 # * Redistributions of source code must retain the above copyright | 9 # * Redistributions of source code must retain the above copyright |
| 10 # notice, this list of conditions and the following disclaimer. | 10 # notice, this list of conditions and the following disclaimer. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 TYPE_TO_SERIALIZE_METHOD: A dictionary with field types and serialization | 38 TYPE_TO_SERIALIZE_METHOD: A dictionary with field types and serialization |
| 39 function. | 39 function. |
| 40 FIELD_TYPE_TO_WIRE_TYPE: A dictionary with field typed and their | 40 FIELD_TYPE_TO_WIRE_TYPE: A dictionary with field typed and their |
| 41 coresponding wire types. | 41 coresponding wire types. |
| 42 TYPE_TO_DESERIALIZE_METHOD: A dictionary with field types and deserialization | 42 TYPE_TO_DESERIALIZE_METHOD: A dictionary with field types and deserialization |
| 43 function. | 43 function. |
| 44 """ | 44 """ |
| 45 | 45 |
| 46 __author__ = 'robinson@google.com (Will Robinson)' | 46 __author__ = 'robinson@google.com (Will Robinson)' |
| 47 | 47 |
| 48 import numbers |
| 48 import six | 49 import six |
| 49 | 50 |
| 50 if six.PY3: | 51 if six.PY3: |
| 51 long = int | 52 long = int |
| 52 | 53 |
| 53 from google.protobuf.internal import api_implementation | 54 from google.protobuf.internal import api_implementation |
| 54 from google.protobuf.internal import decoder | 55 from google.protobuf.internal import decoder |
| 55 from google.protobuf.internal import encoder | 56 from google.protobuf.internal import encoder |
| 56 from google.protobuf.internal import wire_format | 57 from google.protobuf.internal import wire_format |
| 57 from google.protobuf import descriptor | 58 from google.protobuf import descriptor |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 return self._default_value | 120 return self._default_value |
| 120 | 121 |
| 121 | 122 |
| 122 # IntValueChecker and its subclasses perform integer type-checks | 123 # IntValueChecker and its subclasses perform integer type-checks |
| 123 # and bounds-checks. | 124 # and bounds-checks. |
| 124 class IntValueChecker(object): | 125 class IntValueChecker(object): |
| 125 | 126 |
| 126 """Checker used for integer fields. Performs type-check and range check.""" | 127 """Checker used for integer fields. Performs type-check and range check.""" |
| 127 | 128 |
| 128 def CheckValue(self, proposed_value): | 129 def CheckValue(self, proposed_value): |
| 129 if not isinstance(proposed_value, six.integer_types): | 130 if not isinstance(proposed_value, numbers.Integral): |
| 130 message = ('%.1024r has type %s, but expected one of: %s' % | 131 message = ('%.1024r has type %s, but expected one of: %s' % |
| 131 (proposed_value, type(proposed_value), six.integer_types)) | 132 (proposed_value, type(proposed_value), six.integer_types)) |
| 132 raise TypeError(message) | 133 raise TypeError(message) |
| 133 if not self._MIN <= proposed_value <= self._MAX: | 134 if not self._MIN <= int(proposed_value) <= self._MAX: |
| 134 raise ValueError('Value out of range: %d' % proposed_value) | 135 raise ValueError('Value out of range: %d' % proposed_value) |
| 135 # We force 32-bit values to int and 64-bit values to long to make | 136 # We force 32-bit values to int and 64-bit values to long to make |
| 136 # alternate implementations where the distinction is more significant | 137 # alternate implementations where the distinction is more significant |
| 137 # (e.g. the C++ implementation) simpler. | 138 # (e.g. the C++ implementation) simpler. |
| 138 proposed_value = self._TYPE(proposed_value) | 139 proposed_value = self._TYPE(proposed_value) |
| 139 return proposed_value | 140 return proposed_value |
| 140 | 141 |
| 141 def DefaultValue(self): | 142 def DefaultValue(self): |
| 142 return 0 | 143 return 0 |
| 143 | 144 |
| 144 | 145 |
| 145 class EnumValueChecker(object): | 146 class EnumValueChecker(object): |
| 146 | 147 |
| 147 """Checker used for enum fields. Performs type-check and range check.""" | 148 """Checker used for enum fields. Performs type-check and range check.""" |
| 148 | 149 |
| 149 def __init__(self, enum_type): | 150 def __init__(self, enum_type): |
| 150 self._enum_type = enum_type | 151 self._enum_type = enum_type |
| 151 | 152 |
| 152 def CheckValue(self, proposed_value): | 153 def CheckValue(self, proposed_value): |
| 153 if not isinstance(proposed_value, six.integer_types): | 154 if not isinstance(proposed_value, numbers.Integral): |
| 154 message = ('%.1024r has type %s, but expected one of: %s' % | 155 message = ('%.1024r has type %s, but expected one of: %s' % |
| 155 (proposed_value, type(proposed_value), six.integer_types)) | 156 (proposed_value, type(proposed_value), six.integer_types)) |
| 156 raise TypeError(message) | 157 raise TypeError(message) |
| 157 if proposed_value not in self._enum_type.values_by_number: | 158 if int(proposed_value) not in self._enum_type.values_by_number: |
| 158 raise ValueError('Unknown enum value: %d' % proposed_value) | 159 raise ValueError('Unknown enum value: %d' % proposed_value) |
| 159 return proposed_value | 160 return proposed_value |
| 160 | 161 |
| 161 def DefaultValue(self): | 162 def DefaultValue(self): |
| 162 return self._enum_type.values[0].number | 163 return self._enum_type.values[0].number |
| 163 | 164 |
| 164 | 165 |
| 165 class UnicodeValueChecker(object): | 166 class UnicodeValueChecker(object): |
| 166 | 167 |
| 167 """Checker used for string fields. | 168 """Checker used for string fields. |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 _TYPE = long | 217 _TYPE = long |
| 217 | 218 |
| 218 | 219 |
| 219 # Type-checkers for all scalar CPPTYPEs. | 220 # Type-checkers for all scalar CPPTYPEs. |
| 220 _VALUE_CHECKERS = { | 221 _VALUE_CHECKERS = { |
| 221 _FieldDescriptor.CPPTYPE_INT32: Int32ValueChecker(), | 222 _FieldDescriptor.CPPTYPE_INT32: Int32ValueChecker(), |
| 222 _FieldDescriptor.CPPTYPE_INT64: Int64ValueChecker(), | 223 _FieldDescriptor.CPPTYPE_INT64: Int64ValueChecker(), |
| 223 _FieldDescriptor.CPPTYPE_UINT32: Uint32ValueChecker(), | 224 _FieldDescriptor.CPPTYPE_UINT32: Uint32ValueChecker(), |
| 224 _FieldDescriptor.CPPTYPE_UINT64: Uint64ValueChecker(), | 225 _FieldDescriptor.CPPTYPE_UINT64: Uint64ValueChecker(), |
| 225 _FieldDescriptor.CPPTYPE_DOUBLE: TypeCheckerWithDefault( | 226 _FieldDescriptor.CPPTYPE_DOUBLE: TypeCheckerWithDefault( |
| 226 0.0, float, int, long), | 227 0.0, numbers.Real), |
| 227 _FieldDescriptor.CPPTYPE_FLOAT: TypeCheckerWithDefault( | 228 _FieldDescriptor.CPPTYPE_FLOAT: TypeCheckerWithDefault( |
| 228 0.0, float, int, long), | 229 0.0, numbers.Real), |
| 229 _FieldDescriptor.CPPTYPE_BOOL: TypeCheckerWithDefault( | 230 _FieldDescriptor.CPPTYPE_BOOL: TypeCheckerWithDefault( |
| 230 False, bool, int), | 231 False, bool, numbers.Integral), |
| 231 _FieldDescriptor.CPPTYPE_STRING: TypeCheckerWithDefault(b'', bytes), | 232 _FieldDescriptor.CPPTYPE_STRING: TypeCheckerWithDefault(b'', bytes), |
| 232 } | 233 } |
| 233 | 234 |
| 234 | 235 |
| 235 # Map from field type to a function F, such that F(field_num, value) | 236 # Map from field type to a function F, such that F(field_num, value) |
| 236 # gives the total byte size for a value of the given type. This | 237 # gives the total byte size for a value of the given type. This |
| 237 # byte size includes tag information and any other additional space | 238 # byte size includes tag information and any other additional space |
| 238 # associated with serializing "value". | 239 # associated with serializing "value". |
| 239 TYPE_TO_BYTE_SIZE_FN = { | 240 TYPE_TO_BYTE_SIZE_FN = { |
| 240 _FieldDescriptor.TYPE_DOUBLE: wire_format.DoubleByteSize, | 241 _FieldDescriptor.TYPE_DOUBLE: wire_format.DoubleByteSize, |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 wire_format.WIRETYPE_LENGTH_DELIMITED, | 344 wire_format.WIRETYPE_LENGTH_DELIMITED, |
| 344 _FieldDescriptor.TYPE_BYTES: | 345 _FieldDescriptor.TYPE_BYTES: |
| 345 wire_format.WIRETYPE_LENGTH_DELIMITED, | 346 wire_format.WIRETYPE_LENGTH_DELIMITED, |
| 346 _FieldDescriptor.TYPE_UINT32: wire_format.WIRETYPE_VARINT, | 347 _FieldDescriptor.TYPE_UINT32: wire_format.WIRETYPE_VARINT, |
| 347 _FieldDescriptor.TYPE_ENUM: wire_format.WIRETYPE_VARINT, | 348 _FieldDescriptor.TYPE_ENUM: wire_format.WIRETYPE_VARINT, |
| 348 _FieldDescriptor.TYPE_SFIXED32: wire_format.WIRETYPE_FIXED32, | 349 _FieldDescriptor.TYPE_SFIXED32: wire_format.WIRETYPE_FIXED32, |
| 349 _FieldDescriptor.TYPE_SFIXED64: wire_format.WIRETYPE_FIXED64, | 350 _FieldDescriptor.TYPE_SFIXED64: wire_format.WIRETYPE_FIXED64, |
| 350 _FieldDescriptor.TYPE_SINT32: wire_format.WIRETYPE_VARINT, | 351 _FieldDescriptor.TYPE_SINT32: wire_format.WIRETYPE_VARINT, |
| 351 _FieldDescriptor.TYPE_SINT64: wire_format.WIRETYPE_VARINT, | 352 _FieldDescriptor.TYPE_SINT64: wire_format.WIRETYPE_VARINT, |
| 352 } | 353 } |
| OLD | NEW |