From 393e453033f7343c7b9a03654ddd2213467d1235 Mon Sep 17 00:00:00 2001 From: Alexander Reynolds Date: Mon, 22 Feb 2021 02:25:37 -0800 Subject: [PATCH 1/2] Add MethodDescriptor.CopyToProto() --- python/google/protobuf/descriptor.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/python/google/protobuf/descriptor.py b/python/google/protobuf/descriptor.py index 190b89536fdf..c8f7b4133f25 100644 --- a/python/google/protobuf/descriptor.py +++ b/python/google/protobuf/descriptor.py @@ -912,6 +912,24 @@ def __init__(self, name, full_name, index, containing_service, 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): From 3c782fc3d6e18c9b1ecd34b1e407eab21eeb364b Mon Sep 17 00:00:00 2001 From: Alexander Reynolds Date: Wed, 7 Apr 2021 02:21:46 -0700 Subject: [PATCH 2/2] Remove py impl skip for method desc CopyToProto test --- python/google/protobuf/internal/descriptor_test.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/python/google/protobuf/internal/descriptor_test.py b/python/google/protobuf/internal/descriptor_test.py index 5e3b0a9b4c9a..4fc80492b8fc 100755 --- a/python/google/protobuf/internal/descriptor_test.py +++ b/python/google/protobuf/internal/descriptor_test.py @@ -907,10 +907,6 @@ def testCopyToProto_ServiceDescriptor(self): 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'