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