diff options
author | Adam Cozzette <acozzette@google.com> | 2017-12-01 10:05:10 -0800 |
---|---|---|
committer | Adam Cozzette <acozzette@google.com> | 2017-12-01 10:05:10 -0800 |
commit | 92a7e778e7394386f413cec28d67a07630f784b1 (patch) | |
tree | cb5673c7c09b0d3905b48a24765d07e423bc5b30 /python/google | |
parent | ce0a53273a400369932ba788d17500336a6ecaad (diff) |
Integrated internal changes from Google
Diffstat (limited to 'python/google')
-rwxr-xr-x | python/google/protobuf/descriptor.py | 4 | ||||
-rw-r--r-- | python/google/protobuf/descriptor_database.py | 26 | ||||
-rw-r--r-- | python/google/protobuf/descriptor_pool.py | 43 | ||||
-rw-r--r-- | python/google/protobuf/internal/descriptor_database_test.py | 21 | ||||
-rw-r--r-- | python/google/protobuf/internal/descriptor_pool_test.py | 56 | ||||
-rwxr-xr-x | python/google/protobuf/internal/encoder.py | 3 | ||||
-rwxr-xr-x | python/google/protobuf/internal/text_format_test.py | 7 | ||||
-rw-r--r-- | python/google/protobuf/pyext/descriptor_containers.cc | 530 | ||||
-rw-r--r-- | python/google/protobuf/pyext/message.cc | 90 | ||||
-rw-r--r-- | python/google/protobuf/pyext/message.h | 12 | ||||
-rwxr-xr-x | python/google/protobuf/text_format.py | 2 |
11 files changed, 462 insertions, 332 deletions
diff --git a/python/google/protobuf/descriptor.py b/python/google/protobuf/descriptor.py index b1f3ca38..0d35425f 100755 --- a/python/google/protobuf/descriptor.py +++ b/python/google/protobuf/descriptor.py @@ -41,8 +41,8 @@ from google.protobuf.internal import api_implementation _USE_C_DESCRIPTORS = False if api_implementation.Type() == 'cpp': # Used by MakeDescriptor in cpp mode + import binascii import os - import uuid from google.protobuf.pyext import _message _USE_C_DESCRIPTORS = getattr(_message, '_USE_C_DESCRIPTORS', False) @@ -952,7 +952,7 @@ def MakeDescriptor(desc_proto, package='', build_file_if_cpp=True, # imported ones. We need to specify a file name so the descriptor pool # accepts our FileDescriptorProto, but it is not important what that file # name is actually set to. - proto_name = str(uuid.uuid4()) + proto_name = binascii.hexlify(os.urandom(16)).decode('ascii') if package: file_descriptor_proto.name = os.path.join(package.replace('.', '/'), diff --git a/python/google/protobuf/descriptor_database.py b/python/google/protobuf/descriptor_database.py index b8f5140b..8b7715cd 100644 --- a/python/google/protobuf/descriptor_database.py +++ b/python/google/protobuf/descriptor_database.py @@ -32,6 +32,8 @@ __author__ = 'matthewtoia@google.com (Matt Toia)' +import warnings + class Error(Exception): pass @@ -64,21 +66,20 @@ class DescriptorDatabase(object): elif self._file_desc_protos_by_file[proto_name] != file_desc_proto: raise DescriptorDatabaseConflictingDefinitionError( '%s already added, but with different descriptor.' % proto_name) + else: + return # Add all the top-level descriptors to the index. package = file_desc_proto.package for message in file_desc_proto.message_type: - self._file_desc_protos_by_symbol.update( - (name, file_desc_proto) for name in _ExtractSymbols(message, package)) + for name in _ExtractSymbols(message, package): + self._AddSymbol(name, file_desc_proto) for enum in file_desc_proto.enum_type: - self._file_desc_protos_by_symbol[ - '.'.join((package, enum.name))] = file_desc_proto + self._AddSymbol(('.'.join((package, enum.name))), file_desc_proto) for extension in file_desc_proto.extension: - self._file_desc_protos_by_symbol[ - '.'.join((package, extension.name))] = file_desc_proto + self._AddSymbol(('.'.join((package, extension.name))), file_desc_proto) for service in file_desc_proto.service: - self._file_desc_protos_by_symbol[ - '.'.join((package, service.name))] = file_desc_proto + self._AddSymbol(('.'.join((package, service.name))), file_desc_proto) def FindFileByName(self, name): """Finds the file descriptor proto by file name. @@ -132,6 +133,15 @@ class DescriptorDatabase(object): top_level, _, _ = symbol.rpartition('.') return self._file_desc_protos_by_symbol[top_level] + def _AddSymbol(self, name, file_desc_proto): + if name in self._file_desc_protos_by_symbol: + warn_msg = ('Conflict register for file "' + file_desc_proto.name + + '": ' + name + + ' is already defined in file "' + + self._file_desc_protos_by_symbol[name].name + '"') + warnings.warn(warn_msg, RuntimeWarning) + self._file_desc_protos_by_symbol[name] = file_desc_proto + def _ExtractSymbols(desc_proto, package): """Pulls out all the symbols from a descriptor proto. diff --git a/python/google/protobuf/descriptor_pool.py b/python/google/protobuf/descriptor_pool.py index cb7146b6..8983f76f 100644 --- a/python/google/protobuf/descriptor_pool.py +++ b/python/google/protobuf/descriptor_pool.py @@ -58,6 +58,7 @@ directly instead of this class. __author__ = 'matthewtoia@google.com (Matt Toia)' import collections +import warnings from google.protobuf import descriptor from google.protobuf import descriptor_database @@ -127,12 +128,38 @@ class DescriptorPool(object): self._service_descriptors = {} self._file_descriptors = {} self._toplevel_extensions = {} + # TODO(jieluo): Remove _file_desc_by_toplevel_extension after + # maybe year 2020 for compatibility issue (with 3.4.1 only). + self._file_desc_by_toplevel_extension = {} # We store extensions in two two-level mappings: The first key is the # descriptor of the message being extended, the second key is the extension # full name or its tag number. self._extensions_by_name = collections.defaultdict(dict) self._extensions_by_number = collections.defaultdict(dict) + def _CheckConflictRegister(self, desc): + """Check if the descriptor name conflicts with another of the same name. + + Args: + desc: Descriptor of a message, enum, service or extension. + """ + desc_name = desc.full_name + for register, descriptor_type in [ + (self._descriptors, descriptor.Descriptor), + (self._enum_descriptors, descriptor.EnumDescriptor), + (self._service_descriptors, descriptor.ServiceDescriptor), + (self._toplevel_extensions, descriptor.FieldDescriptor)]: + if desc_name in register: + file_name = register[desc_name].file.name + if not isinstance(desc, descriptor_type) or ( + file_name != desc.file.name): + warn_msg = ('Conflict register for file "' + desc.file.name + + '": ' + desc_name + + ' is already defined in file "' + + file_name + '"') + warnings.warn(warn_msg, RuntimeWarning) + return + def Add(self, file_desc_proto): """Adds the FileDescriptorProto and its types to this pool. @@ -169,6 +196,8 @@ class DescriptorPool(object): if not isinstance(desc, descriptor.Descriptor): raise TypeError('Expected instance of descriptor.Descriptor.') + self._CheckConflictRegister(desc) + self._descriptors[desc.full_name] = desc self._AddFileDescriptor(desc.file) @@ -184,6 +213,7 @@ class DescriptorPool(object): if not isinstance(enum_desc, descriptor.EnumDescriptor): raise TypeError('Expected instance of descriptor.EnumDescriptor.') + self._CheckConflictRegister(enum_desc) self._enum_descriptors[enum_desc.full_name] = enum_desc self._AddFileDescriptor(enum_desc.file) @@ -197,6 +227,7 @@ class DescriptorPool(object): if not isinstance(service_desc, descriptor.ServiceDescriptor): raise TypeError('Expected instance of descriptor.ServiceDescriptor.') + self._CheckConflictRegister(service_desc) self._service_descriptors[service_desc.full_name] = service_desc def AddExtensionDescriptor(self, extension): @@ -216,6 +247,7 @@ class DescriptorPool(object): raise TypeError('Expected an extension descriptor.') if extension.extension_scope is None: + self._CheckConflictRegister(extension) self._toplevel_extensions[extension.full_name] = extension try: @@ -252,6 +284,12 @@ class DescriptorPool(object): """ self._AddFileDescriptor(file_desc) + # TODO(jieluo): This is a temporary solution for FieldDescriptor.file. + # FieldDescriptor.file is added in code gen. Remove this solution after + # maybe 2020 for compatibility reason (with 3.4.1 only). + for extension in file_desc.extensions_by_name.values(): + self._file_desc_by_toplevel_extension[ + extension.full_name] = file_desc def _AddFileDescriptor(self, file_desc): """Adds a FileDescriptor to the pool, non-recursively. @@ -331,7 +369,7 @@ class DescriptorPool(object): pass try: - return self._toplevel_extensions[symbol].file + return self._file_desc_by_toplevel_extension[symbol] except KeyError: pass @@ -680,6 +718,7 @@ class DescriptorPool(object): fields[field_index].containing_oneof = oneofs[oneof_index] scope[_PrefixWithDot(desc_name)] = desc + self._CheckConflictRegister(desc) self._descriptors[desc_name] = desc return desc @@ -718,6 +757,7 @@ class DescriptorPool(object): containing_type=containing_type, options=_OptionsOrNone(enum_proto)) scope['.%s' % enum_name] = desc + self._CheckConflictRegister(desc) self._enum_descriptors[enum_name] = desc return desc @@ -914,6 +954,7 @@ class DescriptorPool(object): methods=methods, options=_OptionsOrNone(service_proto), file=file_desc) + self._CheckConflictRegister(desc) self._service_descriptors[service_name] = desc return desc diff --git a/python/google/protobuf/internal/descriptor_database_test.py b/python/google/protobuf/internal/descriptor_database_test.py index 1f1a3db9..f97477b3 100644 --- a/python/google/protobuf/internal/descriptor_database_test.py +++ b/python/google/protobuf/internal/descriptor_database_test.py @@ -38,6 +38,7 @@ try: import unittest2 as unittest #PY26 except ImportError: import unittest +import warnings from google.protobuf import unittest_pb2 from google.protobuf import descriptor_pb2 @@ -98,6 +99,26 @@ class DescriptorDatabaseTest(unittest.TestCase): db.FindFileContainingSymbol, 'protobuf_unittest.NoneMessage') + def testConflictRegister(self): + db = descriptor_database.DescriptorDatabase() + unittest_fd = descriptor_pb2.FileDescriptorProto.FromString( + unittest_pb2.DESCRIPTOR.serialized_pb) + db.Add(unittest_fd) + conflict_fd = descriptor_pb2.FileDescriptorProto.FromString( + unittest_pb2.DESCRIPTOR.serialized_pb) + conflict_fd.name = 'other_file' + with warnings.catch_warnings(record=True) as w: + # Cause all warnings to always be triggered. + warnings.simplefilter('always') + db.Add(conflict_fd) + self.assertTrue(len(w)) + self.assertIs(w[0].category, RuntimeWarning) + self.assertIn('Conflict register for file "other_file": ', + str(w[0].message)) + self.assertIn('already defined in file ' + '"google/protobuf/unittest.proto"', + str(w[0].message)) + if __name__ == '__main__': unittest.main() diff --git a/python/google/protobuf/internal/descriptor_pool_test.py b/python/google/protobuf/internal/descriptor_pool_test.py index 15c857bb..2cbf7813 100644 --- a/python/google/protobuf/internal/descriptor_pool_test.py +++ b/python/google/protobuf/internal/descriptor_pool_test.py @@ -34,8 +34,10 @@ __author__ = 'matthewtoia@google.com (Matt Toia)' +import copy import os import sys +import warnings try: import unittest2 as unittest #PY26 @@ -119,6 +121,14 @@ class DescriptorPoolTestBase(object): self.assertEqual('google/protobuf/unittest.proto', file_desc5.name) + # Tests the generated pool. + assert descriptor_pool.Default().FindFileContainingSymbol( + 'google.protobuf.python.internal.Factory2Message.one_more_field') + assert descriptor_pool.Default().FindFileContainingSymbol( + 'google.protobuf.python.internal.another_field') + assert descriptor_pool.Default().FindFileContainingSymbol( + 'protobuf_unittest.TestService') + def testFindFileContainingSymbolFailure(self): with self.assertRaises(KeyError): self.pool.FindFileContainingSymbol('Does not exist') @@ -492,6 +502,52 @@ class DescriptorPoolTestBase(object): TEST1_FILE.CheckFile(self, self.pool) TEST2_FILE.CheckFile(self, self.pool) + def testConflictRegister(self): + if isinstance(self, SecondaryDescriptorFromDescriptorDB): + if api_implementation.Type() == 'cpp': + # Cpp extension cannot call Add on a DescriptorPool + # that uses a DescriptorDatabase. + # TODO(jieluo): Fix python and cpp extension diff. + return + unittest_fd = descriptor_pb2.FileDescriptorProto.FromString( + unittest_pb2.DESCRIPTOR.serialized_pb) + conflict_fd = copy.deepcopy(unittest_fd) + conflict_fd.name = 'other_file' + if api_implementation.Type() == 'cpp': + try: + self.pool.Add(unittest_fd) + self.pool.Add(conflict_fd) + except TypeError: + pass + else: + with warnings.catch_warnings(record=True) as w: + # Cause all warnings to always be triggered. + warnings.simplefilter('always') + pool = copy.deepcopy(self.pool) + # No warnings to add the same descriptors. + file_descriptor = unittest_pb2.DESCRIPTOR + pool.AddDescriptor( + file_descriptor.message_types_by_name['TestAllTypes']) + pool.AddEnumDescriptor( + file_descriptor.enum_types_by_name['ForeignEnum']) + pool.AddServiceDescriptor( + file_descriptor.services_by_name['TestService']) + pool.AddExtensionDescriptor( + file_descriptor.extensions_by_name['optional_int32_extension']) + self.assertEqual(len(w), 0) + # Check warnings for conflict descriptors with the same name. + pool.Add(unittest_fd) + pool.Add(conflict_fd) + pool.FindFileByName(unittest_fd.name) + pool.FindFileByName(conflict_fd.name) + self.assertTrue(len(w)) + self.assertIs(w[0].category, RuntimeWarning) + self.assertIn('Conflict register for file "other_file": ', + str(w[0].message)) + self.assertIn('already defined in file ' + '"google/protobuf/unittest.proto"', + str(w[0].message)) + class DefaultDescriptorPoolTest(DescriptorPoolTestBase, unittest.TestCase): diff --git a/python/google/protobuf/internal/encoder.py b/python/google/protobuf/internal/encoder.py index 40c62d67..dc7a8ce8 100755 --- a/python/google/protobuf/internal/encoder.py +++ b/python/google/protobuf/internal/encoder.py @@ -418,7 +418,8 @@ def _VarintBytes(value): def TagBytes(field_number, wire_type): """Encode the given tag and return the bytes. Only called at startup.""" - return six.binary_type( _VarintBytes(wire_format.PackTag(field_number, wire_type)) ) + return six.binary_type( + _VarintBytes(wire_format.PackTag(field_number, wire_type))) # -------------------------------------------------------------------- # As with sizers (see above), we have a number of common encoder diff --git a/python/google/protobuf/internal/text_format_test.py b/python/google/protobuf/internal/text_format_test.py index a52f133f..ed3445f2 100755 --- a/python/google/protobuf/internal/text_format_test.py +++ b/python/google/protobuf/internal/text_format_test.py @@ -828,6 +828,10 @@ class Proto2Tests(TextFormatBase): ' }\n' ' }\n' ' [unknown_extension]: 5\n' + ' [unknown_extension_with_number_field] {\n' + ' 1: "some_field"\n' + ' 2: -0.451\n' + ' }\n' '}\n') text_format.Parse(text, message, allow_unknown_extension=True) golden = 'message_set {\n}\n' @@ -894,7 +898,6 @@ class Proto2Tests(TextFormatBase): message = unittest_mset_pb2.TestMessageSetContainer() malformed = ('message_set {\n' ' unknown_field: true\n' - ' \n' # Missing '>' here. '}\n') six.assertRaisesRegex(self, text_format.ParseError, @@ -906,7 +909,7 @@ class Proto2Tests(TextFormatBase): message, allow_unknown_extension=True) - # Parse known extension correcty. + # Parse known extension correctly. message = unittest_mset_pb2.TestMessageSetContainer() text = ('message_set {\n' ' [protobuf_unittest.TestMessageSetExtension1] {\n' diff --git a/python/google/protobuf/pyext/descriptor_containers.cc b/python/google/protobuf/pyext/descriptor_containers.cc index d0aae9c9..bc007f7e 100644 --- a/python/google/protobuf/pyext/descriptor_containers.cc +++ b/python/google/protobuf/pyext/descriptor_containers.cc @@ -957,55 +957,55 @@ static int Count(PyContainer* self) { return GetDescriptor(self)->field_count(); } -static ItemDescriptor GetByName(PyContainer* self, const string& name) { +static const void* GetByName(PyContainer* self, const string& name) { return GetDescriptor(self)->FindFieldByName(name); } -static ItemDescriptor GetByCamelcaseName(PyContainer* self, +static const void* GetByCamelcaseName(PyContainer* self, const string& name) { return GetDescriptor(self)->FindFieldByCamelcaseName(name); } -static ItemDescriptor GetByNumber(PyContainer* self, int number) { +static const void* GetByNumber(PyContainer* self, int number) { return GetDescriptor(self)->FindFieldByNumber(number); } -static ItemDescriptor GetByIndex(PyContainer* self, int index) { +static const void* GetByIndex(PyContainer* self, int index) { return GetDescriptor(self)->field(index); } -static PyObject* NewObjectFromItem(ItemDescriptor item) { - return PyFieldDescriptor_FromDescriptor(item); +static PyObject* NewObjectFromItem(const void* item) { + return PyFieldDescriptor_FromDescriptor(static_cast<ItemDescriptor>(item)); } -static const string& GetItemName(ItemDescriptor item) { - return item->name(); +static const string& GetItemName(const void* item) { + return static_cast<ItemDescriptor>(item)->name(); } -static const string& GetItemCamelcaseName(ItemDescriptor item) { - return item->camelcase_name(); +static const string& GetItemCamelcaseName(const void* item) { + return static_cast<ItemDescriptor>(item)->camelcase_name(); } -static int GetItemNumber(ItemDescriptor item) { - return item->number(); +static int GetItemNumber(const void* item) { + return static_cast<ItemDescriptor>(item)->number(); } -static int GetItemIndex(ItemDescriptor item) { - return item->index(); +static int GetItemIndex(const void* item) { + return static_cast<ItemDescriptor>(item)->index(); } static DescriptorContainerDef ContainerDef = { "MessageFields", - (CountMethod)Count, - (GetByIndexMethod)GetByIndex, - (GetByNameMethod)GetByName, - (GetByCamelcaseNameMethod)GetByCamelcaseName, - (GetByNumberMethod)GetByNumber, - (NewObjectFromItemMethod)NewObjectFromItem, - (GetItemNameMethod)GetItemName, - (GetItemCamelcaseNameMethod)GetItemCamelcaseName, - (GetItemNumberMethod)GetItemNumber, - (GetItemIndexMethod)GetItemIndex, + Count, + GetByIndex, + GetByName, + GetByCamelcaseName, + GetByNumber, + NewObjectFromItem, + GetItemName, + GetItemCamelcaseName, + GetItemNumber, + GetItemIndex, }; } // namespace fields @@ -1035,38 +1035,38 @@ static int Count(PyContainer* self) { return GetDescriptor(self)->nested_type_count(); } -static ItemDescriptor GetByName(PyContainer* self, const string& name) { +static const void* GetByName(PyContainer* self, const string& name) { return GetDescriptor(self)->FindNestedTypeByName(name); } -static ItemDescriptor GetByIndex(PyContainer* self, int index) { +static const void* GetByIndex(PyContainer* self, int index) { return GetDescriptor(self)->nested_type(index); } -static PyObject* NewObjectFromItem(ItemDescriptor item) { - return PyMessageDescriptor_FromDescriptor(item); +static PyObject* NewObjectFromItem(const void* item) { + return PyMessageDescriptor_FromDescriptor(static_cast<ItemDescriptor>(item)); } -static const string& GetItemName(ItemDescriptor item) { - return item->name(); +static const string& GetItemName(const void* item) { + return static_cast<ItemDescriptor>(item)->name(); } -static int GetItemIndex(ItemDescriptor item) { - return item->index(); +static int GetItemIndex(const void* item) { + return static_cast<ItemDescriptor>(item)->index(); } static DescriptorContainerDef ContainerDef = { "MessageNestedTypes", - (CountMethod)Count, - (GetByIndexMethod)GetByIndex, - (GetByNameMethod)GetByName, - (GetByCamelcaseNameMethod)NULL, - (GetByNumberMethod)NULL, - (NewObjectFromItemMethod)NewObjectFromItem, - (GetItemNameMethod)GetItemName, - (GetItemCamelcaseNameMethod)NULL, - (GetItemNumberMethod)NULL, - (GetItemIndexMethod)GetItemIndex, + Count, + GetByIndex, + GetByName, + NULL, + NULL, + NewObjectFromItem, + GetItemName, + NULL, + NULL, + GetItemIndex, }; } // namespace nested_types @@ -1087,38 +1087,38 @@ static int Count(PyContainer* self) { return GetDescriptor(self)->enum_type_count(); } -static ItemDescriptor GetByName(PyContainer* self, const string& name) { +static const void* GetByName(PyContainer* self, const string& name) { return GetDescriptor(self)->FindEnumTypeByName(name); } -static ItemDescriptor GetByIndex(PyContainer* self, int index) { +static const void* GetByIndex(PyContainer* self, int index) { return GetDescriptor(self)->enum_type(index); } -static PyObject* NewObjectFromItem(ItemDescriptor item) { - return PyEnumDescriptor_FromDescriptor(item); +static PyObject* NewObjectFromItem(const void* item) { + return PyEnumDescriptor_FromDescriptor(static_cast<ItemDescriptor>(item)); } -static const string& GetItemName(ItemDescriptor item) { - return item->name(); +static const string& GetItemName(const void* item) { + return static_cast<ItemDescriptor>(item)->name(); } -static int GetItemIndex(ItemDescriptor item) { - return item->index(); +static int GetItemIndex(const void* item) { + return static_cast<ItemDescriptor>(item)->index(); } static DescriptorContainerDef ContainerDef = { "MessageNestedEnums", - (CountMethod)Count, - (GetByIndexMethod)GetByIndex, - (GetByNameMethod)GetByName, - (GetByCamelcaseNameMethod)NULL, - (GetByNumberMethod)NULL, - (NewObjectFromItemMethod)NewObjectFromItem, - (GetItemNameMethod)GetItemName, - (GetItemCamelcaseNameMethod)NULL, - (GetItemNumberMethod)NULL, - (GetItemIndexMethod)GetItemIndex, + Count, + GetByIndex, + GetByName, + NULL, + NULL, + NewObjectFromItem, + GetItemName, + NULL, + NULL, + GetItemIndex, }; } // namespace enums @@ -1150,11 +1150,11 @@ static int Count(PyContainer* self) { return count; } -static ItemDescriptor GetByName(PyContainer* self, const string& name) { +static const void* GetByName(PyContainer* self, const string& name) { return GetDescriptor(self)->FindEnumValueByName(name); } -static ItemDescriptor GetByIndex(PyContainer* self, int index) { +static const void* GetByIndex(PyContainer* self, int index) { // This is not optimal, but the number of enums *types* in a given message // is small. This function is only used when iterating over the mapping. const EnumDescriptor* enum_type = NULL; @@ -1173,26 +1173,27 @@ static ItemDescriptor GetByIndex(PyContainer* self, int index) { return enum_type->value(index); } -static PyObject* NewObjectFromItem(ItemDescriptor item) { - return PyEnumValueDescriptor_FromDescriptor(item); +static PyObject* NewObjectFromItem(const void* item) { + return PyEnumValueDescriptor_FromDescriptor( + static_cast<ItemDescriptor>(item)); } -static const string& GetItemName(ItemDescriptor item) { - return item->name(); +static const string& GetItemName(const void* item) { + return static_cast<ItemDescriptor>(item)->name(); } static DescriptorContainerDef ContainerDef = { "MessageEnumValues", - (CountMethod)Count, - (GetByIndexMethod)GetByIndex, - (GetByNameMethod)GetByName, - (GetByCamelcaseNameMethod)NULL, - (GetByNumberMethod)NULL, - (NewObjectFromItemMethod)NewObjectFromItem, - (GetItemNameMethod)GetItemName, - (GetItemCamelcaseNameMethod)NULL, - (GetItemNumberMethod)NULL, - (GetItemIndexMethod)NULL, + Count, + GetByIndex, + GetByName, + NULL, + NULL, + NewObjectFromItem, + GetItemName, + NULL, + NULL, + NULL, }; } // namespace enumvalues @@ -1209,38 +1210,38 @@ static int Count(PyContainer* self) { return GetDescriptor(self)->extension_count(); } -static ItemDescriptor GetByName(PyContainer* self, const string& name) { +static const void* GetByName(PyContainer* self, const string& name) { return GetDescriptor(self)->FindExtensionByName(name); } -static ItemDescriptor GetByIndex(PyContainer* self, int index) { +static const void* GetByIndex(PyContainer* self, int index) { return GetDescriptor(self)->extension(index); } -static PyObject* NewObjectFromItem(ItemDescriptor item) { - return PyFieldDescriptor_FromDescriptor(item); +static PyObject* NewObjectFromItem(const void* item) { + return PyFieldDescriptor_FromDescriptor(static_cast<ItemDescriptor>(item)); } -static const string& GetItemName(ItemDescriptor item) { - return item->name(); +static const string& GetItemName(const void* item) { + return static_cast<ItemDescriptor>(item)->name(); } -static int GetItemIndex(ItemDescriptor item) { - return item->index(); +static int GetItemIndex(const void* item) { + return static_cast<ItemDescriptor>(item)->index(); } static DescriptorContainerDef ContainerDef = { "MessageExtensions", - (CountMethod)Count, - (GetByIndexMethod)GetByIndex, - (GetByNameMethod)GetByName, - (GetByCamelcaseNameMethod)NULL, - (GetByNumberMethod)NULL, - (NewObjectFromItemMethod)NewObjectFromItem, - (GetItemNameMethod)GetItemName, - (GetItemCamelcaseNameMethod)NULL, - (GetItemNumberMethod)NULL, - (GetItemIndexMethod)GetItemIndex, + Count, + GetByIndex, + GetByName, + NULL, + NULL, + NewObjectFromItem, + GetItemName, + NULL, + NULL, + GetItemIndex, }; } // namespace extensions @@ -1261,38 +1262,38 @@ static int Count(PyContainer* self) { return GetDescriptor(self)->oneof_decl_count(); } -static ItemDescriptor GetByName(PyContainer* self, const string& name) { +static const void* GetByName(PyContainer* self, const string& name) { return GetDescriptor(self)->FindOneofByName(name); } -static ItemDescriptor GetByIndex(PyContainer* self, int index) { +static const void* GetByIndex(PyContainer* self, int index) { return GetDescriptor(self)->oneof_decl(index); } -static PyObject* NewObjectFromItem(ItemDescriptor item) { - return PyOneofDescriptor_FromDescriptor(item); +static PyObject* NewObjectFromItem(const void* item) { + return PyOneofDescriptor_FromDescriptor(static_cast<ItemDescriptor>(item)); } -static const string& GetItemName(ItemDescriptor item) { - return item->name(); +static const string& GetItemName(const void* item) { + return static_cast<ItemDescriptor>(item)->name(); } -static int GetItemIndex(ItemDescriptor item) { - return item->index(); +static int GetItemIndex(const void* item) { + return static_cast<ItemDescriptor>(item)->index(); } static DescriptorContainerDef ContainerDef = { "MessageOneofs", - (CountMethod)Count, - (GetByIndexMethod)GetByIndex, - (GetByNameMethod)GetByName, - (GetByCamelcaseNameMethod)NULL, - (GetByNumberMethod)NULL, - (NewObjectFromItemMethod)NewObjectFromItem, - (GetItemNameMethod)GetItemName, - (GetItemCamelcaseNameMethod)NULL, - (GetItemNumberMethod)NULL, - (GetItemIndexMethod)GetItemIndex, + Count, + GetByIndex, + GetByName, + NULL, + NULL, + NewObjectFromItem, + GetItemName, + NULL, + NULL, + GetItemIndex, }; } // namespace oneofs @@ -1323,46 +1324,47 @@ static int Count(PyContainer* self) { return GetDescriptor(self)->value_count(); } -static ItemDescriptor GetByIndex(PyContainer* self, int index) { +static const void* GetByIndex(PyContainer* self, int index) { return GetDescriptor(self)->value(index); } -static ItemDescriptor GetByName(PyContainer* self, const string& name) { +static const void* GetByName(PyContainer* self, const string& name) { return GetDescriptor(self)->FindValueByName(name); } -static ItemDescriptor GetByNumber(PyContainer* self, int number) { +static const void* GetByNumber(PyContainer* self, int number) { return GetDescriptor(self)->FindValueByNumber(number); } -static PyObject* NewObjectFromItem(ItemDescriptor item) { - return PyEnumValueDescriptor_FromDescriptor(item); +static PyObject* NewObjectFromItem(const void* item) { + return PyEnumValueDescriptor_FromDescriptor( + static_cast<ItemDescriptor>(item)); } -static const string& GetItemName(ItemDescriptor item) { - return item->name(); +static const string& GetItemName(const void* item) { + return static_cast<ItemDescriptor>(item)->name(); } -static int GetItemNumber(ItemDescriptor item) { - return item->number(); +static int GetItemNumber(const void* item) { + return static_cast<ItemDescriptor>(item)->number(); } -static int GetItemIndex(ItemDescriptor item) { - return item->index(); +static int GetItemIndex(const void* item) { + return static_cast<ItemDescriptor>(item)->index(); } static DescriptorContainerDef ContainerDef = { "EnumValues", - (CountMethod)Count, - (GetByIndexMethod)GetByIndex, - (GetByNameMethod)GetByName, - (GetByCamelcaseNameMethod)NULL, - (GetByNumberMethod)GetByNumber, - (NewObjectFromItemMethod)NewObjectFromItem, - (GetItemNameMethod)GetItemName, - (GetItemCamelcaseNameMethod)NULL, - (GetItemNumberMethod)GetItemNumber, - (GetItemIndexMethod)GetItemIndex, + Count, + GetByIndex, + GetByName, + NULL, + GetByNumber, + NewObjectFromItem, + GetItemName, + NULL, + GetItemNumber, + GetItemIndex, }; } // namespace enumvalues @@ -1397,30 +1399,30 @@ static int Count(PyContainer* self) { return GetDescriptor(self)->field_count(); } -static ItemDescriptor GetByIndex(PyContainer* self, int index) { +static const void* GetByIndex(PyContainer* self, int index) { return GetDescriptor(self)->field(index); } -static PyObject* NewObjectFromItem(ItemDescriptor item) { - return PyFieldDescriptor_FromDescriptor(item); +static PyObject* NewObjectFromItem(const void* item) { + return PyFieldDescriptor_FromDescriptor(static_cast<ItemDescriptor>(item)); } -static int GetItemIndex(ItemDescriptor item) { - return item->index_in_oneof(); +static int GetItemIndex(const void* item) { + return static_cast<ItemDescriptor>(item)->index_in_oneof(); } static DescriptorContainerDef ContainerDef = { "OneofFields", - (CountMethod)Count, - (GetByIndexMethod)GetByIndex, - (GetByNameMethod)NULL, - (GetByCamelcaseNameMethod)NULL, - (GetByNumberMethod)NULL, - (NewObjectFromItemMethod)NewObjectFromItem, - (GetItemNameMethod)NULL, - (GetItemCamelcaseNameMethod)NULL, - (GetItemNumberMethod)NULL, - (GetItemIndexMethod)GetItemIndex, + Count, + GetByIndex, + NULL, + NULL, + NULL, + NewObjectFromItem, + NULL, + NULL, + NULL, + GetItemIndex, }; } // namespace fields @@ -1447,38 +1449,38 @@ static int Count(PyContainer* self) { return GetDescriptor(self)->method_count(); } -static ItemDescriptor GetByName(PyContainer* self, const string& name) { +static const void* GetByName(PyContainer* self, const string& name) { return GetDescriptor(self)->FindMethodByName(name); } -static ItemDescriptor GetByIndex(PyContainer* self, int index) { +static const void* GetByIndex(PyContainer* self, int index) { return GetDescriptor(self)->method(index); } -static PyObject* NewObjectFromItem(ItemDescriptor item) { - return PyMethodDescriptor_FromDescriptor(item); +static PyObject* NewObjectFromItem(const void* item) { + return PyMethodDescriptor_FromDescriptor(static_cast<ItemDescriptor>(item)); } -static const string& GetItemName(ItemDescriptor item) { - return item->name(); +static const string& GetItemName(const void* item) { + return static_cast<ItemDescriptor>(item)->name(); } -static int GetItemIndex(ItemDescriptor item) { - return item->index(); +static int GetItemIndex(const void* item) { + return static_cast<ItemDescriptor>(item)->index(); } static DescriptorContainerDef ContainerDef = { "ServiceMethods", - (CountMethod)Count, - (GetByIndexMethod)GetByIndex, - (GetByNameMethod)GetByName, - (GetByCamelcaseNameMethod)NULL, - (GetByNumberMethod)NULL, - (NewObjectFromItemMethod)NewObjectFromItem, - (GetItemNameMethod)GetItemName, - (GetItemCamelcaseNameMethod)NULL, - (GetItemNumberMethod)NULL, - (GetItemIndexMethod)GetItemIndex, + Count, + GetByIndex, + GetByName, + NULL, + NULL, + NewObjectFromItem, + GetItemName, + NULL, + NULL, + GetItemIndex, }; } // namespace methods @@ -1509,38 +1511,38 @@ static int Count(PyContainer* self) { return GetDescriptor(self)->message_type_count(); } -static ItemDescriptor GetByName(PyContainer* self, const string& name) { +static const void* GetByName(PyContainer* self, const string& name) { return GetDescriptor(self)->FindMessageTypeByName(name); } -static ItemDescriptor GetByIndex(PyContainer* self, int index) { +static const void* GetByIndex(PyContainer* self, int index) { return GetDescriptor(self)->message_type(index); } -static PyObject* NewObjectFromItem(ItemDescriptor item) { - return PyMessageDescriptor_FromDescriptor(item); +static PyObject* NewObjectFromItem(const void* item) { + return PyMessageDescriptor_FromDescriptor(static_cast<ItemDescriptor>(item)); } -static const string& GetItemName(ItemDescriptor item) { - return item->name(); +static const string& GetItemName(const void* item) { + return static_cast<ItemDescriptor>(item)->name(); } -static int GetItemIndex(ItemDescriptor item) { - return item->index(); +static int GetItemIndex(const void* item) { + return static_cast<ItemDescriptor>(item)->index(); } static DescriptorContainerDef ContainerDef = { "FileMessages", - (CountMethod)Count, - (GetByIndexMethod)GetByIndex, - (GetByNameMethod)GetByName, - (GetByCamelcaseNameMethod)NULL, - (GetByNumberMethod)NULL, - (NewObjectFromItemMethod)NewObjectFromItem, - (GetItemNameMethod)GetItemName, - (GetItemCamelcaseNameMethod)NULL, - (GetItemNumberMethod)NULL, - (GetItemIndexMethod)GetItemIndex, + Count, + GetByIndex, + GetByName, + NULL, + NULL, + NewObjectFromItem, + GetItemName, + NULL, + NULL, + GetItemIndex, }; } // namespace messages @@ -1557,38 +1559,38 @@ static int Count(PyContainer* self) { return GetDescriptor(self)->enum_type_count(); } -static ItemDescriptor GetByName(PyContainer* self, const string& name) { +static const void* GetByName(PyContainer* self, const string& name) { return GetDescriptor(self)->FindEnumTypeByName(name); } -static ItemDescriptor GetByIndex(PyContainer* self, int index) { +static const void* GetByIndex(PyContainer* self, int index) { return GetDescriptor(self)->enum_type(index); } -static PyObject* NewObjectFromItem(ItemDescriptor item) { - return PyEnumDescriptor_FromDescriptor(item); +static PyObject* NewObjectFromItem(const void* item) { + return PyEnumDescriptor_FromDescriptor(static_cast<ItemDescriptor>(item)); } -static const string& GetItemName(ItemDescriptor item) { - return item->name(); +static const string& GetItemName(const void* item) { + return static_cast<ItemDescriptor>(item)->name(); } -static int GetItemIndex(ItemDescriptor item) { - return item->index(); +static int GetItemIndex(const void* item) { + return static_cast<ItemDescriptor>(item)->index(); } static DescriptorContainerDef ContainerDef = { "FileEnums", - (CountMethod)Count, - (GetByIndexMethod)GetByIndex, - (GetByNameMethod)GetByName, - (GetByCamelcaseNameMethod)NULL, - (GetByNumberMethod)NULL, - (NewObjectFromItemMethod)NewObjectFromItem, - (GetItemNameMethod)GetItemName, - (GetItemCamelcaseNameMethod)NULL, - (GetItemNumberMethod)NULL, - (GetItemIndexMethod)GetItemIndex, + Count, + GetByIndex, + GetByName, + NULL, + NULL, + NewObjectFromItem, + GetItemName, + NULL, + NULL, + GetItemIndex, }; } // namespace enums @@ -1605,38 +1607,38 @@ static int Count(PyContainer* self) { return GetDescriptor(self)->extension_count(); } -static ItemDescriptor GetByName(PyContainer* self, const string& name) { +static const void* GetByName(PyContainer* self, const string& name) { return GetDescriptor(self)->FindExtensionByName(name); } -static ItemDescriptor GetByIndex(PyContainer* self, int index) { +static const void* GetByIndex(PyContainer* self, int index) { return GetDescriptor(self)->extension(index); } -static PyObject* NewObjectFromItem(ItemDescriptor item) { - return PyFieldDescriptor_FromDescriptor(item); +static PyObject* NewObjectFromItem(const void* item) { + return PyFieldDescriptor_FromDescriptor(static_cast<ItemDescriptor>(item)); } -static const string& GetItemName(ItemDescriptor item) { - return item->name(); +static const string& GetItemName(const void* item) { + return static_cast<ItemDescriptor>(item)->name(); } -static int GetItemIndex(ItemDescriptor item) { - return item->index(); +static int GetItemIndex(const void* item) { + return static_cast<ItemDescriptor>(item)->index(); } static DescriptorContainerDef ContainerDef = { "FileExtensions", - (CountMethod)Count, - (GetByIndexMethod)GetByIndex, - (GetByNameMethod)GetByName, - (GetByCamelcaseNameMethod)NULL, - (GetByNumberMethod)NULL, - (NewObjectFromItemMethod)NewObjectFromItem, - (GetItemNameMethod)GetItemName, - (GetItemCamelcaseNameMethod)NULL, - (GetItemNumberMethod)NULL, - (GetItemIndexMethod)GetItemIndex, + Count, + GetByIndex, + GetByName, + NULL, + NULL, + NewObjectFromItem, + GetItemName, + NULL, + NULL, + GetItemIndex, }; } // namespace extensions @@ -1653,38 +1655,38 @@ static int Count(PyContainer* self) { return GetDescriptor(self)->service_count(); } -static ItemDescriptor GetByName(PyContainer* self, const string& name) { +static const void* GetByName(PyContainer* self, const string& name) { return GetDescriptor(self)->FindServiceByName(name); } -static ItemDescriptor GetByIndex(PyContainer* self, int index) { +static const void* GetByIndex(PyContainer* self, int index) { return GetDescriptor(self)->service(index); } -static PyObject* NewObjectFromItem(ItemDescriptor item) { - return PyServiceDescriptor_FromDescriptor(item); +static PyObject* NewObjectFromItem(const void* item) { + return PyServiceDescriptor_FromDescriptor(static_cast<ItemDescriptor>(item)); } -static const string& GetItemName(ItemDescriptor item) { - return item->name(); +static const string& GetItemName(const void* item) { + return static_cast<ItemDescriptor>(item)->name(); } -static int GetItemIndex(ItemDescriptor item) { - return item->index(); +static int GetItemIndex(const void* item) { + return static_cast<ItemDescriptor>(item)->index(); } static DescriptorContainerDef ContainerDef = { "FileServices", - (CountMethod)Count, - (GetByIndexMethod)GetByIndex, - (GetByNameMethod)GetByName, - (GetByCamelcaseNameMethod)NULL, - (GetByNumberMethod)NULL, - (NewObjectFromItemMethod)NewObjectFromItem, - (GetItemNameMethod)GetItemName, - (GetItemCamelcaseNameMethod)NULL, - (GetItemNumberMethod)NULL, - (GetItemIndexMethod)GetItemIndex, + Count, + GetByIndex, + GetByName, + NULL, + NULL, + NewObjectFromItem, + GetItemName, + NULL, + NULL, + GetItemIndex, }; } // namespace services @@ -1701,26 +1703,26 @@ static int Count(PyContainer* self) { return GetDescriptor(self)->dependency_count(); } -static ItemDescriptor GetByIndex(PyContainer* self, int index) { +static const void* GetByIndex(PyContainer* self, int index) { return GetDescriptor(self)->dependency(index); } -static PyObject* NewObjectFromItem(ItemDescriptor item) { - return PyFileDescriptor_FromDescriptor(item); +static PyObject* NewObjectFromItem(const void* item) { + return PyFileDescriptor_FromDescriptor(static_cast<ItemDescriptor>(item)); } static DescriptorContainerDef ContainerDef = { "FileDependencies", - (CountMethod)Count, - (GetByIndexMethod)GetByIndex, - (GetByNameMethod)NULL, - (GetByCamelcaseNameMethod)NULL, - (GetByNumberMethod)NULL, - (NewObjectFromItemMethod)NewObjectFromItem, - (GetItemNameMethod)NULL, - (GetItemCamelcaseNameMethod)NULL, - (GetItemNumberMethod)NULL, - (GetItemIndexMethod)NULL, + Count, + GetByIndex, + NULL, + NULL, + NULL, + NewObjectFromItem, + NULL, + NULL, + NULL, + NULL, }; } // namespace dependencies @@ -1737,26 +1739,26 @@ static int Count(PyContainer* self) { return GetDescriptor(self)->public_dependency_count(); } -static ItemDescriptor GetByIndex(PyContainer* self, int index) { +static const void* GetByIndex(PyContainer* self, int index) { return GetDescriptor(self)->public_dependency(index); } -static PyObject* NewObjectFromItem(ItemDescriptor item) { - return PyFileDescriptor_FromDescriptor(item); +static PyObject* NewObjectFromItem(const void* item) { + return PyFileDescriptor_FromDescriptor(static_cast<ItemDescriptor>(item)); } static DescriptorContainerDef ContainerDef = { "FilePublicDependencies", - (CountMethod)Count, - (GetByIndexMethod)GetByIndex, - (GetByNameMethod)NULL, - (GetByCamelcaseNameMethod)NULL, - (GetByNumberMethod)NULL, - (NewObjectFromItemMethod)NewObjectFromItem, - (GetItemNameMethod)NULL, - (GetItemCamelcaseNameMethod)NULL, - (GetItemNumberMethod)NULL, - (GetItemIndexMethod)NULL, + Count, + GetByIndex, + NULL, + NULL, + NULL, + NewObjectFromItem, + NULL, + NULL, + NULL, + NULL, }; } // namespace public_dependencies diff --git a/python/google/protobuf/pyext/message.cc b/python/google/protobuf/pyext/message.cc index ef75acab..f515b560 100644 --- a/python/google/protobuf/pyext/message.cc +++ b/python/google/protobuf/pyext/message.cc @@ -605,18 +605,16 @@ void OutOfRangeError(PyObject* arg) { template<class RangeType, class ValueType> bool VerifyIntegerCastAndRange(PyObject* arg, ValueType value) { - if - GOOGLE_PREDICT_FALSE(value == -1 && PyErr_Occurred()) { - if (PyErr_ExceptionMatches(PyExc_OverflowError)) { - // Replace it with the same ValueError as pure python protos instead of - // the default one. - PyErr_Clear(); - OutOfRangeError(arg); - } // Otherwise propagate existing error. - return false; + if (GOOGLE_PREDICT_FALSE(value == -1 && PyErr_Occurred())) { + if (PyErr_ExceptionMatches(PyExc_OverflowError)) { + // Replace it with the same ValueError as pure python protos instead of + // the default one. + PyErr_Clear(); + OutOfRangeError(arg); + } // Otherwise propagate existing error. + return false; } - if - GOOGLE_PREDICT_FALSE(!IsValidNumericCast<RangeType>(value)) { + if (GOOGLE_PREDICT_FALSE(!IsValidNumericCast<RangeType>(value))) { OutOfRangeError(arg); return false; } @@ -628,26 +626,22 @@ bool CheckAndGetInteger(PyObject* arg, T* value) { // The fast path. #if PY_MAJOR_VERSION < 3 // For the typical case, offer a fast path. - if - GOOGLE_PREDICT_TRUE(PyInt_Check(arg)) { - long int_result = PyInt_AsLong(arg); - if - GOOGLE_PREDICT_TRUE(IsValidNumericCast<T>(int_result)) { - *value = static_cast<T>(int_result); - return true; - } - else { - OutOfRangeError(arg); - return false; - } + if (GOOGLE_PREDICT_TRUE(PyInt_Check(arg))) { + long int_result = PyInt_AsLong(arg); + if (GOOGLE_PREDICT_TRUE(IsValidNumericCast<T>(int_result))) { + *value = static_cast<T>(int_result); + return true; + } else { + OutOfRangeError(arg); + return false; + } } #endif // This effectively defines an integer as "an object that can be cast as // an integer and can be used as an ordinal number". // This definition includes everything that implements numbers.Integral // and shouldn't cast the net too wide. - if - GOOGLE_PREDICT_FALSE(!PyIndex_Check(arg)) { + if (GOOGLE_PREDICT_FALSE(!PyIndex_Check(arg))) { FormatTypeError(arg, "int, long"); return false; } @@ -664,10 +658,9 @@ bool CheckAndGetInteger(PyObject* arg, T* value) { // Unlike PyLong_AsLongLong, PyLong_AsUnsignedLongLong is very // picky about the exact type. PyObject* casted = PyNumber_Long(arg); - if - GOOGLE_PREDICT_FALSE(casted == NULL) { - // Propagate existing error. - return false; + if (GOOGLE_PREDICT_FALSE(casted == NULL)) { + // Propagate existing error. + return false; } ulong_result = PyLong_AsUnsignedLongLong(casted); Py_DECREF(casted); @@ -690,10 +683,9 @@ bool CheckAndGetInteger(PyObject* arg, T* value) { // Valid subclasses of numbers.Integral should have a __long__() method // so fall back to that. PyObject* casted = PyNumber_Long(arg); - if - GOOGLE_PREDICT_FALSE(casted == NULL) { - // Propagate existing error. - return false; + if (GOOGLE_PREDICT_FALSE(casted == NULL)) { + // Propagate existing error. + return false; } long_result = PyLong_AsLongLong(casted); Py_DECREF(casted); @@ -717,10 +709,9 @@ template bool CheckAndGetInteger<uint64>(PyObject*, uint64*); bool CheckAndGetDouble(PyObject* arg, double* value) { *value = PyFloat_AsDouble(arg); - if - GOOGLE_PREDICT_FALSE(*value == -1 && PyErr_Occurred()) { - FormatTypeError(arg, "int, long, float"); - return false; + if (GOOGLE_PREDICT_FALSE(*value == -1 && PyErr_Occurred())) { + FormatTypeError(arg, "int, long, float"); + return false; } return true; } @@ -1187,7 +1178,7 @@ int InitAttributes(CMessage* self, PyObject* args, PyObject* kwargs) { continue; } if (descriptor->is_map()) { - ScopedPyObjectPtr map(GetAttr(self, name)); + ScopedPyObjectPtr map(GetAttr(reinterpret_cast<PyObject*>(self), name)); const FieldDescriptor* value_descriptor = descriptor->message_type()->FindFieldByName("value"); if (value_descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { @@ -1215,7 +1206,8 @@ int InitAttributes(CMessage* self, PyObject* args, PyObject* kwargs) { } } } else if (descriptor->label() == FieldDescriptor::LABEL_REPEATED) { - ScopedPyObjectPtr container(GetAttr(self, name)); + ScopedPyObjectPtr container( + GetAttr(reinterpret_cast<PyObject*>(self), name)); if (container == NULL) { return -1; } @@ -1282,7 +1274,8 @@ int InitAttributes(CMessage* self, PyObject* args, PyObject* kwargs) { } } } else if (descriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { - ScopedPyObjectPtr message(GetAttr(self, name)); + ScopedPyObjectPtr message( + GetAttr(reinterpret_cast<PyObject*>(self), name)); if (message == NULL) { return -1; } @@ -1307,8 +1300,8 @@ int InitAttributes(CMessage* self, PyObject* args, PyObject* kwargs) { return -1; } } - if (SetAttr(self, name, (new_val.get() == NULL) ? value : new_val.get()) < - 0) { + if (SetAttr(reinterpret_cast<PyObject*>(self), name, + (new_val.get() == NULL) ? value : new_val.get()) < 0) { return -1; } } @@ -2211,7 +2204,8 @@ static PyObject* ListFields(CMessage* self) { return NULL; } - PyObject* field_value = GetAttr(self, py_field_name.get()); + PyObject* field_value = + GetAttr(reinterpret_cast<PyObject*>(self), py_field_name.get()); if (field_value == NULL) { PyErr_SetObject(PyExc_ValueError, py_field_name.get()); return NULL; @@ -2707,7 +2701,8 @@ static bool SetCompositeField( return PyDict_SetItem(self->composite_fields, name, value) == 0; } -PyObject* GetAttr(CMessage* self, PyObject* name) { +PyObject* GetAttr(PyObject* pself, PyObject* name) { + CMessage* self = reinterpret_cast<CMessage*>(pself); PyObject* value = self->composite_fields ? PyDict_GetItem(self->composite_fields, name) : NULL; if (value != NULL) { @@ -2785,7 +2780,8 @@ PyObject* GetAttr(CMessage* self, PyObject* name) { return InternalGetScalar(self->message, field_descriptor); } -int SetAttr(CMessage* self, PyObject* name, PyObject* value) { +int SetAttr(PyObject* pself, PyObject* name, PyObject* value) { + CMessage* self = reinterpret_cast<CMessage*>(pself); if (self->composite_fields && PyDict_Contains(self->composite_fields, name)) { PyErr_SetString(PyExc_TypeError, "Can't set composite field"); return -1; @@ -2837,8 +2833,8 @@ PyTypeObject CMessage_Type = { PyObject_HashNotImplemented, // tp_hash 0, // tp_call (reprfunc)cmessage::ToStr, // tp_str - (getattrofunc)cmessage::GetAttr, // tp_getattro - (setattrofunc)cmessage::SetAttr, // tp_setattro + cmessage::GetAttr, // tp_getattro + cmessage::SetAttr, // tp_setattro 0, // tp_as_buffer Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, // tp_flags "A ProtocolMessage", // tp_doc diff --git a/python/google/protobuf/pyext/message.h b/python/google/protobuf/pyext/message.h index ce80497e..576d098c 100644 --- a/python/google/protobuf/pyext/message.h +++ b/python/google/protobuf/pyext/message.h @@ -240,15 +240,15 @@ PyObject* MergeFrom(CMessage* self, PyObject* arg); // has been registered with the same field number on this class. PyObject* RegisterExtension(PyObject* cls, PyObject* extension_handle); -// Retrieves an attribute named 'name' from CMessage 'self'. Returns -// the attribute value on success, or NULL on failure. +// Retrieves an attribute named 'name' from 'self', which is interpreted as a +// CMessage. Returns the attribute value on success, or null on failure. // // Returns a new reference. -PyObject* GetAttr(CMessage* self, PyObject* name); +PyObject* GetAttr(PyObject* self, PyObject* name); -// Set the value of the attribute named 'name', for CMessage 'self', -// to the value 'value'. Returns -1 on failure. -int SetAttr(CMessage* self, PyObject* name, PyObject* value); +// Set the value of the attribute named 'name', for 'self', which is interpreted +// as a CMessage, to the value 'value'. Returns -1 on failure. +int SetAttr(PyObject* self, PyObject* name, PyObject* value); PyObject* FindInitializationErrors(CMessage* self); diff --git a/python/google/protobuf/text_format.py b/python/google/protobuf/text_format.py index 6b12632e..36ddd1b7 100755 --- a/python/google/protobuf/text_format.py +++ b/python/google/protobuf/text_format.py @@ -938,7 +938,7 @@ def _SkipField(tokenizer): tokenizer.ConsumeIdentifier() tokenizer.Consume(']') else: - tokenizer.ConsumeIdentifier() + tokenizer.ConsumeIdentifierOrNumber() _SkipFieldContents(tokenizer) |