Merge pull request #8327 from alkasm/alkasm/method-desc-copy-to-proto
[python-runtime] Add MethodDescriptor.CopyToProto()
diff --git a/python/google/protobuf/descriptor.py b/python/google/protobuf/descriptor.py
index 190b895..c8f7b41 100644
--- a/python/google/protobuf/descriptor.py
+++ b/python/google/protobuf/descriptor.py
@@ -912,6 +912,24 @@
self.containing_service = containing_service
self.input_type = input_type
self.output_type = output_type
+
+ def CopyToProto(self, proto):
+ """Copies this to a descriptor_pb2.MethodDescriptorProto.
+
+ Args:
+ proto (descriptor_pb2.MethodDescriptorProto): An empty descriptor proto.
+
+ Raises:
+ Error: If self couldn't be serialized, due to too few constructor
+ arguments.
+ """
+ if self.containing_service is not None:
+ from google.protobuf import descriptor_pb2
+ service_proto = descriptor_pb2.ServiceDescriptorProto()
+ self.containing_service.CopyToProto(service_proto)
+ proto.CopyFrom(service_proto.method[self.index])
+ else:
+ raise Error('Descriptor does not contain a service.')
class FileDescriptor(DescriptorBase):
diff --git a/python/google/protobuf/internal/descriptor_test.py b/python/google/protobuf/internal/descriptor_test.py
index 5e3b0a9..4fc8049 100755
--- a/python/google/protobuf/internal/descriptor_test.py
+++ b/python/google/protobuf/internal/descriptor_test.py
@@ -907,10 +907,6 @@
descriptor_pb2.ServiceDescriptorProto,
TEST_SERVICE_ASCII)
- @unittest.skipIf(
- api_implementation.Type() == 'python',
- 'It is not implemented in python.')
- # TODO(jieluo): Add support for pure python or remove in c extension.
def testCopyToProto_MethodDescriptor(self):
expected_ascii = """
name: 'Foo'