| 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_pool.h" |
| 29 |
|
| 30 |
#include "google/protobuf/descriptor.upbdefs.h" |
| 31 |
#include "python/convert.h" |
| 32 |
#include "python/descriptor.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 |
// DescriptorPool |
| 40 |
// ----------------------------------------------------------------------------- |
| 41 |
|
| 42 |
typedef struct { |
| 43 |
PyObject_HEAD; |
| 44 |
upb_DefPool* symtab; |
| 45 |
PyObject* db; // The DescriptorDatabase underlying this pool. May be NULL. |
| 46 |
} PyUpb_DescriptorPool; |
| 47 |
|
| 48 |
PyObject* PyUpb_DescriptorPool_GetDefaultPool() { |
| 49 |
PyUpb_ModuleState* s = PyUpb_ModuleState_Get(); |
| 50 |
return s->default_pool; |
| 51 |
} |
| 52 |
|
| 53 |
const upb_MessageDef* PyUpb_DescriptorPool_GetFileProtoDef(void) { |
| 54 |
PyUpb_ModuleState* s = PyUpb_ModuleState_Get(); |
| 55 |
if (!s->c_descriptor_symtab) { |
| 56 |
s->c_descriptor_symtab = upb_DefPool_New(); |
| 57 |
} |
| 58 |
return google_protobuf_FileDescriptorProto_getmsgdef(s->c_descriptor_symtab); |
| 59 |
} |
| 60 |
|
| 61 |
static PyObject* PyUpb_DescriptorPool_DoCreateWithCache( |
| 62 |
PyTypeObject* type, PyObject* db, PyUpb_WeakMap* obj_cache) { |
| 63 |
PyUpb_DescriptorPool* pool = (void*)PyType_GenericAlloc(type, 0); |
| 64 |
pool->symtab = upb_DefPool_New(); |
| 65 |
pool->db = db; |
| 66 |
Py_XINCREF(pool->db); |
| 67 |
PyUpb_WeakMap_Add(obj_cache, pool->symtab, &pool->ob_base); |
| 68 |
return &pool->ob_base; |
| 69 |
} |
| 70 |
|
| 71 |
static PyObject* PyUpb_DescriptorPool_DoCreate(PyTypeObject* type, |
| 72 |
PyObject* db) { |
| 73 |
return PyUpb_DescriptorPool_DoCreateWithCache(type, db, |
| 74 |
PyUpb_ObjCache_Instance()); |
| 75 |
} |
| 76 |
|
| 77 |
upb_DefPool* PyUpb_DescriptorPool_GetSymtab(PyObject* pool) { |
| 78 |
return ((PyUpb_DescriptorPool*)pool)->symtab; |
| 79 |
} |
| 80 |
|
| 81 |
static int PyUpb_DescriptorPool_Traverse(PyUpb_DescriptorPool* self, |
| 82 |
visitproc visit, void* arg) { |
| 83 |
Py_VISIT(self->db); |
| 84 |
return 0; |
| 85 |
} |
| 86 |
|
| 87 |
static int PyUpb_DescriptorPool_Clear(PyUpb_DescriptorPool* self) { |
| 88 |
Py_CLEAR(self->db); |
| 89 |
return 0; |
| 90 |
} |
| 91 |
|
| 92 |
PyObject* PyUpb_DescriptorPool_Get(const upb_DefPool* symtab) { |
| 93 |
PyObject* pool = PyUpb_ObjCache_Get(symtab); |
| 94 |
assert(pool); |
| 95 |
return pool; |
| 96 |
} |
| 97 |
|
| 98 |
static void PyUpb_DescriptorPool_Dealloc(PyUpb_DescriptorPool* self) { |
| 99 |
PyUpb_DescriptorPool_Clear(self); |
| 100 |
upb_DefPool_Free(self->symtab); |
| 101 |
PyUpb_ObjCache_Delete(self->symtab); |
| 102 |
PyUpb_Dealloc(self); |
| 103 |
} |
| 104 |
|
| 105 |
/* |
| 106 |
* DescriptorPool.__new__() |
| 107 |
* |
| 108 |
* Implements: |
| 109 |
* DescriptorPool(descriptor_db=None) |
| 110 |
*/ |
| 111 |
static PyObject* PyUpb_DescriptorPool_New(PyTypeObject* type, PyObject* args, |
| 112 |
PyObject* kwargs) { |
| 113 |
char* kwlist[] = {"descriptor_db", 0}; |
| 114 |
PyObject* db = NULL; |
| 115 |
|
| 116 |
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &db)) { |
| 117 |
return NULL; |
| 118 |
} |
| 119 |
|
| 120 |
if (db == Py_None) db = NULL; |
| 121 |
return PyUpb_DescriptorPool_DoCreate(type, db); |
| 122 |
} |
| 123 |
|
| 124 |
static PyObject* PyUpb_DescriptorPool_DoAdd(PyObject* _self, |
| 125 |
PyObject* file_desc); |
| 126 |
|
| 127 |
static bool PyUpb_DescriptorPool_TryLoadFileProto(PyUpb_DescriptorPool* self, |
| 128 |
PyObject* proto) { |
| 129 |
if (proto == NULL) { |
| 130 |
if (PyErr_ExceptionMatches(PyExc_KeyError)) { |
| 131 |
// Expected error: item was simply not found. |
| 132 |
PyErr_Clear(); |
| 133 |
return true; // We didn't accomplish our goal, but we didn't error out. |
| 134 |
} |
| 135 |
return false; |
| 136 |
} |
| 137 |
if (proto == Py_None) return true; |
| 138 |
PyObject* ret = PyUpb_DescriptorPool_DoAdd((PyObject*)self, proto); |
| 139 |
bool ok = ret != NULL; |
| 140 |
Py_XDECREF(ret); |
| 141 |
return ok; |
| 142 |
} |
| 143 |
|
| 144 |
static bool PyUpb_DescriptorPool_TryLoadSymbol(PyUpb_DescriptorPool* self, |
| 145 |
PyObject* sym) { |
| 146 |
if (!self->db) return false; |
| 147 |
PyObject* file_proto = |
| 148 |
PyObject_CallMethod(self->db, "FindFileContainingSymbol", "O", sym); |
| 149 |
bool ret = PyUpb_DescriptorPool_TryLoadFileProto(self, file_proto); |
| 150 |
Py_XDECREF(file_proto); |
| 151 |
return ret; |
| 152 |
} |
| 153 |
|
| 154 |
static bool PyUpb_DescriptorPool_TryLoadFilename(PyUpb_DescriptorPool* self, |
| 155 |
PyObject* filename) { |
| 156 |
if (!self->db) return false; |
| 157 |
PyObject* file_proto = |
| 158 |
PyObject_CallMethod(self->db, "FindFileByName", "O", filename); |
| 159 |
bool ret = PyUpb_DescriptorPool_TryLoadFileProto(self, file_proto); |
| 160 |
Py_XDECREF(file_proto); |
| 161 |
return ret; |
| 162 |
} |
| 163 |
|
| 164 |
bool PyUpb_DescriptorPool_CheckNoDatabase(PyObject* _self) { return true; } |
| 165 |
|
| 166 |
static bool PyUpb_DescriptorPool_LoadDependentFiles( |
| 167 |
PyUpb_DescriptorPool* self, google_protobuf_FileDescriptorProto* proto) { |
| 168 |
size_t n; |
| 169 |
const upb_StringView* deps = |
| 170 |
google_protobuf_FileDescriptorProto_dependency(proto, &n); |
| 171 |
for (size_t i = 0; i < n; i++) { |
| 172 |
const upb_FileDef* dep = upb_DefPool_FindFileByNameWithSize( |
| 173 |
self->symtab, deps[i].data, deps[i].size); |
| 174 |
if (!dep) { |
| 175 |
PyObject* filename = |
| 176 |
PyUnicode_FromStringAndSize(deps[i].data, deps[i].size); |
| 177 |
if (!filename) return false; |
| 178 |
bool ok = PyUpb_DescriptorPool_TryLoadFilename(self, filename); |
| 179 |
Py_DECREF(filename); |
| 180 |
if (!ok) return false; |
| 181 |
} |
| 182 |
} |
| 183 |
return true; |
| 184 |
} |
| 185 |
|
| 186 |
static PyObject* PyUpb_DescriptorPool_DoAddSerializedFile( |
| 187 |
PyObject* _self, PyObject* serialized_pb) { |
| 188 |
PyUpb_DescriptorPool* self = (PyUpb_DescriptorPool*)_self; |
| 189 |
upb_Arena* arena = upb_Arena_New(); |
| 190 |
if (!arena) PYUPB_RETURN_OOM; |
| 191 |
PyObject* result = NULL; |
| 192 |
|
| 193 |
char* buf; |
| 194 |
Py_ssize_t size; |
| 195 |
if (PyBytes_AsStringAndSize(serialized_pb, &buf, &size) < 0) { |
| 196 |
goto done; |
| 197 |
} |
| 198 |
|
| 199 |
google_protobuf_FileDescriptorProto* proto = |
| 200 |
google_protobuf_FileDescriptorProto_parse(buf, size, arena); |
| 201 |
if (!proto) { |
| 202 |
PyErr_SetString(PyExc_TypeError, "Couldn't parse file content!"); |
| 203 |
goto done; |
| 204 |
} |
| 205 |
|
| 206 |
upb_StringView name = google_protobuf_FileDescriptorProto_name(proto); |
| 207 |
const upb_FileDef* file = |
| 208 |
upb_DefPool_FindFileByNameWithSize(self->symtab, name.data, name.size); |
| 209 |
|
| 210 |
if (file) { |
| 211 |
// If the existing file is equal to the new file, then silently ignore the |
| 212 |
// duplicate add. |
| 213 |
google_protobuf_FileDescriptorProto* existing = |
| 214 |
upb_FileDef_ToProto(file, arena); |
| 215 |
if (!existing) { |
| 216 |
PyErr_SetNone(PyExc_MemoryError); |
| 217 |
goto done; |
| 218 |
} |
| 219 |
const upb_MessageDef* m = PyUpb_DescriptorPool_GetFileProtoDef(); |
| 220 |
if (upb_Message_IsEqual(proto, existing, m)) { |
| 221 |
Py_INCREF(Py_None); |
| 222 |
result = Py_None; |
| 223 |
goto done; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
if (self->db) { |
| 228 |
if (!PyUpb_DescriptorPool_LoadDependentFiles(self, proto)) goto done; |
| 229 |
} |
| 230 |
|
| 231 |
upb_Status status; |
| 232 |
upb_Status_Clear(&status); |
| 233 |
|
| 234 |
const upb_FileDef* filedef = |
| 235 |
upb_DefPool_AddFile(self->symtab, proto, &status); |
| 236 |
if (!filedef) { |
| 237 |
PyErr_Format(PyExc_TypeError, |
| 238 |
"Couldn't build proto file into descriptor pool: %s", |
| 239 |
upb_Status_ErrorMessage(&status)); |
| 240 |
goto done; |
| 241 |
} |
| 242 |
|
| 243 |
result = PyUpb_FileDescriptor_Get(filedef); |
| 244 |
|
| 245 |
done: |
| 246 |
upb_Arena_Free(arena); |
| 247 |
return result; |
| 248 |
} |
| 249 |
|
| 250 |
static PyObject* PyUpb_DescriptorPool_DoAdd(PyObject* _self, |
| 251 |
PyObject* file_desc) { |
| 252 |
if (!PyUpb_Message_Verify(file_desc)) return NULL; |
| 253 |
const upb_MessageDef* m = PyUpb_Message_GetMsgdef(file_desc); |
| 254 |
const char* file_proto_name = |
| 255 |
PYUPB_DESCRIPTOR_PROTO_PACKAGE ".FileDescriptorProto"; |
| 256 |
if (strcmp(upb_MessageDef_FullName(m), file_proto_name) != 0) { |
| 257 |
return PyErr_Format(PyExc_TypeError, "Can only add FileDescriptorProto"); |
| 258 |
} |
| 259 |
PyObject* subargs = PyTuple_New(0); |
| 260 |
if (!subargs) return NULL; |
| 261 |
PyObject* serialized = |
| 262 |
PyUpb_Message_SerializeToString(file_desc, subargs, NULL); |
| 263 |
Py_DECREF(subargs); |
| 264 |
if (!serialized) return NULL; |
| 265 |
PyObject* ret = PyUpb_DescriptorPool_DoAddSerializedFile(_self, serialized); |
| 266 |
Py_DECREF(serialized); |
| 267 |
return ret; |
| 268 |
} |
| 269 |
|
| 270 |
/* |
| 271 |
* PyUpb_DescriptorPool_AddSerializedFile() |
| 272 |
* |
| 273 |
* Implements: |
| 274 |
* DescriptorPool.AddSerializedFile(self, serialized_file_descriptor) |
| 275 |
* |
| 276 |
* Adds the given serialized FileDescriptorProto to the pool. |
| 277 |
*/ |
| 278 |
static PyObject* PyUpb_DescriptorPool_AddSerializedFile( |
| 279 |
PyObject* _self, PyObject* serialized_pb) { |
| 280 |
PyUpb_DescriptorPool* self = (PyUpb_DescriptorPool*)_self; |
| 281 |
if (self->db) { |
| 282 |
PyErr_SetString( |
| 283 |
PyExc_ValueError, |
| 284 |
"Cannot call AddSerializedFile on a DescriptorPool that uses a " |
| 285 |
"DescriptorDatabase. Add your file to the underlying database."); |
| 286 |
return false; |
| 287 |
} |
| 288 |
return PyUpb_DescriptorPool_DoAddSerializedFile(_self, serialized_pb); |
| 289 |
} |
| 290 |
|
| 291 |
static PyObject* PyUpb_DescriptorPool_Add(PyObject* _self, |
| 292 |
PyObject* file_desc) { |
| 293 |
PyUpb_DescriptorPool* self = (PyUpb_DescriptorPool*)_self; |
| 294 |
if (self->db) { |
| 295 |
PyErr_SetString( |
| 296 |
PyExc_ValueError, |
| 297 |
"Cannot call Add on a DescriptorPool that uses a DescriptorDatabase. " |
| 298 |
"Add your file to the underlying database."); |
| 299 |
return false; |
| 300 |
} |
| 301 |
return PyUpb_DescriptorPool_DoAdd(_self, file_desc); |
| 302 |
} |
| 303 |
|
| 304 |
/* |
| 305 |
* PyUpb_DescriptorPool_FindFileByName() |
| 306 |
* |
| 307 |
* Implements: |
| 308 |
* DescriptorPool.FindFileByName(self, name) |
| 309 |
*/ |
| 310 |
static PyObject* PyUpb_DescriptorPool_FindFileByName(PyObject* _self, |
| 311 |
PyObject* arg) { |
| 312 |
PyUpb_DescriptorPool* self = (PyUpb_DescriptorPool*)_self; |
| 313 |
|
| 314 |
const char* name = PyUpb_VerifyStrData(arg); |
| 315 |
if (!name) return NULL; |
| 316 |
|
| 317 |
const upb_FileDef* file = upb_DefPool_FindFileByName(self->symtab, name); |
| 318 |
if (file == NULL && self->db) { |
| 319 |
if (!PyUpb_DescriptorPool_TryLoadFilename(self, arg)) return NULL; |
| 320 |
file = upb_DefPool_FindFileByName(self->symtab, name); |
| 321 |
} |
| 322 |
if (file == NULL) { |
| 323 |
return PyErr_Format(PyExc_KeyError, "Couldn't find file %.200s", name); |
| 324 |
} |
| 325 |
|
| 326 |
return PyUpb_FileDescriptor_Get(file); |
| 327 |
} |
| 328 |
|
| 329 |
/* |
| 330 |
* PyUpb_DescriptorPool_FindExtensionByName() |
| 331 |
* |
| 332 |
* Implements: |
| 333 |
* DescriptorPool.FindExtensionByName(self, name) |
| 334 |
*/ |
| 335 |
static PyObject* PyUpb_DescriptorPool_FindExtensionByName(PyObject* _self, |
| 336 |
PyObject* arg) { |
| 337 |
PyUpb_DescriptorPool* self = (PyUpb_DescriptorPool*)_self; |
| 338 |
|
| 339 |
const char* name = PyUpb_VerifyStrData(arg); |
| 340 |
if (!name) return NULL; |
| 341 |
|
| 342 |
const upb_FieldDef* field = |
| 343 |
upb_DefPool_FindExtensionByName(self->symtab, name); |
| 344 |
if (field == NULL && self->db) { |
| 345 |
if (!PyUpb_DescriptorPool_TryLoadSymbol(self, arg)) return NULL; |
| 346 |
field = upb_DefPool_FindExtensionByName(self->symtab, name); |
| 347 |
} |
| 348 |
if (field == NULL) { |
| 349 |
return PyErr_Format(PyExc_KeyError, "Couldn't find extension %.200s", name); |
| 350 |
} |
| 351 |
|
| 352 |
return PyUpb_FieldDescriptor_Get(field); |
| 353 |
} |
| 354 |
|
| 355 |
/* |
| 356 |
* PyUpb_DescriptorPool_FindMessageTypeByName() |
| 357 |
* |
| 358 |
* Implements: |
| 359 |
* DescriptorPool.FindMessageTypeByName(self, name) |
| 360 |
*/ |
| 361 |
static PyObject* PyUpb_DescriptorPool_FindMessageTypeByName(PyObject* _self, |
| 362 |
PyObject* arg) { |
| 363 |
PyUpb_DescriptorPool* self = (PyUpb_DescriptorPool*)_self; |
| 364 |
|
| 365 |
const char* name = PyUpb_VerifyStrData(arg); |
| 366 |
if (!name) return NULL; |
| 367 |
|
| 368 |
const upb_MessageDef* m = upb_DefPool_FindMessageByName(self->symtab, name); |
| 369 |
if (m == NULL && self->db) { |
| 370 |
if (!PyUpb_DescriptorPool_TryLoadSymbol(self, arg)) return NULL; |
| 371 |
m = upb_DefPool_FindMessageByName(self->symtab, name); |
| 372 |
} |
| 373 |
if (m == NULL) { |
| 374 |
return PyErr_Format(PyExc_KeyError, "Couldn't find message %.200s", name); |
| 375 |
} |
| 376 |
|
| 377 |
return PyUpb_Descriptor_Get(m); |
| 378 |
} |
| 379 |
|
| 380 |
// Splits a dotted symbol like foo.bar.baz on the last dot. Returns the portion |
| 381 |
// after the last dot (baz) and updates `*parent_size` to the length of the |
| 382 |
// parent (foo.bar). Returns NULL if no dots were present. |
| 383 |
static const char* PyUpb_DescriptorPool_SplitSymbolName(const char* sym, |
| 384 |
size_t* parent_size) { |
| 385 |
const char* last_dot = strrchr(sym, '.'); |
| 386 |
if (!last_dot) return NULL; |
| 387 |
*parent_size = last_dot - sym; |
| 388 |
return last_dot + 1; |
| 389 |
} |
| 390 |
|
| 391 |
/* |
| 392 |
* PyUpb_DescriptorPool_FindFieldByName() |
| 393 |
* |
| 394 |
* Implements: |
| 395 |
* DescriptorPool.FindFieldByName(self, name) |
| 396 |
*/ |
| 397 |
static PyObject* PyUpb_DescriptorPool_FindFieldByName(PyObject* _self, |
| 398 |
PyObject* arg) { |
| 399 |
PyUpb_DescriptorPool* self = (PyUpb_DescriptorPool*)_self; |
| 400 |
|
| 401 |
const char* name = PyUpb_VerifyStrData(arg); |
| 402 |
if (!name) return NULL; |
| 403 |
|
| 404 |
size_t parent_size; |
| 405 |
const char* child = PyUpb_DescriptorPool_SplitSymbolName(name, &parent_size); |
| 406 |
const upb_FieldDef* f = NULL; |
| 407 |
if (child) { |
| 408 |
const upb_MessageDef* parent = |
| 409 |
upb_DefPool_FindMessageByNameWithSize(self->symtab, name, parent_size); |
| 410 |
if (parent == NULL && self->db) { |
| 411 |
if (!PyUpb_DescriptorPool_TryLoadSymbol(self, arg)) return NULL; |
| 412 |
parent = upb_DefPool_FindMessageByNameWithSize(self->symtab, name, |
| 413 |
parent_size); |
| 414 |
} |
| 415 |
if (parent) { |
| 416 |
f = upb_MessageDef_FindFieldByName(parent, child); |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
if (!f) { |
| 421 |
return PyErr_Format(PyExc_KeyError, "Couldn't find message %.200s", name); |
| 422 |
} |
| 423 |
|
| 424 |
return PyUpb_FieldDescriptor_Get(f); |
| 425 |
} |
| 426 |
|
| 427 |
/* |
| 428 |
* PyUpb_DescriptorPool_FindEnumTypeByName() |
| 429 |
* |
| 430 |
* Implements: |
| 431 |
* DescriptorPool.FindEnumTypeByName(self, name) |
| 432 |
*/ |
| 433 |
static PyObject* PyUpb_DescriptorPool_FindEnumTypeByName(PyObject* _self, |
| 434 |
PyObject* arg) { |
| 435 |
PyUpb_DescriptorPool* self = (PyUpb_DescriptorPool*)_self; |
| 436 |
|
| 437 |
const char* name = PyUpb_VerifyStrData(arg); |
| 438 |
if (!name) return NULL; |
| 439 |
|
| 440 |
const upb_EnumDef* e = upb_DefPool_FindEnumByName(self->symtab, name); |
| 441 |
if (e == NULL && self->db) { |
| 442 |
if (!PyUpb_DescriptorPool_TryLoadSymbol(self, arg)) return NULL; |
| 443 |
e = upb_DefPool_FindEnumByName(self->symtab, name); |
| 444 |
} |
| 445 |
if (e == NULL) { |
| 446 |
return PyErr_Format(PyExc_KeyError, "Couldn't find enum %.200s", name); |
| 447 |
} |
| 448 |
|
| 449 |
return PyUpb_EnumDescriptor_Get(e); |
| 450 |
} |
| 451 |
|
| 452 |
/* |
| 453 |
* PyUpb_DescriptorPool_FindOneofByName() |
| 454 |
* |
| 455 |
* Implements: |
| 456 |
* DescriptorPool.FindOneofByName(self, name) |
| 457 |
*/ |
| 458 |
static PyObject* PyUpb_DescriptorPool_FindOneofByName(PyObject* _self, |
| 459 |
PyObject* arg) { |
| 460 |
PyUpb_DescriptorPool* self = (PyUpb_DescriptorPool*)_self; |
| 461 |
|
| 462 |
const char* name = PyUpb_VerifyStrData(arg); |
| 463 |
if (!name) return NULL; |
| 464 |
|
| 465 |
size_t parent_size; |
| 466 |
const char* child = PyUpb_DescriptorPool_SplitSymbolName(name, &parent_size); |
| 467 |
|
| 468 |
if (child) { |
| 469 |
const upb_MessageDef* parent = |
| 470 |
upb_DefPool_FindMessageByNameWithSize(self->symtab, name, parent_size); |
| 471 |
if (parent == NULL && self->db) { |
| 472 |
if (!PyUpb_DescriptorPool_TryLoadSymbol(self, arg)) return NULL; |
| 473 |
parent = upb_DefPool_FindMessageByNameWithSize(self->symtab, name, |
| 474 |
parent_size); |
| 475 |
} |
| 476 |
if (parent) { |
| 477 |
const upb_OneofDef* o = upb_MessageDef_FindOneofByName(parent, child); |
| 478 |
return PyUpb_OneofDescriptor_Get(o); |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
return PyErr_Format(PyExc_KeyError, "Couldn't find oneof %.200s", name); |
| 483 |
} |
| 484 |
|
| 485 |
static PyObject* PyUpb_DescriptorPool_FindServiceByName(PyObject* _self, |
| 486 |
PyObject* arg) { |
| 487 |
PyUpb_DescriptorPool* self = (PyUpb_DescriptorPool*)_self; |
| 488 |
|
| 489 |
const char* name = PyUpb_VerifyStrData(arg); |
| 490 |
if (!name) return NULL; |
| 491 |
|
| 492 |
const upb_ServiceDef* s = upb_DefPool_FindServiceByName(self->symtab, name); |
| 493 |
if (s == NULL && self->db) { |
| 494 |
if (!PyUpb_DescriptorPool_TryLoadSymbol(self, arg)) return NULL; |
| 495 |
s = upb_DefPool_FindServiceByName(self->symtab, name); |
| 496 |
} |
| 497 |
if (s == NULL) { |
| 498 |
return PyErr_Format(PyExc_KeyError, "Couldn't find service %.200s", name); |
| 499 |
} |
| 500 |
|
| 501 |
return PyUpb_ServiceDescriptor_Get(s); |
| 502 |
} |
| 503 |
|
| 504 |
static PyObject* PyUpb_DescriptorPool_FindMethodByName(PyObject* _self, |
| 505 |
PyObject* arg) { |
| 506 |
PyUpb_DescriptorPool* self = (PyUpb_DescriptorPool*)_self; |
| 507 |
|
| 508 |
const char* name = PyUpb_VerifyStrData(arg); |
| 509 |
if (!name) return NULL; |
| 510 |
size_t parent_size; |
| 511 |
const char* child = PyUpb_DescriptorPool_SplitSymbolName(name, &parent_size); |
| 512 |
|
| 513 |
if (!child) goto err; |
| 514 |
const upb_ServiceDef* parent = |
| 515 |
upb_DefPool_FindServiceByNameWithSize(self->symtab, name, parent_size); |
| 516 |
if (parent == NULL && self->db) { |
| 517 |
if (!PyUpb_DescriptorPool_TryLoadSymbol(self, arg)) return NULL; |
| 518 |
parent = |
| 519 |
upb_DefPool_FindServiceByNameWithSize(self->symtab, name, parent_size); |
| 520 |
} |
| 521 |
if (!parent) goto err; |
| 522 |
const upb_MethodDef* m = upb_ServiceDef_FindMethodByName(parent, child); |
| 523 |
if (!m) goto err; |
| 524 |
return PyUpb_MethodDescriptor_Get(m); |
| 525 |
|
| 526 |
err: |
| 527 |
return PyErr_Format(PyExc_KeyError, "Couldn't find method %.200s", name); |
| 528 |
} |
| 529 |
|
| 530 |
static PyObject* PyUpb_DescriptorPool_FindFileContainingSymbol(PyObject* _self, |
| 531 |
PyObject* arg) { |
| 532 |
PyUpb_DescriptorPool* self = (PyUpb_DescriptorPool*)_self; |
| 533 |
|
| 534 |
const char* name = PyUpb_VerifyStrData(arg); |
| 535 |
if (!name) return NULL; |
| 536 |
|
| 537 |
const upb_FileDef* f = |
| 538 |
upb_DefPool_FindFileContainingSymbol(self->symtab, name); |
| 539 |
if (f == NULL && self->db) { |
| 540 |
if (!PyUpb_DescriptorPool_TryLoadSymbol(self, arg)) return NULL; |
| 541 |
f = upb_DefPool_FindFileContainingSymbol(self->symtab, name); |
| 542 |
} |
| 543 |
if (f == NULL) { |
| 544 |
return PyErr_Format(PyExc_KeyError, "Couldn't find symbol %.200s", name); |
| 545 |
} |
| 546 |
|
| 547 |
return PyUpb_FileDescriptor_Get(f); |
| 548 |
} |
| 549 |
|
| 550 |
static PyObject* PyUpb_DescriptorPool_FindExtensionByNumber(PyObject* _self, |
| 551 |
PyObject* args) { |
| 552 |
PyUpb_DescriptorPool* self = (PyUpb_DescriptorPool*)_self; |
| 553 |
PyObject* message_descriptor; |
| 554 |
int number; |
| 555 |
if (!PyArg_ParseTuple(args, "Oi", &message_descriptor, &number)) { |
| 556 |
return NULL; |
| 557 |
} |
| 558 |
|
| 559 |
const upb_FieldDef* f = upb_DefPool_FindExtensionByNumber( |
| 560 |
self->symtab, PyUpb_Descriptor_GetDef(message_descriptor), number); |
| 561 |
if (f == NULL) { |
| 562 |
return PyErr_Format(PyExc_KeyError, "Couldn't find Extension %d", number); |
| 563 |
} |
| 564 |
|
| 565 |
return PyUpb_FieldDescriptor_Get(f); |
| 566 |
} |
| 567 |
|
| 568 |
static PyObject* PyUpb_DescriptorPool_FindAllExtensions(PyObject* _self, |
| 569 |
PyObject* msg_desc) { |
| 570 |
PyUpb_DescriptorPool* self = (PyUpb_DescriptorPool*)_self; |
| 571 |
const upb_MessageDef* m = PyUpb_Descriptor_GetDef(msg_desc); |
| 572 |
size_t n; |
| 573 |
const upb_FieldDef** ext = upb_DefPool_GetAllExtensions(self->symtab, m, &n); |
| 574 |
PyObject* ret = PyList_New(n); |
| 575 |
if (!ret) goto done; |
| 576 |
for (size_t i = 0; i < n; i++) { |
| 577 |
PyObject* field = PyUpb_FieldDescriptor_Get(ext[i]); |
| 578 |
if (!field) { |
| 579 |
Py_DECREF(ret); |
| 580 |
ret = NULL; |
| 581 |
goto done; |
| 582 |
} |
| 583 |
PyList_SetItem(ret, i, field); |
| 584 |
} |
| 585 |
done: |
| 586 |
free(ext); |
| 587 |
return ret; |
| 588 |
} |
| 589 |
|
| 590 |
static PyMethodDef PyUpb_DescriptorPool_Methods[] = { |
| 591 |
{"Add", PyUpb_DescriptorPool_Add, METH_O, |
| 592 |
"Adds the FileDescriptorProto and its types to this pool."}, |
| 593 |
{"AddSerializedFile", PyUpb_DescriptorPool_AddSerializedFile, METH_O, |
| 594 |
"Adds a serialized FileDescriptorProto to this pool."}, |
| 595 |
{"FindFileByName", PyUpb_DescriptorPool_FindFileByName, METH_O, |
| 596 |
"Searches for a file descriptor by its .proto name."}, |
| 597 |
{"FindMessageTypeByName", PyUpb_DescriptorPool_FindMessageTypeByName, |
| 598 |
METH_O, "Searches for a message descriptor by full name."}, |
| 599 |
{"FindFieldByName", PyUpb_DescriptorPool_FindFieldByName, METH_O, |
| 600 |
"Searches for a field descriptor by full name."}, |
| 601 |
{"FindExtensionByName", PyUpb_DescriptorPool_FindExtensionByName, METH_O, |
| 602 |
"Searches for extension descriptor by full name."}, |
| 603 |
{"FindEnumTypeByName", PyUpb_DescriptorPool_FindEnumTypeByName, METH_O, |
| 604 |
"Searches for enum type descriptor by full name."}, |
| 605 |
{"FindOneofByName", PyUpb_DescriptorPool_FindOneofByName, METH_O, |
| 606 |
"Searches for oneof descriptor by full name."}, |
| 607 |
{"FindServiceByName", PyUpb_DescriptorPool_FindServiceByName, METH_O, |
| 608 |
"Searches for service descriptor by full name."}, |
| 609 |
{"FindMethodByName", PyUpb_DescriptorPool_FindMethodByName, METH_O, |
| 610 |
"Searches for method descriptor by full name."}, |
| 611 |
{"FindFileContainingSymbol", PyUpb_DescriptorPool_FindFileContainingSymbol, |
| 612 |
METH_O, "Gets the FileDescriptor containing the specified symbol."}, |
| 613 |
{"FindExtensionByNumber", PyUpb_DescriptorPool_FindExtensionByNumber, |
| 614 |
METH_VARARGS, "Gets the extension descriptor for the given number."}, |
| 615 |
{"FindAllExtensions", PyUpb_DescriptorPool_FindAllExtensions, METH_O, |
| 616 |
"Gets all known extensions of the given message descriptor."}, |
| 617 |
{NULL}}; |
| 618 |
|
| 619 |
static PyType_Slot PyUpb_DescriptorPool_Slots[] = { |
| 620 |
{Py_tp_clear, PyUpb_DescriptorPool_Clear}, |
| 621 |
{Py_tp_dealloc, PyUpb_DescriptorPool_Dealloc}, |
| 622 |
{Py_tp_methods, PyUpb_DescriptorPool_Methods}, |
| 623 |
{Py_tp_new, PyUpb_DescriptorPool_New}, |
| 624 |
{Py_tp_traverse, PyUpb_DescriptorPool_Traverse}, |
| 625 |
{0, NULL}}; |
| 626 |
|
| 627 |
static PyType_Spec PyUpb_DescriptorPool_Spec = { |
| 628 |
PYUPB_MODULE_NAME ".DescriptorPool", |
| 629 |
sizeof(PyUpb_DescriptorPool), |
| 630 |
0, // tp_itemsize |
| 631 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
| 632 |
PyUpb_DescriptorPool_Slots, |
| 633 |
}; |
| 634 |
|
| 635 |
// ----------------------------------------------------------------------------- |
| 636 |
// Top Level |
| 637 |
// ----------------------------------------------------------------------------- |
| 638 |
|
| 639 |
bool PyUpb_InitDescriptorPool(PyObject* m) { |
| 640 |
PyUpb_ModuleState* state = PyUpb_ModuleState_GetFromModule(m); |
| 641 |
PyTypeObject* descriptor_pool_type = |
| 642 |
PyUpb_AddClass(m, &PyUpb_DescriptorPool_Spec); |
| 643 |
|
| 644 |
if (!descriptor_pool_type) return false; |
| 645 |
|
| 646 |
state->default_pool = PyUpb_DescriptorPool_DoCreateWithCache( |
| 647 |
descriptor_pool_type, NULL, state->obj_cache); |
| 648 |
return state->default_pool && |
| 649 |
PyModule_AddObject(m, "default_pool", state->default_pool) == 0; |
| 650 |
} |