~ubuntu-branches/debian/sid/protobuf/sid

« back to all changes in this revision

Viewing changes to python/google/protobuf/reflection.py

  • Committer: Bazaar Package Importer
  • Author(s): Julien Cristau
  • Date: 2009-06-02 16:19:00 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090602161900-vm176i3ryt35yk91
Tags: 2.0.3-2.2
* Non-maintainer upload.
* Fix FTBFS from -2.1: don't fail when we can't clean up the java build,
  such as when openjdk isn't installed.
* Disable parallel builds, because libtool is made of fail (if binary-arch
  and build-indep run concurrently, we relink a library while it's being
  used; that doesn't work so well).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Protocol Buffers - Google's data interchange format
2
 
# Copyright 2008 Google Inc.
 
2
# Copyright 2008 Google Inc.  All rights reserved.
3
3
# http://code.google.com/p/protobuf/
4
4
#
5
 
# Licensed under the Apache License, Version 2.0 (the "License");
6
 
# you may not use this file except in compliance with the License.
7
 
# You may obtain a copy of the License at
8
 
#
9
 
#      http://www.apache.org/licenses/LICENSE-2.0
10
 
#
11
 
# Unless required by applicable law or agreed to in writing, software
12
 
# distributed under the License is distributed on an "AS IS" BASIS,
13
 
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 
# See the License for the specific language governing permissions and
15
 
# limitations under the License.
 
5
# Redistribution and use in source and binary forms, with or without
 
6
# modification, are permitted provided that the following conditions are
 
7
# met:
 
8
#
 
9
#     * Redistributions of source code must retain the above copyright
 
10
# notice, this list of conditions and the following disclaimer.
 
11
#     * Redistributions in binary form must reproduce the above
 
12
# copyright notice, this list of conditions and the following disclaimer
 
13
# in the documentation and/or other materials provided with the
 
14
# distribution.
 
15
#     * Neither the name of Google Inc. nor the names of its
 
16
# contributors may be used to endorse or promote products derived from
 
17
# this software without specific prior written permission.
 
18
#
 
19
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
20
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
21
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
22
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
23
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
24
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
25
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
26
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
27
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
28
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
29
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16
30
 
17
31
# This code is meant to work on Python 2.4 and above only.
18
32
#
40
54
import threading
41
55
import weakref
42
56
# We use "as" to avoid name collisions with variables.
 
57
from google.protobuf.internal import containers
43
58
from google.protobuf.internal import decoder
44
59
from google.protobuf.internal import encoder
45
60
from google.protobuf.internal import message_listener as message_listener_mod
 
61
from google.protobuf.internal import type_checkers
46
62
from google.protobuf.internal import wire_format
47
63
from google.protobuf import descriptor as descriptor_mod
48
64
from google.protobuf import message as message_mod
137
153
    _AddMessageMethods(descriptor, cls)
138
154
    _AddPrivateHelperMethods(cls)
139
155
    superclass = super(GeneratedProtocolMessageType, cls)
140
 
    superclass.__init__(cls, name, bases, dictionary)
 
156
    superclass.__init__(name, bases, dictionary)
141
157
 
142
158
 
143
159
# Stateless helpers for GeneratedProtocolMessageType below.
259
275
    if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
260
276
      # We can't look at _concrete_class yet since it might not have
261
277
      # been set.  (Depends on order in which we initialize the classes).
262
 
      return _RepeatedCompositeFieldContainer(listener, field.message_type)
 
278
      return containers.RepeatedCompositeFieldContainer(
 
279
          listener, field.message_type)
263
280
    else:
264
 
      return _RepeatedScalarFieldContainer(listener,
265
 
                                           _VALUE_CHECKERS[field.cpp_type])
 
281
      return containers.RepeatedScalarFieldContainer(
 
282
          listener, type_checkers.GetTypeChecker(field.cpp_type, field.type))
266
283
 
267
284
  if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
268
285
    assert field.default_value is None
370
387
  python_field_name = _ValueFieldName(proto_field_name)
371
388
  has_field_name = _HasFieldName(proto_field_name)
372
389
  property_name = _PropertyName(proto_field_name)
373
 
  type_checker = _VALUE_CHECKERS[field.cpp_type]
 
390
  type_checker = type_checkers.GetTypeChecker(field.cpp_type, field.type)
374
391
 
375
392
  def getter(self):
376
393
    return getattr(self, python_field_name)
614
631
      within FieldDescriptor.
615
632
  """
616
633
  try:
617
 
    fn = _TYPE_TO_BYTE_SIZE_FN[field_type]
 
634
    fn = type_checkers.TYPE_TO_BYTE_SIZE_FN[field_type]
618
635
    return fn(field_number, value)
619
636
  except KeyError:
620
637
    raise message_mod.EncodeError('Unrecognized field type: %d' % field_type)
707
724
    return
708
725
 
709
726
  try:
710
 
    method = _TYPE_TO_SERIALIZE_METHOD[field_descriptor.type]
 
727
    method = type_checkers.TYPE_TO_SERIALIZE_METHOD[field_descriptor.type]
711
728
    method(encoder, field_number, value)
712
729
  except KeyError:
713
730
    raise message_mod.EncodeError('Unrecognized field type: %d' %
748
765
 
749
766
def _AddSerializeToStringMethod(message_descriptor, cls):
750
767
  """Helper for _AddMessageMethods()."""
 
768
 
 
769
  def SerializeToString(self):
 
770
    # Check if the message has all of its required fields set.
 
771
    errors = []
 
772
    if not _InternalIsInitialized(self, errors):
 
773
      raise message_mod.EncodeError('\n'.join(errors))
 
774
    return self.SerializePartialToString()
 
775
  cls.SerializeToString = SerializeToString
 
776
 
 
777
 
 
778
def _AddSerializePartialToStringMethod(message_descriptor, cls):
 
779
  """Helper for _AddMessageMethods()."""
751
780
  Encoder = encoder.Encoder
752
781
 
753
 
  def SerializeToString(self):
 
782
  def SerializePartialToString(self):
754
783
    encoder = Encoder()
755
784
    # We need to serialize all extension and non-extension fields
756
785
    # together, in sorted order by field number.
757
 
 
758
 
    # Step 3: Iterate over all extension and non-extension fields, sorted
759
 
    # in order of tag number, and serialize each one to the wire.
760
786
    for field_descriptor, field_value in self.ListFields():
761
787
      if field_descriptor.label == _FieldDescriptor.LABEL_REPEATED:
762
788
        repeated_value = field_value
766
792
        _SerializeValueToEncoder(element, field_descriptor.number,
767
793
                                 field_descriptor, encoder)
768
794
    return encoder.ToString()
769
 
  cls.SerializeToString = SerializeToString
 
795
  cls.SerializePartialToString = SerializePartialToString
770
796
 
771
797
 
772
798
def _WireTypeForFieldType(field_type):
773
799
  """Given a field type, returns the expected wire type."""
774
800
  try:
775
 
    return _FIELD_TYPE_TO_WIRE_TYPE[field_type]
 
801
    return type_checkers.FIELD_TYPE_TO_WIRE_TYPE[field_type]
776
802
  except KeyError:
777
803
    raise message_mod.DecodeError('Unknown field type: %d' % field_type)
778
804
 
804
830
  be a scalar (non-group, non-message) FieldDescriptor.FIELD_* constant.
805
831
  """
806
832
  try:
807
 
    method = _TYPE_TO_DESERIALIZE_METHOD[field_type]
 
833
    method = type_checkers.TYPE_TO_DESERIALIZE_METHOD[field_type]
808
834
    return method(decoder)
809
835
  except KeyError:
810
836
    raise message_mod.DecodeError('Unrecognized field type: %d' % field_type)
820
846
      just after reading the the tag and wire type of the field.
821
847
  """
822
848
  if wire_type == wire_format.WIRETYPE_VARINT:
823
 
    decoder.ReadInt32()
 
849
    decoder.ReadUInt64()
824
850
  elif wire_type == wire_format.WIRETYPE_FIXED64:
825
851
    decoder.ReadFixed64()
826
852
  elif wire_type == wire_format.WIRETYPE_LENGTH_DELIMITED:
1034
1060
    return message.HasField(field_or_extension.name)
1035
1061
 
1036
1062
 
1037
 
def _IsFieldOrExtensionInitialized(message, field):
 
1063
def _IsFieldOrExtensionInitialized(message, field, errors=None):
1038
1064
  """Checks if a message field or extension is initialized.
1039
1065
 
1040
1066
  Args:
1041
1067
    message: The message which contains the field or extension.
1042
1068
    field: Field or extension to check. This must be a FieldDescriptor instance.
 
1069
    errors: Errors will be appended to it, if set to a meaningful value.
1043
1070
 
1044
1071
  Returns:
1045
1072
    True if the field/extension can be considered initialized.
1047
1074
  # If the field is required and is not set, it isn't initialized.
1048
1075
  if field.label == _FieldDescriptor.LABEL_REQUIRED:
1049
1076
    if not _HasFieldOrExtension(message, field):
 
1077
      if errors is not None:
 
1078
        errors.append('Required field %s is not set.' % field.full_name)
1050
1079
      return False
1051
1080
 
1052
1081
  # If the field is optional and is not set, or if it
1062
1091
  # If all submessages in this field are initialized, the field is
1063
1092
  # considered initialized.
1064
1093
  for message in messages:
1065
 
    if not message.IsInitialized():
 
1094
    if not _InternalIsInitialized(message, errors):
 
1095
      return False
 
1096
  return True
 
1097
 
 
1098
 
 
1099
def _InternalIsInitialized(message, errors=None):
 
1100
  """Checks if all required fields of a message are set.
 
1101
 
 
1102
  Args:
 
1103
    message: The message to check.
 
1104
    errors: If set, initialization errors will be appended to it.
 
1105
 
 
1106
  Returns:
 
1107
    True iff the specified message has all required fields set.
 
1108
  """
 
1109
  fields_and_extensions = []
 
1110
  fields_and_extensions.extend(message.DESCRIPTOR.fields)
 
1111
  fields_and_extensions.extend(
 
1112
      [extension[0] for extension in message.Extensions._ListSetExtensions()])
 
1113
  for field_or_extension in fields_and_extensions:
 
1114
    if not _IsFieldOrExtensionInitialized(message, field_or_extension, errors):
1066
1115
      return False
1067
1116
  return True
1068
1117
 
1082
1131
  cls.MergeFromString = MergeFromString
1083
1132
 
1084
1133
 
1085
 
def _AddIsInitializedMethod(message_descriptor, cls):
 
1134
def _AddIsInitializedMethod(cls):
1086
1135
  """Adds the IsInitialized method to the protocol message class."""
1087
 
  def IsInitialized(self):
1088
 
    fields_and_extensions = []
1089
 
    fields_and_extensions.extend(message_descriptor.fields)
1090
 
    fields_and_extensions.extend(
1091
 
        self.Extensions._AllExtensionsByNumber().values())
1092
 
    for field_or_extension in fields_and_extensions:
1093
 
      if not _IsFieldOrExtensionInitialized(self, field_or_extension):
1094
 
        return False
1095
 
    return True
1096
 
  cls.IsInitialized = IsInitialized
 
1136
  cls.IsInitialized = _InternalIsInitialized
 
1137
 
 
1138
 
 
1139
def _MergeFieldOrExtension(destination_msg, field, value):
 
1140
  """Merges a specified message field into another message."""
 
1141
  property_name = _PropertyName(field.name)
 
1142
  is_extension = field.is_extension
 
1143
 
 
1144
  if not is_extension:
 
1145
    destination = getattr(destination_msg, property_name)
 
1146
  elif (field.label == _FieldDescriptor.LABEL_REPEATED or
 
1147
        field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE):
 
1148
    destination = destination_msg.Extensions[field]
 
1149
 
 
1150
  # Case 1 - a composite field.
 
1151
  if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
 
1152
    if field.label == _FieldDescriptor.LABEL_REPEATED:
 
1153
      for v in value:
 
1154
        destination.add().MergeFrom(v)
 
1155
    else:
 
1156
      destination.MergeFrom(value)
 
1157
    return
 
1158
 
 
1159
  # Case 2 - a repeated field.
 
1160
  if field.label == _FieldDescriptor.LABEL_REPEATED:
 
1161
    for v in value:
 
1162
      destination.append(v)
 
1163
    return
 
1164
 
 
1165
  # Case 3 - a singular field.
 
1166
  if is_extension:
 
1167
    destination_msg.Extensions[field] = value
 
1168
  else:
 
1169
    setattr(destination_msg, property_name, value)
 
1170
 
 
1171
 
 
1172
def _AddMergeFromMethod(cls):
 
1173
  def MergeFrom(self, msg):
 
1174
    assert msg is not self
 
1175
    for field in msg.ListFields():
 
1176
      _MergeFieldOrExtension(self, field[0], field[1])
 
1177
  cls.MergeFrom = MergeFrom
1097
1178
 
1098
1179
 
1099
1180
def _AddMessageMethods(message_descriptor, cls):
1100
1181
  """Adds implementations of all Message methods to cls."""
1101
 
 
1102
 
  # TODO(robinson): Add support for remaining Message methods.
1103
 
 
1104
1182
  _AddListFieldsMethod(message_descriptor, cls)
1105
1183
  _AddHasFieldMethod(cls)
1106
1184
  _AddClearFieldMethod(cls)
1111
1189
  _AddSetListenerMethod(cls)
1112
1190
  _AddByteSizeMethod(message_descriptor, cls)
1113
1191
  _AddSerializeToStringMethod(message_descriptor, cls)
 
1192
  _AddSerializePartialToStringMethod(message_descriptor, cls)
1114
1193
  _AddMergeFromStringMethod(message_descriptor, cls)
1115
 
  _AddIsInitializedMethod(message_descriptor, cls)
 
1194
  _AddIsInitializedMethod(cls)
 
1195
  _AddMergeFromMethod(cls)
1116
1196
 
1117
1197
 
1118
1198
def _AddPrivateHelperMethods(cls):
1192
1272
      pass
1193
1273
 
1194
1274
 
1195
 
# TODO(robinson): Move elsewhere?
1196
 
# TODO(robinson): Provide a clear() method here in addition to ClearField()?
1197
 
class _RepeatedScalarFieldContainer(object):
1198
 
 
1199
 
  """Simple, type-checked, list-like container for holding repeated scalars.
1200
 
  """
1201
 
 
1202
 
  def __init__(self, message_listener, type_checker):
1203
 
    """
1204
 
    Args:
1205
 
      message_listener: A MessageListener implementation.
1206
 
        The _RepeatedScalarFieldContaininer will call this object's
1207
 
        TransitionToNonempty() method when it transitions from being empty to
1208
 
        being nonempty.
1209
 
      type_checker: A _ValueChecker instance to run on elements inserted
1210
 
        into this container.
1211
 
    """
1212
 
    self._message_listener = message_listener
1213
 
    self._type_checker = type_checker
1214
 
    self._values = []
1215
 
 
1216
 
  def append(self, elem):
1217
 
    self._type_checker.CheckValue(elem)
1218
 
    self._values.append(elem)
1219
 
    self._message_listener.ByteSizeDirty()
1220
 
    if len(self._values) == 1:
1221
 
      self._message_listener.TransitionToNonempty()
1222
 
 
1223
 
  # List-like __getitem__() support also makes us iterable (via "iter(foo)"
1224
 
  # or implicitly via "for i in mylist:") for free.
1225
 
  def __getitem__(self, key):
1226
 
    return self._values[key]
1227
 
 
1228
 
  def __setitem__(self, key, value):
1229
 
    # No need to call TransitionToNonempty(), since if we're able to
1230
 
    # set the element at this index, we were already nonempty before
1231
 
    # this method was called.
1232
 
    self._message_listener.ByteSizeDirty()
1233
 
    self._type_checker.CheckValue(value)
1234
 
    self._values[key] = value
1235
 
 
1236
 
  def __len__(self):
1237
 
    return len(self._values)
1238
 
 
1239
 
  def __eq__(self, other):
1240
 
    if self is other:
1241
 
      return True
1242
 
    # Special case for the same type which should be common and fast.
1243
 
    if isinstance(other, self.__class__):
1244
 
      return other._values == self._values
1245
 
    # We are presumably comparing against some other sequence type.
1246
 
    return other == self._values
1247
 
 
1248
 
  def __ne__(self, other):
1249
 
    # Can't use != here since it would infinitely recurse.
1250
 
    return not self == other
1251
 
 
1252
 
 
1253
 
# TODO(robinson): Move elsewhere?
1254
 
# TODO(robinson): Provide a clear() method here in addition to ClearField()?
1255
 
# TODO(robinson): Unify common functionality with
1256
 
# _RepeatedScalarFieldContaininer?
1257
 
class _RepeatedCompositeFieldContainer(object):
1258
 
 
1259
 
  """Simple, list-like container for holding repeated composite fields.
1260
 
  """
1261
 
 
1262
 
  def __init__(self, message_listener, message_descriptor):
1263
 
    """Note that we pass in a descriptor instead of the generated directly,
1264
 
    since at the time we construct a _RepeatedCompositeFieldContainer we
1265
 
    haven't yet necessarily initialized the type that will be contained in the
1266
 
    container.
1267
 
 
1268
 
    Args:
1269
 
      message_listener: A MessageListener implementation.
1270
 
        The _RepeatedCompositeFieldContainer will call this object's
1271
 
        TransitionToNonempty() method when it transitions from being empty to
1272
 
        being nonempty.
1273
 
      message_descriptor: A Descriptor instance describing the protocol type
1274
 
        that should be present in this container.  We'll use the
1275
 
        _concrete_class field of this descriptor when the client calls add().
1276
 
    """
1277
 
    self._message_listener = message_listener
1278
 
    self._message_descriptor = message_descriptor
1279
 
    self._values = []
1280
 
 
1281
 
  def add(self):
1282
 
    new_element = self._message_descriptor._concrete_class()
1283
 
    new_element._SetListener(self._message_listener)
1284
 
    self._values.append(new_element)
1285
 
    self._message_listener.ByteSizeDirty()
1286
 
    self._message_listener.TransitionToNonempty()
1287
 
    return new_element
1288
 
 
1289
 
  # List-like __getitem__() support also makes us iterable (via "iter(foo)"
1290
 
  # or implicitly via "for i in mylist:") for free.
1291
 
  def __getitem__(self, key):
1292
 
    return self._values[key]
1293
 
 
1294
 
  def __len__(self):
1295
 
    return len(self._values)
1296
 
 
1297
 
  def __eq__(self, other):
1298
 
    if self is other:
1299
 
      return True
1300
 
    if not isinstance(other, self.__class__):
1301
 
      raise TypeError('Can only compare repeated composite fields against '
1302
 
                      'other repeated composite fields.')
1303
 
    return self._values == other._values
1304
 
 
1305
 
  def __ne__(self, other):
1306
 
    # Can't use != here since it would infinitely recurse.
1307
 
    return not self == other
1308
 
 
1309
 
  # TODO(robinson): Implement, document, and test slicing support.
1310
 
 
1311
 
 
1312
1275
# TODO(robinson): Move elsewhere?  This file is getting pretty ridiculous...
1313
1276
# TODO(robinson): Unify error handling of "unknown extension" crap.
1314
1277
# TODO(robinson): There's so much similarity between the way that
1440
1403
        and field.cpp_type != _FieldDescriptor.CPPTYPE_MESSAGE):
1441
1404
      # It's slightly wasteful to lookup the type checker each time,
1442
1405
      # but we expect this to be a vanishingly uncommon case anyway.
1443
 
      type_checker = _VALUE_CHECKERS[field.cpp_type]
 
1406
      type_checker = type_checkers.GetTypeChecker(field.cpp_type, field.type)
1444
1407
      type_checker.CheckValue(value)
1445
1408
      self._values[handle_id] = value
1446
1409
      self._has_bits[handle_id] = True
1561
1524
    # be careful when we move away from having _known_extensions as a
1562
1525
    # deep-copied member of this object.
1563
1526
    return dict((f.number, f) for f in self._known_extensions.itervalues())
1564
 
 
1565
 
 
1566
 
# None of the typecheckers below make any attempt to guard against people
1567
 
# subclassing builtin types and doing weird things.  We're not trying to
1568
 
# protect against malicious clients here, just people accidentally shooting
1569
 
# themselves in the foot in obvious ways.
1570
 
 
1571
 
class _TypeChecker(object):
1572
 
 
1573
 
  """Type checker used to catch type errors as early as possible
1574
 
  when the client is setting scalar fields in protocol messages.
1575
 
  """
1576
 
 
1577
 
  def __init__(self, *acceptable_types):
1578
 
    self._acceptable_types = acceptable_types
1579
 
 
1580
 
  def CheckValue(self, proposed_value):
1581
 
    if not isinstance(proposed_value, self._acceptable_types):
1582
 
      message = ('%.1024r has type %s, but expected one of: %s' %
1583
 
                 (proposed_value, type(proposed_value), self._acceptable_types))
1584
 
      raise TypeError(message)
1585
 
 
1586
 
 
1587
 
# _IntValueChecker and its subclasses perform integer type-checks
1588
 
# and bounds-checks.
1589
 
class _IntValueChecker(object):
1590
 
 
1591
 
  """Checker used for integer fields.  Performs type-check and range check."""
1592
 
 
1593
 
  def CheckValue(self, proposed_value):
1594
 
    if not isinstance(proposed_value, (int, long)):
1595
 
      message = ('%.1024r has type %s, but expected one of: %s' %
1596
 
                 (proposed_value, type(proposed_value), (int, long)))
1597
 
      raise TypeError(message)
1598
 
    if not self._MIN <= proposed_value <= self._MAX:
1599
 
      raise ValueError('Value out of range: %d' % proposed_value)
1600
 
 
1601
 
class _Int32ValueChecker(_IntValueChecker):
1602
 
  # We're sure to use ints instead of longs here since comparison may be more
1603
 
  # efficient.
1604
 
  _MIN = -2147483648
1605
 
  _MAX = 2147483647
1606
 
 
1607
 
class _Uint32ValueChecker(_IntValueChecker):
1608
 
  _MIN = 0
1609
 
  _MAX = (1 << 32) - 1
1610
 
 
1611
 
class _Int64ValueChecker(_IntValueChecker):
1612
 
  _MIN = -(1 << 63)
1613
 
  _MAX = (1 << 63) - 1
1614
 
 
1615
 
class _Uint64ValueChecker(_IntValueChecker):
1616
 
  _MIN = 0
1617
 
  _MAX = (1 << 64) - 1
1618
 
 
1619
 
 
1620
 
# Type-checkers for all scalar CPPTYPEs.
1621
 
_VALUE_CHECKERS = {
1622
 
    _FieldDescriptor.CPPTYPE_INT32: _Int32ValueChecker(),
1623
 
    _FieldDescriptor.CPPTYPE_INT64: _Int64ValueChecker(),
1624
 
    _FieldDescriptor.CPPTYPE_UINT32: _Uint32ValueChecker(),
1625
 
    _FieldDescriptor.CPPTYPE_UINT64: _Uint64ValueChecker(),
1626
 
    _FieldDescriptor.CPPTYPE_DOUBLE: _TypeChecker(
1627
 
        float, int, long),
1628
 
    _FieldDescriptor.CPPTYPE_FLOAT: _TypeChecker(
1629
 
        float, int, long),
1630
 
    _FieldDescriptor.CPPTYPE_BOOL: _TypeChecker(bool, int),
1631
 
    _FieldDescriptor.CPPTYPE_ENUM: _Int32ValueChecker(),
1632
 
    _FieldDescriptor.CPPTYPE_STRING: _TypeChecker(str),
1633
 
    }
1634
 
 
1635
 
 
1636
 
# Map from field type to a function F, such that F(field_num, value)
1637
 
# gives the total byte size for a value of the given type.  This
1638
 
# byte size includes tag information and any other additional space
1639
 
# associated with serializing "value".
1640
 
_TYPE_TO_BYTE_SIZE_FN = {
1641
 
    _FieldDescriptor.TYPE_DOUBLE: wire_format.DoubleByteSize,
1642
 
    _FieldDescriptor.TYPE_FLOAT: wire_format.FloatByteSize,
1643
 
    _FieldDescriptor.TYPE_INT64: wire_format.Int64ByteSize,
1644
 
    _FieldDescriptor.TYPE_UINT64: wire_format.UInt64ByteSize,
1645
 
    _FieldDescriptor.TYPE_INT32: wire_format.Int32ByteSize,
1646
 
    _FieldDescriptor.TYPE_FIXED64: wire_format.Fixed64ByteSize,
1647
 
    _FieldDescriptor.TYPE_FIXED32: wire_format.Fixed32ByteSize,
1648
 
    _FieldDescriptor.TYPE_BOOL: wire_format.BoolByteSize,
1649
 
    _FieldDescriptor.TYPE_STRING: wire_format.StringByteSize,
1650
 
    _FieldDescriptor.TYPE_GROUP: wire_format.GroupByteSize,
1651
 
    _FieldDescriptor.TYPE_MESSAGE: wire_format.MessageByteSize,
1652
 
    _FieldDescriptor.TYPE_BYTES: wire_format.BytesByteSize,
1653
 
    _FieldDescriptor.TYPE_UINT32: wire_format.UInt32ByteSize,
1654
 
    _FieldDescriptor.TYPE_ENUM: wire_format.EnumByteSize,
1655
 
    _FieldDescriptor.TYPE_SFIXED32: wire_format.SFixed32ByteSize,
1656
 
    _FieldDescriptor.TYPE_SFIXED64: wire_format.SFixed64ByteSize,
1657
 
    _FieldDescriptor.TYPE_SINT32: wire_format.SInt32ByteSize,
1658
 
    _FieldDescriptor.TYPE_SINT64: wire_format.SInt64ByteSize
1659
 
    }
1660
 
 
1661
 
# Maps from field type to an unbound Encoder method F, such that
1662
 
# F(encoder, field_number, value) will append the serialization
1663
 
# of a value of this type to the encoder.
1664
 
_Encoder = encoder.Encoder
1665
 
_TYPE_TO_SERIALIZE_METHOD = {
1666
 
    _FieldDescriptor.TYPE_DOUBLE: _Encoder.AppendDouble,
1667
 
    _FieldDescriptor.TYPE_FLOAT: _Encoder.AppendFloat,
1668
 
    _FieldDescriptor.TYPE_INT64: _Encoder.AppendInt64,
1669
 
    _FieldDescriptor.TYPE_UINT64: _Encoder.AppendUInt64,
1670
 
    _FieldDescriptor.TYPE_INT32: _Encoder.AppendInt32,
1671
 
    _FieldDescriptor.TYPE_FIXED64: _Encoder.AppendFixed64,
1672
 
    _FieldDescriptor.TYPE_FIXED32: _Encoder.AppendFixed32,
1673
 
    _FieldDescriptor.TYPE_BOOL: _Encoder.AppendBool,
1674
 
    _FieldDescriptor.TYPE_STRING: _Encoder.AppendString,
1675
 
    _FieldDescriptor.TYPE_GROUP: _Encoder.AppendGroup,
1676
 
    _FieldDescriptor.TYPE_MESSAGE: _Encoder.AppendMessage,
1677
 
    _FieldDescriptor.TYPE_BYTES: _Encoder.AppendBytes,
1678
 
    _FieldDescriptor.TYPE_UINT32: _Encoder.AppendUInt32,
1679
 
    _FieldDescriptor.TYPE_ENUM: _Encoder.AppendEnum,
1680
 
    _FieldDescriptor.TYPE_SFIXED32: _Encoder.AppendSFixed32,
1681
 
    _FieldDescriptor.TYPE_SFIXED64: _Encoder.AppendSFixed64,
1682
 
    _FieldDescriptor.TYPE_SINT32: _Encoder.AppendSInt32,
1683
 
    _FieldDescriptor.TYPE_SINT64: _Encoder.AppendSInt64,
1684
 
    }
1685
 
 
1686
 
# Maps from field type to expected wiretype.
1687
 
_FIELD_TYPE_TO_WIRE_TYPE = {
1688
 
    _FieldDescriptor.TYPE_DOUBLE: wire_format.WIRETYPE_FIXED64,
1689
 
    _FieldDescriptor.TYPE_FLOAT: wire_format.WIRETYPE_FIXED32,
1690
 
    _FieldDescriptor.TYPE_INT64: wire_format.WIRETYPE_VARINT,
1691
 
    _FieldDescriptor.TYPE_UINT64: wire_format.WIRETYPE_VARINT,
1692
 
    _FieldDescriptor.TYPE_INT32: wire_format.WIRETYPE_VARINT,
1693
 
    _FieldDescriptor.TYPE_FIXED64: wire_format.WIRETYPE_FIXED64,
1694
 
    _FieldDescriptor.TYPE_FIXED32: wire_format.WIRETYPE_FIXED32,
1695
 
    _FieldDescriptor.TYPE_BOOL: wire_format.WIRETYPE_VARINT,
1696
 
    _FieldDescriptor.TYPE_STRING:
1697
 
      wire_format.WIRETYPE_LENGTH_DELIMITED,
1698
 
    _FieldDescriptor.TYPE_GROUP: wire_format.WIRETYPE_START_GROUP,
1699
 
    _FieldDescriptor.TYPE_MESSAGE:
1700
 
      wire_format.WIRETYPE_LENGTH_DELIMITED,
1701
 
    _FieldDescriptor.TYPE_BYTES:
1702
 
      wire_format.WIRETYPE_LENGTH_DELIMITED,
1703
 
    _FieldDescriptor.TYPE_UINT32: wire_format.WIRETYPE_VARINT,
1704
 
    _FieldDescriptor.TYPE_ENUM: wire_format.WIRETYPE_VARINT,
1705
 
    _FieldDescriptor.TYPE_SFIXED32: wire_format.WIRETYPE_FIXED32,
1706
 
    _FieldDescriptor.TYPE_SFIXED64: wire_format.WIRETYPE_FIXED64,
1707
 
    _FieldDescriptor.TYPE_SINT32: wire_format.WIRETYPE_VARINT,
1708
 
    _FieldDescriptor.TYPE_SINT64: wire_format.WIRETYPE_VARINT,
1709
 
    }
1710
 
 
1711
 
# Maps from field type to an unbound Decoder method F,
1712
 
# such that F(decoder) will read a field of the requested type.
1713
 
#
1714
 
# Note that Message and Group are intentionally missing here.
1715
 
# They're handled by _RecursivelyMerge().
1716
 
_Decoder = decoder.Decoder
1717
 
_TYPE_TO_DESERIALIZE_METHOD = {
1718
 
    _FieldDescriptor.TYPE_DOUBLE: _Decoder.ReadDouble,
1719
 
    _FieldDescriptor.TYPE_FLOAT: _Decoder.ReadFloat,
1720
 
    _FieldDescriptor.TYPE_INT64: _Decoder.ReadInt64,
1721
 
    _FieldDescriptor.TYPE_UINT64: _Decoder.ReadUInt64,
1722
 
    _FieldDescriptor.TYPE_INT32: _Decoder.ReadInt32,
1723
 
    _FieldDescriptor.TYPE_FIXED64: _Decoder.ReadFixed64,
1724
 
    _FieldDescriptor.TYPE_FIXED32: _Decoder.ReadFixed32,
1725
 
    _FieldDescriptor.TYPE_BOOL: _Decoder.ReadBool,
1726
 
    _FieldDescriptor.TYPE_STRING: _Decoder.ReadString,
1727
 
    _FieldDescriptor.TYPE_BYTES: _Decoder.ReadBytes,
1728
 
    _FieldDescriptor.TYPE_UINT32: _Decoder.ReadUInt32,
1729
 
    _FieldDescriptor.TYPE_ENUM: _Decoder.ReadEnum,
1730
 
    _FieldDescriptor.TYPE_SFIXED32: _Decoder.ReadSFixed32,
1731
 
    _FieldDescriptor.TYPE_SFIXED64: _Decoder.ReadSFixed64,
1732
 
    _FieldDescriptor.TYPE_SINT32: _Decoder.ReadSInt32,
1733
 
    _FieldDescriptor.TYPE_SINT64: _Decoder.ReadSInt64,
1734
 
    }