Skip to content

Commit

Permalink
Add has_options to ServiceDescriptor and MethodDescriptor in UPB pyth…
Browse files Browse the repository at this point in the history
…on and cpp python to match with other descriptors and pure python

PiperOrigin-RevId: 714950915
  • Loading branch information
anandolee authored and copybara-github committed Jan 13, 2025
1 parent 6e393fd commit 4214ee7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
15 changes: 15 additions & 0 deletions python/descriptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -1527,6 +1527,12 @@ static PyObject* PyUpb_MethodDescriptor_GetServerStreaming(PyObject* self,
return PyBool_FromLong(upb_MethodDef_ServerStreaming(m) ? 1 : 0);
}

static PyObject* PyUpb_MethodDescriptor_GetHasOptions(PyObject* _self,
void* closure) {
PyUpb_DescriptorBase* self = (void*)_self;
return PyBool_FromLong(upb_MethodDef_HasOptions(self->def));
}

static PyObject* PyUpb_MethodDescriptor_GetOptions(PyObject* _self,
PyObject* args) {
PyUpb_DescriptorBase* self = (void*)_self;
Expand Down Expand Up @@ -1565,6 +1571,8 @@ static PyGetSetDef PyUpb_MethodDescriptor_Getters[] = {
"Client streaming", NULL},
{"server_streaming", PyUpb_MethodDescriptor_GetServerStreaming, NULL,
"Server streaming", NULL},
{"has_options", PyUpb_MethodDescriptor_GetHasOptions, NULL, "Has Options"},

{NULL}};

static PyMethodDef PyUpb_MethodDescriptor_Methods[] = {
Expand Down Expand Up @@ -1754,6 +1762,12 @@ static PyObject* PyUpb_ServiceDescriptor_GetMethodsByName(PyObject* _self,
return PyUpb_ByNameMap_New(&funcs, self->def, self->pool);
}

static PyObject* PyUpb_ServiceDescriptor_GetHasOptions(PyObject* _self,
void* closure) {
PyUpb_DescriptorBase* self = (void*)_self;
return PyBool_FromLong(upb_ServiceDef_HasOptions(self->def));
}

static PyObject* PyUpb_ServiceDescriptor_GetOptions(PyObject* _self,
PyObject* args) {
PyUpb_DescriptorBase* self = (void*)_self;
Expand Down Expand Up @@ -1799,6 +1813,7 @@ static PyGetSetDef PyUpb_ServiceDescriptor_Getters[] = {
{"methods", PyUpb_ServiceDescriptor_GetMethods, NULL, "Methods", NULL},
{"methods_by_name", PyUpb_ServiceDescriptor_GetMethodsByName, NULL,
"Methods by name", NULL},
{"has_options", PyUpb_ServiceDescriptor_GetHasOptions, NULL, "Has Options"},
{NULL}};

static PyMethodDef PyUpb_ServiceDescriptor_Methods[] = {
Expand Down
2 changes: 2 additions & 0 deletions python/google/protobuf/internal/descriptor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ def testSimpleCustomOptions(self):
service_options = service_descriptor.GetOptions()
service_opt1 = unittest_custom_options_pb2.service_opt1
self.assertEqual(-9876543210, service_options.Extensions[service_opt1])
self.assertTrue(method_descriptor.has_options)
method_options = method_descriptor.GetOptions()
method_opt1 = unittest_custom_options_pb2.method_opt1
self.assertEqual(unittest_custom_options_pb2.METHODOPT1_VAL2,
Expand Down Expand Up @@ -792,6 +793,7 @@ def testServiceDescriptor(self):
self.assertIs(service_descriptor.file, unittest_pb2.DESCRIPTOR)
self.assertEqual(service_descriptor.index, 0)
self.CheckDescriptorMapping(service_descriptor.methods_by_name)
self.assertFalse(service_descriptor.has_options)

def testOneofDescriptor(self):
message_descriptor = unittest_pb2.TestAllTypes.DESCRIPTOR
Expand Down
20 changes: 20 additions & 0 deletions python/google/protobuf/pyext/descriptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1832,6 +1832,15 @@ static PyObject* FindMethodByName(PyBaseDescriptor *self, PyObject* arg) {
return PyMethodDescriptor_FromDescriptor(method_descriptor);
}

static PyObject* GetHasOptions(PyBaseDescriptor* self, void* closure) {
const ServiceOptions& options = _GetDescriptor(self)->options();
if (&options != &ServiceOptions::default_instance()) {
Py_RETURN_TRUE;
} else {
Py_RETURN_FALSE;
}
}

static PyObject* GetOptions(PyBaseDescriptor *self) {
return GetOrBuildOptions(_GetDescriptor(self));
}
Expand All @@ -1853,6 +1862,7 @@ static PyGetSetDef Getters[] = {
{"methods", (getter)GetMethods, nullptr, "Methods", nullptr},
{"methods_by_name", (getter)GetMethodsByName, nullptr, "Methods by name",
nullptr},
{"has_options", (getter)GetHasOptions, nullptr},
{nullptr},
};

Expand Down Expand Up @@ -1963,6 +1973,15 @@ static PyObject* GetServerStreaming(PyBaseDescriptor* self, void* closure) {
return PyBool_FromLong(_GetDescriptor(self)->server_streaming() ? 1 : 0);
}

static PyObject* GetHasOptions(PyBaseDescriptor* self, void* closure) {
const MethodOptions& options = _GetDescriptor(self)->options();
if (&options != &MethodOptions::default_instance()) {
Py_RETURN_TRUE;
} else {
Py_RETURN_FALSE;
}
}

static PyObject* GetOptions(PyBaseDescriptor *self) {
return GetOrBuildOptions(_GetDescriptor(self));
}
Expand All @@ -1987,6 +2006,7 @@ static PyGetSetDef Getters[] = {
"Client streaming", nullptr},
{"server_streaming", (getter)GetServerStreaming, nullptr,
"Server streaming", nullptr},
{"has_options", (getter)GetHasOptions, nullptr},
{nullptr},
};

Expand Down

0 comments on commit 4214ee7

Please sign in to comment.