Esempio n. 1
0
 def __init__(self, name, full_name, index, number, type, cpp_type, label, default_value, message_type, enum_type, containing_type, is_extension, extension_scope, options=None, has_default_value=True):
     super(FieldDescriptor, self).__init__(options, 'FieldOptions')
     self.name = name
     self.full_name = full_name
     self.index = index
     self.number = number
     self.type = type
     self.cpp_type = cpp_type
     self.label = label
     self.has_default_value = has_default_value
     self.default_value = default_value
     self.containing_type = containing_type
     self.message_type = message_type
     self.enum_type = enum_type
     self.is_extension = is_extension
     self.extension_scope = extension_scope
     if api_implementation.Type() == 'cpp':
         if is_extension:
             if api_implementation.Version() == 2:
                 self._cdescriptor = _message.GetExtensionDescriptor(full_name)
             else:
                 self._cdescriptor = cpp_message.GetExtensionDescriptor(full_name)
                 if api_implementation.Version() == 2:
                     self._cdescriptor = _message.GetFieldDescriptor(full_name)
                 else:
                     self._cdescriptor = cpp_message.GetFieldDescriptor(full_name)
         elif api_implementation.Version() == 2:
             self._cdescriptor = _message.GetFieldDescriptor(full_name)
         else:
             self._cdescriptor = cpp_message.GetFieldDescriptor(full_name)
     else:
         self._cdescriptor = None
Esempio n. 2
0
    def __init__(self,
                 name,
                 full_name,
                 index,
                 number,
                 type,
                 cpp_type,
                 label,
                 default_value,
                 message_type,
                 enum_type,
                 containing_type,
                 is_extension,
                 extension_scope,
                 options=None,
                 has_default_value=True):
        """The arguments are as described in the description of FieldDescriptor
    attributes above.

    Note that containing_type may be None, and may be set later if necessary
    (to deal with circular references between message types, for example).
    Likewise for extension_scope.
    """
        super(FieldDescriptor, self).__init__(options, 'FieldOptions')
        self.name = name
        self.full_name = full_name
        self.index = index
        self.number = number
        self.type = type
        self.cpp_type = cpp_type
        self.label = label
        self.has_default_value = has_default_value
        self.default_value = default_value
        self.containing_type = containing_type
        self.message_type = message_type
        self.enum_type = enum_type
        self.is_extension = is_extension
        self.extension_scope = extension_scope
        if api_implementation.Type() == 'cpp':
            if is_extension:
                if api_implementation.Version() == 2:
                    self._cdescriptor = _message.GetExtensionDescriptor(
                        full_name)
                else:
                    self._cdescriptor = cpp_message.GetExtensionDescriptor(
                        full_name)
            else:
                if api_implementation.Version() == 2:
                    self._cdescriptor = _message.GetFieldDescriptor(full_name)
                else:
                    self._cdescriptor = cpp_message.GetFieldDescriptor(
                        full_name)
        else:
            self._cdescriptor = None
Esempio n. 3
0
    def __init__(self,
                 name,
                 package,
                 options=None,
                 serialized_pb=None,
                 dependencies=None):
        """Constructor."""
        super(FileDescriptor, self).__init__(options, 'FileOptions')

        self.message_types_by_name = {}
        self.name = name
        self.package = package
        self.serialized_pb = serialized_pb

        self.enum_types_by_name = {}
        self.extensions_by_name = {}
        self.dependencies = (dependencies or [])

        if (api_implementation.Type() == 'cpp'
                and self.serialized_pb is not None):
            if api_implementation.Version() == 2:
                # pylint: disable=protected-access
                _message.Message._BuildFile(self.serialized_pb)
                # pylint: enable=protected-access
            else:
                cpp_message.BuildFile(self.serialized_pb)
Esempio n. 4
0
 def __init__(self, name, package, options=None, serialized_pb=None):
     super(FileDescriptor, self).__init__(options, 'FileOptions')
     self.message_types_by_name = {}
     self.name = name
     self.package = package
     self.serialized_pb = serialized_pb
     if api_implementation.Type() == 'cpp' and self.serialized_pb is not None:
         if api_implementation.Version() == 2:
             _message.BuildFile(self.serialized_pb)
         else:
             cpp_message.BuildFile(self.serialized_pb)
 def testPickleRepeatedScalarContainer(self, message_module):
   # TODO(tibell): The pure-Python implementation support pickling of
   #   scalar containers in *some* cases. For now the cpp2 version
   #   throws an exception to avoid a segfault. Investigate if we
   #   want to support pickling of these fields.
   #
   # For more information see: https://b2.corp.google.com/u/0/issues/18677897
   if (api_implementation.Type() != 'cpp' or
       api_implementation.Version() == 2):
     return
   m = message_module.TestAllTypes()
   with self.assertRaises(pickle.PickleError) as _:
     pickle.dumps(m.repeated_int32, pickle.HIGHEST_PROTOCOL)
Esempio n. 6
0
class UnknownFieldsTest(basetest.TestCase):
    def setUp(self):
        self.descriptor = unittest_pb2.TestAllTypes.DESCRIPTOR
        self.all_fields = unittest_pb2.TestAllTypes()
        test_util.SetAllFields(self.all_fields)
        self.all_fields_data = self.all_fields.SerializeToString()
        self.empty_message = unittest_pb2.TestEmptyMessage()
        self.empty_message.ParseFromString(self.all_fields_data)

    def testSerialize(self):
        data = self.empty_message.SerializeToString()

        # Don't use assertEqual because we don't want to dump raw binary data to
        # stdout.
        self.assertTrue(data == self.all_fields_data)

    def testSerializeProto3(self):
        # Verify that proto3 doesn't preserve unknown fields.
        message = unittest_proto3_arena_pb2.TestEmptyMessage()
        message.ParseFromString(self.all_fields_data)
        self.assertEqual(0, len(message.SerializeToString()))

    def testByteSize(self):
        self.assertEqual(self.all_fields.ByteSize(),
                         self.empty_message.ByteSize())

    def testListFields(self):
        # Make sure ListFields doesn't return unknown fields.
        self.assertEqual(0, len(self.empty_message.ListFields()))

    def testSerializeMessageSetWireFormatUnknownExtension(self):
        # Create a message using the message set wire format with an unknown
        # message.
        raw = unittest_mset_pb2.RawMessageSet()

        # Add an unknown extension.
        item = raw.item.add()
        item.type_id = 1545009
        message1 = unittest_mset_pb2.TestMessageSetExtension1()
        message1.i = 12345
        item.message = message1.SerializeToString()

        serialized = raw.SerializeToString()

        # Parse message using the message set wire format.
        proto = unittest_mset_pb2.TestMessageSet()
        proto.MergeFromString(serialized)

        # Verify that the unknown extension is serialized unchanged
        reserialized = proto.SerializeToString()
        new_raw = unittest_mset_pb2.RawMessageSet()
        new_raw.MergeFromString(reserialized)
        self.assertEqual(raw, new_raw)

    # C++ implementation for proto2 does not currently take into account unknown
    # fields when checking equality.
    #
    # TODO(haberman): fix this.
    @basetest.unittest.skipIf(
        api_implementation.Type() == 'cpp'
        and api_implementation.Version() == 2,
        'C++ implementation does not expose unknown fields to Python')
    def testEquals(self):
        message = unittest_pb2.TestEmptyMessage()
        message.ParseFromString(self.all_fields_data)
        self.assertEqual(self.empty_message, message)

        self.all_fields.ClearField('optional_string')
        message.ParseFromString(self.all_fields.SerializeToString())
        self.assertNotEqual(self.empty_message, message)
Esempio n. 7
0
    @basetest.unittest.skipIf(
        api_implementation.Type() == 'cpp'
        and api_implementation.Version() == 2,
        'C++ implementation does not expose unknown fields to Python')
    def testEquals(self):
        message = unittest_pb2.TestEmptyMessage()
        message.ParseFromString(self.all_fields_data)
        self.assertEqual(self.empty_message, message)

        self.all_fields.ClearField('optional_string')
        message.ParseFromString(self.all_fields.SerializeToString())
        self.assertNotEqual(self.empty_message, message)


@basetest.unittest.skipIf(
    api_implementation.Type() == 'cpp' and api_implementation.Version() == 2,
    'C++ implementation does not expose unknown fields to Python')
class UnknownFieldsAccessorsTest(basetest.TestCase):
    def setUp(self):
        self.descriptor = unittest_pb2.TestAllTypes.DESCRIPTOR
        self.all_fields = unittest_pb2.TestAllTypes()
        test_util.SetAllFields(self.all_fields)
        self.all_fields_data = self.all_fields.SerializeToString()
        self.empty_message = unittest_pb2.TestEmptyMessage()
        self.empty_message.ParseFromString(self.all_fields_data)
        self.unknown_fields = self.empty_message._unknown_fields

    def GetField(self, name):
        field_descriptor = self.descriptor.fields_by_name[name]
        wire_type = type_checkers.FIELD_TYPE_TO_WIRE_TYPE[
            field_descriptor.type]
Esempio n. 8
0
The upshot of all this is that the real implementation
details for ALL pure-Python protocol buffers are *here in
this file*.
"""

__author__ = '[email protected] (Will Robinson)'

from google.protobuf.internal import api_implementation
from google.protobuf import descriptor as descriptor_mod
from google.protobuf import message

_FieldDescriptor = descriptor_mod.FieldDescriptor

if api_implementation.Type() == 'cpp':
    if api_implementation.Version() == 2:
        from google.protobuf.internal.cpp import cpp_message
        _NewMessage = cpp_message.NewMessage
        _InitMessage = cpp_message.InitMessage
    else:
        from google.protobuf.internal import cpp_message
        _NewMessage = cpp_message.NewMessage
        _InitMessage = cpp_message.InitMessage
else:
    from google.protobuf.internal import python_message
    _NewMessage = python_message.NewMessage
    _InitMessage = python_message.InitMessage


class GeneratedProtocolMessageType(type):
    """Metaclass for protocol message classes created at runtime from Descriptors.
Esempio n. 9
0
class DescriptorTest(unittest.TestCase):
    def setUp(self):
        file_proto = descriptor_pb2.FileDescriptorProto(
            name='some/filename/some.proto', package='protobuf_unittest')
        message_proto = file_proto.message_type.add(name='NestedMessage')
        message_proto.field.add(
            name='bb',
            number=1,
            type=descriptor_pb2.FieldDescriptorProto.TYPE_INT32,
            label=descriptor_pb2.FieldDescriptorProto.LABEL_OPTIONAL)
        enum_proto = message_proto.enum_type.add(name='ForeignEnum')
        enum_proto.value.add(name='FOREIGN_FOO', number=4)
        enum_proto.value.add(name='FOREIGN_BAR', number=5)
        enum_proto.value.add(name='FOREIGN_BAZ', number=6)

        file_proto.message_type.add(name='ResponseMessage')
        service_proto = file_proto.service.add(name='Service')
        method_proto = service_proto.method.add(
            name='CallMethod',
            input_type='.protobuf_unittest.NestedMessage',
            output_type='.protobuf_unittest.ResponseMessage')

        # Note: Calling DescriptorPool.Add() multiple times with the same file only
        # works if the input is canonical; in particular, all type names must be
        # fully qualified.
        self.pool = self.GetDescriptorPool()
        self.pool.Add(file_proto)
        self.my_file = self.pool.FindFileByName(file_proto.name)
        self.my_message = self.my_file.message_types_by_name[
            message_proto.name]
        self.my_enum = self.my_message.enum_types_by_name[enum_proto.name]
        self.my_service = self.my_file.services_by_name[service_proto.name]
        self.my_method = self.my_service.methods_by_name[method_proto.name]

    def GetDescriptorPool(self):
        return symbol_database.Default().pool

    def testEnumValueName(self):
        self.assertEqual(self.my_message.EnumValueName('ForeignEnum', 4),
                         'FOREIGN_FOO')

        self.assertEqual(
            self.my_message.enum_types_by_name['ForeignEnum'].
            values_by_number[4].name,
            self.my_message.EnumValueName('ForeignEnum', 4))

    def testEnumFixups(self):
        self.assertEqual(self.my_enum, self.my_enum.values[0].type)

    def testContainingTypeFixups(self):
        self.assertEqual(self.my_message,
                         self.my_message.fields[0].containing_type)
        self.assertEqual(self.my_message, self.my_enum.containing_type)

    def testContainingServiceFixups(self):
        self.assertEqual(self.my_service, self.my_method.containing_service)

    def testGetOptions(self):
        self.assertEqual(self.my_enum.GetOptions(),
                         descriptor_pb2.EnumOptions())
        self.assertEqual(self.my_enum.values[0].GetOptions(),
                         descriptor_pb2.EnumValueOptions())
        self.assertEqual(self.my_message.GetOptions(),
                         descriptor_pb2.MessageOptions())
        self.assertEqual(self.my_message.fields[0].GetOptions(),
                         descriptor_pb2.FieldOptions())
        self.assertEqual(self.my_method.GetOptions(),
                         descriptor_pb2.MethodOptions())
        self.assertEqual(self.my_service.GetOptions(),
                         descriptor_pb2.ServiceOptions())

    def testSimpleCustomOptions(self):
        file_descriptor = unittest_custom_options_pb2.DESCRIPTOR
        message_descriptor =\
            unittest_custom_options_pb2.TestMessageWithCustomOptions.DESCRIPTOR
        field_descriptor = message_descriptor.fields_by_name['field1']
        oneof_descriptor = message_descriptor.oneofs_by_name['AnOneof']
        enum_descriptor = message_descriptor.enum_types_by_name['AnEnum']
        enum_value_descriptor =\
            message_descriptor.enum_values_by_name['ANENUM_VAL2']
        service_descriptor =\
            unittest_custom_options_pb2.TestServiceWithCustomOptions.DESCRIPTOR
        method_descriptor = service_descriptor.FindMethodByName('Foo')

        file_options = file_descriptor.GetOptions()
        file_opt1 = unittest_custom_options_pb2.file_opt1
        self.assertEqual(9876543210, file_options.Extensions[file_opt1])
        message_options = message_descriptor.GetOptions()
        message_opt1 = unittest_custom_options_pb2.message_opt1
        self.assertEqual(-56, message_options.Extensions[message_opt1])
        field_options = field_descriptor.GetOptions()
        field_opt1 = unittest_custom_options_pb2.field_opt1
        self.assertEqual(8765432109, field_options.Extensions[field_opt1])
        field_opt2 = unittest_custom_options_pb2.field_opt2
        self.assertEqual(42, field_options.Extensions[field_opt2])
        oneof_options = oneof_descriptor.GetOptions()
        oneof_opt1 = unittest_custom_options_pb2.oneof_opt1
        self.assertEqual(-99, oneof_options.Extensions[oneof_opt1])
        enum_options = enum_descriptor.GetOptions()
        enum_opt1 = unittest_custom_options_pb2.enum_opt1
        self.assertEqual(-789, enum_options.Extensions[enum_opt1])
        enum_value_options = enum_value_descriptor.GetOptions()
        enum_value_opt1 = unittest_custom_options_pb2.enum_value_opt1
        self.assertEqual(123, enum_value_options.Extensions[enum_value_opt1])

        service_options = service_descriptor.GetOptions()
        service_opt1 = unittest_custom_options_pb2.service_opt1
        self.assertEqual(-9876543210, service_options.Extensions[service_opt1])
        method_options = method_descriptor.GetOptions()
        method_opt1 = unittest_custom_options_pb2.method_opt1
        self.assertEqual(unittest_custom_options_pb2.METHODOPT1_VAL2,
                         method_options.Extensions[method_opt1])

        message_descriptor = (
            unittest_custom_options_pb2.DummyMessageContainingEnum.DESCRIPTOR)
        self.assertTrue(file_descriptor.has_options)
        self.assertFalse(message_descriptor.has_options)

    def testDifferentCustomOptionTypes(self):
        kint32min = -2**31
        kint64min = -2**63
        kint32max = 2**31 - 1
        kint64max = 2**63 - 1
        kuint32max = 2**32 - 1
        kuint64max = 2**64 - 1

        message_descriptor =\
            unittest_custom_options_pb2.CustomOptionMinIntegerValues.DESCRIPTOR
        message_options = message_descriptor.GetOptions()
        self.assertEqual(
            False,
            message_options.Extensions[unittest_custom_options_pb2.bool_opt])
        self.assertEqual(
            kint32min,
            message_options.Extensions[unittest_custom_options_pb2.int32_opt])
        self.assertEqual(
            kint64min,
            message_options.Extensions[unittest_custom_options_pb2.int64_opt])
        self.assertEqual(
            0,
            message_options.Extensions[unittest_custom_options_pb2.uint32_opt])
        self.assertEqual(
            0,
            message_options.Extensions[unittest_custom_options_pb2.uint64_opt])
        self.assertEqual(
            kint32min,
            message_options.Extensions[unittest_custom_options_pb2.sint32_opt])
        self.assertEqual(
            kint64min,
            message_options.Extensions[unittest_custom_options_pb2.sint64_opt])
        self.assertEqual(
            0, message_options.Extensions[
                unittest_custom_options_pb2.fixed32_opt])
        self.assertEqual(
            0, message_options.Extensions[
                unittest_custom_options_pb2.fixed64_opt])
        self.assertEqual(
            kint32min, message_options.Extensions[
                unittest_custom_options_pb2.sfixed32_opt])
        self.assertEqual(
            kint64min, message_options.Extensions[
                unittest_custom_options_pb2.sfixed64_opt])

        message_descriptor =\
            unittest_custom_options_pb2.CustomOptionMaxIntegerValues.DESCRIPTOR
        message_options = message_descriptor.GetOptions()
        self.assertEqual(
            True,
            message_options.Extensions[unittest_custom_options_pb2.bool_opt])
        self.assertEqual(
            kint32max,
            message_options.Extensions[unittest_custom_options_pb2.int32_opt])
        self.assertEqual(
            kint64max,
            message_options.Extensions[unittest_custom_options_pb2.int64_opt])
        self.assertEqual(
            kuint32max,
            message_options.Extensions[unittest_custom_options_pb2.uint32_opt])
        self.assertEqual(
            kuint64max,
            message_options.Extensions[unittest_custom_options_pb2.uint64_opt])
        self.assertEqual(
            kint32max,
            message_options.Extensions[unittest_custom_options_pb2.sint32_opt])
        self.assertEqual(
            kint64max,
            message_options.Extensions[unittest_custom_options_pb2.sint64_opt])
        self.assertEqual(
            kuint32max, message_options.Extensions[
                unittest_custom_options_pb2.fixed32_opt])
        self.assertEqual(
            kuint64max, message_options.Extensions[
                unittest_custom_options_pb2.fixed64_opt])
        self.assertEqual(
            kint32max, message_options.Extensions[
                unittest_custom_options_pb2.sfixed32_opt])
        self.assertEqual(
            kint64max, message_options.Extensions[
                unittest_custom_options_pb2.sfixed64_opt])

        message_descriptor =\
            unittest_custom_options_pb2.CustomOptionOtherValues.DESCRIPTOR
        message_options = message_descriptor.GetOptions()
        self.assertEqual(
            -100,
            message_options.Extensions[unittest_custom_options_pb2.int32_opt])
        self.assertAlmostEqual(
            12.3456789,
            message_options.Extensions[unittest_custom_options_pb2.float_opt],
            6)
        self.assertAlmostEqual(
            1.234567890123456789,
            message_options.Extensions[unittest_custom_options_pb2.double_opt])
        self.assertEqual(
            "Hello, \"World\"",
            message_options.Extensions[unittest_custom_options_pb2.string_opt])
        self.assertEqual(
            b"Hello\0World",
            message_options.Extensions[unittest_custom_options_pb2.bytes_opt])
        dummy_enum = unittest_custom_options_pb2.DummyMessageContainingEnum
        self.assertEqual(
            dummy_enum.TEST_OPTION_ENUM_TYPE2,
            message_options.Extensions[unittest_custom_options_pb2.enum_opt])

        message_descriptor =\
            unittest_custom_options_pb2.SettingRealsFromPositiveInts.DESCRIPTOR
        message_options = message_descriptor.GetOptions()
        self.assertAlmostEqual(
            12,
            message_options.Extensions[unittest_custom_options_pb2.float_opt],
            6)
        self.assertAlmostEqual(
            154,
            message_options.Extensions[unittest_custom_options_pb2.double_opt])

        message_descriptor =\
            unittest_custom_options_pb2.SettingRealsFromNegativeInts.DESCRIPTOR
        message_options = message_descriptor.GetOptions()
        self.assertAlmostEqual(
            -12,
            message_options.Extensions[unittest_custom_options_pb2.float_opt],
            6)
        self.assertAlmostEqual(
            -154,
            message_options.Extensions[unittest_custom_options_pb2.double_opt])

    def testComplexExtensionOptions(self):
        descriptor =\
            unittest_custom_options_pb2.VariousComplexOptions.DESCRIPTOR
        options = descriptor.GetOptions()
        self.assertEqual(
            42,
            options.Extensions[unittest_custom_options_pb2.complex_opt1].foo)
        self.assertEqual(
            324, options.Extensions[unittest_custom_options_pb2.complex_opt1].
            Extensions[unittest_custom_options_pb2.quux])
        self.assertEqual(
            876, options.Extensions[unittest_custom_options_pb2.complex_opt1].
            Extensions[unittest_custom_options_pb2.corge].qux)
        self.assertEqual(
            987,
            options.Extensions[unittest_custom_options_pb2.complex_opt2].baz)
        self.assertEqual(
            654, options.Extensions[unittest_custom_options_pb2.complex_opt2].
            Extensions[unittest_custom_options_pb2.grault])
        self.assertEqual(
            743, options.Extensions[
                unittest_custom_options_pb2.complex_opt2].bar.foo)
        self.assertEqual(
            1999, options.Extensions[unittest_custom_options_pb2.complex_opt2].
            bar.Extensions[unittest_custom_options_pb2.quux])
        self.assertEqual(
            2008, options.Extensions[unittest_custom_options_pb2.complex_opt2].
            bar.Extensions[unittest_custom_options_pb2.corge].qux)
        self.assertEqual(
            741, options.Extensions[unittest_custom_options_pb2.complex_opt2].
            Extensions[unittest_custom_options_pb2.garply].foo)
        self.assertEqual(
            1998, options.Extensions[unittest_custom_options_pb2.complex_opt2].
            Extensions[unittest_custom_options_pb2.garply].Extensions[
                unittest_custom_options_pb2.quux])
        self.assertEqual(
            2121, options.Extensions[unittest_custom_options_pb2.complex_opt2].
            Extensions[unittest_custom_options_pb2.garply].Extensions[
                unittest_custom_options_pb2.corge].qux)
        self.assertEqual(
            1971,
            options.Extensions[unittest_custom_options_pb2.ComplexOptionType2.
                               ComplexOptionType4.complex_opt4].waldo)
        self.assertEqual(
            321, options.Extensions[
                unittest_custom_options_pb2.complex_opt2].fred.waldo)
        self.assertEqual(
            9,
            options.Extensions[unittest_custom_options_pb2.complex_opt3].qux)
        self.assertEqual(
            22, options.Extensions[unittest_custom_options_pb2.complex_opt3].
            complexoptiontype5.plugh)
        self.assertEqual(
            24,
            options.Extensions[unittest_custom_options_pb2.complexopt6].xyzzy)

    # Check that aggregate options were parsed and saved correctly in
    # the appropriate descriptors.
    def testAggregateOptions(self):
        file_descriptor = unittest_custom_options_pb2.DESCRIPTOR
        message_descriptor =\
            unittest_custom_options_pb2.AggregateMessage.DESCRIPTOR
        field_descriptor = message_descriptor.fields_by_name["fieldname"]
        enum_descriptor = unittest_custom_options_pb2.AggregateEnum.DESCRIPTOR
        enum_value_descriptor = enum_descriptor.values_by_name["VALUE"]
        service_descriptor =\
            unittest_custom_options_pb2.AggregateService.DESCRIPTOR
        method_descriptor = service_descriptor.FindMethodByName("Method")

        # Tests for the different types of data embedded in fileopt
        file_options = file_descriptor.GetOptions().Extensions[
            unittest_custom_options_pb2.fileopt]
        self.assertEqual(100, file_options.i)
        self.assertEqual("FileAnnotation", file_options.s)
        self.assertEqual("NestedFileAnnotation", file_options.sub.s)
        self.assertEqual(
            "FileExtensionAnnotation", file_options.file.Extensions[
                unittest_custom_options_pb2.fileopt].s)
        self.assertEqual(
            "EmbeddedMessageSetElement", file_options.mset.Extensions[
                unittest_custom_options_pb2.AggregateMessageSetElement.
                message_set_extension].s)

        # Simple tests for all the other types of annotations
        self.assertEqual(
            "MessageAnnotation",
            message_descriptor.GetOptions().Extensions[
                unittest_custom_options_pb2.msgopt].s)
        self.assertEqual(
            "FieldAnnotation",
            field_descriptor.GetOptions().Extensions[
                unittest_custom_options_pb2.fieldopt].s)
        self.assertEqual(
            "EnumAnnotation",
            enum_descriptor.GetOptions().Extensions[
                unittest_custom_options_pb2.enumopt].s)
        self.assertEqual(
            "EnumValueAnnotation",
            enum_value_descriptor.GetOptions().Extensions[
                unittest_custom_options_pb2.enumvalopt].s)
        self.assertEqual(
            "ServiceAnnotation",
            service_descriptor.GetOptions().Extensions[
                unittest_custom_options_pb2.serviceopt].s)
        self.assertEqual(
            "MethodAnnotation",
            method_descriptor.GetOptions().Extensions[
                unittest_custom_options_pb2.methodopt].s)

    def testNestedOptions(self):
        nested_message =\
            unittest_custom_options_pb2.NestedOptionType.NestedMessage.DESCRIPTOR
        self.assertEqual(
            1001,
            nested_message.GetOptions().Extensions[
                unittest_custom_options_pb2.message_opt1])
        nested_field = nested_message.fields_by_name["nested_field"]
        self.assertEqual(
            1002,
            nested_field.GetOptions().Extensions[
                unittest_custom_options_pb2.field_opt1])
        outer_message =\
            unittest_custom_options_pb2.NestedOptionType.DESCRIPTOR
        nested_enum = outer_message.enum_types_by_name["NestedEnum"]
        self.assertEqual(
            1003,
            nested_enum.GetOptions().Extensions[
                unittest_custom_options_pb2.enum_opt1])
        nested_enum_value = outer_message.enum_values_by_name[
            "NESTED_ENUM_VALUE"]
        self.assertEqual(
            1004,
            nested_enum_value.GetOptions().Extensions[
                unittest_custom_options_pb2.enum_value_opt1])
        nested_extension = outer_message.extensions_by_name["nested_extension"]
        self.assertEqual(
            1005,
            nested_extension.GetOptions().Extensions[
                unittest_custom_options_pb2.field_opt2])

    def testFileDescriptorReferences(self):
        self.assertEqual(self.my_enum.file, self.my_file)
        self.assertEqual(self.my_message.file, self.my_file)

    def testFileDescriptor(self):
        self.assertEqual(self.my_file.name, 'some/filename/some.proto')
        self.assertEqual(self.my_file.package, 'protobuf_unittest')
        self.assertEqual(self.my_file.pool, self.pool)
        # Generated modules also belong to the default pool.
        self.assertEqual(unittest_pb2.DESCRIPTOR.pool,
                         descriptor_pool.Default())

    @unittest.skipIf(
        api_implementation.Type() != 'cpp'
        or api_implementation.Version() != 2,
        'Immutability of descriptors is only enforced in v2 implementation')
    def testImmutableCppDescriptor(self):
        message_descriptor = unittest_pb2.TestAllTypes.DESCRIPTOR
        with self.assertRaises(AttributeError):
            message_descriptor.fields_by_name = None
        with self.assertRaises(TypeError):
            message_descriptor.fields_by_name['Another'] = None
        with self.assertRaises(TypeError):
            message_descriptor.fields.append(None)
Esempio n. 10
0
def SkipCheckUnknownFieldIfCppImplementation(func):
    return unittest.skipIf(
        api_implementation.Type() == 'cpp'
        and api_implementation.Version() == 2,
        'Addtional test for pure python involved protect members')(func)
Esempio n. 11
0
 def testImplementationSetting(self):
   self.assertEqual('cpp', api_implementation.Type())
   self.assertEqual(2, api_implementation.Version())
Esempio n. 12
0
    @unittest.skipIf(
        api_implementation.Type() == 'cpp'
        and api_implementation.Version() == 2,
        'C++ implementation does not expose unknown fields to Python')
    def testEquals(self):
        message = unittest_pb2.TestEmptyMessage()
        message.ParseFromString(self.all_fields_data)
        self.assertEqual(self.empty_message, message)

        self.all_fields.ClearField('optional_string')
        message.ParseFromString(self.all_fields.SerializeToString())
        self.assertNotEqual(self.empty_message, message)


@unittest.skipIf(api_implementation.Type() == 'cpp'
                 and api_implementation.Version() == 2,
                 'C++ implementation does not expose unknown fields to Python')
class UnknownFieldsAccessorsTest(unittest.TestCase):
    def setUp(self):
        self.descriptor = unittest_pb2.TestAllTypes.DESCRIPTOR
        self.all_fields = unittest_pb2.TestAllTypes()
        test_util.SetAllFields(self.all_fields)
        self.all_fields_data = self.all_fields.SerializeToString()
        self.empty_message = unittest_pb2.TestEmptyMessage()
        self.empty_message.ParseFromString(self.all_fields_data)
        self.unknown_fields = self.empty_message._unknown_fields

    def GetField(self, name):
        field_descriptor = self.descriptor.fields_by_name[name]
        wire_type = type_checkers.FIELD_TYPE_TO_WIRE_TYPE[
            field_descriptor.type]
Esempio n. 13
0
 def testThatCppApiV2IsTheDefault(self):
     """If -DPYTHON_PROTO_*IMPL* was given at build time, this may fail."""
     self.assertEqual('cpp', api_implementation.Type())
     self.assertEqual(2, api_implementation.Version())
Esempio n. 14
0
def MakeDescriptor(desc_proto, package='', build_file_if_cpp=True):
    """Make a protobuf Descriptor given a DescriptorProto protobuf.

  Handles nested descriptors. Note that this is limited to the scope of defining
  a message inside of another message. Composite fields can currently only be
  resolved if the message is defined in the same scope as the field.

  Args:
    desc_proto: The descriptor_pb2.DescriptorProto protobuf message.
    package: Optional package name for the new message Descriptor (string).
    build_file_if_cpp: Update the C++ descriptor pool if api matches.
                       Set to False on recursion, so no duplicates are created.
  Returns:
    A Descriptor for protobuf messages.
  """
    if api_implementation.Type() == 'cpp' and build_file_if_cpp:
        # The C++ implementation requires all descriptors to be backed by the same
        # definition in the C++ descriptor pool. To do this, we build a
        # FileDescriptorProto with the same definition as this descriptor and build
        # it into the pool.
        from google.protobuf import descriptor_pb2
        file_descriptor_proto = descriptor_pb2.FileDescriptorProto()
        file_descriptor_proto.message_type.add().MergeFrom(desc_proto)

        # Generate a random name for this proto file to prevent conflicts with
        # any imported ones. We need to specify a file name so BuildFile accepts
        # our FileDescriptorProto, but it is not important what that file name
        # is actually set to.
        proto_name = str(uuid.uuid4())

        if package:
            file_descriptor_proto.name = os.path.join(
                package.replace('.', '/'), proto_name + '.proto')
            file_descriptor_proto.package = package
        else:
            file_descriptor_proto.name = proto_name + '.proto'

        if api_implementation.Version() == 2:
            # pylint: disable=protected-access
            _message.Message._BuildFile(
                file_descriptor_proto.SerializeToString())
            # pylint: enable=protected-access
        else:
            cpp_message.BuildFile(file_descriptor_proto.SerializeToString())

    full_message_name = [desc_proto.name]
    if package: full_message_name.insert(0, package)

    # Create Descriptors for enum types
    enum_types = {}
    for enum_proto in desc_proto.enum_type:
        full_name = '.'.join(full_message_name + [enum_proto.name])
        enum_desc = EnumDescriptor(enum_proto.name, full_name, None, [
            EnumValueDescriptor(enum_val.name, ii, enum_val.number)
            for ii, enum_val in enumerate(enum_proto.value)
        ])
        enum_types[full_name] = enum_desc

    # Create Descriptors for nested types
    nested_types = {}
    for nested_proto in desc_proto.nested_type:
        full_name = '.'.join(full_message_name + [nested_proto.name])
        # Nested types are just those defined inside of the message, not all types
        # used by fields in the message, so no loops are possible here.
        nested_desc = MakeDescriptor(nested_proto,
                                     package='.'.join(full_message_name),
                                     build_file_if_cpp=False)
        nested_types[full_name] = nested_desc

    fields = []
    for field_proto in desc_proto.field:
        full_name = '.'.join(full_message_name + [field_proto.name])
        enum_desc = None
        nested_desc = None
        if field_proto.HasField('type_name'):
            type_name = field_proto.type_name
            full_type_name = '.'.join(full_message_name +
                                      [type_name[type_name.rfind('.') + 1:]])
            if full_type_name in nested_types:
                nested_desc = nested_types[full_type_name]
            elif full_type_name in enum_types:
                enum_desc = enum_types[full_type_name]
            # Else type_name references a non-local type, which isn't implemented
        field = FieldDescriptor(field_proto.name,
                                full_name,
                                field_proto.number - 1,
                                field_proto.number,
                                field_proto.type,
                                FieldDescriptor.ProtoTypeToCppProtoType(
                                    field_proto.type),
                                field_proto.label,
                                None,
                                nested_desc,
                                enum_desc,
                                None,
                                False,
                                None,
                                has_default_value=False)
        fields.append(field)

    desc_name = '.'.join(full_message_name)
    return Descriptor(desc_proto.name, desc_name, None, None, fields,
                      list(nested_types.values()), list(enum_types.values()),
                      [])
Esempio n. 15
0
def SkipIfCppImplementation(func):
  return unittest.skipIf(
      api_implementation.Type() == 'cpp' and api_implementation.Version() == 2,
      'C++ implementation does not expose unknown fields to Python')(func)