aboutsummaryrefslogtreecommitdiffhomepage
path: root/python/google/protobuf
diff options
context:
space:
mode:
authorGravatar temporal <temporal@630680e5-0e50-0410-840e-4b1c322b438d>2008-08-13 03:15:00 +0000
committerGravatar temporal <temporal@630680e5-0e50-0410-840e-4b1c322b438d>2008-08-13 03:15:00 +0000
commit779f61c6a3ce02a119e28e802f229e61b69b9046 (patch)
tree9131ef5f0acdc3d708a795fc6703488674741ee0 /python/google/protobuf
parenta0f27fcd96c5bf2509ca88cca54f00b78f7b8bc5 (diff)
Integrate recent changes from google3.
protoc - New flags --encode and --decode can be used to convert between protobuf text format and binary format from the command-line. - New flag --descriptor_set_out can be used to write FileDescriptorProtos for all parsed files directly into a single output file. This is particularly useful if you wish to parse .proto files from programs written in languages other than C++: just run protoc as a background process and have it output a FileDescriptorList, then parse that natively. C++ - Reflection objects are now per-class rather than per-instance. To make this possible, the Reflection interface had to be changed such that all methods take the Message instance as a parameter. This change improves performance significantly in memory-bandwidth-limited use cases, since it makes the message objects smaller. Note that source-incompatible interface changes like this will not be made again after the library leaves beta. Python - MergeFrom(message) and CopyFrom(message) are now implemented. - SerializeToString() raises an exception if the message is missing required fields. - Code organization improvements. - Fixed doc comments for RpcController and RpcChannel, which had somehow been swapped.
Diffstat (limited to 'python/google/protobuf')
-rwxr-xr-xpython/google/protobuf/internal/reflection_test.py220
-rwxr-xr-xpython/google/protobuf/message.py50
-rwxr-xr-xpython/google/protobuf/reflection.py295
-rwxr-xr-xpython/google/protobuf/service.py36
4 files changed, 378 insertions, 223 deletions
diff --git a/python/google/protobuf/internal/reflection_test.py b/python/google/protobuf/internal/reflection_test.py
index 5947f97a..55777819 100755
--- a/python/google/protobuf/internal/reflection_test.py
+++ b/python/google/protobuf/internal/reflection_test.py
@@ -656,9 +656,172 @@ class RefectionTest(unittest.TestCase):
self.assertRaises(KeyError, extendee_proto.HasExtension,
unittest_pb2.repeated_string_extension)
- def testCopyFrom(self):
- # TODO(robinson): Implement.
- pass
+ def testMergeFromSingularField(self):
+ # Test merge with just a singular field.
+ proto1 = unittest_pb2.TestAllTypes()
+ proto1.optional_int32 = 1
+
+ proto2 = unittest_pb2.TestAllTypes()
+ # This shouldn't get overwritten.
+ proto2.optional_string = 'value'
+
+ proto2.MergeFrom(proto1)
+ self.assertEqual(1, proto2.optional_int32)
+ self.assertEqual('value', proto2.optional_string)
+
+ def testMergeFromRepeatedField(self):
+ # Test merge with just a repeated field.
+ proto1 = unittest_pb2.TestAllTypes()
+ proto1.repeated_int32.append(1)
+ proto1.repeated_int32.append(2)
+
+ proto2 = unittest_pb2.TestAllTypes()
+ proto2.repeated_int32.append(0)
+ proto2.MergeFrom(proto1)
+
+ self.assertEqual(0, proto2.repeated_int32[0])
+ self.assertEqual(1, proto2.repeated_int32[1])
+ self.assertEqual(2, proto2.repeated_int32[2])
+
+ def testMergeFromOptionalGroup(self):
+ # Test merge with an optional group.
+ proto1 = unittest_pb2.TestAllTypes()
+ proto1.optionalgroup.a = 12
+ proto2 = unittest_pb2.TestAllTypes()
+ proto2.MergeFrom(proto1)
+ self.assertEqual(12, proto2.optionalgroup.a)
+
+ def testMergeFromRepeatedNestedMessage(self):
+ # Test merge with a repeated nested message.
+ proto1 = unittest_pb2.TestAllTypes()
+ m = proto1.repeated_nested_message.add()
+ m.bb = 123
+ m = proto1.repeated_nested_message.add()
+ m.bb = 321
+
+ proto2 = unittest_pb2.TestAllTypes()
+ m = proto2.repeated_nested_message.add()
+ m.bb = 999
+ proto2.MergeFrom(proto1)
+ self.assertEqual(999, proto2.repeated_nested_message[0].bb)
+ self.assertEqual(123, proto2.repeated_nested_message[1].bb)
+ self.assertEqual(321, proto2.repeated_nested_message[2].bb)
+
+ def testMergeFromAllFields(self):
+ # With all fields set.
+ proto1 = unittest_pb2.TestAllTypes()
+ test_util.SetAllFields(proto1)
+ proto2 = unittest_pb2.TestAllTypes()
+ proto2.MergeFrom(proto1)
+
+ # Messages should be equal.
+ self.assertEqual(proto2, proto1)
+
+ # Serialized string should be equal too.
+ string1 = proto1.SerializeToString()
+ string2 = proto2.SerializeToString()
+ self.assertEqual(string1, string2)
+
+ def testMergeFromExtensionsSingular(self):
+ proto1 = unittest_pb2.TestAllExtensions()
+ proto1.Extensions[unittest_pb2.optional_int32_extension] = 1
+
+ proto2 = unittest_pb2.TestAllExtensions()
+ proto2.MergeFrom(proto1)
+ self.assertEqual(
+ 1, proto2.Extensions[unittest_pb2.optional_int32_extension])
+
+ def testMergeFromExtensionsRepeated(self):
+ proto1 = unittest_pb2.TestAllExtensions()
+ proto1.Extensions[unittest_pb2.repeated_int32_extension].append(1)
+ proto1.Extensions[unittest_pb2.repeated_int32_extension].append(2)
+
+ proto2 = unittest_pb2.TestAllExtensions()
+ proto2.Extensions[unittest_pb2.repeated_int32_extension].append(0)
+ proto2.MergeFrom(proto1)
+ self.assertEqual(
+ 3, len(proto2.Extensions[unittest_pb2.repeated_int32_extension]))
+ self.assertEqual(
+ 0, proto2.Extensions[unittest_pb2.repeated_int32_extension][0])
+ self.assertEqual(
+ 1, proto2.Extensions[unittest_pb2.repeated_int32_extension][1])
+ self.assertEqual(
+ 2, proto2.Extensions[unittest_pb2.repeated_int32_extension][2])
+
+ def testMergeFromExtensionsNestedMessage(self):
+ proto1 = unittest_pb2.TestAllExtensions()
+ ext1 = proto1.Extensions[
+ unittest_pb2.repeated_nested_message_extension]
+ m = ext1.add()
+ m.bb = 222
+ m = ext1.add()
+ m.bb = 333
+
+ proto2 = unittest_pb2.TestAllExtensions()
+ ext2 = proto2.Extensions[
+ unittest_pb2.repeated_nested_message_extension]
+ m = ext2.add()
+ m.bb = 111
+
+ proto2.MergeFrom(proto1)
+ ext2 = proto2.Extensions[
+ unittest_pb2.repeated_nested_message_extension]
+ self.assertEqual(3, len(ext2))
+ self.assertEqual(111, ext2[0].bb)
+ self.assertEqual(222, ext2[1].bb)
+ self.assertEqual(333, ext2[2].bb)
+
+ def testCopyFromSingularField(self):
+ # Test copy with just a singular field.
+ proto1 = unittest_pb2.TestAllTypes()
+ proto1.optional_int32 = 1
+ proto1.optional_string = 'important-text'
+
+ proto2 = unittest_pb2.TestAllTypes()
+ proto2.optional_string = 'value'
+
+ proto2.CopyFrom(proto1)
+ self.assertEqual(1, proto2.optional_int32)
+ self.assertEqual('important-text', proto2.optional_string)
+
+ def testCopyFromRepeatedField(self):
+ # Test copy with a repeated field.
+ proto1 = unittest_pb2.TestAllTypes()
+ proto1.repeated_int32.append(1)
+ proto1.repeated_int32.append(2)
+
+ proto2 = unittest_pb2.TestAllTypes()
+ proto2.repeated_int32.append(0)
+ proto2.CopyFrom(proto1)
+
+ self.assertEqual(1, proto2.repeated_int32[0])
+ self.assertEqual(2, proto2.repeated_int32[1])
+
+ def testCopyFromAllFields(self):
+ # With all fields set.
+ proto1 = unittest_pb2.TestAllTypes()
+ test_util.SetAllFields(proto1)
+ proto2 = unittest_pb2.TestAllTypes()
+ proto2.CopyFrom(proto1)
+
+ # Messages should be equal.
+ self.assertEqual(proto2, proto1)
+
+ # Serialized string should be equal too.
+ string1 = proto1.SerializeToString()
+ string2 = proto2.SerializeToString()
+ self.assertEqual(string1, string2)
+
+ def testCopyFromSelf(self):
+ proto1 = unittest_pb2.TestAllTypes()
+ proto1.repeated_int32.append(1)
+ proto1.optional_int32 = 2
+ proto1.optional_string = 'important-text'
+
+ proto1.CopyFrom(proto1)
+ self.assertEqual(1, proto1.repeated_int32[0])
+ self.assertEqual(2, proto1.optional_int32)
+ self.assertEqual('important-text', proto1.optional_string)
def testClear(self):
proto = unittest_pb2.TestAllTypes()
@@ -1256,6 +1419,57 @@ class SerializationTest(unittest.TestCase):
# Parsing this message should succeed.
proto2.MergeFromString(serialized)
+ def _CheckRaises(self, exc_class, callable_obj, exception):
+ """This method checks if the excpetion type and message are as expected."""
+ try:
+ callable_obj()
+ except exc_class, ex:
+ # Check if the exception message is the right one.
+ self.assertEqual(exception, str(ex))
+ return
+ else:
+ raise self.failureException('%s not raised' % str(exc_class))
+
+ def testSerializeUninitialized(self):
+ proto = unittest_pb2.TestRequired()
+ self._CheckRaises(
+ message.EncodeError,
+ proto.SerializeToString,
+ 'Required field protobuf_unittest.TestRequired.a is not set.')
+ # Shouldn't raise exceptions.
+ partial = proto.SerializePartialToString()
+
+ proto.a = 1
+ self._CheckRaises(
+ message.EncodeError,
+ proto.SerializeToString,
+ 'Required field protobuf_unittest.TestRequired.b is not set.')
+ # Shouldn't raise exceptions.
+ partial = proto.SerializePartialToString()
+
+ proto.b = 2
+ self._CheckRaises(
+ message.EncodeError,
+ proto.SerializeToString,
+ 'Required field protobuf_unittest.TestRequired.c is not set.')
+ # Shouldn't raise exceptions.
+ partial = proto.SerializePartialToString()
+
+ proto.c = 3
+ serialized = proto.SerializeToString()
+ # Shouldn't raise exceptions.
+ partial = proto.SerializePartialToString()
+
+ proto2 = unittest_pb2.TestRequired()
+ proto2.MergeFromString(serialized)
+ self.assertEqual(1, proto2.a)
+ self.assertEqual(2, proto2.b)
+ self.assertEqual(3, proto2.c)
+ proto2.ParseFromString(partial)
+ self.assertEqual(1, proto2.a)
+ self.assertEqual(2, proto2.b)
+ self.assertEqual(3, proto2.c)
+
class OptionsTest(unittest.TestCase):
diff --git a/python/google/protobuf/message.py b/python/google/protobuf/message.py
index 9b48f889..593f6a63 100755
--- a/python/google/protobuf/message.py
+++ b/python/google/protobuf/message.py
@@ -65,15 +65,43 @@ class Message(object):
return text_format.MessageToString(self)
def MergeFrom(self, other_msg):
+ """Merges the contents of the specified message into current message.
+
+ This method merges the contents of the specified message into the current
+ message. Singular fields that are set in the specified message overwrite
+ the corresponding fields in the current message. Repeated fields are
+ appended. Singular sub-messages and groups are recursively merged.
+
+ Args:
+ other_msg: Message to merge into the current message.
+ """
raise NotImplementedError
def CopyFrom(self, other_msg):
- raise NotImplementedError
+ """Copies the content of the specified message into the current message.
+
+ The method clears the current message and then merges the specified
+ message using MergeFrom.
+
+ Args:
+ other_msg: Message to copy into the current one.
+ """
+ if self == other_msg:
+ return
+ self.Clear()
+ self.MergeFrom(other_msg)
def Clear(self):
+ """Clears all data that was set in the message."""
raise NotImplementedError
def IsInitialized(self):
+ """Checks if the message is initialized.
+
+ Returns:
+ The method returns True if the message is initialized (i.e. all of its
+ required fields are set).
+ """
raise NotImplementedError
# TODO(robinson): MergeFromString() should probably return None and be
@@ -118,6 +146,26 @@ class Message(object):
self.MergeFromString(serialized)
def SerializeToString(self):
+ """Serializes the protocol message to a binary string.
+
+ Returns:
+ A binary string representation of the message if all of the required
+ fields in the message are set (i.e. the message is initialized).
+
+ Raises:
+ message.EncodeError if the message isn't initialized.
+ """
+ raise NotImplementedError
+
+ def SerializePartialToString(self):
+ """Serializes the protocol message to a binary string.
+
+ This method is similar to SerializeToString but doesn't check if the
+ message is initialized.
+
+ Returns:
+ A string representation of the partial message.
+ """
raise NotImplementedError
# TODO(robinson): Decide whether we like these better
diff --git a/python/google/protobuf/reflection.py b/python/google/protobuf/reflection.py
index 75202c4e..ef054466 100755
--- a/python/google/protobuf/reflection.py
+++ b/python/google/protobuf/reflection.py
@@ -43,6 +43,7 @@ import weakref
from google.protobuf.internal import decoder
from google.protobuf.internal import encoder
from google.protobuf.internal import message_listener as message_listener_mod
+from google.protobuf.internal import type_checkers
from google.protobuf.internal import wire_format
from google.protobuf import descriptor as descriptor_mod
from google.protobuf import message as message_mod
@@ -261,8 +262,8 @@ def _DefaultValueForField(message, field):
# been set. (Depends on order in which we initialize the classes).
return _RepeatedCompositeFieldContainer(listener, field.message_type)
else:
- return _RepeatedScalarFieldContainer(listener,
- _VALUE_CHECKERS[field.cpp_type])
+ return _RepeatedScalarFieldContainer(
+ listener, type_checkers.VALUE_CHECKERS[field.cpp_type])
if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
assert field.default_value is None
@@ -370,7 +371,7 @@ def _AddPropertiesForNonRepeatedScalarField(field, cls):
python_field_name = _ValueFieldName(proto_field_name)
has_field_name = _HasFieldName(proto_field_name)
property_name = _PropertyName(proto_field_name)
- type_checker = _VALUE_CHECKERS[field.cpp_type]
+ type_checker = type_checkers.VALUE_CHECKERS[field.cpp_type]
def getter(self):
return getattr(self, python_field_name)
@@ -614,7 +615,7 @@ def _BytesForNonRepeatedElement(value, field_number, field_type):
within FieldDescriptor.
"""
try:
- fn = _TYPE_TO_BYTE_SIZE_FN[field_type]
+ fn = type_checkers.TYPE_TO_BYTE_SIZE_FN[field_type]
return fn(field_number, value)
except KeyError:
raise message_mod.EncodeError('Unrecognized field type: %d' % field_type)
@@ -707,7 +708,7 @@ def _SerializeValueToEncoder(value, field_number, field_descriptor, encoder):
return
try:
- method = _TYPE_TO_SERIALIZE_METHOD[field_descriptor.type]
+ method = type_checkers.TYPE_TO_SERIALIZE_METHOD[field_descriptor.type]
method(encoder, field_number, value)
except KeyError:
raise message_mod.EncodeError('Unrecognized field type: %d' %
@@ -748,15 +749,24 @@ def _ImergeSorted(*streams):
def _AddSerializeToStringMethod(message_descriptor, cls):
"""Helper for _AddMessageMethods()."""
- Encoder = encoder.Encoder
def SerializeToString(self):
+ # Check if the message has all of its required fields set.
+ errors = []
+ if not _InternalIsInitialized(self, errors):
+ raise message_mod.EncodeError('\n'.join(errors))
+ return self.SerializePartialToString()
+ cls.SerializeToString = SerializeToString
+
+
+def _AddSerializePartialToStringMethod(message_descriptor, cls):
+ """Helper for _AddMessageMethods()."""
+ Encoder = encoder.Encoder
+
+ def SerializePartialToString(self):
encoder = Encoder()
# We need to serialize all extension and non-extension fields
# together, in sorted order by field number.
-
- # Step 3: Iterate over all extension and non-extension fields, sorted
- # in order of tag number, and serialize each one to the wire.
for field_descriptor, field_value in self.ListFields():
if field_descriptor.label == _FieldDescriptor.LABEL_REPEATED:
repeated_value = field_value
@@ -766,13 +776,13 @@ def _AddSerializeToStringMethod(message_descriptor, cls):
_SerializeValueToEncoder(element, field_descriptor.number,
field_descriptor, encoder)
return encoder.ToString()
- cls.SerializeToString = SerializeToString
+ cls.SerializePartialToString = SerializePartialToString
def _WireTypeForFieldType(field_type):
"""Given a field type, returns the expected wire type."""
try:
- return _FIELD_TYPE_TO_WIRE_TYPE[field_type]
+ return type_checkers.FIELD_TYPE_TO_WIRE_TYPE[field_type]
except KeyError:
raise message_mod.DecodeError('Unknown field type: %d' % field_type)
@@ -804,7 +814,7 @@ def _DeserializeScalarFromDecoder(field_type, decoder):
be a scalar (non-group, non-message) FieldDescriptor.FIELD_* constant.
"""
try:
- method = _TYPE_TO_DESERIALIZE_METHOD[field_type]
+ method = type_checkers.TYPE_TO_DESERIALIZE_METHOD[field_type]
return method(decoder)
except KeyError:
raise message_mod.DecodeError('Unrecognized field type: %d' % field_type)
@@ -1034,12 +1044,13 @@ def _HasFieldOrExtension(message, field_or_extension):
return message.HasField(field_or_extension.name)
-def _IsFieldOrExtensionInitialized(message, field):
+def _IsFieldOrExtensionInitialized(message, field, errors=None):
"""Checks if a message field or extension is initialized.
Args:
message: The message which contains the field or extension.
field: Field or extension to check. This must be a FieldDescriptor instance.
+ errors: Errors will be appended to it, if set to a meaningful value.
Returns:
True if the field/extension can be considered initialized.
@@ -1047,6 +1058,8 @@ def _IsFieldOrExtensionInitialized(message, field):
# If the field is required and is not set, it isn't initialized.
if field.label == _FieldDescriptor.LABEL_REQUIRED:
if not _HasFieldOrExtension(message, field):
+ if errors is not None:
+ errors.append('Required field %s is not set.' % field.full_name)
return False
# If the field is optional and is not set, or if it
@@ -1062,7 +1075,27 @@ def _IsFieldOrExtensionInitialized(message, field):
# If all submessages in this field are initialized, the field is
# considered initialized.
for message in messages:
- if not message.IsInitialized():
+ if not _InternalIsInitialized(message, errors):
+ return False
+ return True
+
+
+def _InternalIsInitialized(message, errors=None):
+ """Checks if all required fields of a message are set.
+
+ Args:
+ message: The message to check.
+ errors: If set, initialization errors will be appended to it.
+
+ Returns:
+ True iff the specified message has all required fields set.
+ """
+ fields_and_extensions = []
+ fields_and_extensions.extend(message.DESCRIPTOR.fields)
+ fields_and_extensions.extend(
+ [extension[0] for extension in message.Extensions._ListSetExtensions()])
+ for field_or_extension in fields_and_extensions:
+ if not _IsFieldOrExtensionInitialized(message, field_or_extension, errors):
return False
return True
@@ -1082,25 +1115,54 @@ def _AddMergeFromStringMethod(message_descriptor, cls):
cls.MergeFromString = MergeFromString
-def _AddIsInitializedMethod(message_descriptor, cls):
+def _AddIsInitializedMethod(cls):
"""Adds the IsInitialized method to the protocol message class."""
- def IsInitialized(self):
- fields_and_extensions = []
- fields_and_extensions.extend(message_descriptor.fields)
- fields_and_extensions.extend(
- self.Extensions._AllExtensionsByNumber().values())
- for field_or_extension in fields_and_extensions:
- if not _IsFieldOrExtensionInitialized(self, field_or_extension):
- return False
- return True
- cls.IsInitialized = IsInitialized
+ cls.IsInitialized = _InternalIsInitialized
-def _AddMessageMethods(message_descriptor, cls):
- """Adds implementations of all Message methods to cls."""
+def _MergeFieldOrExtension(destination_msg, field, value):
+ """Merges a specified message field into another message."""
+ property_name = _PropertyName(field.name)
+ is_extension = field.is_extension
- # TODO(robinson): Add support for remaining Message methods.
+ if not is_extension:
+ destination = getattr(destination_msg, property_name)
+ elif (field.label == _FieldDescriptor.LABEL_REPEATED or
+ field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE):
+ destination = destination_msg.Extensions[field]
+ # Case 1 - a composite field.
+ if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
+ if field.label == _FieldDescriptor.LABEL_REPEATED:
+ for v in value:
+ destination.add().MergeFrom(v)
+ else:
+ destination.MergeFrom(value)
+ return
+
+ # Case 2 - a repeated field.
+ if field.label == _FieldDescriptor.LABEL_REPEATED:
+ for v in value:
+ destination.append(v)
+ return
+
+ # Case 3 - a singular field.
+ if is_extension:
+ destination_msg.Extensions[field] = value
+ else:
+ setattr(destination_msg, property_name, value)
+
+
+def _AddMergeFromMethod(cls):
+ def MergeFrom(self, msg):
+ assert msg is not self
+ for field in msg.ListFields():
+ _MergeFieldOrExtension(self, field[0], field[1])
+ cls.MergeFrom = MergeFrom
+
+
+def _AddMessageMethods(message_descriptor, cls):
+ """Adds implementations of all Message methods to cls."""
_AddListFieldsMethod(message_descriptor, cls)
_AddHasFieldMethod(cls)
_AddClearFieldMethod(cls)
@@ -1111,8 +1173,10 @@ def _AddMessageMethods(message_descriptor, cls):
_AddSetListenerMethod(cls)
_AddByteSizeMethod(message_descriptor, cls)
_AddSerializeToStringMethod(message_descriptor, cls)
+ _AddSerializePartialToStringMethod(message_descriptor, cls)
_AddMergeFromStringMethod(message_descriptor, cls)
- _AddIsInitializedMethod(message_descriptor, cls)
+ _AddIsInitializedMethod(cls)
+ _AddMergeFromMethod(cls)
def _AddPrivateHelperMethods(cls):
@@ -1440,7 +1504,7 @@ class _ExtensionDict(object):
and field.cpp_type != _FieldDescriptor.CPPTYPE_MESSAGE):
# It's slightly wasteful to lookup the type checker each time,
# but we expect this to be a vanishingly uncommon case anyway.
- type_checker = _VALUE_CHECKERS[field.cpp_type]
+ type_checker = type_checkers.VALUE_CHECKERS[field.cpp_type]
type_checker.CheckValue(value)
self._values[handle_id] = value
self._has_bits[handle_id] = True
@@ -1561,174 +1625,3 @@ class _ExtensionDict(object):
# be careful when we move away from having _known_extensions as a
# deep-copied member of this object.
return dict((f.number, f) for f in self._known_extensions.itervalues())
-
-
-# None of the typecheckers below make any attempt to guard against people
-# subclassing builtin types and doing weird things. We're not trying to
-# protect against malicious clients here, just people accidentally shooting
-# themselves in the foot in obvious ways.
-
-class _TypeChecker(object):
-
- """Type checker used to catch type errors as early as possible
- when the client is setting scalar fields in protocol messages.
- """
-
- def __init__(self, *acceptable_types):
- self._acceptable_types = acceptable_types
-
- def CheckValue(self, proposed_value):
- if not isinstance(proposed_value, self._acceptable_types):
- message = ('%.1024r has type %s, but expected one of: %s' %
- (proposed_value, type(proposed_value), self._acceptable_types))
- raise TypeError(message)
-
-
-# _IntValueChecker and its subclasses perform integer type-checks
-# and bounds-checks.
-class _IntValueChecker(object):
-
- """Checker used for integer fields. Performs type-check and range check."""
-
- def CheckValue(self, proposed_value):
- if not isinstance(proposed_value, (int, long)):
- message = ('%.1024r has type %s, but expected one of: %s' %
- (proposed_value, type(proposed_value), (int, long)))
- raise TypeError(message)
- if not self._MIN <= proposed_value <= self._MAX:
- raise ValueError('Value out of range: %d' % proposed_value)
-
-class _Int32ValueChecker(_IntValueChecker):
- # We're sure to use ints instead of longs here since comparison may be more
- # efficient.
- _MIN = -2147483648
- _MAX = 2147483647
-
-class _Uint32ValueChecker(_IntValueChecker):
- _MIN = 0
- _MAX = (1 << 32) - 1
-
-class _Int64ValueChecker(_IntValueChecker):
- _MIN = -(1 << 63)
- _MAX = (1 << 63) - 1
-
-class _Uint64ValueChecker(_IntValueChecker):
- _MIN = 0
- _MAX = (1 << 64) - 1
-
-
-# Type-checkers for all scalar CPPTYPEs.
-_VALUE_CHECKERS = {
- _FieldDescriptor.CPPTYPE_INT32: _Int32ValueChecker(),
- _FieldDescriptor.CPPTYPE_INT64: _Int64ValueChecker(),
- _FieldDescriptor.CPPTYPE_UINT32: _Uint32ValueChecker(),
- _FieldDescriptor.CPPTYPE_UINT64: _Uint64ValueChecker(),
- _FieldDescriptor.CPPTYPE_DOUBLE: _TypeChecker(
- float, int, long),
- _FieldDescriptor.CPPTYPE_FLOAT: _TypeChecker(
- float, int, long),
- _FieldDescriptor.CPPTYPE_BOOL: _TypeChecker(bool, int),
- _FieldDescriptor.CPPTYPE_ENUM: _Int32ValueChecker(),
- _FieldDescriptor.CPPTYPE_STRING: _TypeChecker(str),
- }
-
-
-# Map from field type to a function F, such that F(field_num, value)
-# gives the total byte size for a value of the given type. This
-# byte size includes tag information and any other additional space
-# associated with serializing "value".
-_TYPE_TO_BYTE_SIZE_FN = {
- _FieldDescriptor.TYPE_DOUBLE: wire_format.DoubleByteSize,
- _FieldDescriptor.TYPE_FLOAT: wire_format.FloatByteSize,
- _FieldDescriptor.TYPE_INT64: wire_format.Int64ByteSize,
- _FieldDescriptor.TYPE_UINT64: wire_format.UInt64ByteSize,
- _FieldDescriptor.TYPE_INT32: wire_format.Int32ByteSize,
- _FieldDescriptor.TYPE_FIXED64: wire_format.Fixed64ByteSize,
- _FieldDescriptor.TYPE_FIXED32: wire_format.Fixed32ByteSize,
- _FieldDescriptor.TYPE_BOOL: wire_format.BoolByteSize,
- _FieldDescriptor.TYPE_STRING: wire_format.StringByteSize,
- _FieldDescriptor.TYPE_GROUP: wire_format.GroupByteSize,
- _FieldDescriptor.TYPE_MESSAGE: wire_format.MessageByteSize,
- _FieldDescriptor.TYPE_BYTES: wire_format.BytesByteSize,
- _FieldDescriptor.TYPE_UINT32: wire_format.UInt32ByteSize,
- _FieldDescriptor.TYPE_ENUM: wire_format.EnumByteSize,
- _FieldDescriptor.TYPE_SFIXED32: wire_format.SFixed32ByteSize,
- _FieldDescriptor.TYPE_SFIXED64: wire_format.SFixed64ByteSize,
- _FieldDescriptor.TYPE_SINT32: wire_format.SInt32ByteSize,
- _FieldDescriptor.TYPE_SINT64: wire_format.SInt64ByteSize
- }
-
-# Maps from field type to an unbound Encoder method F, such that
-# F(encoder, field_number, value) will append the serialization
-# of a value of this type to the encoder.
-_Encoder = encoder.Encoder
-_TYPE_TO_SERIALIZE_METHOD = {
- _FieldDescriptor.TYPE_DOUBLE: _Encoder.AppendDouble,
- _FieldDescriptor.TYPE_FLOAT: _Encoder.AppendFloat,
- _FieldDescriptor.TYPE_INT64: _Encoder.AppendInt64,
- _FieldDescriptor.TYPE_UINT64: _Encoder.AppendUInt64,
- _FieldDescriptor.TYPE_INT32: _Encoder.AppendInt32,
- _FieldDescriptor.TYPE_FIXED64: _Encoder.AppendFixed64,
- _FieldDescriptor.TYPE_FIXED32: _Encoder.AppendFixed32,
- _FieldDescriptor.TYPE_BOOL: _Encoder.AppendBool,
- _FieldDescriptor.TYPE_STRING: _Encoder.AppendString,
- _FieldDescriptor.TYPE_GROUP: _Encoder.AppendGroup,
- _FieldDescriptor.TYPE_MESSAGE: _Encoder.AppendMessage,
- _FieldDescriptor.TYPE_BYTES: _Encoder.AppendBytes,
- _FieldDescriptor.TYPE_UINT32: _Encoder.AppendUInt32,
- _FieldDescriptor.TYPE_ENUM: _Encoder.AppendEnum,
- _FieldDescriptor.TYPE_SFIXED32: _Encoder.AppendSFixed32,
- _FieldDescriptor.TYPE_SFIXED64: _Encoder.AppendSFixed64,
- _FieldDescriptor.TYPE_SINT32: _Encoder.AppendSInt32,
- _FieldDescriptor.TYPE_SINT64: _Encoder.AppendSInt64,
- }
-
-# Maps from field type to expected wiretype.
-_FIELD_TYPE_TO_WIRE_TYPE = {
- _FieldDescriptor.TYPE_DOUBLE: wire_format.WIRETYPE_FIXED64,
- _FieldDescriptor.TYPE_FLOAT: wire_format.WIRETYPE_FIXED32,
- _FieldDescriptor.TYPE_INT64: wire_format.WIRETYPE_VARINT,
- _FieldDescriptor.TYPE_UINT64: wire_format.WIRETYPE_VARINT,
- _FieldDescriptor.TYPE_INT32: wire_format.WIRETYPE_VARINT,
- _FieldDescriptor.TYPE_FIXED64: wire_format.WIRETYPE_FIXED64,
- _FieldDescriptor.TYPE_FIXED32: wire_format.WIRETYPE_FIXED32,
- _FieldDescriptor.TYPE_BOOL: wire_format.WIRETYPE_VARINT,
- _FieldDescriptor.TYPE_STRING:
- wire_format.WIRETYPE_LENGTH_DELIMITED,
- _FieldDescriptor.TYPE_GROUP: wire_format.WIRETYPE_START_GROUP,
- _FieldDescriptor.TYPE_MESSAGE:
- wire_format.WIRETYPE_LENGTH_DELIMITED,
- _FieldDescriptor.TYPE_BYTES:
- wire_format.WIRETYPE_LENGTH_DELIMITED,
- _FieldDescriptor.TYPE_UINT32: wire_format.WIRETYPE_VARINT,
- _FieldDescriptor.TYPE_ENUM: wire_format.WIRETYPE_VARINT,
- _FieldDescriptor.TYPE_SFIXED32: wire_format.WIRETYPE_FIXED32,
- _FieldDescriptor.TYPE_SFIXED64: wire_format.WIRETYPE_FIXED64,
- _FieldDescriptor.TYPE_SINT32: wire_format.WIRETYPE_VARINT,
- _FieldDescriptor.TYPE_SINT64: wire_format.WIRETYPE_VARINT,
- }
-
-# Maps from field type to an unbound Decoder method F,
-# such that F(decoder) will read a field of the requested type.
-#
-# Note that Message and Group are intentionally missing here.
-# They're handled by _RecursivelyMerge().
-_Decoder = decoder.Decoder
-_TYPE_TO_DESERIALIZE_METHOD = {
- _FieldDescriptor.TYPE_DOUBLE: _Decoder.ReadDouble,
- _FieldDescriptor.TYPE_FLOAT: _Decoder.ReadFloat,
- _FieldDescriptor.TYPE_INT64: _Decoder.ReadInt64,
- _FieldDescriptor.TYPE_UINT64: _Decoder.ReadUInt64,
- _FieldDescriptor.TYPE_INT32: _Decoder.ReadInt32,
- _FieldDescriptor.TYPE_FIXED64: _Decoder.ReadFixed64,
- _FieldDescriptor.TYPE_FIXED32: _Decoder.ReadFixed32,
- _FieldDescriptor.TYPE_BOOL: _Decoder.ReadBool,
- _FieldDescriptor.TYPE_STRING: _Decoder.ReadString,
- _FieldDescriptor.TYPE_BYTES: _Decoder.ReadBytes,
- _FieldDescriptor.TYPE_UINT32: _Decoder.ReadUInt32,
- _FieldDescriptor.TYPE_ENUM: _Decoder.ReadEnum,
- _FieldDescriptor.TYPE_SFIXED32: _Decoder.ReadSFixed32,
- _FieldDescriptor.TYPE_SFIXED64: _Decoder.ReadSFixed64,
- _FieldDescriptor.TYPE_SINT32: _Decoder.ReadSInt32,
- _FieldDescriptor.TYPE_SINT64: _Decoder.ReadSInt64,
- }
diff --git a/python/google/protobuf/service.py b/python/google/protobuf/service.py
index 461031b7..5d343957 100755
--- a/python/google/protobuf/service.py
+++ b/python/google/protobuf/service.py
@@ -85,18 +85,14 @@ class Service(object):
class RpcController(object):
- """Abstract interface for an RPC channel.
-
- An RpcChannel represents a communication line to a service which can be used
- to call that service's methods. The service may be running on another
- machine. Normally, you should not use an RpcChannel directly, but instead
- construct a stub {@link Service} wrapping it. Example:
+ """An RpcController mediates a single method call.
- Example:
- RpcChannel channel = rpcImpl.Channel("remotehost.example.com:1234")
- RpcController controller = rpcImpl.Controller()
- MyService service = MyService_Stub(channel)
- service.MyMethod(controller, request, callback)
+ The primary purpose of the controller is to provide a way to manipulate
+ settings specific to the RPC implementation and to find out about RPC-level
+ errors. The methods provided by the RpcController interface are intended
+ to be a "least common denominator" set of features which we expect all
+ implementations to support. Specific implementations may provide more
+ advanced features (e.g. deadline propagation).
"""
# Client-side methods below
@@ -172,14 +168,18 @@ class RpcController(object):
class RpcChannel(object):
- """An RpcController mediates a single method call.
+ """Abstract interface for an RPC channel.
- The primary purpose of the controller is to provide a way to manipulate
- settings specific to the RPC implementation and to find out about RPC-level
- errors. The methods provided by the RpcController interface are intended
- to be a "least common denominator" set of features which we expect all
- implementations to support. Specific implementations may provide more
- advanced features (e.g. deadline propagation).
+ An RpcChannel represents a communication line to a service which can be used
+ to call that service's methods. The service may be running on another
+ machine. Normally, you should not use an RpcChannel directly, but instead
+ construct a stub {@link Service} wrapping it. Example:
+
+ Example:
+ RpcChannel channel = rpcImpl.Channel("remotehost.example.com:1234")
+ RpcController controller = rpcImpl.Controller()
+ MyService service = MyService_Stub(channel)
+ service.MyMethod(controller, request, callback)
"""
def CallMethod(self, method_descriptor, rpc_controller,