| 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 | |
| 49 import six | 48 import six |
| 50 | 49 |
| 51 if six.PY3: | 50 if six.PY3: |
| 52 long = int | 51 long = int |
| 53 | 52 |
| 54 from google.protobuf.internal import api_implementation | 53 from google.protobuf.internal import api_implementation |
| 55 from google.protobuf.internal import decoder | 54 from google.protobuf.internal import decoder |
| 56 from google.protobuf.internal import encoder | 55 from google.protobuf.internal import encoder |
| 57 from google.protobuf.internal import wire_format | 56 from google.protobuf.internal import wire_format |
| 58 from google.protobuf import descriptor | 57 from google.protobuf import descriptor |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 return self._default_value | 119 return self._default_value |
| 121 | 120 |
| 122 | 121 |
| 123 # IntValueChecker and its subclasses perform integer type-checks | 122 # IntValueChecker and its subclasses perform integer type-checks |
| 124 # and bounds-checks. | 123 # and bounds-checks. |
| 125 class IntValueChecker(object): | 124 class IntValueChecker(object): |
| 126 | 125 |
| 127 """Checker used for integer fields. Performs type-check and range check.""" | 126 """Checker used for integer fields. Performs type-check and range check.""" |
| 128 | 127 |
| 129 def CheckValue(self, proposed_value): | 128 def CheckValue(self, proposed_value): |
| 130 if not isinstance(proposed_value, numbers.Integral): | 129 if not isinstance(proposed_value, six.integer_types): |
| 131 message = ('%.1024r has type %s, but expected one of: %s' % | 130 message = ('%.1024r has type %s, but expected one of: %s' % |
| 132 (proposed_value, type(proposed_value), six.integer_types)) | 131 (proposed_value, type(proposed_value), six.integer_types)) |
| 133 raise TypeError(message) | 132 raise TypeError(message) |
| 134 if not self._MIN <= int(proposed_value) <= self._MAX: | 133 if not self._MIN <= proposed_value <= self._MAX: |
| 135 raise ValueError('Value out of range: %d' % proposed_value) | 134 raise ValueError('Value out of range: %d' % proposed_value) |
| 136 # We force 32-bit values to int and 64-bit values to long to make | 135 # We force 32-bit values to int and 64-bit values to long to make |
| 137 # alternate implementations where the distinction is more significant | 136 # alternate implementations where the distinction is more significant |
| 138 # (e.g. the C++ implementation) simpler. | 137 # (e.g. the C++ implementation) simpler. |
| 139 proposed_value = self._TYPE(proposed_value) | 138 proposed_value = self._TYPE(proposed_value) |
| 140 return proposed_value | 139 return proposed_value |
| 141 | 140 |
| 142 def DefaultValue(self): | 141 def DefaultValue(self): |
| 143 return 0 | 142 return 0 |
| 144 | 143 |
| 145 | 144 |
| 146 class EnumValueChecker(object): | 145 class EnumValueChecker(object): |
| 147 | 146 |
| 148 """Checker used for enum fields. Performs type-check and range check.""" | 147 """Checker used for enum fields. Performs type-check and range check.""" |
| 149 | 148 |
| 150 def __init__(self, enum_type): | 149 def __init__(self, enum_type): |
| 151 self._enum_type = enum_type | 150 self._enum_type = enum_type |
| 152 | 151 |
| 153 def CheckValue(self, proposed_value): | 152 def CheckValue(self, proposed_value): |
| 154 if not isinstance(proposed_value, numbers.Integral): | 153 if not isinstance(proposed_value, six.integer_types): |
| 155 message = ('%.1024r has type %s, but expected one of: %s' % | 154 message = ('%.1024r has type %s, but expected one of: %s' % |
| 156 (proposed_value, type(proposed_value), six.integer_types)) | 155 (proposed_value, type(proposed_value), six.integer_types)) |
| 157 raise TypeError(message) | 156 raise TypeError(message) |
| 158 if int(proposed_value) not in self._enum_type.values_by_number: | 157 if proposed_value not in self._enum_type.values_by_number: |
| 159 raise ValueError('Unknown enum value: %d' % proposed_value) | 158 raise ValueError('Unknown enum value: %d' % proposed_value) |
| 160 return proposed_value | 159 return proposed_value |
| 161 | 160 |
| 162 def DefaultValue(self): | 161 def DefaultValue(self): |
| 163 return self._enum_type.values[0].number | 162 return self._enum_type.values[0].number |
| 164 | 163 |
| 165 | 164 |
| 166 class UnicodeValueChecker(object): | 165 class UnicodeValueChecker(object): |
| 167 | 166 |
| 168 """Checker used for string fields. | 167 """Checker used for string fields. |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 _TYPE = long | 216 _TYPE = long |
| 218 | 217 |
| 219 | 218 |
| 220 # Type-checkers for all scalar CPPTYPEs. | 219 # Type-checkers for all scalar CPPTYPEs. |
| 221 _VALUE_CHECKERS = { | 220 _VALUE_CHECKERS = { |
| 222 _FieldDescriptor.CPPTYPE_INT32: Int32ValueChecker(), | 221 _FieldDescriptor.CPPTYPE_INT32: Int32ValueChecker(), |
| 223 _FieldDescriptor.CPPTYPE_INT64: Int64ValueChecker(), | 222 _FieldDescriptor.CPPTYPE_INT64: Int64ValueChecker(), |
| 224 _FieldDescriptor.CPPTYPE_UINT32: Uint32ValueChecker(), | 223 _FieldDescriptor.CPPTYPE_UINT32: Uint32ValueChecker(), |
| 225 _FieldDescriptor.CPPTYPE_UINT64: Uint64ValueChecker(), | 224 _FieldDescriptor.CPPTYPE_UINT64: Uint64ValueChecker(), |
| 226 _FieldDescriptor.CPPTYPE_DOUBLE: TypeCheckerWithDefault( | 225 _FieldDescriptor.CPPTYPE_DOUBLE: TypeCheckerWithDefault( |
| 227 0.0, numbers.Real), | 226 0.0, float, int, long), |
| 228 _FieldDescriptor.CPPTYPE_FLOAT: TypeCheckerWithDefault( | 227 _FieldDescriptor.CPPTYPE_FLOAT: TypeCheckerWithDefault( |
| 229 0.0, numbers.Real), | 228 0.0, float, int, long), |
| 230 _FieldDescriptor.CPPTYPE_BOOL: TypeCheckerWithDefault( | 229 _FieldDescriptor.CPPTYPE_BOOL: TypeCheckerWithDefault( |
| 231 False, bool, numbers.Integral), | 230 False, bool, int), |
| 232 _FieldDescriptor.CPPTYPE_STRING: TypeCheckerWithDefault(b'', bytes), | 231 _FieldDescriptor.CPPTYPE_STRING: TypeCheckerWithDefault(b'', bytes), |
| 233 } | 232 } |
| 234 | 233 |
| 235 | 234 |
| 236 # Map from field type to a function F, such that F(field_num, value) | 235 # Map from field type to a function F, such that F(field_num, value) |
| 237 # gives the total byte size for a value of the given type. This | 236 # gives the total byte size for a value of the given type. This |
| 238 # byte size includes tag information and any other additional space | 237 # byte size includes tag information and any other additional space |
| 239 # associated with serializing "value". | 238 # associated with serializing "value". |
| 240 TYPE_TO_BYTE_SIZE_FN = { | 239 TYPE_TO_BYTE_SIZE_FN = { |
| 241 _FieldDescriptor.TYPE_DOUBLE: wire_format.DoubleByteSize, | 240 _FieldDescriptor.TYPE_DOUBLE: wire_format.DoubleByteSize, |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 wire_format.WIRETYPE_LENGTH_DELIMITED, | 343 wire_format.WIRETYPE_LENGTH_DELIMITED, |
| 345 _FieldDescriptor.TYPE_BYTES: | 344 _FieldDescriptor.TYPE_BYTES: |
| 346 wire_format.WIRETYPE_LENGTH_DELIMITED, | 345 wire_format.WIRETYPE_LENGTH_DELIMITED, |
| 347 _FieldDescriptor.TYPE_UINT32: wire_format.WIRETYPE_VARINT, | 346 _FieldDescriptor.TYPE_UINT32: wire_format.WIRETYPE_VARINT, |
| 348 _FieldDescriptor.TYPE_ENUM: wire_format.WIRETYPE_VARINT, | 347 _FieldDescriptor.TYPE_ENUM: wire_format.WIRETYPE_VARINT, |
| 349 _FieldDescriptor.TYPE_SFIXED32: wire_format.WIRETYPE_FIXED32, | 348 _FieldDescriptor.TYPE_SFIXED32: wire_format.WIRETYPE_FIXED32, |
| 350 _FieldDescriptor.TYPE_SFIXED64: wire_format.WIRETYPE_FIXED64, | 349 _FieldDescriptor.TYPE_SFIXED64: wire_format.WIRETYPE_FIXED64, |
| 351 _FieldDescriptor.TYPE_SINT32: wire_format.WIRETYPE_VARINT, | 350 _FieldDescriptor.TYPE_SINT32: wire_format.WIRETYPE_VARINT, |
| 352 _FieldDescriptor.TYPE_SINT64: wire_format.WIRETYPE_VARINT, | 351 _FieldDescriptor.TYPE_SINT64: wire_format.WIRETYPE_VARINT, |
| 353 } | 352 } |
| OLD | NEW |