~ubuntu-branches/debian/stretch/protobuf/stretch

« back to all changes in this revision

Viewing changes to python/google/protobuf/internal/type_checkers.py

  • Committer: Package Import Robot
  • Author(s): Robert S. Edmonds
  • Date: 2014-09-11 22:50:10 UTC
  • mfrom: (10.1.9 experimental)
  • Revision ID: package-import@ubuntu.com-20140911225010-wt4yo9dpc1fzuq5g
Tags: 2.6.0-3
Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
29
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
30
 
 
31
#PY25 compatible for GAE.
 
32
#
 
33
# Copyright 2008 Google Inc. All Rights Reserved.
 
34
 
31
35
"""Provides type checking routines.
32
36
 
33
37
This module defines type checking utilities in the forms of dictionaries:
45
49
 
46
50
__author__ = 'robinson@google.com (Will Robinson)'
47
51
 
 
52
import sys  ##PY25
 
53
if sys.version < '2.6': bytes = str  ##PY25
 
54
from google.protobuf.internal import api_implementation
48
55
from google.protobuf.internal import decoder
49
56
from google.protobuf.internal import encoder
50
57
from google.protobuf.internal import wire_format
53
60
_FieldDescriptor = descriptor.FieldDescriptor
54
61
 
55
62
 
56
 
def GetTypeChecker(cpp_type, field_type):
 
63
def GetTypeChecker(field):
57
64
  """Returns a type checker for a message field of the specified types.
58
65
 
59
66
  Args:
60
 
    cpp_type: C++ type of the field (see descriptor.py).
61
 
    field_type: Protocol message field type (see descriptor.py).
 
67
    field: FieldDescriptor object for this field.
62
68
 
63
69
  Returns:
64
70
    An instance of TypeChecker which can be used to verify the types
65
71
    of values assigned to a field of the specified type.
66
72
  """
67
 
  if (cpp_type == _FieldDescriptor.CPPTYPE_STRING and
68
 
      field_type == _FieldDescriptor.TYPE_STRING):
 
73
  if (field.cpp_type == _FieldDescriptor.CPPTYPE_STRING and
 
74
      field.type == _FieldDescriptor.TYPE_STRING):
69
75
    return UnicodeValueChecker()
70
 
  return _VALUE_CHECKERS[cpp_type]
 
76
  if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
 
77
    return EnumValueChecker(field.enum_type)
 
78
  return _VALUE_CHECKERS[field.cpp_type]
71
79
 
72
80
 
73
81
# None of the typecheckers below make any attempt to guard against people
85
93
    self._acceptable_types = acceptable_types
86
94
 
87
95
  def CheckValue(self, proposed_value):
 
96
    """Type check the provided value and return it.
 
97
 
 
98
    The returned value might have been normalized to another type.
 
99
    """
88
100
    if not isinstance(proposed_value, self._acceptable_types):
89
101
      message = ('%.1024r has type %s, but expected one of: %s' %
90
102
                 (proposed_value, type(proposed_value), self._acceptable_types))
91
103
      raise TypeError(message)
 
104
    return proposed_value
92
105
 
93
106
 
94
107
# IntValueChecker and its subclasses perform integer type-checks
104
117
      raise TypeError(message)
105
118
    if not self._MIN <= proposed_value <= self._MAX:
106
119
      raise ValueError('Value out of range: %d' % proposed_value)
 
120
    # We force 32-bit values to int and 64-bit values to long to make
 
121
    # alternate implementations where the distinction is more significant
 
122
    # (e.g. the C++ implementation) simpler.
 
123
    proposed_value = self._TYPE(proposed_value)
 
124
    return proposed_value
 
125
 
 
126
 
 
127
class EnumValueChecker(object):
 
128
 
 
129
  """Checker used for enum fields.  Performs type-check and range check."""
 
130
 
 
131
  def __init__(self, enum_type):
 
132
    self._enum_type = enum_type
 
133
 
 
134
  def CheckValue(self, proposed_value):
 
135
    if not isinstance(proposed_value, (int, long)):
 
136
      message = ('%.1024r has type %s, but expected one of: %s' %
 
137
                 (proposed_value, type(proposed_value), (int, long)))
 
138
      raise TypeError(message)
 
139
    if proposed_value not in self._enum_type.values_by_number:
 
140
      raise ValueError('Unknown enum value: %d' % proposed_value)
 
141
    return proposed_value
107
142
 
108
143
 
109
144
class UnicodeValueChecker(object):
110
145
 
111
 
  """Checker used for string fields."""
 
146
  """Checker used for string fields.
 
147
 
 
148
  Always returns a unicode value, even if the input is of type str.
 
149
  """
112
150
 
113
151
  def CheckValue(self, proposed_value):
114
 
    if not isinstance(proposed_value, (str, unicode)):
 
152
    if not isinstance(proposed_value, (bytes, unicode)):
115
153
      message = ('%.1024r has type %s, but expected one of: %s' %
116
 
                 (proposed_value, type(proposed_value), (str, unicode)))
 
154
                 (proposed_value, type(proposed_value), (bytes, unicode)))
117
155
      raise TypeError(message)
118
156
 
119
 
    # If the value is of type 'str' make sure that it is in 7-bit ASCII
 
157
    # If the value is of type 'bytes' make sure that it is in 7-bit ASCII
120
158
    # encoding.
121
 
    if isinstance(proposed_value, str):
 
159
    if isinstance(proposed_value, bytes):
122
160
      try:
123
 
        unicode(proposed_value, 'ascii')
 
161
        proposed_value = proposed_value.decode('ascii')
124
162
      except UnicodeDecodeError:
125
 
        raise ValueError('%.1024r has type str, but isn\'t in 7-bit ASCII '
 
163
        raise ValueError('%.1024r has type bytes, but isn\'t in 7-bit ASCII '
126
164
                         'encoding. Non-ASCII strings must be converted to '
127
165
                         'unicode objects before being added.' %
128
166
                         (proposed_value))
 
167
    return proposed_value
129
168
 
130
169
 
131
170
class Int32ValueChecker(IntValueChecker):
133
172
  # efficient.
134
173
  _MIN = -2147483648
135
174
  _MAX = 2147483647
 
175
  _TYPE = int
136
176
 
137
177
 
138
178
class Uint32ValueChecker(IntValueChecker):
139
179
  _MIN = 0
140
180
  _MAX = (1 << 32) - 1
 
181
  _TYPE = int
141
182
 
142
183
 
143
184
class Int64ValueChecker(IntValueChecker):
144
185
  _MIN = -(1 << 63)
145
186
  _MAX = (1 << 63) - 1
 
187
  _TYPE = long
146
188
 
147
189
 
148
190
class Uint64ValueChecker(IntValueChecker):
149
191
  _MIN = 0
150
192
  _MAX = (1 << 64) - 1
 
193
  _TYPE = long
151
194
 
152
195
 
153
196
# Type-checkers for all scalar CPPTYPEs.
161
204
    _FieldDescriptor.CPPTYPE_FLOAT: TypeChecker(
162
205
        float, int, long),
163
206
    _FieldDescriptor.CPPTYPE_BOOL: TypeChecker(bool, int),
164
 
    _FieldDescriptor.CPPTYPE_ENUM: Int32ValueChecker(),
165
 
    _FieldDescriptor.CPPTYPE_STRING: TypeChecker(str),
 
207
    _FieldDescriptor.CPPTYPE_STRING: TypeChecker(bytes),
166
208
    }
167
209
 
168
210