| 1 |
/* |
| 2 |
* Copyright (c) 2009-2021, Google LLC |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions are met: |
| 7 |
* * Redistributions of source code must retain the above copyright |
| 8 |
* notice, this list of conditions and the following disclaimer. |
| 9 |
* * Redistributions in binary form must reproduce the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer in the |
| 11 |
* documentation and/or other materials provided with the distribution. |
| 12 |
* * Neither the name of Google LLC nor the |
| 13 |
* names of its contributors may be used to endorse or promote products |
| 14 |
* derived from this software without specific prior written permission. |
| 15 |
* |
| 16 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 |
* ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT, |
| 20 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 |
*/ |
| 27 |
|
| 28 |
#include "python/descriptor.h" |
| 29 |
|
| 30 |
#include "python/convert.h" |
| 31 |
#include "python/descriptor_containers.h" |
| 32 |
#include "python/descriptor_pool.h" |
| 33 |
#include "python/message.h" |
| 34 |
#include "python/protobuf.h" |
| 35 |
#include "upb/def.h" |
| 36 |
#include "upb/util/def_to_proto.h" |
| 37 |
|
| 38 |
// ----------------------------------------------------------------------------- |
| 39 |
// DescriptorBase |
| 40 |
// ----------------------------------------------------------------------------- |
| 41 |
|
| 42 |
// This representation is used by all concrete descriptors. |
| 43 |
|
| 44 |
typedef struct { |
| 45 |
PyObject_HEAD; |
| 46 |
PyObject* pool; // We own a ref. |
| 47 |
const void* def; // Type depends on the class. Kept alive by "pool". |
| 48 |
PyObject* options; // NULL if not present or not cached. |
| 49 |
} PyUpb_DescriptorBase; |
| 50 |
|
| 51 |
PyObject* PyUpb_AnyDescriptor_GetPool(PyObject* desc) { |
| 52 |
PyUpb_DescriptorBase* base = (void*)desc; |
| 53 |
return base->pool; |
| 54 |
} |
| 55 |
|
| 56 |
const void* PyUpb_AnyDescriptor_GetDef(PyObject* desc) { |
| 57 |
PyUpb_DescriptorBase* base = (void*)desc; |
| 58 |
return base->def; |
| 59 |
} |
| 60 |
|
| 61 |
static PyUpb_DescriptorBase* PyUpb_DescriptorBase_DoCreate( |
| 62 |
PyUpb_DescriptorType type, const void* def, const upb_FileDef* file) { |
| 63 |
PyUpb_ModuleState* state = PyUpb_ModuleState_Get(); |
| 64 |
PyTypeObject* type_obj = state->descriptor_types[type]; |
| 65 |
assert(def); |
| 66 |
|
| 67 |
PyUpb_DescriptorBase* base = (void*)PyType_GenericAlloc(type_obj, 0); |
| 68 |
base->pool = PyUpb_DescriptorPool_Get(upb_FileDef_Pool(file)); |
| 69 |
base->def = def; |
| 70 |
base->options = NULL; |
| 71 |
|
| 72 |
PyUpb_ObjCache_Add(def, &base->ob_base); |
| 73 |
return base; |
| 74 |
} |
| 75 |
|
| 76 |
// Returns a Python object wrapping |def|, of descriptor type |type|. If a |
| 77 |
// wrapper was previously created for this def, returns it, otherwise creates a |
| 78 |
// new wrapper. |
| 79 |
static PyObject* PyUpb_DescriptorBase_Get(PyUpb_DescriptorType type, |
| 80 |
const void* def, |
| 81 |
const upb_FileDef* file) { |
| 82 |
PyUpb_DescriptorBase* base = (PyUpb_DescriptorBase*)PyUpb_ObjCache_Get(def); |
| 83 |
|
| 84 |
if (!base) { |
| 85 |
base = PyUpb_DescriptorBase_DoCreate(type, def, file); |
| 86 |
} |
| 87 |
|
| 88 |
return &base->ob_base; |
| 89 |
} |
| 90 |
|
| 91 |
static PyUpb_DescriptorBase* PyUpb_DescriptorBase_Check( |
| 92 |
PyObject* obj, PyUpb_DescriptorType type) { |
| 93 |
PyUpb_ModuleState* state = PyUpb_ModuleState_Get(); |
| 94 |
PyTypeObject* type_obj = state->descriptor_types[type]; |
| 95 |
if (!PyObject_TypeCheck(obj, type_obj)) { |
| 96 |
PyErr_Format(PyExc_TypeError, "Expected object of type %S, but got %R", |
| 97 |
type_obj, obj); |
| 98 |
return NULL; |
| 99 |
} |
| 100 |
return (PyUpb_DescriptorBase*)obj; |
| 101 |
} |
| 102 |
|
| 103 |
static PyObject* PyUpb_DescriptorBase_GetOptions(PyUpb_DescriptorBase* self, |
| 104 |
const upb_Message* opts, |
| 105 |
const upb_MiniTable* layout, |
| 106 |
const char* msg_name) { |
| 107 |
if (!self->options) { |
| 108 |
// Load descriptors protos if they are not loaded already. We have to do |
| 109 |
// this lazily, otherwise, it would lead to circular imports. |
| 110 |
PyObject* mod = PyImport_ImportModule(PYUPB_DESCRIPTOR_MODULE); |
| 111 |
Py_DECREF(mod); |
| 112 |
|
| 113 |
// Find the correct options message. |
| 114 |
PyObject* default_pool = PyUpb_DescriptorPool_GetDefaultPool(); |
| 115 |
const upb_DefPool* symtab = PyUpb_DescriptorPool_GetSymtab(default_pool); |
| 116 |
const upb_MessageDef* m = upb_DefPool_FindMessageByName(symtab, msg_name); |
| 117 |
assert(m); |
| 118 |
|
| 119 |
// Copy the options message from C to Python using serialize+parse. |
| 120 |
// We don't wrap the C object directly because there is no guarantee that |
| 121 |
// the descriptor_pb2 that was loaded at runtime has the same members or |
| 122 |
// layout as the C types that were compiled in. |
| 123 |
size_t size; |
| 124 |
PyObject* py_arena = PyUpb_Arena_New(); |
| 125 |
upb_Arena* arena = PyUpb_Arena_Get(py_arena); |
| 126 |
char* pb; |
| 127 |
// TODO(b/235839510): Need to correctly handle failed return codes. |
| 128 |
(void)upb_Encode(opts, layout, 0, arena, &pb, &size); |
| 129 |
upb_Message* opts2 = upb_Message_New(m, arena); |
| 130 |
assert(opts2); |
| 131 |
upb_DecodeStatus ds = |
| 132 |
upb_Decode(pb, size, opts2, upb_MessageDef_MiniTable(m), |
| 133 |
upb_DefPool_ExtensionRegistry(symtab), 0, arena); |
| 134 |
(void)ds; |
| 135 |
assert(ds == kUpb_DecodeStatus_Ok); |
| 136 |
|
| 137 |
self->options = PyUpb_Message_Get(opts2, m, py_arena); |
| 138 |
Py_DECREF(py_arena); |
| 139 |
} |
| 140 |
|
| 141 |
Py_INCREF(self->options); |
| 142 |
return self->options; |
| 143 |
} |
| 144 |
|
| 145 |
typedef void* PyUpb_ToProto_Func(const void* def, upb_Arena* arena); |
| 146 |
|
| 147 |
static PyObject* PyUpb_DescriptorBase_GetSerializedProto( |
| 148 |
PyObject* _self, PyUpb_ToProto_Func* func, const upb_MiniTable* layout) { |
| 149 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 150 |
upb_Arena* arena = upb_Arena_New(); |
| 151 |
if (!arena) PYUPB_RETURN_OOM; |
| 152 |
upb_Message* proto = func(self->def, arena); |
| 153 |
if (!proto) goto oom; |
| 154 |
size_t size; |
| 155 |
char* pb; |
| 156 |
upb_EncodeStatus status = upb_Encode(proto, layout, 0, arena, &pb, &size); |
| 157 |
if (status) goto oom; // TODO(b/235839510) non-oom errors are possible here |
| 158 |
PyObject* str = PyBytes_FromStringAndSize(pb, size); |
| 159 |
upb_Arena_Free(arena); |
| 160 |
return str; |
| 161 |
|
| 162 |
oom: |
| 163 |
upb_Arena_Free(arena); |
| 164 |
PyErr_SetNone(PyExc_MemoryError); |
| 165 |
return NULL; |
| 166 |
} |
| 167 |
|
| 168 |
static PyObject* PyUpb_DescriptorBase_CopyToProto(PyObject* _self, |
| 169 |
PyUpb_ToProto_Func* func, |
| 170 |
const upb_MiniTable* layout, |
| 171 |
const char* expected_type, |
| 172 |
PyObject* py_proto) { |
| 173 |
if (!PyUpb_Message_Verify(py_proto)) return NULL; |
| 174 |
const upb_MessageDef* m = PyUpb_Message_GetMsgdef(py_proto); |
| 175 |
const char* type = upb_MessageDef_FullName(m); |
| 176 |
if (strcmp(type, expected_type) != 0) { |
| 177 |
PyErr_Format( |
| 178 |
PyExc_TypeError, |
| 179 |
"CopyToProto: message is of incorrect type '%s' (expected '%s'", type, |
| 180 |
expected_type); |
| 181 |
return NULL; |
| 182 |
} |
| 183 |
PyObject* serialized = |
| 184 |
PyUpb_DescriptorBase_GetSerializedProto(_self, func, layout); |
| 185 |
if (!serialized) return NULL; |
| 186 |
PyObject* ret = PyUpb_Message_MergeFromString(py_proto, serialized); |
| 187 |
Py_DECREF(serialized); |
| 188 |
return ret; |
| 189 |
} |
| 190 |
|
| 191 |
static void PyUpb_DescriptorBase_Dealloc(PyUpb_DescriptorBase* base) { |
| 192 |
PyUpb_ObjCache_Delete(base->def); |
| 193 |
Py_DECREF(base->pool); |
| 194 |
Py_XDECREF(base->options); |
| 195 |
PyUpb_Dealloc(base); |
| 196 |
} |
| 197 |
|
| 198 |
#define DESCRIPTOR_BASE_SLOTS \ |
| 199 |
{Py_tp_new, (void*)&PyUpb_Forbidden_New}, { \ |
| 200 |
Py_tp_dealloc, (void*)&PyUpb_DescriptorBase_Dealloc \ |
| 201 |
} |
| 202 |
|
| 203 |
// ----------------------------------------------------------------------------- |
| 204 |
// Descriptor |
| 205 |
// ----------------------------------------------------------------------------- |
| 206 |
|
| 207 |
PyObject* PyUpb_Descriptor_Get(const upb_MessageDef* m) { |
| 208 |
assert(m); |
| 209 |
const upb_FileDef* file = upb_MessageDef_File(m); |
| 210 |
return PyUpb_DescriptorBase_Get(kPyUpb_Descriptor, m, file); |
| 211 |
} |
| 212 |
|
| 213 |
PyObject* PyUpb_Descriptor_GetClass(const upb_MessageDef* m) { |
| 214 |
PyObject* ret = PyUpb_ObjCache_Get(upb_MessageDef_MiniTable(m)); |
| 215 |
assert(ret); |
| 216 |
return ret; |
| 217 |
} |
| 218 |
|
| 219 |
// The LookupNested*() functions provide name lookup for entities nested inside |
| 220 |
// a message. This uses the symtab's table, which requires that the symtab is |
| 221 |
// not being mutated concurrently. We can guarantee this for Python-owned |
| 222 |
// symtabs, but upb cannot guarantee it in general for an arbitrary |
| 223 |
// `const upb_MessageDef*`. |
| 224 |
|
| 225 |
static const void* PyUpb_Descriptor_LookupNestedMessage(const upb_MessageDef* m, |
| 226 |
const char* name) { |
| 227 |
const upb_FileDef* filedef = upb_MessageDef_File(m); |
| 228 |
const upb_DefPool* symtab = upb_FileDef_Pool(filedef); |
| 229 |
PyObject* qname = |
| 230 |
PyUnicode_FromFormat("%s.%s", upb_MessageDef_FullName(m), name); |
| 231 |
const upb_MessageDef* ret = upb_DefPool_FindMessageByName( |
| 232 |
symtab, PyUnicode_AsUTF8AndSize(qname, NULL)); |
| 233 |
Py_DECREF(qname); |
| 234 |
return ret; |
| 235 |
} |
| 236 |
|
| 237 |
static const void* PyUpb_Descriptor_LookupNestedEnum(const upb_MessageDef* m, |
| 238 |
const char* name) { |
| 239 |
const upb_FileDef* filedef = upb_MessageDef_File(m); |
| 240 |
const upb_DefPool* symtab = upb_FileDef_Pool(filedef); |
| 241 |
PyObject* qname = |
| 242 |
PyUnicode_FromFormat("%s.%s", upb_MessageDef_FullName(m), name); |
| 243 |
const upb_EnumDef* ret = |
| 244 |
upb_DefPool_FindEnumByName(symtab, PyUnicode_AsUTF8AndSize(qname, NULL)); |
| 245 |
Py_DECREF(qname); |
| 246 |
return ret; |
| 247 |
} |
| 248 |
|
| 249 |
static const void* PyUpb_Descriptor_LookupNestedExtension( |
| 250 |
const upb_MessageDef* m, const char* name) { |
| 251 |
const upb_FileDef* filedef = upb_MessageDef_File(m); |
| 252 |
const upb_DefPool* symtab = upb_FileDef_Pool(filedef); |
| 253 |
PyObject* qname = |
| 254 |
PyUnicode_FromFormat("%s.%s", upb_MessageDef_FullName(m), name); |
| 255 |
const upb_FieldDef* ret = upb_DefPool_FindExtensionByName( |
| 256 |
symtab, PyUnicode_AsUTF8AndSize(qname, NULL)); |
| 257 |
Py_DECREF(qname); |
| 258 |
return ret; |
| 259 |
} |
| 260 |
|
| 261 |
static PyObject* PyUpb_Descriptor_GetExtensionRanges(PyObject* _self, |
| 262 |
void* closure) { |
| 263 |
PyUpb_DescriptorBase* self = (PyUpb_DescriptorBase*)_self; |
| 264 |
int n = upb_MessageDef_ExtensionRangeCount(self->def); |
| 265 |
PyObject* range_list = PyList_New(n); |
| 266 |
|
| 267 |
for (int i = 0; i < n; i++) { |
| 268 |
const upb_ExtensionRange* range = |
| 269 |
upb_MessageDef_ExtensionRange(self->def, i); |
| 270 |
PyObject* start = PyLong_FromLong(upb_ExtensionRange_Start(range)); |
| 271 |
PyObject* end = PyLong_FromLong(upb_ExtensionRange_End(range)); |
| 272 |
PyList_SetItem(range_list, i, PyTuple_Pack(2, start, end)); |
| 273 |
} |
| 274 |
|
| 275 |
return range_list; |
| 276 |
} |
| 277 |
|
| 278 |
static PyObject* PyUpb_Descriptor_GetExtensions(PyObject* _self, |
| 279 |
void* closure) { |
| 280 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 281 |
static PyUpb_GenericSequence_Funcs funcs = { |
| 282 |
(void*)&upb_MessageDef_NestedExtensionCount, |
| 283 |
(void*)&upb_MessageDef_NestedExtension, |
| 284 |
(void*)&PyUpb_FieldDescriptor_Get, |
| 285 |
}; |
| 286 |
return PyUpb_GenericSequence_New(&funcs, self->def, self->pool); |
| 287 |
} |
| 288 |
|
| 289 |
static PyObject* PyUpb_Descriptor_GetExtensionsByName(PyObject* _self, |
| 290 |
void* closure) { |
| 291 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 292 |
static PyUpb_ByNameMap_Funcs funcs = { |
| 293 |
{ |
| 294 |
(void*)&upb_MessageDef_NestedExtensionCount, |
| 295 |
(void*)&upb_MessageDef_NestedExtension, |
| 296 |
(void*)&PyUpb_FieldDescriptor_Get, |
| 297 |
}, |
| 298 |
(void*)&PyUpb_Descriptor_LookupNestedExtension, |
| 299 |
(void*)&upb_FieldDef_Name, |
| 300 |
}; |
| 301 |
return PyUpb_ByNameMap_New(&funcs, self->def, self->pool); |
| 302 |
} |
| 303 |
|
| 304 |
static PyObject* PyUpb_Descriptor_GetEnumTypes(PyObject* _self, void* closure) { |
| 305 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 306 |
static PyUpb_GenericSequence_Funcs funcs = { |
| 307 |
(void*)&upb_MessageDef_NestedEnumCount, |
| 308 |
(void*)&upb_MessageDef_NestedEnum, |
| 309 |
(void*)&PyUpb_EnumDescriptor_Get, |
| 310 |
}; |
| 311 |
return PyUpb_GenericSequence_New(&funcs, self->def, self->pool); |
| 312 |
} |
| 313 |
|
| 314 |
static PyObject* PyUpb_Descriptor_GetOneofs(PyObject* _self, void* closure) { |
| 315 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 316 |
static PyUpb_GenericSequence_Funcs funcs = { |
| 317 |
(void*)&upb_MessageDef_OneofCount, |
| 318 |
(void*)&upb_MessageDef_Oneof, |
| 319 |
(void*)&PyUpb_OneofDescriptor_Get, |
| 320 |
}; |
| 321 |
return PyUpb_GenericSequence_New(&funcs, self->def, self->pool); |
| 322 |
} |
| 323 |
|
| 324 |
static PyObject* PyUpb_Descriptor_GetOptions(PyObject* _self, PyObject* args) { |
| 325 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 326 |
return PyUpb_DescriptorBase_GetOptions( |
| 327 |
self, upb_MessageDef_Options(self->def), |
| 328 |
&google_protobuf_MessageOptions_msginit, |
| 329 |
PYUPB_DESCRIPTOR_PROTO_PACKAGE ".MessageOptions"); |
| 330 |
} |
| 331 |
|
| 332 |
static PyObject* PyUpb_Descriptor_CopyToProto(PyObject* _self, |
| 333 |
PyObject* py_proto) { |
| 334 |
return PyUpb_DescriptorBase_CopyToProto( |
| 335 |
_self, (PyUpb_ToProto_Func*)&upb_MessageDef_ToProto, |
| 336 |
&google_protobuf_DescriptorProto_msginit, |
| 337 |
PYUPB_DESCRIPTOR_PROTO_PACKAGE ".DescriptorProto", py_proto); |
| 338 |
} |
| 339 |
|
| 340 |
static PyObject* PyUpb_Descriptor_EnumValueName(PyObject* _self, |
| 341 |
PyObject* args) { |
| 342 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 343 |
const char* enum_name; |
| 344 |
int number; |
| 345 |
if (!PyArg_ParseTuple(args, "si", &enum_name, &number)) return NULL; |
| 346 |
const upb_EnumDef* e = |
| 347 |
PyUpb_Descriptor_LookupNestedEnum(self->def, enum_name); |
| 348 |
if (!e) { |
| 349 |
PyErr_SetString(PyExc_KeyError, enum_name); |
| 350 |
return NULL; |
| 351 |
} |
| 352 |
const upb_EnumValueDef* ev = upb_EnumDef_FindValueByNumber(e, number); |
| 353 |
if (!ev) { |
| 354 |
PyErr_Format(PyExc_KeyError, "%d", number); |
| 355 |
return NULL; |
| 356 |
} |
| 357 |
return PyUnicode_FromString(upb_EnumValueDef_Name(ev)); |
| 358 |
} |
| 359 |
|
| 360 |
static PyObject* PyUpb_Descriptor_GetFieldsByName(PyObject* _self, |
| 361 |
void* closure) { |
| 362 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 363 |
static PyUpb_ByNameMap_Funcs funcs = { |
| 364 |
{ |
| 365 |
(void*)&upb_MessageDef_FieldCount, |
| 366 |
(void*)&upb_MessageDef_Field, |
| 367 |
(void*)&PyUpb_FieldDescriptor_Get, |
| 368 |
}, |
| 369 |
(void*)&upb_MessageDef_FindFieldByName, |
| 370 |
(void*)&upb_FieldDef_Name, |
| 371 |
}; |
| 372 |
return PyUpb_ByNameMap_New(&funcs, self->def, self->pool); |
| 373 |
} |
| 374 |
|
| 375 |
static PyObject* PyUpb_Descriptor_GetFieldsByCamelCaseName(PyObject* _self, |
| 376 |
void* closure) { |
| 377 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 378 |
static PyUpb_ByNameMap_Funcs funcs = { |
| 379 |
{ |
| 380 |
(void*)&upb_MessageDef_FieldCount, |
| 381 |
(void*)&upb_MessageDef_Field, |
| 382 |
(void*)&PyUpb_FieldDescriptor_Get, |
| 383 |
}, |
| 384 |
(void*)&upb_MessageDef_FindByJsonName, |
| 385 |
(void*)&upb_FieldDef_JsonName, |
| 386 |
}; |
| 387 |
return PyUpb_ByNameMap_New(&funcs, self->def, self->pool); |
| 388 |
} |
| 389 |
|
| 390 |
static PyObject* PyUpb_Descriptor_GetFieldsByNumber(PyObject* _self, |
| 391 |
void* closure) { |
| 392 |
static PyUpb_ByNumberMap_Funcs funcs = { |
| 393 |
{ |
| 394 |
(void*)&upb_MessageDef_FieldCount, |
| 395 |
(void*)&upb_MessageDef_Field, |
| 396 |
(void*)&PyUpb_FieldDescriptor_Get, |
| 397 |
}, |
| 398 |
(void*)&upb_MessageDef_FindFieldByNumber, |
| 399 |
(void*)&upb_FieldDef_Number, |
| 400 |
}; |
| 401 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 402 |
return PyUpb_ByNumberMap_New(&funcs, self->def, self->pool); |
| 403 |
} |
| 404 |
|
| 405 |
static PyObject* PyUpb_Descriptor_GetNestedTypes(PyObject* _self, |
| 406 |
void* closure) { |
| 407 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 408 |
static PyUpb_GenericSequence_Funcs funcs = { |
| 409 |
(void*)&upb_MessageDef_NestedMessageCount, |
| 410 |
(void*)&upb_MessageDef_NestedMessage, |
| 411 |
(void*)&PyUpb_Descriptor_Get, |
| 412 |
}; |
| 413 |
return PyUpb_GenericSequence_New(&funcs, self->def, self->pool); |
| 414 |
} |
| 415 |
|
| 416 |
static PyObject* PyUpb_Descriptor_GetNestedTypesByName(PyObject* _self, |
| 417 |
void* closure) { |
| 418 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 419 |
static PyUpb_ByNameMap_Funcs funcs = { |
| 420 |
{ |
| 421 |
(void*)&upb_MessageDef_NestedMessageCount, |
| 422 |
(void*)&upb_MessageDef_NestedMessage, |
| 423 |
(void*)&PyUpb_Descriptor_Get, |
| 424 |
}, |
| 425 |
(void*)&PyUpb_Descriptor_LookupNestedMessage, |
| 426 |
(void*)&upb_MessageDef_Name, |
| 427 |
}; |
| 428 |
return PyUpb_ByNameMap_New(&funcs, self->def, self->pool); |
| 429 |
} |
| 430 |
|
| 431 |
static PyObject* PyUpb_Descriptor_GetContainingType(PyObject* _self, |
| 432 |
void* closure) { |
| 433 |
// upb does not natively store the lexical parent of a message type, but we |
| 434 |
// can derive it with some string manipulation and a lookup. |
| 435 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 436 |
const upb_MessageDef* m = self->def; |
| 437 |
const upb_FileDef* file = upb_MessageDef_File(m); |
| 438 |
const upb_DefPool* symtab = upb_FileDef_Pool(file); |
| 439 |
const char* full_name = upb_MessageDef_FullName(m); |
| 440 |
const char* last_dot = strrchr(full_name, '.'); |
| 441 |
if (!last_dot) Py_RETURN_NONE; |
| 442 |
const upb_MessageDef* parent = upb_DefPool_FindMessageByNameWithSize( |
| 443 |
symtab, full_name, last_dot - full_name); |
| 444 |
if (!parent) Py_RETURN_NONE; |
| 445 |
return PyUpb_Descriptor_Get(parent); |
| 446 |
} |
| 447 |
|
| 448 |
static PyObject* PyUpb_Descriptor_GetEnumTypesByName(PyObject* _self, |
| 449 |
void* closure) { |
| 450 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 451 |
static PyUpb_ByNameMap_Funcs funcs = { |
| 452 |
{ |
| 453 |
(void*)&upb_MessageDef_NestedEnumCount, |
| 454 |
(void*)&upb_MessageDef_NestedEnum, |
| 455 |
(void*)&PyUpb_EnumDescriptor_Get, |
| 456 |
}, |
| 457 |
(void*)&PyUpb_Descriptor_LookupNestedEnum, |
| 458 |
(void*)&upb_EnumDef_Name, |
| 459 |
}; |
| 460 |
return PyUpb_ByNameMap_New(&funcs, self->def, self->pool); |
| 461 |
} |
| 462 |
|
| 463 |
static PyObject* PyUpb_Descriptor_GetIsExtendable(PyObject* _self, |
| 464 |
void* closure) { |
| 465 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 466 |
if (upb_MessageDef_ExtensionRangeCount(self->def) > 0) { |
| 467 |
Py_RETURN_TRUE; |
| 468 |
} else { |
| 469 |
Py_RETURN_FALSE; |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
static PyObject* PyUpb_Descriptor_GetFullName(PyObject* self, void* closure) { |
| 474 |
const upb_MessageDef* msgdef = PyUpb_Descriptor_GetDef(self); |
| 475 |
return PyUnicode_FromString(upb_MessageDef_FullName(msgdef)); |
| 476 |
} |
| 477 |
|
| 478 |
static PyObject* PyUpb_Descriptor_GetConcreteClass(PyObject* self, |
| 479 |
void* closure) { |
| 480 |
const upb_MessageDef* msgdef = PyUpb_Descriptor_GetDef(self); |
| 481 |
return PyUpb_Descriptor_GetClass(msgdef); |
| 482 |
} |
| 483 |
|
| 484 |
static PyObject* PyUpb_Descriptor_GetFile(PyObject* self, void* closure) { |
| 485 |
const upb_MessageDef* msgdef = PyUpb_Descriptor_GetDef(self); |
| 486 |
return PyUpb_FileDescriptor_Get(upb_MessageDef_File(msgdef)); |
| 487 |
} |
| 488 |
|
| 489 |
static PyObject* PyUpb_Descriptor_GetFields(PyObject* _self, void* closure) { |
| 490 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 491 |
static PyUpb_GenericSequence_Funcs funcs = { |
| 492 |
(void*)&upb_MessageDef_FieldCount, |
| 493 |
(void*)&upb_MessageDef_Field, |
| 494 |
(void*)&PyUpb_FieldDescriptor_Get, |
| 495 |
}; |
| 496 |
return PyUpb_GenericSequence_New(&funcs, self->def, self->pool); |
| 497 |
} |
| 498 |
|
| 499 |
static PyObject* PyUpb_Descriptor_GetHasOptions(PyObject* _self, |
| 500 |
void* closure) { |
| 501 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 502 |
return PyBool_FromLong(upb_MessageDef_HasOptions(self->def)); |
| 503 |
} |
| 504 |
|
| 505 |
static PyObject* PyUpb_Descriptor_GetName(PyObject* self, void* closure) { |
| 506 |
const upb_MessageDef* msgdef = PyUpb_Descriptor_GetDef(self); |
| 507 |
return PyUnicode_FromString(upb_MessageDef_Name(msgdef)); |
| 508 |
} |
| 509 |
|
| 510 |
static PyObject* PyUpb_Descriptor_GetEnumValuesByName(PyObject* _self, |
| 511 |
void* closure) { |
| 512 |
// upb does not natively store any table containing all nested values. |
| 513 |
// Consider: |
| 514 |
// message M { |
| 515 |
// enum E1 { |
| 516 |
// A = 0; |
| 517 |
// B = 1; |
| 518 |
// } |
| 519 |
// enum E2 { |
| 520 |
// C = 0; |
| 521 |
// D = 1; |
| 522 |
// } |
| 523 |
// } |
| 524 |
// |
| 525 |
// In this case, upb stores tables for E1 and E2, but it does not store a |
| 526 |
// table for M that combines them (it is rarely needed and costs precious |
| 527 |
// space and time to build). |
| 528 |
// |
| 529 |
// To work around this, we build an actual Python dict whenever a user |
| 530 |
// actually asks for this. |
| 531 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 532 |
PyObject* ret = PyDict_New(); |
| 533 |
if (!ret) return NULL; |
| 534 |
int enum_count = upb_MessageDef_NestedEnumCount(self->def); |
| 535 |
for (int i = 0; i < enum_count; i++) { |
| 536 |
const upb_EnumDef* e = upb_MessageDef_NestedEnum(self->def, i); |
| 537 |
int value_count = upb_EnumDef_ValueCount(e); |
| 538 |
for (int j = 0; j < value_count; j++) { |
| 539 |
// Collisions should be impossible here, as uniqueness is checked by |
| 540 |
// protoc (this is an invariant of the protobuf language). However this |
| 541 |
// uniqueness constraint is not currently checked by upb/def.c at load |
| 542 |
// time, so if the user supplies a manually-constructed descriptor that |
| 543 |
// does not respect this constraint, a collision could be possible and the |
| 544 |
// last-defined enumerator would win. This could be seen as an argument |
| 545 |
// for having upb actually build the table at load time, thus checking the |
| 546 |
// constraint proactively, but upb is always checking a subset of the full |
| 547 |
// validation performed by C++, and we have to pick and choose the biggest |
| 548 |
// bang for the buck. |
| 549 |
const upb_EnumValueDef* ev = upb_EnumDef_Value(e, j); |
| 550 |
const char* name = upb_EnumValueDef_Name(ev); |
| 551 |
PyObject* val = PyUpb_EnumValueDescriptor_Get(ev); |
| 552 |
if (!val || PyDict_SetItemString(ret, name, val) < 0) { |
| 553 |
Py_XDECREF(val); |
| 554 |
Py_DECREF(ret); |
| 555 |
return NULL; |
| 556 |
} |
| 557 |
Py_DECREF(val); |
| 558 |
} |
| 559 |
} |
| 560 |
return ret; |
| 561 |
} |
| 562 |
|
| 563 |
static PyObject* PyUpb_Descriptor_GetOneofsByName(PyObject* _self, |
| 564 |
void* closure) { |
| 565 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 566 |
static PyUpb_ByNameMap_Funcs funcs = { |
| 567 |
{ |
| 568 |
(void*)&upb_MessageDef_OneofCount, |
| 569 |
(void*)&upb_MessageDef_Oneof, |
| 570 |
(void*)&PyUpb_OneofDescriptor_Get, |
| 571 |
}, |
| 572 |
(void*)&upb_MessageDef_FindOneofByName, |
| 573 |
(void*)&upb_OneofDef_Name, |
| 574 |
}; |
| 575 |
return PyUpb_ByNameMap_New(&funcs, self->def, self->pool); |
| 576 |
} |
| 577 |
|
| 578 |
static PyObject* PyUpb_Descriptor_GetSyntax(PyObject* self, void* closure) { |
| 579 |
const upb_MessageDef* msgdef = PyUpb_Descriptor_GetDef(self); |
| 580 |
const char* syntax = |
| 581 |
upb_MessageDef_Syntax(msgdef) == kUpb_Syntax_Proto2 ? "proto2" : "proto3"; |
| 582 |
return PyUnicode_InternFromString(syntax); |
| 583 |
} |
| 584 |
|
| 585 |
static PyGetSetDef PyUpb_Descriptor_Getters[] = { |
| 586 |
{"name", PyUpb_Descriptor_GetName, NULL, "Last name"}, |
| 587 |
{"full_name", PyUpb_Descriptor_GetFullName, NULL, "Full name"}, |
| 588 |
{"_concrete_class", PyUpb_Descriptor_GetConcreteClass, NULL, |
| 589 |
"concrete class"}, |
| 590 |
{"file", PyUpb_Descriptor_GetFile, NULL, "File descriptor"}, |
| 591 |
{"fields", PyUpb_Descriptor_GetFields, NULL, "Fields sequence"}, |
| 592 |
{"fields_by_name", PyUpb_Descriptor_GetFieldsByName, NULL, |
| 593 |
"Fields by name"}, |
| 594 |
{"fields_by_camelcase_name", PyUpb_Descriptor_GetFieldsByCamelCaseName, |
| 595 |
NULL, "Fields by camelCase name"}, |
| 596 |
{"fields_by_number", PyUpb_Descriptor_GetFieldsByNumber, NULL, |
| 597 |
"Fields by number"}, |
| 598 |
{"nested_types", PyUpb_Descriptor_GetNestedTypes, NULL, |
| 599 |
"Nested types sequence"}, |
| 600 |
{"nested_types_by_name", PyUpb_Descriptor_GetNestedTypesByName, NULL, |
| 601 |
"Nested types by name"}, |
| 602 |
{"extensions", PyUpb_Descriptor_GetExtensions, NULL, "Extensions Sequence"}, |
| 603 |
{"extensions_by_name", PyUpb_Descriptor_GetExtensionsByName, NULL, |
| 604 |
"Extensions by name"}, |
| 605 |
{"extension_ranges", PyUpb_Descriptor_GetExtensionRanges, NULL, |
| 606 |
"Extension ranges"}, |
| 607 |
{"enum_types", PyUpb_Descriptor_GetEnumTypes, NULL, "Enum sequence"}, |
| 608 |
{"enum_types_by_name", PyUpb_Descriptor_GetEnumTypesByName, NULL, |
| 609 |
"Enum types by name"}, |
| 610 |
{"enum_values_by_name", PyUpb_Descriptor_GetEnumValuesByName, NULL, |
| 611 |
"Enum values by name"}, |
| 612 |
{"oneofs_by_name", PyUpb_Descriptor_GetOneofsByName, NULL, |
| 613 |
"Oneofs by name"}, |
| 614 |
{"oneofs", PyUpb_Descriptor_GetOneofs, NULL, "Oneofs Sequence"}, |
| 615 |
{"containing_type", PyUpb_Descriptor_GetContainingType, NULL, |
| 616 |
"Containing type"}, |
| 617 |
{"is_extendable", PyUpb_Descriptor_GetIsExtendable, NULL}, |
| 618 |
{"has_options", PyUpb_Descriptor_GetHasOptions, NULL, "Has Options"}, |
| 619 |
{"syntax", &PyUpb_Descriptor_GetSyntax, NULL, "Syntax"}, |
| 620 |
{NULL}}; |
| 621 |
|
| 622 |
static PyMethodDef PyUpb_Descriptor_Methods[] = { |
| 623 |
{"GetOptions", PyUpb_Descriptor_GetOptions, METH_NOARGS}, |
| 624 |
{"CopyToProto", PyUpb_Descriptor_CopyToProto, METH_O}, |
| 625 |
{"EnumValueName", PyUpb_Descriptor_EnumValueName, METH_VARARGS}, |
| 626 |
{NULL}}; |
| 627 |
|
| 628 |
static PyType_Slot PyUpb_Descriptor_Slots[] = { |
| 629 |
DESCRIPTOR_BASE_SLOTS, |
| 630 |
{Py_tp_methods, PyUpb_Descriptor_Methods}, |
| 631 |
{Py_tp_getset, PyUpb_Descriptor_Getters}, |
| 632 |
{0, NULL}}; |
| 633 |
|
| 634 |
static PyType_Spec PyUpb_Descriptor_Spec = { |
| 635 |
PYUPB_MODULE_NAME ".Descriptor", // tp_name |
| 636 |
sizeof(PyUpb_DescriptorBase), // tp_basicsize |
| 637 |
0, // tp_itemsize |
| 638 |
Py_TPFLAGS_DEFAULT, // tp_flags |
| 639 |
PyUpb_Descriptor_Slots, |
| 640 |
}; |
| 641 |
|
| 642 |
const upb_MessageDef* PyUpb_Descriptor_GetDef(PyObject* _self) { |
| 643 |
PyUpb_DescriptorBase* self = |
| 644 |
PyUpb_DescriptorBase_Check(_self, kPyUpb_Descriptor); |
| 645 |
return self ? self->def : NULL; |
| 646 |
} |
| 647 |
|
| 648 |
// ----------------------------------------------------------------------------- |
| 649 |
// EnumDescriptor |
| 650 |
// ----------------------------------------------------------------------------- |
| 651 |
|
| 652 |
PyObject* PyUpb_EnumDescriptor_Get(const upb_EnumDef* enumdef) { |
| 653 |
const upb_FileDef* file = upb_EnumDef_File(enumdef); |
| 654 |
return PyUpb_DescriptorBase_Get(kPyUpb_EnumDescriptor, enumdef, file); |
| 655 |
} |
| 656 |
|
| 657 |
const upb_EnumDef* PyUpb_EnumDescriptor_GetDef(PyObject* _self) { |
| 658 |
PyUpb_DescriptorBase* self = |
| 659 |
PyUpb_DescriptorBase_Check(_self, kPyUpb_EnumDescriptor); |
| 660 |
return self ? self->def : NULL; |
| 661 |
} |
| 662 |
|
| 663 |
static PyObject* PyUpb_EnumDescriptor_GetFullName(PyObject* self, |
| 664 |
void* closure) { |
| 665 |
const upb_EnumDef* enumdef = PyUpb_EnumDescriptor_GetDef(self); |
| 666 |
return PyUnicode_FromString(upb_EnumDef_FullName(enumdef)); |
| 667 |
} |
| 668 |
|
| 669 |
static PyObject* PyUpb_EnumDescriptor_GetName(PyObject* self, void* closure) { |
| 670 |
const upb_EnumDef* enumdef = PyUpb_EnumDescriptor_GetDef(self); |
| 671 |
return PyUnicode_FromString(upb_EnumDef_Name(enumdef)); |
| 672 |
} |
| 673 |
|
| 674 |
static PyObject* PyUpb_EnumDescriptor_GetFile(PyObject* self, void* closure) { |
| 675 |
const upb_EnumDef* enumdef = PyUpb_EnumDescriptor_GetDef(self); |
| 676 |
return PyUpb_FileDescriptor_Get(upb_EnumDef_File(enumdef)); |
| 677 |
} |
| 678 |
|
| 679 |
static PyObject* PyUpb_EnumDescriptor_GetValues(PyObject* _self, |
| 680 |
void* closure) { |
| 681 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 682 |
static PyUpb_GenericSequence_Funcs funcs = { |
| 683 |
(void*)&upb_EnumDef_ValueCount, |
| 684 |
(void*)&upb_EnumDef_Value, |
| 685 |
(void*)&PyUpb_EnumValueDescriptor_Get, |
| 686 |
}; |
| 687 |
return PyUpb_GenericSequence_New(&funcs, self->def, self->pool); |
| 688 |
} |
| 689 |
|
| 690 |
static PyObject* PyUpb_EnumDescriptor_GetValuesByName(PyObject* _self, |
| 691 |
void* closure) { |
| 692 |
static PyUpb_ByNameMap_Funcs funcs = { |
| 693 |
{ |
| 694 |
(void*)&upb_EnumDef_ValueCount, |
| 695 |
(void*)&upb_EnumDef_Value, |
| 696 |
(void*)&PyUpb_EnumValueDescriptor_Get, |
| 697 |
}, |
| 698 |
(void*)&upb_EnumDef_FindValueByName, |
| 699 |
(void*)&upb_EnumValueDef_Name, |
| 700 |
}; |
| 701 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 702 |
return PyUpb_ByNameMap_New(&funcs, self->def, self->pool); |
| 703 |
} |
| 704 |
|
| 705 |
static PyObject* PyUpb_EnumDescriptor_GetValuesByNumber(PyObject* _self, |
| 706 |
void* closure) { |
| 707 |
static PyUpb_ByNumberMap_Funcs funcs = { |
| 708 |
{ |
| 709 |
(void*)&upb_EnumDef_ValueCount, |
| 710 |
(void*)&upb_EnumDef_Value, |
| 711 |
(void*)&PyUpb_EnumValueDescriptor_Get, |
| 712 |
}, |
| 713 |
(void*)&upb_EnumDef_FindValueByNumber, |
| 714 |
(void*)&upb_EnumValueDef_Number, |
| 715 |
}; |
| 716 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 717 |
return PyUpb_ByNumberMap_New(&funcs, self->def, self->pool); |
| 718 |
} |
| 719 |
|
| 720 |
static PyObject* PyUpb_EnumDescriptor_GetContainingType(PyObject* _self, |
| 721 |
void* closure) { |
| 722 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 723 |
const upb_MessageDef* m = upb_EnumDef_ContainingType(self->def); |
| 724 |
if (!m) Py_RETURN_NONE; |
| 725 |
return PyUpb_Descriptor_Get(m); |
| 726 |
} |
| 727 |
|
| 728 |
static PyObject* PyUpb_EnumDescriptor_GetHasOptions(PyObject* _self, |
| 729 |
void* closure) { |
| 730 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 731 |
return PyBool_FromLong(upb_EnumDef_HasOptions(self->def)); |
| 732 |
} |
| 733 |
|
| 734 |
static PyObject* PyUpb_EnumDescriptor_GetOptions(PyObject* _self, |
| 735 |
PyObject* args) { |
| 736 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 737 |
return PyUpb_DescriptorBase_GetOptions(self, upb_EnumDef_Options(self->def), |
| 738 |
&google_protobuf_EnumOptions_msginit, |
| 739 |
PYUPB_DESCRIPTOR_PROTO_PACKAGE |
| 740 |
".EnumOptions"); |
| 741 |
} |
| 742 |
|
| 743 |
static PyObject* PyUpb_EnumDescriptor_CopyToProto(PyObject* _self, |
| 744 |
PyObject* py_proto) { |
| 745 |
return PyUpb_DescriptorBase_CopyToProto( |
| 746 |
_self, (PyUpb_ToProto_Func*)&upb_EnumDef_ToProto, |
| 747 |
&google_protobuf_EnumDescriptorProto_msginit, |
| 748 |
PYUPB_DESCRIPTOR_PROTO_PACKAGE ".EnumDescriptorProto", py_proto); |
| 749 |
} |
| 750 |
|
| 751 |
static PyGetSetDef PyUpb_EnumDescriptor_Getters[] = { |
| 752 |
{"full_name", PyUpb_EnumDescriptor_GetFullName, NULL, "Full name"}, |
| 753 |
{"name", PyUpb_EnumDescriptor_GetName, NULL, "last name"}, |
| 754 |
{"file", PyUpb_EnumDescriptor_GetFile, NULL, "File descriptor"}, |
| 755 |
{"values", PyUpb_EnumDescriptor_GetValues, NULL, "values"}, |
| 756 |
{"values_by_name", PyUpb_EnumDescriptor_GetValuesByName, NULL, |
| 757 |
"Enum values by name"}, |
| 758 |
{"values_by_number", PyUpb_EnumDescriptor_GetValuesByNumber, NULL, |
| 759 |
"Enum values by number"}, |
| 760 |
{"containing_type", PyUpb_EnumDescriptor_GetContainingType, NULL, |
| 761 |
"Containing type"}, |
| 762 |
{"has_options", PyUpb_EnumDescriptor_GetHasOptions, NULL, "Has Options"}, |
| 763 |
{NULL}}; |
| 764 |
|
| 765 |
static PyMethodDef PyUpb_EnumDescriptor_Methods[] = { |
| 766 |
{"GetOptions", PyUpb_EnumDescriptor_GetOptions, METH_NOARGS}, |
| 767 |
{"CopyToProto", PyUpb_EnumDescriptor_CopyToProto, METH_O}, |
| 768 |
{NULL}}; |
| 769 |
|
| 770 |
static PyType_Slot PyUpb_EnumDescriptor_Slots[] = { |
| 771 |
DESCRIPTOR_BASE_SLOTS, |
| 772 |
{Py_tp_methods, PyUpb_EnumDescriptor_Methods}, |
| 773 |
{Py_tp_getset, PyUpb_EnumDescriptor_Getters}, |
| 774 |
{0, NULL}}; |
| 775 |
|
| 776 |
static PyType_Spec PyUpb_EnumDescriptor_Spec = { |
| 777 |
PYUPB_MODULE_NAME ".EnumDescriptor", // tp_name |
| 778 |
sizeof(PyUpb_DescriptorBase), // tp_basicsize |
| 779 |
0, // tp_itemsize |
| 780 |
Py_TPFLAGS_DEFAULT, // tp_flags |
| 781 |
PyUpb_EnumDescriptor_Slots, |
| 782 |
}; |
| 783 |
|
| 784 |
// ----------------------------------------------------------------------------- |
| 785 |
// EnumValueDescriptor |
| 786 |
// ----------------------------------------------------------------------------- |
| 787 |
|
| 788 |
PyObject* PyUpb_EnumValueDescriptor_Get(const upb_EnumValueDef* ev) { |
| 789 |
const upb_FileDef* file = upb_EnumDef_File(upb_EnumValueDef_Enum(ev)); |
| 790 |
return PyUpb_DescriptorBase_Get(kPyUpb_EnumValueDescriptor, ev, file); |
| 791 |
} |
| 792 |
|
| 793 |
static PyObject* PyUpb_EnumValueDescriptor_GetName(PyObject* self, |
| 794 |
void* closure) { |
| 795 |
PyUpb_DescriptorBase* base = (PyUpb_DescriptorBase*)self; |
| 796 |
return PyUnicode_FromString(upb_EnumValueDef_Name(base->def)); |
| 797 |
} |
| 798 |
|
| 799 |
static PyObject* PyUpb_EnumValueDescriptor_GetNumber(PyObject* self, |
| 800 |
void* closure) { |
| 801 |
PyUpb_DescriptorBase* base = (PyUpb_DescriptorBase*)self; |
| 802 |
return PyLong_FromLong(upb_EnumValueDef_Number(base->def)); |
| 803 |
} |
| 804 |
|
| 805 |
static PyObject* PyUpb_EnumValueDescriptor_GetIndex(PyObject* self, |
| 806 |
void* closure) { |
| 807 |
PyUpb_DescriptorBase* base = (PyUpb_DescriptorBase*)self; |
| 808 |
return PyLong_FromLong(upb_EnumValueDef_Index(base->def)); |
| 809 |
} |
| 810 |
|
| 811 |
static PyObject* PyUpb_EnumValueDescriptor_GetType(PyObject* self, |
| 812 |
void* closure) { |
| 813 |
PyUpb_DescriptorBase* base = (PyUpb_DescriptorBase*)self; |
| 814 |
return PyUpb_EnumDescriptor_Get(upb_EnumValueDef_Enum(base->def)); |
| 815 |
} |
| 816 |
|
| 817 |
static PyObject* PyUpb_EnumValueDescriptor_GetHasOptions(PyObject* _self, |
| 818 |
void* closure) { |
| 819 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 820 |
return PyBool_FromLong(upb_EnumValueDef_HasOptions(self->def)); |
| 821 |
} |
| 822 |
|
| 823 |
static PyObject* PyUpb_EnumValueDescriptor_GetOptions(PyObject* _self, |
| 824 |
PyObject* args) { |
| 825 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 826 |
return PyUpb_DescriptorBase_GetOptions( |
| 827 |
self, upb_EnumValueDef_Options(self->def), |
| 828 |
&google_protobuf_EnumValueOptions_msginit, |
| 829 |
PYUPB_DESCRIPTOR_PROTO_PACKAGE ".EnumValueOptions"); |
| 830 |
} |
| 831 |
|
| 832 |
static PyGetSetDef PyUpb_EnumValueDescriptor_Getters[] = { |
| 833 |
{"name", PyUpb_EnumValueDescriptor_GetName, NULL, "name"}, |
| 834 |
{"number", PyUpb_EnumValueDescriptor_GetNumber, NULL, "number"}, |
| 835 |
{"index", PyUpb_EnumValueDescriptor_GetIndex, NULL, "index"}, |
| 836 |
{"type", PyUpb_EnumValueDescriptor_GetType, NULL, "index"}, |
| 837 |
{"has_options", PyUpb_EnumValueDescriptor_GetHasOptions, NULL, |
| 838 |
"Has Options"}, |
| 839 |
{NULL}}; |
| 840 |
|
| 841 |
static PyMethodDef PyUpb_EnumValueDescriptor_Methods[] = { |
| 842 |
{ |
| 843 |
"GetOptions", |
| 844 |
PyUpb_EnumValueDescriptor_GetOptions, |
| 845 |
METH_NOARGS, |
| 846 |
}, |
| 847 |
{NULL}}; |
| 848 |
|
| 849 |
static PyType_Slot PyUpb_EnumValueDescriptor_Slots[] = { |
| 850 |
DESCRIPTOR_BASE_SLOTS, |
| 851 |
{Py_tp_methods, PyUpb_EnumValueDescriptor_Methods}, |
| 852 |
{Py_tp_getset, PyUpb_EnumValueDescriptor_Getters}, |
| 853 |
{0, NULL}}; |
| 854 |
|
| 855 |
static PyType_Spec PyUpb_EnumValueDescriptor_Spec = { |
| 856 |
PYUPB_MODULE_NAME ".EnumValueDescriptor", // tp_name |
| 857 |
sizeof(PyUpb_DescriptorBase), // tp_basicsize |
| 858 |
0, // tp_itemsize |
| 859 |
Py_TPFLAGS_DEFAULT, // tp_flags |
| 860 |
PyUpb_EnumValueDescriptor_Slots, |
| 861 |
}; |
| 862 |
|
| 863 |
// ----------------------------------------------------------------------------- |
| 864 |
// FieldDescriptor |
| 865 |
// ----------------------------------------------------------------------------- |
| 866 |
|
| 867 |
const upb_FieldDef* PyUpb_FieldDescriptor_GetDef(PyObject* _self) { |
| 868 |
PyUpb_DescriptorBase* self = |
| 869 |
PyUpb_DescriptorBase_Check(_self, kPyUpb_FieldDescriptor); |
| 870 |
return self ? self->def : NULL; |
| 871 |
} |
| 872 |
|
| 873 |
PyObject* PyUpb_FieldDescriptor_Get(const upb_FieldDef* field) { |
| 874 |
const upb_FileDef* file = upb_FieldDef_File(field); |
| 875 |
return PyUpb_DescriptorBase_Get(kPyUpb_FieldDescriptor, field, file); |
| 876 |
} |
| 877 |
|
| 878 |
static PyObject* PyUpb_FieldDescriptor_GetFullName(PyUpb_DescriptorBase* self, |
| 879 |
void* closure) { |
| 880 |
return PyUnicode_FromString(upb_FieldDef_FullName(self->def)); |
| 881 |
} |
| 882 |
|
| 883 |
static PyObject* PyUpb_FieldDescriptor_GetName(PyUpb_DescriptorBase* self, |
| 884 |
void* closure) { |
| 885 |
return PyUnicode_FromString(upb_FieldDef_Name(self->def)); |
| 886 |
} |
| 887 |
|
| 888 |
static PyObject* PyUpb_FieldDescriptor_GetCamelCaseName( |
| 889 |
PyUpb_DescriptorBase* self, void* closure) { |
| 890 |
// TODO: Ok to use jsonname here? |
| 891 |
return PyUnicode_FromString(upb_FieldDef_JsonName(self->def)); |
| 892 |
} |
| 893 |
|
| 894 |
static PyObject* PyUpb_FieldDescriptor_GetJsonName(PyUpb_DescriptorBase* self, |
| 895 |
void* closure) { |
| 896 |
return PyUnicode_FromString(upb_FieldDef_JsonName(self->def)); |
| 897 |
} |
| 898 |
|
| 899 |
static PyObject* PyUpb_FieldDescriptor_GetFile(PyUpb_DescriptorBase* self, |
| 900 |
void* closure) { |
| 901 |
const upb_FileDef* file = upb_FieldDef_File(self->def); |
| 902 |
if (!file) Py_RETURN_NONE; |
| 903 |
return PyUpb_FileDescriptor_Get(file); |
| 904 |
} |
| 905 |
|
| 906 |
static PyObject* PyUpb_FieldDescriptor_GetType(PyUpb_DescriptorBase* self, |
| 907 |
void* closure) { |
| 908 |
return PyLong_FromLong(upb_FieldDef_Type(self->def)); |
| 909 |
} |
| 910 |
|
| 911 |
static PyObject* PyUpb_FieldDescriptor_GetCppType(PyUpb_DescriptorBase* self, |
| 912 |
void* closure) { |
| 913 |
// Enum values copied from descriptor.h in C++. |
| 914 |
enum CppType { |
| 915 |
CPPTYPE_INT32 = 1, // TYPE_INT32, TYPE_SINT32, TYPE_SFIXED32 |
| 916 |
CPPTYPE_INT64 = 2, // TYPE_INT64, TYPE_SINT64, TYPE_SFIXED64 |
| 917 |
CPPTYPE_UINT32 = 3, // TYPE_UINT32, TYPE_FIXED32 |
| 918 |
CPPTYPE_UINT64 = 4, // TYPE_UINT64, TYPE_FIXED64 |
| 919 |
CPPTYPE_DOUBLE = 5, // TYPE_DOUBLE |
| 920 |
CPPTYPE_FLOAT = 6, // TYPE_FLOAT |
| 921 |
CPPTYPE_BOOL = 7, // TYPE_BOOL |
| 922 |
CPPTYPE_ENUM = 8, // TYPE_ENUM |
| 923 |
CPPTYPE_STRING = 9, // TYPE_STRING, TYPE_BYTES |
| 924 |
CPPTYPE_MESSAGE = 10, // TYPE_MESSAGE, TYPE_GROUP |
| 925 |
}; |
| 926 |
static const uint8_t cpp_types[] = { |
| 927 |
-1, |
| 928 |
[kUpb_CType_Int32] = CPPTYPE_INT32, |
| 929 |
[kUpb_CType_Int64] = CPPTYPE_INT64, |
| 930 |
[kUpb_CType_UInt32] = CPPTYPE_UINT32, |
| 931 |
[kUpb_CType_UInt64] = CPPTYPE_UINT64, |
| 932 |
[kUpb_CType_Double] = CPPTYPE_DOUBLE, |
| 933 |
[kUpb_CType_Float] = CPPTYPE_FLOAT, |
| 934 |
[kUpb_CType_Bool] = CPPTYPE_BOOL, |
| 935 |
[kUpb_CType_Enum] = CPPTYPE_ENUM, |
| 936 |
[kUpb_CType_String] = CPPTYPE_STRING, |
| 937 |
[kUpb_CType_Bytes] = CPPTYPE_STRING, |
| 938 |
[kUpb_CType_Message] = CPPTYPE_MESSAGE, |
| 939 |
}; |
| 940 |
return PyLong_FromLong(cpp_types[upb_FieldDef_CType(self->def)]); |
| 941 |
} |
| 942 |
|
| 943 |
static PyObject* PyUpb_FieldDescriptor_GetLabel(PyUpb_DescriptorBase* self, |
| 944 |
void* closure) { |
| 945 |
return PyLong_FromLong(upb_FieldDef_Label(self->def)); |
| 946 |
} |
| 947 |
|
| 948 |
static PyObject* PyUpb_FieldDescriptor_GetIsExtension( |
| 949 |
PyUpb_DescriptorBase* self, void* closure) { |
| 950 |
return PyBool_FromLong(upb_FieldDef_IsExtension(self->def)); |
| 951 |
} |
| 952 |
|
| 953 |
static PyObject* PyUpb_FieldDescriptor_GetNumber(PyUpb_DescriptorBase* self, |
| 954 |
void* closure) { |
| 955 |
return PyLong_FromLong(upb_FieldDef_Number(self->def)); |
| 956 |
} |
| 957 |
|
| 958 |
static PyObject* PyUpb_FieldDescriptor_GetIndex(PyUpb_DescriptorBase* self, |
| 959 |
void* closure) { |
| 960 |
return PyLong_FromLong(upb_FieldDef_Index(self->def)); |
| 961 |
} |
| 962 |
|
| 963 |
static PyObject* PyUpb_FieldDescriptor_GetMessageType( |
| 964 |
PyUpb_DescriptorBase* self, void* closure) { |
| 965 |
const upb_MessageDef* subdef = upb_FieldDef_MessageSubDef(self->def); |
| 966 |
if (!subdef) Py_RETURN_NONE; |
| 967 |
return PyUpb_Descriptor_Get(subdef); |
| 968 |
} |
| 969 |
|
| 970 |
static PyObject* PyUpb_FieldDescriptor_GetEnumType(PyUpb_DescriptorBase* self, |
| 971 |
void* closure) { |
| 972 |
const upb_EnumDef* enumdef = upb_FieldDef_EnumSubDef(self->def); |
| 973 |
if (!enumdef) Py_RETURN_NONE; |
| 974 |
return PyUpb_EnumDescriptor_Get(enumdef); |
| 975 |
} |
| 976 |
|
| 977 |
static PyObject* PyUpb_FieldDescriptor_GetContainingType( |
| 978 |
PyUpb_DescriptorBase* self, void* closure) { |
| 979 |
const upb_MessageDef* m = upb_FieldDef_ContainingType(self->def); |
| 980 |
if (!m) Py_RETURN_NONE; |
| 981 |
return PyUpb_Descriptor_Get(m); |
| 982 |
} |
| 983 |
|
| 984 |
static PyObject* PyUpb_FieldDescriptor_GetExtensionScope( |
| 985 |
PyUpb_DescriptorBase* self, void* closure) { |
| 986 |
const upb_MessageDef* m = upb_FieldDef_ExtensionScope(self->def); |
| 987 |
if (!m) Py_RETURN_NONE; |
| 988 |
return PyUpb_Descriptor_Get(m); |
| 989 |
} |
| 990 |
|
| 991 |
static PyObject* PyUpb_FieldDescriptor_HasDefaultValue( |
| 992 |
PyUpb_DescriptorBase* self, void* closure) { |
| 993 |
return PyBool_FromLong(upb_FieldDef_HasDefault(self->def)); |
| 994 |
} |
| 995 |
|
| 996 |
static PyObject* PyUpb_FieldDescriptor_GetDefaultValue( |
| 997 |
PyUpb_DescriptorBase* self, void* closure) { |
| 998 |
const upb_FieldDef* f = self->def; |
| 999 |
if (upb_FieldDef_IsRepeated(f)) return PyList_New(0); |
| 1000 |
if (upb_FieldDef_IsSubMessage(f)) Py_RETURN_NONE; |
| 1001 |
return PyUpb_UpbToPy(upb_FieldDef_Default(self->def), self->def, NULL); |
| 1002 |
} |
| 1003 |
|
| 1004 |
static PyObject* PyUpb_FieldDescriptor_GetContainingOneof( |
| 1005 |
PyUpb_DescriptorBase* self, void* closure) { |
| 1006 |
const upb_OneofDef* oneof = upb_FieldDef_ContainingOneof(self->def); |
| 1007 |
if (!oneof) Py_RETURN_NONE; |
| 1008 |
return PyUpb_OneofDescriptor_Get(oneof); |
| 1009 |
} |
| 1010 |
|
| 1011 |
static PyObject* PyUpb_FieldDescriptor_GetHasOptions( |
| 1012 |
PyUpb_DescriptorBase* _self, void* closure) { |
| 1013 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 1014 |
return PyBool_FromLong(upb_FieldDef_HasOptions(self->def)); |
| 1015 |
} |
| 1016 |
|
| 1017 |
static PyObject* PyUpb_FieldDescriptor_GetHasPresence( |
| 1018 |
PyUpb_DescriptorBase* _self, void* closure) { |
| 1019 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 1020 |
return PyBool_FromLong(upb_FieldDef_HasPresence(self->def)); |
| 1021 |
} |
| 1022 |
|
| 1023 |
static PyObject* PyUpb_FieldDescriptor_GetOptions(PyObject* _self, |
| 1024 |
PyObject* args) { |
| 1025 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 1026 |
return PyUpb_DescriptorBase_GetOptions(self, upb_FieldDef_Options(self->def), |
| 1027 |
&google_protobuf_FieldOptions_msginit, |
| 1028 |
PYUPB_DESCRIPTOR_PROTO_PACKAGE |
| 1029 |
".FieldOptions"); |
| 1030 |
} |
| 1031 |
|
| 1032 |
static PyGetSetDef PyUpb_FieldDescriptor_Getters[] = { |
| 1033 |
{"full_name", (getter)PyUpb_FieldDescriptor_GetFullName, NULL, "Full name"}, |
| 1034 |
{"name", (getter)PyUpb_FieldDescriptor_GetName, NULL, "Unqualified name"}, |
| 1035 |
{"camelcase_name", (getter)PyUpb_FieldDescriptor_GetCamelCaseName, NULL, |
| 1036 |
"CamelCase name"}, |
| 1037 |
{"json_name", (getter)PyUpb_FieldDescriptor_GetJsonName, NULL, "Json name"}, |
| 1038 |
{"file", (getter)PyUpb_FieldDescriptor_GetFile, NULL, "File Descriptor"}, |
| 1039 |
{"type", (getter)PyUpb_FieldDescriptor_GetType, NULL, "Type"}, |
| 1040 |
{"cpp_type", (getter)PyUpb_FieldDescriptor_GetCppType, NULL, "C++ Type"}, |
| 1041 |
{"label", (getter)PyUpb_FieldDescriptor_GetLabel, NULL, "Label"}, |
| 1042 |
{"number", (getter)PyUpb_FieldDescriptor_GetNumber, NULL, "Number"}, |
| 1043 |
{"index", (getter)PyUpb_FieldDescriptor_GetIndex, NULL, "Index"}, |
| 1044 |
{"default_value", (getter)PyUpb_FieldDescriptor_GetDefaultValue, NULL, |
| 1045 |
"Default Value"}, |
| 1046 |
{"has_default_value", (getter)PyUpb_FieldDescriptor_HasDefaultValue}, |
| 1047 |
{"is_extension", (getter)PyUpb_FieldDescriptor_GetIsExtension, NULL, "ID"}, |
| 1048 |
// TODO(https://github.com/protocolbuffers/upb/issues/459) |
| 1049 |
//{ "id", (getter)GetID, NULL, "ID"}, |
| 1050 |
{"message_type", (getter)PyUpb_FieldDescriptor_GetMessageType, NULL, |
| 1051 |
"Message type"}, |
| 1052 |
{"enum_type", (getter)PyUpb_FieldDescriptor_GetEnumType, NULL, "Enum type"}, |
| 1053 |
{"containing_type", (getter)PyUpb_FieldDescriptor_GetContainingType, NULL, |
| 1054 |
"Containing type"}, |
| 1055 |
{"extension_scope", (getter)PyUpb_FieldDescriptor_GetExtensionScope, NULL, |
| 1056 |
"Extension scope"}, |
| 1057 |
{"containing_oneof", (getter)PyUpb_FieldDescriptor_GetContainingOneof, NULL, |
| 1058 |
"Containing oneof"}, |
| 1059 |
{"has_options", (getter)PyUpb_FieldDescriptor_GetHasOptions, NULL, |
| 1060 |
"Has Options"}, |
| 1061 |
{"has_presence", (getter)PyUpb_FieldDescriptor_GetHasPresence, NULL, |
| 1062 |
"Has Presence"}, |
| 1063 |
// TODO(https://github.com/protocolbuffers/upb/issues/459) |
| 1064 |
//{ "_options", |
| 1065 |
//(getter)NULL, (setter)SetOptions, "Options"}, { "_serialized_options", |
| 1066 |
//(getter)NULL, (setter)SetSerializedOptions, "Serialized Options"}, |
| 1067 |
{NULL}}; |
| 1068 |
|
| 1069 |
static PyMethodDef PyUpb_FieldDescriptor_Methods[] = { |
| 1070 |
{ |
| 1071 |
"GetOptions", |
| 1072 |
PyUpb_FieldDescriptor_GetOptions, |
| 1073 |
METH_NOARGS, |
| 1074 |
}, |
| 1075 |
{NULL}}; |
| 1076 |
|
| 1077 |
static PyType_Slot PyUpb_FieldDescriptor_Slots[] = { |
| 1078 |
DESCRIPTOR_BASE_SLOTS, |
| 1079 |
{Py_tp_methods, PyUpb_FieldDescriptor_Methods}, |
| 1080 |
{Py_tp_getset, PyUpb_FieldDescriptor_Getters}, |
| 1081 |
{0, NULL}}; |
| 1082 |
|
| 1083 |
static PyType_Spec PyUpb_FieldDescriptor_Spec = { |
| 1084 |
PYUPB_MODULE_NAME ".FieldDescriptor", |
| 1085 |
sizeof(PyUpb_DescriptorBase), |
| 1086 |
0, // tp_itemsize |
| 1087 |
Py_TPFLAGS_DEFAULT, |
| 1088 |
PyUpb_FieldDescriptor_Slots, |
| 1089 |
}; |
| 1090 |
|
| 1091 |
// ----------------------------------------------------------------------------- |
| 1092 |
// FileDescriptor |
| 1093 |
// ----------------------------------------------------------------------------- |
| 1094 |
|
| 1095 |
PyObject* PyUpb_FileDescriptor_Get(const upb_FileDef* file) { |
| 1096 |
return PyUpb_DescriptorBase_Get(kPyUpb_FileDescriptor, file, file); |
| 1097 |
} |
| 1098 |
|
| 1099 |
// These are not provided on upb_FileDef because they use the underlying |
| 1100 |
// symtab's hash table. This works for Python because everything happens under |
| 1101 |
// the GIL, but in general the caller has to guarantee that the symtab is not |
| 1102 |
// being mutated concurrently. |
| 1103 |
typedef const void* PyUpb_FileDescriptor_LookupFunc(const upb_DefPool*, |
| 1104 |
const char*); |
| 1105 |
|
| 1106 |
static const void* PyUpb_FileDescriptor_NestedLookup( |
| 1107 |
const upb_FileDef* filedef, const char* name, |
| 1108 |
PyUpb_FileDescriptor_LookupFunc* func) { |
| 1109 |
const upb_DefPool* symtab = upb_FileDef_Pool(filedef); |
| 1110 |
const char* package = upb_FileDef_Package(filedef); |
| 1111 |
if (package) { |
| 1112 |
PyObject* qname = PyUnicode_FromFormat("%s.%s", package, name); |
| 1113 |
const void* ret = func(symtab, PyUnicode_AsUTF8AndSize(qname, NULL)); |
| 1114 |
Py_DECREF(qname); |
| 1115 |
return ret; |
| 1116 |
} else { |
| 1117 |
return func(symtab, name); |
| 1118 |
} |
| 1119 |
} |
| 1120 |
|
| 1121 |
static const void* PyUpb_FileDescriptor_LookupMessage( |
| 1122 |
const upb_FileDef* filedef, const char* name) { |
| 1123 |
return PyUpb_FileDescriptor_NestedLookup( |
| 1124 |
filedef, name, (void*)&upb_DefPool_FindMessageByName); |
| 1125 |
} |
| 1126 |
|
| 1127 |
static const void* PyUpb_FileDescriptor_LookupEnum(const upb_FileDef* filedef, |
| 1128 |
const char* name) { |
| 1129 |
return PyUpb_FileDescriptor_NestedLookup(filedef, name, |
| 1130 |
(void*)&upb_DefPool_FindEnumByName); |
| 1131 |
} |
| 1132 |
|
| 1133 |
static const void* PyUpb_FileDescriptor_LookupExtension( |
| 1134 |
const upb_FileDef* filedef, const char* name) { |
| 1135 |
return PyUpb_FileDescriptor_NestedLookup( |
| 1136 |
filedef, name, (void*)&upb_DefPool_FindExtensionByName); |
| 1137 |
} |
| 1138 |
|
| 1139 |
static const void* PyUpb_FileDescriptor_LookupService( |
| 1140 |
const upb_FileDef* filedef, const char* name) { |
| 1141 |
return PyUpb_FileDescriptor_NestedLookup( |
| 1142 |
filedef, name, (void*)&upb_DefPool_FindServiceByName); |
| 1143 |
} |
| 1144 |
|
| 1145 |
static PyObject* PyUpb_FileDescriptor_GetName(PyUpb_DescriptorBase* self, |
| 1146 |
void* closure) { |
| 1147 |
return PyUnicode_FromString(upb_FileDef_Name(self->def)); |
| 1148 |
} |
| 1149 |
|
| 1150 |
static PyObject* PyUpb_FileDescriptor_GetPool(PyObject* _self, void* closure) { |
| 1151 |
PyUpb_DescriptorBase* self = (PyUpb_DescriptorBase*)_self; |
| 1152 |
Py_INCREF(self->pool); |
| 1153 |
return self->pool; |
| 1154 |
} |
| 1155 |
|
| 1156 |
static PyObject* PyUpb_FileDescriptor_GetPackage(PyObject* _self, |
| 1157 |
void* closure) { |
| 1158 |
PyUpb_DescriptorBase* self = (PyUpb_DescriptorBase*)_self; |
| 1159 |
return PyUnicode_FromString(upb_FileDef_Package(self->def)); |
| 1160 |
} |
| 1161 |
|
| 1162 |
static PyObject* PyUpb_FileDescriptor_GetSerializedPb(PyObject* self, |
| 1163 |
void* closure) { |
| 1164 |
return PyUpb_DescriptorBase_GetSerializedProto( |
| 1165 |
self, (PyUpb_ToProto_Func*)&upb_FileDef_ToProto, |
| 1166 |
&google_protobuf_FileDescriptorProto_msginit); |
| 1167 |
} |
| 1168 |
|
| 1169 |
static PyObject* PyUpb_FileDescriptor_GetMessageTypesByName(PyObject* _self, |
| 1170 |
void* closure) { |
| 1171 |
static PyUpb_ByNameMap_Funcs funcs = { |
| 1172 |
{ |
| 1173 |
(void*)&upb_FileDef_TopLevelMessageCount, |
| 1174 |
(void*)&upb_FileDef_TopLevelMessage, |
| 1175 |
(void*)&PyUpb_Descriptor_Get, |
| 1176 |
}, |
| 1177 |
(void*)&PyUpb_FileDescriptor_LookupMessage, |
| 1178 |
(void*)&upb_MessageDef_Name, |
| 1179 |
}; |
| 1180 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 1181 |
return PyUpb_ByNameMap_New(&funcs, self->def, self->pool); |
| 1182 |
} |
| 1183 |
|
| 1184 |
static PyObject* PyUpb_FileDescriptor_GetEnumTypesByName(PyObject* _self, |
| 1185 |
void* closure) { |
| 1186 |
static PyUpb_ByNameMap_Funcs funcs = { |
| 1187 |
{ |
| 1188 |
(void*)&upb_FileDef_TopLevelEnumCount, |
| 1189 |
(void*)&upb_FileDef_TopLevelEnum, |
| 1190 |
(void*)&PyUpb_EnumDescriptor_Get, |
| 1191 |
}, |
| 1192 |
(void*)&PyUpb_FileDescriptor_LookupEnum, |
| 1193 |
(void*)&upb_EnumDef_Name, |
| 1194 |
}; |
| 1195 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 1196 |
return PyUpb_ByNameMap_New(&funcs, self->def, self->pool); |
| 1197 |
} |
| 1198 |
|
| 1199 |
static PyObject* PyUpb_FileDescriptor_GetExtensionsByName(PyObject* _self, |
| 1200 |
void* closure) { |
| 1201 |
static PyUpb_ByNameMap_Funcs funcs = { |
| 1202 |
{ |
| 1203 |
(void*)&upb_FileDef_TopLevelExtensionCount, |
| 1204 |
(void*)&upb_FileDef_TopLevelExtension, |
| 1205 |
(void*)&PyUpb_FieldDescriptor_Get, |
| 1206 |
}, |
| 1207 |
(void*)&PyUpb_FileDescriptor_LookupExtension, |
| 1208 |
(void*)&upb_FieldDef_Name, |
| 1209 |
}; |
| 1210 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 1211 |
return PyUpb_ByNameMap_New(&funcs, self->def, self->pool); |
| 1212 |
} |
| 1213 |
|
| 1214 |
static PyObject* PyUpb_FileDescriptor_GetServicesByName(PyObject* _self, |
| 1215 |
void* closure) { |
| 1216 |
static PyUpb_ByNameMap_Funcs funcs = { |
| 1217 |
{ |
| 1218 |
(void*)&upb_FileDef_ServiceCount, |
| 1219 |
(void*)&upb_FileDef_Service, |
| 1220 |
(void*)&PyUpb_ServiceDescriptor_Get, |
| 1221 |
}, |
| 1222 |
(void*)&PyUpb_FileDescriptor_LookupService, |
| 1223 |
(void*)&upb_ServiceDef_Name, |
| 1224 |
}; |
| 1225 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 1226 |
return PyUpb_ByNameMap_New(&funcs, self->def, self->pool); |
| 1227 |
} |
| 1228 |
|
| 1229 |
static PyObject* PyUpb_FileDescriptor_GetDependencies(PyObject* _self, |
| 1230 |
void* closure) { |
| 1231 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 1232 |
static PyUpb_GenericSequence_Funcs funcs = { |
| 1233 |
(void*)&upb_FileDef_DependencyCount, |
| 1234 |
(void*)&upb_FileDef_Dependency, |
| 1235 |
(void*)&PyUpb_FileDescriptor_Get, |
| 1236 |
}; |
| 1237 |
return PyUpb_GenericSequence_New(&funcs, self->def, self->pool); |
| 1238 |
} |
| 1239 |
|
| 1240 |
static PyObject* PyUpb_FileDescriptor_GetPublicDependencies(PyObject* _self, |
| 1241 |
void* closure) { |
| 1242 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 1243 |
static PyUpb_GenericSequence_Funcs funcs = { |
| 1244 |
(void*)&upb_FileDef_PublicDependencyCount, |
| 1245 |
(void*)&upb_FileDef_PublicDependency, |
| 1246 |
(void*)&PyUpb_FileDescriptor_Get, |
| 1247 |
}; |
| 1248 |
return PyUpb_GenericSequence_New(&funcs, self->def, self->pool); |
| 1249 |
} |
| 1250 |
|
| 1251 |
static PyObject* PyUpb_FileDescriptor_GetSyntax(PyObject* _self, |
| 1252 |
void* closure) { |
| 1253 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 1254 |
const char* syntax = |
| 1255 |
upb_FileDef_Syntax(self->def) == kUpb_Syntax_Proto2 ? "proto2" : "proto3"; |
| 1256 |
return PyUnicode_FromString(syntax); |
| 1257 |
} |
| 1258 |
|
| 1259 |
static PyObject* PyUpb_FileDescriptor_GetHasOptions(PyObject* _self, |
| 1260 |
void* closure) { |
| 1261 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 1262 |
return PyBool_FromLong(upb_FileDef_HasOptions(self->def)); |
| 1263 |
} |
| 1264 |
|
| 1265 |
static PyObject* PyUpb_FileDescriptor_GetOptions(PyObject* _self, |
| 1266 |
PyObject* args) { |
| 1267 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 1268 |
return PyUpb_DescriptorBase_GetOptions(self, upb_FileDef_Options(self->def), |
| 1269 |
&google_protobuf_FileOptions_msginit, |
| 1270 |
PYUPB_DESCRIPTOR_PROTO_PACKAGE |
| 1271 |
".FileOptions"); |
| 1272 |
} |
| 1273 |
|
| 1274 |
static PyObject* PyUpb_FileDescriptor_CopyToProto(PyObject* _self, |
| 1275 |
PyObject* py_proto) { |
| 1276 |
return PyUpb_DescriptorBase_CopyToProto( |
| 1277 |
_self, (PyUpb_ToProto_Func*)&upb_FileDef_ToProto, |
| 1278 |
&google_protobuf_FileDescriptorProto_msginit, |
| 1279 |
PYUPB_DESCRIPTOR_PROTO_PACKAGE ".FileDescriptorProto", py_proto); |
| 1280 |
} |
| 1281 |
|
| 1282 |
static PyGetSetDef PyUpb_FileDescriptor_Getters[] = { |
| 1283 |
{"pool", PyUpb_FileDescriptor_GetPool, NULL, "pool"}, |
| 1284 |
{"name", (getter)PyUpb_FileDescriptor_GetName, NULL, "name"}, |
| 1285 |
{"package", PyUpb_FileDescriptor_GetPackage, NULL, "package"}, |
| 1286 |
{"serialized_pb", PyUpb_FileDescriptor_GetSerializedPb}, |
| 1287 |
{"message_types_by_name", PyUpb_FileDescriptor_GetMessageTypesByName, NULL, |
| 1288 |
"Messages by name"}, |
| 1289 |
{"enum_types_by_name", PyUpb_FileDescriptor_GetEnumTypesByName, NULL, |
| 1290 |
"Enums by name"}, |
| 1291 |
{"extensions_by_name", PyUpb_FileDescriptor_GetExtensionsByName, NULL, |
| 1292 |
"Extensions by name"}, |
| 1293 |
{"services_by_name", PyUpb_FileDescriptor_GetServicesByName, NULL, |
| 1294 |
"Services by name"}, |
| 1295 |
{"dependencies", PyUpb_FileDescriptor_GetDependencies, NULL, |
| 1296 |
"Dependencies"}, |
| 1297 |
{"public_dependencies", PyUpb_FileDescriptor_GetPublicDependencies, NULL, |
| 1298 |
"Dependencies"}, |
| 1299 |
{"has_options", PyUpb_FileDescriptor_GetHasOptions, NULL, "Has Options"}, |
| 1300 |
{"syntax", PyUpb_FileDescriptor_GetSyntax, (setter)NULL, "Syntax"}, |
| 1301 |
{NULL}, |
| 1302 |
}; |
| 1303 |
|
| 1304 |
static PyMethodDef PyUpb_FileDescriptor_Methods[] = { |
| 1305 |
{"GetOptions", PyUpb_FileDescriptor_GetOptions, METH_NOARGS}, |
| 1306 |
{"CopyToProto", PyUpb_FileDescriptor_CopyToProto, METH_O}, |
| 1307 |
{NULL}}; |
| 1308 |
|
| 1309 |
static PyType_Slot PyUpb_FileDescriptor_Slots[] = { |
| 1310 |
DESCRIPTOR_BASE_SLOTS, |
| 1311 |
{Py_tp_methods, PyUpb_FileDescriptor_Methods}, |
| 1312 |
{Py_tp_getset, PyUpb_FileDescriptor_Getters}, |
| 1313 |
{0, NULL}}; |
| 1314 |
|
| 1315 |
static PyType_Spec PyUpb_FileDescriptor_Spec = { |
| 1316 |
PYUPB_MODULE_NAME ".FileDescriptor", // tp_name |
| 1317 |
sizeof(PyUpb_DescriptorBase), // tp_basicsize |
| 1318 |
0, // tp_itemsize |
| 1319 |
Py_TPFLAGS_DEFAULT, // tp_flags |
| 1320 |
PyUpb_FileDescriptor_Slots, |
| 1321 |
}; |
| 1322 |
|
| 1323 |
const upb_FileDef* PyUpb_FileDescriptor_GetDef(PyObject* _self) { |
| 1324 |
PyUpb_DescriptorBase* self = |
| 1325 |
PyUpb_DescriptorBase_Check(_self, kPyUpb_FileDescriptor); |
| 1326 |
return self ? self->def : NULL; |
| 1327 |
} |
| 1328 |
|
| 1329 |
// ----------------------------------------------------------------------------- |
| 1330 |
// MethodDescriptor |
| 1331 |
// ----------------------------------------------------------------------------- |
| 1332 |
|
| 1333 |
const upb_MethodDef* PyUpb_MethodDescriptor_GetDef(PyObject* _self) { |
| 1334 |
PyUpb_DescriptorBase* self = |
| 1335 |
PyUpb_DescriptorBase_Check(_self, kPyUpb_MethodDescriptor); |
| 1336 |
return self ? self->def : NULL; |
| 1337 |
} |
| 1338 |
|
| 1339 |
PyObject* PyUpb_MethodDescriptor_Get(const upb_MethodDef* m) { |
| 1340 |
const upb_FileDef* file = upb_ServiceDef_File(upb_MethodDef_Service(m)); |
| 1341 |
return PyUpb_DescriptorBase_Get(kPyUpb_MethodDescriptor, m, file); |
| 1342 |
} |
| 1343 |
|
| 1344 |
static PyObject* PyUpb_MethodDescriptor_GetName(PyObject* self, void* closure) { |
| 1345 |
const upb_MethodDef* m = PyUpb_MethodDescriptor_GetDef(self); |
| 1346 |
return PyUnicode_FromString(upb_MethodDef_Name(m)); |
| 1347 |
} |
| 1348 |
|
| 1349 |
static PyObject* PyUpb_MethodDescriptor_GetFullName(PyObject* self, |
| 1350 |
void* closure) { |
| 1351 |
const upb_MethodDef* m = PyUpb_MethodDescriptor_GetDef(self); |
| 1352 |
return PyUnicode_FromString(upb_MethodDef_FullName(m)); |
| 1353 |
} |
| 1354 |
|
| 1355 |
static PyObject* PyUpb_MethodDescriptor_GetIndex(PyObject* self, |
| 1356 |
void* closure) { |
| 1357 |
const upb_MethodDef* oneof = PyUpb_MethodDescriptor_GetDef(self); |
| 1358 |
return PyLong_FromLong(upb_MethodDef_Index(oneof)); |
| 1359 |
} |
| 1360 |
|
| 1361 |
static PyObject* PyUpb_MethodDescriptor_GetContainingService(PyObject* self, |
| 1362 |
void* closure) { |
| 1363 |
const upb_MethodDef* m = PyUpb_MethodDescriptor_GetDef(self); |
| 1364 |
return PyUpb_ServiceDescriptor_Get(upb_MethodDef_Service(m)); |
| 1365 |
} |
| 1366 |
|
| 1367 |
static PyObject* PyUpb_MethodDescriptor_GetInputType(PyObject* self, |
| 1368 |
void* closure) { |
| 1369 |
const upb_MethodDef* m = PyUpb_MethodDescriptor_GetDef(self); |
| 1370 |
return PyUpb_Descriptor_Get(upb_MethodDef_InputType(m)); |
| 1371 |
} |
| 1372 |
|
| 1373 |
static PyObject* PyUpb_MethodDescriptor_GetOutputType(PyObject* self, |
| 1374 |
void* closure) { |
| 1375 |
const upb_MethodDef* m = PyUpb_MethodDescriptor_GetDef(self); |
| 1376 |
return PyUpb_Descriptor_Get(upb_MethodDef_OutputType(m)); |
| 1377 |
} |
| 1378 |
|
| 1379 |
static PyObject* PyUpb_MethodDescriptor_GetOptions(PyObject* _self, |
| 1380 |
PyObject* args) { |
| 1381 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 1382 |
return PyUpb_DescriptorBase_GetOptions(self, upb_MethodDef_Options(self->def), |
| 1383 |
&google_protobuf_MethodOptions_msginit, |
| 1384 |
PYUPB_DESCRIPTOR_PROTO_PACKAGE |
| 1385 |
".MethodOptions"); |
| 1386 |
} |
| 1387 |
|
| 1388 |
static PyObject* PyUpb_MethodDescriptor_CopyToProto(PyObject* _self, |
| 1389 |
PyObject* py_proto) { |
| 1390 |
return PyUpb_DescriptorBase_CopyToProto( |
| 1391 |
_self, (PyUpb_ToProto_Func*)&upb_MethodDef_ToProto, |
| 1392 |
&google_protobuf_MethodDescriptorProto_msginit, |
| 1393 |
PYUPB_DESCRIPTOR_PROTO_PACKAGE ".MethodDescriptorProto", py_proto); |
| 1394 |
} |
| 1395 |
|
| 1396 |
static PyGetSetDef PyUpb_MethodDescriptor_Getters[] = { |
| 1397 |
{"name", PyUpb_MethodDescriptor_GetName, NULL, "Name", NULL}, |
| 1398 |
{"full_name", PyUpb_MethodDescriptor_GetFullName, NULL, "Full name", NULL}, |
| 1399 |
{"index", PyUpb_MethodDescriptor_GetIndex, NULL, "Index", NULL}, |
| 1400 |
{"containing_service", PyUpb_MethodDescriptor_GetContainingService, NULL, |
| 1401 |
"Containing service", NULL}, |
| 1402 |
{"input_type", PyUpb_MethodDescriptor_GetInputType, NULL, "Input type", |
| 1403 |
NULL}, |
| 1404 |
{"output_type", PyUpb_MethodDescriptor_GetOutputType, NULL, "Output type", |
| 1405 |
NULL}, |
| 1406 |
{NULL}}; |
| 1407 |
|
| 1408 |
static PyMethodDef PyUpb_MethodDescriptor_Methods[] = { |
| 1409 |
{"GetOptions", PyUpb_MethodDescriptor_GetOptions, METH_NOARGS}, |
| 1410 |
{"CopyToProto", PyUpb_MethodDescriptor_CopyToProto, METH_O}, |
| 1411 |
{NULL}}; |
| 1412 |
|
| 1413 |
static PyType_Slot PyUpb_MethodDescriptor_Slots[] = { |
| 1414 |
DESCRIPTOR_BASE_SLOTS, |
| 1415 |
{Py_tp_methods, PyUpb_MethodDescriptor_Methods}, |
| 1416 |
{Py_tp_getset, PyUpb_MethodDescriptor_Getters}, |
| 1417 |
{0, NULL}}; |
| 1418 |
|
| 1419 |
static PyType_Spec PyUpb_MethodDescriptor_Spec = { |
| 1420 |
PYUPB_MODULE_NAME ".MethodDescriptor", // tp_name |
| 1421 |
sizeof(PyUpb_DescriptorBase), // tp_basicsize |
| 1422 |
0, // tp_itemsize |
| 1423 |
Py_TPFLAGS_DEFAULT, // tp_flags |
| 1424 |
PyUpb_MethodDescriptor_Slots, |
| 1425 |
}; |
| 1426 |
|
| 1427 |
// ----------------------------------------------------------------------------- |
| 1428 |
// OneofDescriptor |
| 1429 |
// ----------------------------------------------------------------------------- |
| 1430 |
|
| 1431 |
const upb_OneofDef* PyUpb_OneofDescriptor_GetDef(PyObject* _self) { |
| 1432 |
PyUpb_DescriptorBase* self = |
| 1433 |
PyUpb_DescriptorBase_Check(_self, kPyUpb_OneofDescriptor); |
| 1434 |
return self ? self->def : NULL; |
| 1435 |
} |
| 1436 |
|
| 1437 |
PyObject* PyUpb_OneofDescriptor_Get(const upb_OneofDef* oneof) { |
| 1438 |
const upb_FileDef* file = |
| 1439 |
upb_MessageDef_File(upb_OneofDef_ContainingType(oneof)); |
| 1440 |
return PyUpb_DescriptorBase_Get(kPyUpb_OneofDescriptor, oneof, file); |
| 1441 |
} |
| 1442 |
|
| 1443 |
static PyObject* PyUpb_OneofDescriptor_GetName(PyObject* self, void* closure) { |
| 1444 |
const upb_OneofDef* oneof = PyUpb_OneofDescriptor_GetDef(self); |
| 1445 |
return PyUnicode_FromString(upb_OneofDef_Name(oneof)); |
| 1446 |
} |
| 1447 |
|
| 1448 |
static PyObject* PyUpb_OneofDescriptor_GetFullName(PyObject* self, |
| 1449 |
void* closure) { |
| 1450 |
const upb_OneofDef* oneof = PyUpb_OneofDescriptor_GetDef(self); |
| 1451 |
return PyUnicode_FromFormat( |
| 1452 |
"%s.%s", upb_MessageDef_FullName(upb_OneofDef_ContainingType(oneof)), |
| 1453 |
upb_OneofDef_Name(oneof)); |
| 1454 |
} |
| 1455 |
|
| 1456 |
static PyObject* PyUpb_OneofDescriptor_GetIndex(PyObject* self, void* closure) { |
| 1457 |
const upb_OneofDef* oneof = PyUpb_OneofDescriptor_GetDef(self); |
| 1458 |
return PyLong_FromLong(upb_OneofDef_Index(oneof)); |
| 1459 |
} |
| 1460 |
|
| 1461 |
static PyObject* PyUpb_OneofDescriptor_GetContainingType(PyObject* self, |
| 1462 |
void* closure) { |
| 1463 |
const upb_OneofDef* oneof = PyUpb_OneofDescriptor_GetDef(self); |
| 1464 |
return PyUpb_Descriptor_Get(upb_OneofDef_ContainingType(oneof)); |
| 1465 |
} |
| 1466 |
|
| 1467 |
static PyObject* PyUpb_OneofDescriptor_GetHasOptions(PyObject* _self, |
| 1468 |
void* closure) { |
| 1469 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 1470 |
return PyBool_FromLong(upb_OneofDef_HasOptions(self->def)); |
| 1471 |
} |
| 1472 |
|
| 1473 |
static PyObject* PyUpb_OneofDescriptor_GetFields(PyObject* _self, |
| 1474 |
void* closure) { |
| 1475 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 1476 |
static PyUpb_GenericSequence_Funcs funcs = { |
| 1477 |
(void*)&upb_OneofDef_FieldCount, |
| 1478 |
(void*)&upb_OneofDef_Field, |
| 1479 |
(void*)&PyUpb_FieldDescriptor_Get, |
| 1480 |
}; |
| 1481 |
return PyUpb_GenericSequence_New(&funcs, self->def, self->pool); |
| 1482 |
} |
| 1483 |
|
| 1484 |
static PyObject* PyUpb_OneofDescriptor_GetOptions(PyObject* _self, |
| 1485 |
PyObject* args) { |
| 1486 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 1487 |
return PyUpb_DescriptorBase_GetOptions(self, upb_OneofDef_Options(self->def), |
| 1488 |
&google_protobuf_OneofOptions_msginit, |
| 1489 |
PYUPB_DESCRIPTOR_PROTO_PACKAGE |
| 1490 |
".OneofOptions"); |
| 1491 |
} |
| 1492 |
|
| 1493 |
static PyGetSetDef PyUpb_OneofDescriptor_Getters[] = { |
| 1494 |
{"name", PyUpb_OneofDescriptor_GetName, NULL, "Name"}, |
| 1495 |
{"full_name", PyUpb_OneofDescriptor_GetFullName, NULL, "Full name"}, |
| 1496 |
{"index", PyUpb_OneofDescriptor_GetIndex, NULL, "Index"}, |
| 1497 |
{"containing_type", PyUpb_OneofDescriptor_GetContainingType, NULL, |
| 1498 |
"Containing type"}, |
| 1499 |
{"has_options", PyUpb_OneofDescriptor_GetHasOptions, NULL, "Has Options"}, |
| 1500 |
{"fields", PyUpb_OneofDescriptor_GetFields, NULL, "Fields"}, |
| 1501 |
{NULL}}; |
| 1502 |
|
| 1503 |
static PyMethodDef PyUpb_OneofDescriptor_Methods[] = { |
| 1504 |
{"GetOptions", PyUpb_OneofDescriptor_GetOptions, METH_NOARGS}, {NULL}}; |
| 1505 |
|
| 1506 |
static PyType_Slot PyUpb_OneofDescriptor_Slots[] = { |
| 1507 |
DESCRIPTOR_BASE_SLOTS, |
| 1508 |
{Py_tp_methods, PyUpb_OneofDescriptor_Methods}, |
| 1509 |
{Py_tp_getset, PyUpb_OneofDescriptor_Getters}, |
| 1510 |
{0, NULL}}; |
| 1511 |
|
| 1512 |
static PyType_Spec PyUpb_OneofDescriptor_Spec = { |
| 1513 |
PYUPB_MODULE_NAME ".OneofDescriptor", // tp_name |
| 1514 |
sizeof(PyUpb_DescriptorBase), // tp_basicsize |
| 1515 |
0, // tp_itemsize |
| 1516 |
Py_TPFLAGS_DEFAULT, // tp_flags |
| 1517 |
PyUpb_OneofDescriptor_Slots, |
| 1518 |
}; |
| 1519 |
|
| 1520 |
// ----------------------------------------------------------------------------- |
| 1521 |
// ServiceDescriptor |
| 1522 |
// ----------------------------------------------------------------------------- |
| 1523 |
|
| 1524 |
const upb_ServiceDef* PyUpb_ServiceDescriptor_GetDef(PyObject* _self) { |
| 1525 |
PyUpb_DescriptorBase* self = |
| 1526 |
PyUpb_DescriptorBase_Check(_self, kPyUpb_ServiceDescriptor); |
| 1527 |
return self ? self->def : NULL; |
| 1528 |
} |
| 1529 |
|
| 1530 |
PyObject* PyUpb_ServiceDescriptor_Get(const upb_ServiceDef* s) { |
| 1531 |
const upb_FileDef* file = upb_ServiceDef_File(s); |
| 1532 |
return PyUpb_DescriptorBase_Get(kPyUpb_ServiceDescriptor, s, file); |
| 1533 |
} |
| 1534 |
|
| 1535 |
static PyObject* PyUpb_ServiceDescriptor_GetFullName(PyObject* self, |
| 1536 |
void* closure) { |
| 1537 |
const upb_ServiceDef* s = PyUpb_ServiceDescriptor_GetDef(self); |
| 1538 |
return PyUnicode_FromString(upb_ServiceDef_FullName(s)); |
| 1539 |
} |
| 1540 |
|
| 1541 |
static PyObject* PyUpb_ServiceDescriptor_GetName(PyObject* self, |
| 1542 |
void* closure) { |
| 1543 |
const upb_ServiceDef* s = PyUpb_ServiceDescriptor_GetDef(self); |
| 1544 |
return PyUnicode_FromString(upb_ServiceDef_Name(s)); |
| 1545 |
} |
| 1546 |
|
| 1547 |
static PyObject* PyUpb_ServiceDescriptor_GetFile(PyObject* self, |
| 1548 |
void* closure) { |
| 1549 |
const upb_ServiceDef* s = PyUpb_ServiceDescriptor_GetDef(self); |
| 1550 |
return PyUpb_FileDescriptor_Get(upb_ServiceDef_File(s)); |
| 1551 |
} |
| 1552 |
|
| 1553 |
static PyObject* PyUpb_ServiceDescriptor_GetIndex(PyObject* self, |
| 1554 |
void* closure) { |
| 1555 |
const upb_ServiceDef* s = PyUpb_ServiceDescriptor_GetDef(self); |
| 1556 |
return PyLong_FromLong(upb_ServiceDef_Index(s)); |
| 1557 |
} |
| 1558 |
|
| 1559 |
static PyObject* PyUpb_ServiceDescriptor_GetMethods(PyObject* _self, |
| 1560 |
void* closure) { |
| 1561 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 1562 |
static PyUpb_GenericSequence_Funcs funcs = { |
| 1563 |
(void*)&upb_ServiceDef_MethodCount, |
| 1564 |
(void*)&upb_ServiceDef_Method, |
| 1565 |
(void*)&PyUpb_MethodDescriptor_Get, |
| 1566 |
}; |
| 1567 |
return PyUpb_GenericSequence_New(&funcs, self->def, self->pool); |
| 1568 |
} |
| 1569 |
|
| 1570 |
static PyObject* PyUpb_ServiceDescriptor_GetMethodsByName(PyObject* _self, |
| 1571 |
void* closure) { |
| 1572 |
static PyUpb_ByNameMap_Funcs funcs = { |
| 1573 |
{ |
| 1574 |
(void*)&upb_ServiceDef_MethodCount, |
| 1575 |
(void*)&upb_ServiceDef_Method, |
| 1576 |
(void*)&PyUpb_MethodDescriptor_Get, |
| 1577 |
}, |
| 1578 |
(void*)&upb_ServiceDef_FindMethodByName, |
| 1579 |
(void*)&upb_MethodDef_Name, |
| 1580 |
}; |
| 1581 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 1582 |
return PyUpb_ByNameMap_New(&funcs, self->def, self->pool); |
| 1583 |
} |
| 1584 |
|
| 1585 |
static PyObject* PyUpb_ServiceDescriptor_GetOptions(PyObject* _self, |
| 1586 |
PyObject* args) { |
| 1587 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 1588 |
return PyUpb_DescriptorBase_GetOptions( |
| 1589 |
self, upb_ServiceDef_Options(self->def), |
| 1590 |
&google_protobuf_ServiceOptions_msginit, |
| 1591 |
PYUPB_DESCRIPTOR_PROTO_PACKAGE ".ServiceOptions"); |
| 1592 |
} |
| 1593 |
|
| 1594 |
static PyObject* PyUpb_ServiceDescriptor_CopyToProto(PyObject* _self, |
| 1595 |
PyObject* py_proto) { |
| 1596 |
return PyUpb_DescriptorBase_CopyToProto( |
| 1597 |
_self, (PyUpb_ToProto_Func*)&upb_ServiceDef_ToProto, |
| 1598 |
&google_protobuf_ServiceDescriptorProto_msginit, |
| 1599 |
PYUPB_DESCRIPTOR_PROTO_PACKAGE ".ServiceDescriptorProto", py_proto); |
| 1600 |
} |
| 1601 |
|
| 1602 |
static PyObject* PyUpb_ServiceDescriptor_FindMethodByName(PyObject* _self, |
| 1603 |
PyObject* py_name) { |
| 1604 |
PyUpb_DescriptorBase* self = (void*)_self; |
| 1605 |
const char* name = PyUnicode_AsUTF8AndSize(py_name, NULL); |
| 1606 |
if (!name) return NULL; |
| 1607 |
const upb_MethodDef* method = |
| 1608 |
upb_ServiceDef_FindMethodByName(self->def, name); |
| 1609 |
if (method == NULL) { |
| 1610 |
return PyErr_Format(PyExc_KeyError, "Couldn't find method %.200s", name); |
| 1611 |
} |
| 1612 |
return PyUpb_MethodDescriptor_Get(method); |
| 1613 |
} |
| 1614 |
|
| 1615 |
static PyGetSetDef PyUpb_ServiceDescriptor_Getters[] = { |
| 1616 |
{"name", PyUpb_ServiceDescriptor_GetName, NULL, "Name", NULL}, |
| 1617 |
{"full_name", PyUpb_ServiceDescriptor_GetFullName, NULL, "Full name", NULL}, |
| 1618 |
{"file", PyUpb_ServiceDescriptor_GetFile, NULL, "File descriptor"}, |
| 1619 |
{"index", PyUpb_ServiceDescriptor_GetIndex, NULL, "Index", NULL}, |
| 1620 |
{"methods", PyUpb_ServiceDescriptor_GetMethods, NULL, "Methods", NULL}, |
| 1621 |
{"methods_by_name", PyUpb_ServiceDescriptor_GetMethodsByName, NULL, |
| 1622 |
"Methods by name", NULL}, |
| 1623 |
{NULL}}; |
| 1624 |
|
| 1625 |
static PyMethodDef PyUpb_ServiceDescriptor_Methods[] = { |
| 1626 |
{"GetOptions", PyUpb_ServiceDescriptor_GetOptions, METH_NOARGS}, |
| 1627 |
{"CopyToProto", PyUpb_ServiceDescriptor_CopyToProto, METH_O}, |
| 1628 |
{"FindMethodByName", PyUpb_ServiceDescriptor_FindMethodByName, METH_O}, |
| 1629 |
{NULL}}; |
| 1630 |
|
| 1631 |
static PyType_Slot PyUpb_ServiceDescriptor_Slots[] = { |
| 1632 |
DESCRIPTOR_BASE_SLOTS, |
| 1633 |
{Py_tp_methods, PyUpb_ServiceDescriptor_Methods}, |
| 1634 |
{Py_tp_getset, PyUpb_ServiceDescriptor_Getters}, |
| 1635 |
{0, NULL}}; |
| 1636 |
|
| 1637 |
static PyType_Spec PyUpb_ServiceDescriptor_Spec = { |
| 1638 |
PYUPB_MODULE_NAME ".ServiceDescriptor", // tp_name |
| 1639 |
sizeof(PyUpb_DescriptorBase), // tp_basicsize |
| 1640 |
0, // tp_itemsize |
| 1641 |
Py_TPFLAGS_DEFAULT, // tp_flags |
| 1642 |
PyUpb_ServiceDescriptor_Slots, |
| 1643 |
}; |
| 1644 |
|
| 1645 |
// ----------------------------------------------------------------------------- |
| 1646 |
// Top Level |
| 1647 |
// ----------------------------------------------------------------------------- |
| 1648 |
|
| 1649 |
static bool PyUpb_SetIntAttr(PyObject* obj, const char* name, int val) { |
| 1650 |
PyObject* num = PyLong_FromLong(val); |
| 1651 |
if (!num) return false; |
| 1652 |
int status = PyObject_SetAttrString(obj, name, num); |
| 1653 |
Py_DECREF(num); |
| 1654 |
return status >= 0; |
| 1655 |
} |
| 1656 |
|
| 1657 |
// These must be in the same order as PyUpb_DescriptorType in the header. |
| 1658 |
static PyType_Spec* desc_specs[] = { |
| 1659 |
&PyUpb_Descriptor_Spec, &PyUpb_EnumDescriptor_Spec, |
| 1660 |
&PyUpb_EnumValueDescriptor_Spec, &PyUpb_FieldDescriptor_Spec, |
| 1661 |
&PyUpb_FileDescriptor_Spec, &PyUpb_MethodDescriptor_Spec, |
| 1662 |
&PyUpb_OneofDescriptor_Spec, &PyUpb_ServiceDescriptor_Spec, |
| 1663 |
}; |
| 1664 |
|
| 1665 |
bool PyUpb_InitDescriptor(PyObject* m) { |
| 1666 |
PyUpb_ModuleState* s = PyUpb_ModuleState_GetFromModule(m); |
| 1667 |
|
| 1668 |
for (size_t i = 0; i < kPyUpb_Descriptor_Count; i++) { |
| 1669 |
s->descriptor_types[i] = PyUpb_AddClass(m, desc_specs[i]); |
| 1670 |
if (!s->descriptor_types[i]) { |
| 1671 |
return false; |
| 1672 |
} |
| 1673 |
} |
| 1674 |
|
| 1675 |
PyObject* fd = (PyObject*)s->descriptor_types[kPyUpb_FieldDescriptor]; |
| 1676 |
return PyUpb_SetIntAttr(fd, "LABEL_OPTIONAL", kUpb_Label_Optional) && |
| 1677 |
PyUpb_SetIntAttr(fd, "LABEL_REPEATED", kUpb_Label_Repeated) && |
| 1678 |
PyUpb_SetIntAttr(fd, "LABEL_REQUIRED", kUpb_Label_Required) && |
| 1679 |
PyUpb_SetIntAttr(fd, "TYPE_BOOL", kUpb_FieldType_Bool) && |
| 1680 |
PyUpb_SetIntAttr(fd, "TYPE_BYTES", kUpb_FieldType_Bytes) && |
| 1681 |
PyUpb_SetIntAttr(fd, "TYPE_DOUBLE", kUpb_FieldType_Double) && |
| 1682 |
PyUpb_SetIntAttr(fd, "TYPE_ENUM", kUpb_FieldType_Enum) && |
| 1683 |
PyUpb_SetIntAttr(fd, "TYPE_FIXED32", kUpb_FieldType_Fixed32) && |
| 1684 |
PyUpb_SetIntAttr(fd, "TYPE_FIXED64", kUpb_FieldType_Fixed64) && |
| 1685 |
PyUpb_SetIntAttr(fd, "TYPE_FLOAT", kUpb_FieldType_Float) && |
| 1686 |
PyUpb_SetIntAttr(fd, "TYPE_GROUP", kUpb_FieldType_Group) && |
| 1687 |
PyUpb_SetIntAttr(fd, "TYPE_INT32", kUpb_FieldType_Int32) && |
| 1688 |
PyUpb_SetIntAttr(fd, "TYPE_INT64", kUpb_FieldType_Int64) && |
| 1689 |
PyUpb_SetIntAttr(fd, "TYPE_MESSAGE", kUpb_FieldType_Message) && |
| 1690 |
PyUpb_SetIntAttr(fd, "TYPE_SFIXED32", kUpb_FieldType_SFixed32) && |
| 1691 |
PyUpb_SetIntAttr(fd, "TYPE_SFIXED64", kUpb_FieldType_SFixed64) && |
| 1692 |
PyUpb_SetIntAttr(fd, "TYPE_SINT32", kUpb_FieldType_SInt32) && |
| 1693 |
PyUpb_SetIntAttr(fd, "TYPE_SINT64", kUpb_FieldType_SInt64) && |
| 1694 |
PyUpb_SetIntAttr(fd, "TYPE_STRING", kUpb_FieldType_String) && |
| 1695 |
PyUpb_SetIntAttr(fd, "TYPE_UINT32", kUpb_FieldType_UInt32) && |
| 1696 |
PyUpb_SetIntAttr(fd, "TYPE_UINT64", kUpb_FieldType_UInt64); |
| 1697 |
} |