Browse Source

Fix a Python extension crash in the descriptor's nested types container code (#6455)

Gaël Delalleau 6 years ago
parent
commit
5db5c26ae4

+ 7 - 0
python/google/protobuf/internal/descriptor_test.py

@@ -650,6 +650,13 @@ class GeneratedDescriptorTest(unittest.TestCase):
     del enum
     self.assertEqual('FOO', next(values_iter).name)
 
+  def testDescriptorNestedTypesContainer(self):
+    message_descriptor = unittest_pb2.TestAllTypes.DESCRIPTOR
+    nested_message_descriptor = unittest_pb2.TestAllTypes.NestedMessage.DESCRIPTOR
+    self.assertEqual(len(message_descriptor.nested_types), 3)
+    self.assertFalse(None in message_descriptor.nested_types)
+    self.assertTrue(nested_message_descriptor in message_descriptor.nested_types)
+
   def testServiceDescriptor(self):
     service_descriptor = unittest_pb2.DESCRIPTOR.services_by_name['TestService']
     self.assertEqual(service_descriptor.name, 'TestService')

+ 1 - 0
python/google/protobuf/pyext/descriptor_containers.cc

@@ -639,6 +639,7 @@ int Find(PyContainer* self, PyObject* item) {
   // the .proto file definition.
   const void* descriptor_ptr = PyDescriptor_AsVoidPtr(item);
   if (descriptor_ptr == NULL) {
+    PyErr_Clear();
     // Not a descriptor, it cannot be in the list.
     return -1;
   }