Skip to content

Commit

Permalink
Merge pull request #7498 from psobot/add-python-descriptor-debug-string
Browse files Browse the repository at this point in the history
Add GetDebugString to Python FileDescriptor interface.
  • Loading branch information
deannagarcia authored Oct 15, 2021
2 parents 6c0168b + ef0bd13 commit df5b7fa
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
29 changes: 29 additions & 0 deletions python/google/protobuf/internal/descriptor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,28 @@
name: 'TestEmptyMessage'
"""

TEST_FILE_DESCRIPTOR_DEBUG = """syntax = "proto2";
package protobuf_unittest;
message NestedMessage {
enum ForeignEnum {
FOREIGN_FOO = 4;
FOREIGN_BAR = 5;
FOREIGN_BAZ = 6;
}
optional int32 bb = 1;
}
message ResponseMessage {
}
service Service {
rpc CallMethod(.protobuf_unittest.NestedMessage) returns (.protobuf_unittest.ResponseMessage);
}
"""


warnings.simplefilter('error', DeprecationWarning)

Expand Down Expand Up @@ -121,6 +143,13 @@ def testContainingTypeFixups(self):
def testContainingServiceFixups(self):
self.assertEqual(self.my_service, self.my_method.containing_service)

@unittest.skipIf(
api_implementation.Type() != 'cpp',
'GetDebugString is only available with the cpp implementation',
)
def testGetDebugString(self):
self.assertEqual(self.my_file.GetDebugString(), TEST_FILE_DESCRIPTOR_DEBUG)

def testGetOptions(self):
self.assertEqual(self.my_enum.GetOptions(),
descriptor_pb2.EnumOptions())
Expand Down
5 changes: 5 additions & 0 deletions python/google/protobuf/pyext/descriptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,10 @@ static int SetHasOptions(PyFileDescriptor *self, PyObject *value,
return CheckCalledFromGeneratedFile("has_options");
}

static PyObject* GetDebugString(PyFileDescriptor *self) {
return PyString_FromCppString(_GetDescriptor(self)->DebugString());
}

static PyObject* GetOptions(PyFileDescriptor *self) {
return GetOrBuildOptions(_GetDescriptor(self));
}
Expand Down Expand Up @@ -1439,6 +1443,7 @@ static PyGetSetDef Getters[] = {
};

static PyMethodDef Methods[] = {
{ "GetDebugString", (PyCFunction)GetDebugString, METH_NOARGS, },
{ "GetOptions", (PyCFunction)GetOptions, METH_NOARGS, },
{ "CopyToProto", (PyCFunction)CopyToProto, METH_O, },
{NULL}
Expand Down

0 comments on commit df5b7fa

Please sign in to comment.