Skip to content

Commit df5b7fa

Browse files
authored
Merge pull request #7498 from psobot/add-python-descriptor-debug-string
Add GetDebugString to Python FileDescriptor interface.
2 parents 6c0168b + ef0bd13 commit df5b7fa

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

python/google/protobuf/internal/descriptor_test.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,28 @@
5151
name: 'TestEmptyMessage'
5252
"""
5353

54+
TEST_FILE_DESCRIPTOR_DEBUG = """syntax = "proto2";
55+
56+
package protobuf_unittest;
57+
58+
message NestedMessage {
59+
enum ForeignEnum {
60+
FOREIGN_FOO = 4;
61+
FOREIGN_BAR = 5;
62+
FOREIGN_BAZ = 6;
63+
}
64+
optional int32 bb = 1;
65+
}
66+
67+
message ResponseMessage {
68+
}
69+
70+
service Service {
71+
rpc CallMethod(.protobuf_unittest.NestedMessage) returns (.protobuf_unittest.ResponseMessage);
72+
}
73+
74+
"""
75+
5476

5577
warnings.simplefilter('error', DeprecationWarning)
5678

@@ -121,6 +143,13 @@ def testContainingTypeFixups(self):
121143
def testContainingServiceFixups(self):
122144
self.assertEqual(self.my_service, self.my_method.containing_service)
123145

146+
@unittest.skipIf(
147+
api_implementation.Type() != 'cpp',
148+
'GetDebugString is only available with the cpp implementation',
149+
)
150+
def testGetDebugString(self):
151+
self.assertEqual(self.my_file.GetDebugString(), TEST_FILE_DESCRIPTOR_DEBUG)
152+
124153
def testGetOptions(self):
125154
self.assertEqual(self.my_enum.GetOptions(),
126155
descriptor_pb2.EnumOptions())

python/google/protobuf/pyext/descriptor.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,6 +1393,10 @@ static int SetHasOptions(PyFileDescriptor *self, PyObject *value,
13931393
return CheckCalledFromGeneratedFile("has_options");
13941394
}
13951395

1396+
static PyObject* GetDebugString(PyFileDescriptor *self) {
1397+
return PyString_FromCppString(_GetDescriptor(self)->DebugString());
1398+
}
1399+
13961400
static PyObject* GetOptions(PyFileDescriptor *self) {
13971401
return GetOrBuildOptions(_GetDescriptor(self));
13981402
}
@@ -1439,6 +1443,7 @@ static PyGetSetDef Getters[] = {
14391443
};
14401444

14411445
static PyMethodDef Methods[] = {
1446+
{ "GetDebugString", (PyCFunction)GetDebugString, METH_NOARGS, },
14421447
{ "GetOptions", (PyCFunction)GetOptions, METH_NOARGS, },
14431448
{ "CopyToProto", (PyCFunction)CopyToProto, METH_O, },
14441449
{NULL}

0 commit comments

Comments
 (0)