forked from protocolbuffers/protobuf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
descriptor.c
1886 lines (1666 loc) · 75 KB
/
descriptor.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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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.h"
#include "python/convert.h"
#include "python/descriptor_containers.h"
#include "python/descriptor_pool.h"
#include "python/message.h"
#include "python/protobuf.h"
#include "upb/base/upcast.h"
#include "upb/reflection/def.h"
#include "upb/util/def_to_proto.h"
// -----------------------------------------------------------------------------
// DescriptorBase
// -----------------------------------------------------------------------------
// This representation is used by all concrete descriptors.
typedef struct {
PyObject_HEAD;
PyObject* pool; // We own a ref.
const void* def; // Type depends on the class. Kept alive by "pool".
PyObject* options; // NULL if not present or not cached.
PyObject* features; // NULL if not present or not cached.
PyObject* message_meta; // We own a ref.
} PyUpb_DescriptorBase;
PyObject* PyUpb_AnyDescriptor_GetPool(PyObject* desc) {
PyUpb_DescriptorBase* base = (void*)desc;
return base->pool;
}
const void* PyUpb_AnyDescriptor_GetDef(PyObject* desc) {
PyUpb_DescriptorBase* base = (void*)desc;
return base->def;
}
static PyUpb_DescriptorBase* PyUpb_DescriptorBase_DoCreate(
PyUpb_DescriptorType type, const void* def, const upb_FileDef* file) {
PyUpb_ModuleState* state = PyUpb_ModuleState_Get();
PyTypeObject* type_obj = state->descriptor_types[type];
assert(def);
PyUpb_DescriptorBase* base = (void*)PyType_GenericAlloc(type_obj, 0);
base->pool = PyUpb_DescriptorPool_Get(upb_FileDef_Pool(file));
base->def = def;
base->options = NULL;
base->features = NULL;
base->message_meta = NULL;
PyUpb_ObjCache_Add(def, &base->ob_base);
return base;
}
// Returns a Python object wrapping |def|, of descriptor type |type|. If a
// wrapper was previously created for this def, returns it, otherwise creates a
// new wrapper.
static PyObject* PyUpb_DescriptorBase_Get(PyUpb_DescriptorType type,
const void* def,
const upb_FileDef* file) {
PyUpb_DescriptorBase* base = (PyUpb_DescriptorBase*)PyUpb_ObjCache_Get(def);
if (!base) {
base = PyUpb_DescriptorBase_DoCreate(type, def, file);
}
return &base->ob_base;
}
static PyUpb_DescriptorBase* PyUpb_DescriptorBase_Check(
PyObject* obj, PyUpb_DescriptorType type) {
PyUpb_ModuleState* state = PyUpb_ModuleState_Get();
PyTypeObject* type_obj = state->descriptor_types[type];
if (!PyObject_TypeCheck(obj, type_obj)) {
PyErr_Format(PyExc_TypeError, "Expected object of type %S, but got %R",
type_obj, obj);
return NULL;
}
return (PyUpb_DescriptorBase*)obj;
}
static PyObject* PyUpb_DescriptorBase_GetCached(PyObject** cached,
const upb_Message* opts,
const upb_MiniTable* layout,
const char* msg_name,
const char* strip_field) {
if (!*cached) {
// Load descriptors protos if they are not loaded already. We have to do
// this lazily, otherwise, it would lead to circular imports.
PyObject* mod = PyImport_ImportModuleLevel(PYUPB_DESCRIPTOR_MODULE, NULL,
NULL, NULL, 0);
if (mod == NULL) return NULL;
Py_DECREF(mod);
// Find the correct options message.
PyObject* default_pool = PyUpb_DescriptorPool_GetDefaultPool();
const upb_DefPool* symtab = PyUpb_DescriptorPool_GetSymtab(default_pool);
const upb_MessageDef* m = upb_DefPool_FindMessageByName(symtab, msg_name);
assert(m);
// Copy the options message from C to Python using serialize+parse.
// We don't wrap the C object directly because there is no guarantee that
// the descriptor_pb2 that was loaded at runtime has the same members or
// layout as the C types that were compiled in.
size_t size;
PyObject* py_arena = PyUpb_Arena_New();
upb_Arena* arena = PyUpb_Arena_Get(py_arena);
char* pb;
// TODO: Need to correctly handle failed return codes.
(void)upb_Encode(opts, layout, 0, arena, &pb, &size);
const upb_MiniTable* opts2_layout = upb_MessageDef_MiniTable(m);
upb_Message* opts2 = upb_Message_New(opts2_layout, arena);
assert(opts2);
upb_DecodeStatus ds =
upb_Decode(pb, size, opts2, opts2_layout,
upb_DefPool_ExtensionRegistry(symtab), 0, arena);
(void)ds;
assert(ds == kUpb_DecodeStatus_Ok);
if (strip_field) {
const upb_FieldDef* field =
upb_MessageDef_FindFieldByName(m, strip_field);
assert(field);
upb_Message_ClearFieldByDef(opts2, field);
}
*cached = PyUpb_Message_Get(opts2, m, py_arena);
Py_DECREF(py_arena);
}
Py_INCREF(*cached);
return *cached;
}
static PyObject* PyUpb_DescriptorBase_GetOptions(PyObject** cached,
const upb_Message* opts,
const upb_MiniTable* layout,
const char* msg_name) {
return PyUpb_DescriptorBase_GetCached(cached, opts, layout, msg_name,
"features");
}
static PyObject* PyUpb_DescriptorBase_GetFeatures(PyObject** cached,
const upb_Message* opts) {
return PyUpb_DescriptorBase_GetCached(
cached, opts, &google__protobuf__FeatureSet_msg_init,
PYUPB_DESCRIPTOR_PROTO_PACKAGE ".FeatureSet", NULL);
}
typedef void* PyUpb_ToProto_Func(const void* def, upb_Arena* arena);
static PyObject* PyUpb_DescriptorBase_GetSerializedProto(
PyObject* _self, PyUpb_ToProto_Func* func, const upb_MiniTable* layout) {
PyUpb_DescriptorBase* self = (void*)_self;
upb_Arena* arena = upb_Arena_New();
if (!arena) PYUPB_RETURN_OOM;
upb_Message* proto = func(self->def, arena);
if (!proto) goto oom;
size_t size;
char* pb;
upb_EncodeStatus status = upb_Encode(proto, layout, 0, arena, &pb, &size);
if (status) goto oom; // TODO non-oom errors are possible here
PyObject* str = PyBytes_FromStringAndSize(pb, size);
upb_Arena_Free(arena);
return str;
oom:
upb_Arena_Free(arena);
PyErr_SetNone(PyExc_MemoryError);
return NULL;
}
static PyObject* PyUpb_DescriptorBase_CopyToProto(PyObject* _self,
PyUpb_ToProto_Func* func,
const upb_MiniTable* layout,
const char* expected_type,
PyObject* py_proto) {
if (!PyUpb_Message_Verify(py_proto)) return NULL;
const upb_MessageDef* m = PyUpb_Message_GetMsgdef(py_proto);
const char* type = upb_MessageDef_FullName(m);
if (strcmp(type, expected_type) != 0) {
PyErr_Format(
PyExc_TypeError,
"CopyToProto: message is of incorrect type '%s' (expected '%s'", type,
expected_type);
return NULL;
}
PyObject* serialized =
PyUpb_DescriptorBase_GetSerializedProto(_self, func, layout);
if (!serialized) return NULL;
PyObject* ret = PyUpb_Message_MergeFromString(py_proto, serialized);
Py_DECREF(serialized);
return ret;
}
static void PyUpb_DescriptorBase_Dealloc(PyUpb_DescriptorBase* base) {
// This deallocator can be called on different types (which, despite
// 'Base' in the name of one of them, do not inherit from each other).
// Some of these types are GC types (they have Py_TPFLAGS_HAVE_GC set),
// which means Python's GC can visit them (via tp_visit and/or tp_clear
// methods) at any time. This also means we *must* stop GC from tracking
// instances of them before we start destructing the object. In Python
// 3.11, failing to do so would raise a runtime warning.
if (PyType_HasFeature(Py_TYPE(base), Py_TPFLAGS_HAVE_GC)) {
PyObject_GC_UnTrack(base);
}
PyUpb_ObjCache_Delete(base->def);
// In addition to being visited by GC, instances can also (potentially) be
// accessed whenever arbitrary code is executed. Destructors can execute
// arbitrary code, so any struct members we DECREF should be set to NULL
// or a new value *before* calling Py_DECREF on them. The Py_CLEAR macro
// (and Py_SETREF in Python 3.8+) takes care to do this safely.
Py_CLEAR(base->message_meta);
Py_CLEAR(base->pool);
Py_CLEAR(base->options);
Py_CLEAR(base->features);
PyUpb_Dealloc(base);
}
static int PyUpb_Descriptor_Traverse(PyUpb_DescriptorBase* base,
visitproc visit, void* arg) {
Py_VISIT(base->message_meta);
return 0;
}
static int PyUpb_Descriptor_Clear(PyUpb_DescriptorBase* base) {
Py_CLEAR(base->message_meta);
return 0;
}
#define DESCRIPTOR_BASE_SLOTS \
{Py_tp_new, (void*)&PyUpb_Forbidden_New}, { \
Py_tp_dealloc, (void*)&PyUpb_DescriptorBase_Dealloc \
}
// -----------------------------------------------------------------------------
// Descriptor
// -----------------------------------------------------------------------------
PyObject* PyUpb_Descriptor_Get(const upb_MessageDef* m) {
assert(m);
const upb_FileDef* file = upb_MessageDef_File(m);
return PyUpb_DescriptorBase_Get(kPyUpb_Descriptor, m, file);
}
PyObject* PyUpb_Descriptor_GetClass(const upb_MessageDef* m) {
PyObject* ret = PyUpb_ObjCache_Get(upb_MessageDef_MiniTable(m));
if (ret) return ret;
// On demand create the clss if not exist. However, if users repeatedly
// create and destroy a class, it could trigger a loop. This is not an
// issue now, but if we see CPU waste for repeatedly create and destroy
// in the future, we could make PyUpb_Descriptor_Get() append the descriptor
// to an internal list in DescriptorPool, let the pool keep descriptors alive.
PyObject* py_descriptor = PyUpb_Descriptor_Get(m);
if (py_descriptor == NULL) return NULL;
const char* name = upb_MessageDef_Name(m);
PyObject* dict = PyDict_New();
if (dict == NULL) goto err;
int status = PyDict_SetItemString(dict, "DESCRIPTOR", py_descriptor);
if (status < 0) goto err;
ret = PyUpb_MessageMeta_DoCreateClass(py_descriptor, name, dict);
err:
Py_XDECREF(py_descriptor);
Py_XDECREF(dict);
return ret;
}
void PyUpb_Descriptor_SetClass(PyObject* py_descriptor, PyObject* meta) {
PyUpb_DescriptorBase* base = (PyUpb_DescriptorBase*)py_descriptor;
Py_INCREF(meta);
// Py_SETREF replaces strong references without an intermediate invalid
// object state, which code executed by base->message_meta's destructor
// might see, but it's Python 3.8+.
PyObject* tmp = base->message_meta;
base->message_meta = meta;
Py_XDECREF(tmp);
}
// The LookupNested*() functions provide name lookup for entities nested inside
// a message. This uses the symtab's table, which requires that the symtab is
// not being mutated concurrently. We can guarantee this for Python-owned
// symtabs, but upb cannot guarantee it in general for an arbitrary
// `const upb_MessageDef*`.
static const void* PyUpb_Descriptor_LookupNestedMessage(const upb_MessageDef* m,
const char* name) {
const upb_FileDef* filedef = upb_MessageDef_File(m);
const upb_DefPool* symtab = upb_FileDef_Pool(filedef);
PyObject* qname =
PyUnicode_FromFormat("%s.%s", upb_MessageDef_FullName(m), name);
const upb_MessageDef* ret = upb_DefPool_FindMessageByName(
symtab, PyUnicode_AsUTF8AndSize(qname, NULL));
Py_DECREF(qname);
return ret;
}
static const void* PyUpb_Descriptor_LookupNestedEnum(const upb_MessageDef* m,
const char* name) {
const upb_FileDef* filedef = upb_MessageDef_File(m);
const upb_DefPool* symtab = upb_FileDef_Pool(filedef);
PyObject* qname =
PyUnicode_FromFormat("%s.%s", upb_MessageDef_FullName(m), name);
const upb_EnumDef* ret =
upb_DefPool_FindEnumByName(symtab, PyUnicode_AsUTF8AndSize(qname, NULL));
Py_DECREF(qname);
return ret;
}
static const void* PyUpb_Descriptor_LookupNestedExtension(
const upb_MessageDef* m, const char* name) {
const upb_FileDef* filedef = upb_MessageDef_File(m);
const upb_DefPool* symtab = upb_FileDef_Pool(filedef);
PyObject* qname =
PyUnicode_FromFormat("%s.%s", upb_MessageDef_FullName(m), name);
const upb_FieldDef* ret = upb_DefPool_FindExtensionByName(
symtab, PyUnicode_AsUTF8AndSize(qname, NULL));
Py_DECREF(qname);
return ret;
}
static PyObject* PyUpb_Descriptor_GetExtensionRanges(PyObject* _self,
void* closure) {
PyUpb_DescriptorBase* self = (PyUpb_DescriptorBase*)_self;
int n = upb_MessageDef_ExtensionRangeCount(self->def);
PyObject* range_list = PyList_New(n);
for (int i = 0; i < n; i++) {
const upb_ExtensionRange* range =
upb_MessageDef_ExtensionRange(self->def, i);
PyObject* start = PyLong_FromLong(upb_ExtensionRange_Start(range));
PyObject* end = PyLong_FromLong(upb_ExtensionRange_End(range));
PyList_SetItem(range_list, i, PyTuple_Pack(2, start, end));
}
return range_list;
}
static PyObject* PyUpb_Descriptor_GetExtensions(PyObject* _self,
void* closure) {
PyUpb_DescriptorBase* self = (void*)_self;
static PyUpb_GenericSequence_Funcs funcs = {
(void*)&upb_MessageDef_NestedExtensionCount,
(void*)&upb_MessageDef_NestedExtension,
(void*)&PyUpb_FieldDescriptor_Get,
};
return PyUpb_GenericSequence_New(&funcs, self->def, self->pool);
}
static PyObject* PyUpb_Descriptor_GetExtensionsByName(PyObject* _self,
void* closure) {
PyUpb_DescriptorBase* self = (void*)_self;
static PyUpb_ByNameMap_Funcs funcs = {
{
(void*)&upb_MessageDef_NestedExtensionCount,
(void*)&upb_MessageDef_NestedExtension,
(void*)&PyUpb_FieldDescriptor_Get,
},
(void*)&PyUpb_Descriptor_LookupNestedExtension,
(void*)&upb_FieldDef_Name,
};
return PyUpb_ByNameMap_New(&funcs, self->def, self->pool);
}
static PyObject* PyUpb_Descriptor_GetEnumTypes(PyObject* _self, void* closure) {
PyUpb_DescriptorBase* self = (void*)_self;
static PyUpb_GenericSequence_Funcs funcs = {
(void*)&upb_MessageDef_NestedEnumCount,
(void*)&upb_MessageDef_NestedEnum,
(void*)&PyUpb_EnumDescriptor_Get,
};
return PyUpb_GenericSequence_New(&funcs, self->def, self->pool);
}
static PyObject* PyUpb_Descriptor_GetOneofs(PyObject* _self, void* closure) {
PyUpb_DescriptorBase* self = (void*)_self;
static PyUpb_GenericSequence_Funcs funcs = {
(void*)&upb_MessageDef_OneofCount,
(void*)&upb_MessageDef_Oneof,
(void*)&PyUpb_OneofDescriptor_Get,
};
return PyUpb_GenericSequence_New(&funcs, self->def, self->pool);
}
static PyObject* PyUpb_Descriptor_GetOptions(PyObject* _self, PyObject* args) {
PyUpb_DescriptorBase* self = (void*)_self;
return PyUpb_DescriptorBase_GetOptions(
&self->options, UPB_UPCAST(upb_MessageDef_Options(self->def)),
&google__protobuf__MessageOptions_msg_init,
PYUPB_DESCRIPTOR_PROTO_PACKAGE ".MessageOptions");
}
static PyObject* PyUpb_Descriptor_GetFeatures(PyObject* _self, PyObject* args) {
PyUpb_DescriptorBase* self = (void*)_self;
return PyUpb_DescriptorBase_GetFeatures(
&self->features, UPB_UPCAST(upb_MessageDef_ResolvedFeatures(self->def)));
}
static PyObject* PyUpb_Descriptor_CopyToProto(PyObject* _self,
PyObject* py_proto) {
return PyUpb_DescriptorBase_CopyToProto(
_self, (PyUpb_ToProto_Func*)&upb_MessageDef_ToProto,
&google__protobuf__DescriptorProto_msg_init,
PYUPB_DESCRIPTOR_PROTO_PACKAGE ".DescriptorProto", py_proto);
}
static PyObject* PyUpb_Descriptor_EnumValueName(PyObject* _self,
PyObject* args) {
PyUpb_DescriptorBase* self = (void*)_self;
const char* enum_name;
int number;
if (!PyArg_ParseTuple(args, "si", &enum_name, &number)) return NULL;
const upb_EnumDef* e =
PyUpb_Descriptor_LookupNestedEnum(self->def, enum_name);
if (!e) {
PyErr_SetString(PyExc_KeyError, enum_name);
return NULL;
}
const upb_EnumValueDef* ev = upb_EnumDef_FindValueByNumber(e, number);
if (!ev) {
PyErr_Format(PyExc_KeyError, "%d", number);
return NULL;
}
return PyUnicode_FromString(upb_EnumValueDef_Name(ev));
}
static PyObject* PyUpb_Descriptor_GetFieldsByName(PyObject* _self,
void* closure) {
PyUpb_DescriptorBase* self = (void*)_self;
static PyUpb_ByNameMap_Funcs funcs = {
{
(void*)&upb_MessageDef_FieldCount,
(void*)&upb_MessageDef_Field,
(void*)&PyUpb_FieldDescriptor_Get,
},
(void*)&upb_MessageDef_FindFieldByName,
(void*)&upb_FieldDef_Name,
};
return PyUpb_ByNameMap_New(&funcs, self->def, self->pool);
}
static PyObject* PyUpb_Descriptor_GetFieldsByCamelCaseName(PyObject* _self,
void* closure) {
PyUpb_DescriptorBase* self = (void*)_self;
static PyUpb_ByNameMap_Funcs funcs = {
{
(void*)&upb_MessageDef_FieldCount,
(void*)&upb_MessageDef_Field,
(void*)&PyUpb_FieldDescriptor_Get,
},
(void*)&upb_MessageDef_FindByJsonName,
(void*)&upb_FieldDef_JsonName,
};
return PyUpb_ByNameMap_New(&funcs, self->def, self->pool);
}
static PyObject* PyUpb_Descriptor_GetFieldsByNumber(PyObject* _self,
void* closure) {
static PyUpb_ByNumberMap_Funcs funcs = {
{
(void*)&upb_MessageDef_FieldCount,
(void*)&upb_MessageDef_Field,
(void*)&PyUpb_FieldDescriptor_Get,
},
(void*)&upb_MessageDef_FindFieldByNumber,
(void*)&upb_FieldDef_Number,
};
PyUpb_DescriptorBase* self = (void*)_self;
return PyUpb_ByNumberMap_New(&funcs, self->def, self->pool);
}
static PyObject* PyUpb_Descriptor_GetNestedTypes(PyObject* _self,
void* closure) {
PyUpb_DescriptorBase* self = (void*)_self;
static PyUpb_GenericSequence_Funcs funcs = {
(void*)&upb_MessageDef_NestedMessageCount,
(void*)&upb_MessageDef_NestedMessage,
(void*)&PyUpb_Descriptor_Get,
};
return PyUpb_GenericSequence_New(&funcs, self->def, self->pool);
}
static PyObject* PyUpb_Descriptor_GetNestedTypesByName(PyObject* _self,
void* closure) {
PyUpb_DescriptorBase* self = (void*)_self;
static PyUpb_ByNameMap_Funcs funcs = {
{
(void*)&upb_MessageDef_NestedMessageCount,
(void*)&upb_MessageDef_NestedMessage,
(void*)&PyUpb_Descriptor_Get,
},
(void*)&PyUpb_Descriptor_LookupNestedMessage,
(void*)&upb_MessageDef_Name,
};
return PyUpb_ByNameMap_New(&funcs, self->def, self->pool);
}
static PyObject* PyUpb_Descriptor_GetContainingType(PyObject* _self,
void* closure) {
// upb does not natively store the lexical parent of a message type, but we
// can derive it with some string manipulation and a lookup.
PyUpb_DescriptorBase* self = (void*)_self;
const upb_MessageDef* m = self->def;
const upb_FileDef* file = upb_MessageDef_File(m);
const upb_DefPool* symtab = upb_FileDef_Pool(file);
const char* full_name = upb_MessageDef_FullName(m);
const char* last_dot = strrchr(full_name, '.');
if (!last_dot) Py_RETURN_NONE;
const upb_MessageDef* parent = upb_DefPool_FindMessageByNameWithSize(
symtab, full_name, last_dot - full_name);
if (!parent) Py_RETURN_NONE;
return PyUpb_Descriptor_Get(parent);
}
static PyObject* PyUpb_Descriptor_GetEnumTypesByName(PyObject* _self,
void* closure) {
PyUpb_DescriptorBase* self = (void*)_self;
static PyUpb_ByNameMap_Funcs funcs = {
{
(void*)&upb_MessageDef_NestedEnumCount,
(void*)&upb_MessageDef_NestedEnum,
(void*)&PyUpb_EnumDescriptor_Get,
},
(void*)&PyUpb_Descriptor_LookupNestedEnum,
(void*)&upb_EnumDef_Name,
};
return PyUpb_ByNameMap_New(&funcs, self->def, self->pool);
}
static PyObject* PyUpb_Descriptor_GetIsExtendable(PyObject* _self,
void* closure) {
PyUpb_DescriptorBase* self = (void*)_self;
if (upb_MessageDef_ExtensionRangeCount(self->def) > 0) {
Py_RETURN_TRUE;
} else {
Py_RETURN_FALSE;
}
}
static PyObject* PyUpb_Descriptor_GetFullName(PyObject* self, void* closure) {
const upb_MessageDef* msgdef = PyUpb_Descriptor_GetDef(self);
return PyUnicode_FromString(upb_MessageDef_FullName(msgdef));
}
static PyObject* PyUpb_Descriptor_GetConcreteClass(PyObject* self,
void* closure) {
const upb_MessageDef* msgdef = PyUpb_Descriptor_GetDef(self);
return PyUpb_ObjCache_Get(upb_MessageDef_MiniTable(msgdef));
}
static PyObject* PyUpb_Descriptor_GetFile(PyObject* self, void* closure) {
const upb_MessageDef* msgdef = PyUpb_Descriptor_GetDef(self);
return PyUpb_FileDescriptor_Get(upb_MessageDef_File(msgdef));
}
static PyObject* PyUpb_Descriptor_GetFields(PyObject* _self, void* closure) {
PyUpb_DescriptorBase* self = (void*)_self;
static PyUpb_GenericSequence_Funcs funcs = {
(void*)&upb_MessageDef_FieldCount,
(void*)&upb_MessageDef_Field,
(void*)&PyUpb_FieldDescriptor_Get,
};
return PyUpb_GenericSequence_New(&funcs, self->def, self->pool);
}
static PyObject* PyUpb_Descriptor_GetHasOptions(PyObject* _self,
void* closure) {
PyUpb_DescriptorBase* self = (void*)_self;
return PyBool_FromLong(upb_MessageDef_HasOptions(self->def));
}
static PyObject* PyUpb_Descriptor_GetName(PyObject* self, void* closure) {
const upb_MessageDef* msgdef = PyUpb_Descriptor_GetDef(self);
return PyUnicode_FromString(upb_MessageDef_Name(msgdef));
}
static PyObject* PyUpb_Descriptor_GetEnumValuesByName(PyObject* _self,
void* closure) {
// upb does not natively store any table containing all nested values.
// Consider:
// message M {
// enum E1 {
// A = 0;
// B = 1;
// }
// enum E2 {
// C = 0;
// D = 1;
// }
// }
//
// In this case, upb stores tables for E1 and E2, but it does not store a
// table for M that combines them (it is rarely needed and costs precious
// space and time to build).
//
// To work around this, we build an actual Python dict whenever a user
// actually asks for this.
PyUpb_DescriptorBase* self = (void*)_self;
PyObject* ret = PyDict_New();
if (!ret) return NULL;
int enum_count = upb_MessageDef_NestedEnumCount(self->def);
for (int i = 0; i < enum_count; i++) {
const upb_EnumDef* e = upb_MessageDef_NestedEnum(self->def, i);
int value_count = upb_EnumDef_ValueCount(e);
for (int j = 0; j < value_count; j++) {
// Collisions should be impossible here, as uniqueness is checked by
// protoc (this is an invariant of the protobuf language). However this
// uniqueness constraint is not currently checked by upb/def.c at load
// time, so if the user supplies a manually-constructed descriptor that
// does not respect this constraint, a collision could be possible and the
// last-defined enumerator would win. This could be seen as an argument
// for having upb actually build the table at load time, thus checking the
// constraint proactively, but upb is always checking a subset of the full
// validation performed by C++, and we have to pick and choose the biggest
// bang for the buck.
const upb_EnumValueDef* ev = upb_EnumDef_Value(e, j);
const char* name = upb_EnumValueDef_Name(ev);
PyObject* val = PyUpb_EnumValueDescriptor_Get(ev);
if (!val || PyDict_SetItemString(ret, name, val) < 0) {
Py_XDECREF(val);
Py_DECREF(ret);
return NULL;
}
Py_DECREF(val);
}
}
return ret;
}
static PyObject* PyUpb_Descriptor_GetOneofsByName(PyObject* _self,
void* closure) {
PyUpb_DescriptorBase* self = (void*)_self;
static PyUpb_ByNameMap_Funcs funcs = {
{
(void*)&upb_MessageDef_OneofCount,
(void*)&upb_MessageDef_Oneof,
(void*)&PyUpb_OneofDescriptor_Get,
},
(void*)&upb_MessageDef_FindOneofByName,
(void*)&upb_OneofDef_Name,
};
return PyUpb_ByNameMap_New(&funcs, self->def, self->pool);
}
static PyGetSetDef PyUpb_Descriptor_Getters[] = {
{"name", PyUpb_Descriptor_GetName, NULL, "Last name"},
{"full_name", PyUpb_Descriptor_GetFullName, NULL, "Full name"},
{"_concrete_class", PyUpb_Descriptor_GetConcreteClass, NULL,
"concrete class"},
{"file", PyUpb_Descriptor_GetFile, NULL, "File descriptor"},
{"fields", PyUpb_Descriptor_GetFields, NULL, "Fields sequence"},
{"fields_by_name", PyUpb_Descriptor_GetFieldsByName, NULL,
"Fields by name"},
{"fields_by_camelcase_name", PyUpb_Descriptor_GetFieldsByCamelCaseName,
NULL, "Fields by camelCase name"},
{"fields_by_number", PyUpb_Descriptor_GetFieldsByNumber, NULL,
"Fields by number"},
{"nested_types", PyUpb_Descriptor_GetNestedTypes, NULL,
"Nested types sequence"},
{"nested_types_by_name", PyUpb_Descriptor_GetNestedTypesByName, NULL,
"Nested types by name"},
{"extensions", PyUpb_Descriptor_GetExtensions, NULL, "Extensions Sequence"},
{"extensions_by_name", PyUpb_Descriptor_GetExtensionsByName, NULL,
"Extensions by name"},
{"extension_ranges", PyUpb_Descriptor_GetExtensionRanges, NULL,
"Extension ranges"},
{"enum_types", PyUpb_Descriptor_GetEnumTypes, NULL, "Enum sequence"},
{"enum_types_by_name", PyUpb_Descriptor_GetEnumTypesByName, NULL,
"Enum types by name"},
{"enum_values_by_name", PyUpb_Descriptor_GetEnumValuesByName, NULL,
"Enum values by name"},
{"oneofs_by_name", PyUpb_Descriptor_GetOneofsByName, NULL,
"Oneofs by name"},
{"oneofs", PyUpb_Descriptor_GetOneofs, NULL, "Oneofs Sequence"},
{"containing_type", PyUpb_Descriptor_GetContainingType, NULL,
"Containing type"},
{"is_extendable", PyUpb_Descriptor_GetIsExtendable, NULL},
{"has_options", PyUpb_Descriptor_GetHasOptions, NULL, "Has Options"},
{NULL}};
static PyMethodDef PyUpb_Descriptor_Methods[] = {
{"GetOptions", PyUpb_Descriptor_GetOptions, METH_NOARGS},
{"_GetFeatures", PyUpb_Descriptor_GetFeatures, METH_NOARGS},
{"CopyToProto", PyUpb_Descriptor_CopyToProto, METH_O},
{"EnumValueName", PyUpb_Descriptor_EnumValueName, METH_VARARGS},
{NULL}};
static PyType_Slot PyUpb_Descriptor_Slots[] = {
DESCRIPTOR_BASE_SLOTS,
{Py_tp_methods, PyUpb_Descriptor_Methods},
{Py_tp_getset, PyUpb_Descriptor_Getters},
{Py_tp_traverse, PyUpb_Descriptor_Traverse},
{Py_tp_clear, PyUpb_Descriptor_Clear},
{0, NULL}};
static PyType_Spec PyUpb_Descriptor_Spec = {
PYUPB_MODULE_NAME ".Descriptor", // tp_name
sizeof(PyUpb_DescriptorBase), // tp_basicsize
0, // tp_itemsize
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, // tp_flags
PyUpb_Descriptor_Slots,
};
const upb_MessageDef* PyUpb_Descriptor_GetDef(PyObject* _self) {
PyUpb_DescriptorBase* self =
PyUpb_DescriptorBase_Check(_self, kPyUpb_Descriptor);
return self ? self->def : NULL;
}
// -----------------------------------------------------------------------------
// EnumDescriptor
// -----------------------------------------------------------------------------
PyObject* PyUpb_EnumDescriptor_Get(const upb_EnumDef* enumdef) {
const upb_FileDef* file = upb_EnumDef_File(enumdef);
return PyUpb_DescriptorBase_Get(kPyUpb_EnumDescriptor, enumdef, file);
}
const upb_EnumDef* PyUpb_EnumDescriptor_GetDef(PyObject* _self) {
PyUpb_DescriptorBase* self =
PyUpb_DescriptorBase_Check(_self, kPyUpb_EnumDescriptor);
return self ? self->def : NULL;
}
static PyObject* PyUpb_EnumDescriptor_GetFullName(PyObject* self,
void* closure) {
const upb_EnumDef* enumdef = PyUpb_EnumDescriptor_GetDef(self);
return PyUnicode_FromString(upb_EnumDef_FullName(enumdef));
}
static PyObject* PyUpb_EnumDescriptor_GetName(PyObject* self, void* closure) {
const upb_EnumDef* enumdef = PyUpb_EnumDescriptor_GetDef(self);
return PyUnicode_FromString(upb_EnumDef_Name(enumdef));
}
static PyObject* PyUpb_EnumDescriptor_GetFile(PyObject* self, void* closure) {
const upb_EnumDef* enumdef = PyUpb_EnumDescriptor_GetDef(self);
return PyUpb_FileDescriptor_Get(upb_EnumDef_File(enumdef));
}
static PyObject* PyUpb_EnumDescriptor_GetValues(PyObject* _self,
void* closure) {
PyUpb_DescriptorBase* self = (void*)_self;
static PyUpb_GenericSequence_Funcs funcs = {
(void*)&upb_EnumDef_ValueCount,
(void*)&upb_EnumDef_Value,
(void*)&PyUpb_EnumValueDescriptor_Get,
};
return PyUpb_GenericSequence_New(&funcs, self->def, self->pool);
}
static PyObject* PyUpb_EnumDescriptor_GetValuesByName(PyObject* _self,
void* closure) {
static PyUpb_ByNameMap_Funcs funcs = {
{
(void*)&upb_EnumDef_ValueCount,
(void*)&upb_EnumDef_Value,
(void*)&PyUpb_EnumValueDescriptor_Get,
},
(void*)&upb_EnumDef_FindValueByName,
(void*)&upb_EnumValueDef_Name,
};
PyUpb_DescriptorBase* self = (void*)_self;
return PyUpb_ByNameMap_New(&funcs, self->def, self->pool);
}
static PyObject* PyUpb_EnumDescriptor_GetValuesByNumber(PyObject* _self,
void* closure) {
static PyUpb_ByNumberMap_Funcs funcs = {
{
(void*)&upb_EnumDef_ValueCount,
(void*)&upb_EnumDef_Value,
(void*)&PyUpb_EnumValueDescriptor_Get,
},
(void*)&upb_EnumDef_FindValueByNumber,
(void*)&upb_EnumValueDef_Number,
};
PyUpb_DescriptorBase* self = (void*)_self;
return PyUpb_ByNumberMap_New(&funcs, self->def, self->pool);
}
static PyObject* PyUpb_EnumDescriptor_GetContainingType(PyObject* _self,
void* closure) {
PyUpb_DescriptorBase* self = (void*)_self;
const upb_MessageDef* m = upb_EnumDef_ContainingType(self->def);
if (!m) Py_RETURN_NONE;
return PyUpb_Descriptor_Get(m);
}
static PyObject* PyUpb_EnumDescriptor_GetHasOptions(PyObject* _self,
void* closure) {
PyUpb_DescriptorBase* self = (void*)_self;
return PyBool_FromLong(upb_EnumDef_HasOptions(self->def));
}
static PyObject* PyUpb_EnumDescriptor_GetIsClosed(PyObject* _self,
void* closure) {
const upb_EnumDef* enumdef = PyUpb_EnumDescriptor_GetDef(_self);
return PyBool_FromLong(upb_EnumDef_IsClosed(enumdef));
}
static PyObject* PyUpb_EnumDescriptor_GetOptions(PyObject* _self,
PyObject* args) {
PyUpb_DescriptorBase* self = (void*)_self;
return PyUpb_DescriptorBase_GetOptions(
&self->options, UPB_UPCAST(upb_EnumDef_Options(self->def)),
&google__protobuf__EnumOptions_msg_init,
PYUPB_DESCRIPTOR_PROTO_PACKAGE ".EnumOptions");
}
static PyObject* PyUpb_EnumDescriptor_GetFeatures(PyObject* _self,
PyObject* args) {
PyUpb_DescriptorBase* self = (void*)_self;
return PyUpb_DescriptorBase_GetFeatures(
&self->features, UPB_UPCAST(upb_EnumDef_ResolvedFeatures(self->def)));
}
static PyObject* PyUpb_EnumDescriptor_CopyToProto(PyObject* _self,
PyObject* py_proto) {
return PyUpb_DescriptorBase_CopyToProto(
_self, (PyUpb_ToProto_Func*)&upb_EnumDef_ToProto,
&google__protobuf__EnumDescriptorProto_msg_init,
PYUPB_DESCRIPTOR_PROTO_PACKAGE ".EnumDescriptorProto", py_proto);
}
static PyGetSetDef PyUpb_EnumDescriptor_Getters[] = {
{"full_name", PyUpb_EnumDescriptor_GetFullName, NULL, "Full name"},
{"name", PyUpb_EnumDescriptor_GetName, NULL, "last name"},
{"file", PyUpb_EnumDescriptor_GetFile, NULL, "File descriptor"},
{"values", PyUpb_EnumDescriptor_GetValues, NULL, "values"},
{"values_by_name", PyUpb_EnumDescriptor_GetValuesByName, NULL,
"Enum values by name"},
{"values_by_number", PyUpb_EnumDescriptor_GetValuesByNumber, NULL,
"Enum values by number"},
{"containing_type", PyUpb_EnumDescriptor_GetContainingType, NULL,
"Containing type"},
{"has_options", PyUpb_EnumDescriptor_GetHasOptions, NULL, "Has Options"},
{"is_closed", PyUpb_EnumDescriptor_GetIsClosed, NULL,
"Checks if the enum is closed"},
{NULL}};
static PyMethodDef PyUpb_EnumDescriptor_Methods[] = {
{"GetOptions", PyUpb_EnumDescriptor_GetOptions, METH_NOARGS},
{"_GetFeatures", PyUpb_EnumDescriptor_GetFeatures, METH_NOARGS},
{"CopyToProto", PyUpb_EnumDescriptor_CopyToProto, METH_O},
{NULL}};
static PyType_Slot PyUpb_EnumDescriptor_Slots[] = {
DESCRIPTOR_BASE_SLOTS,
{Py_tp_methods, PyUpb_EnumDescriptor_Methods},
{Py_tp_getset, PyUpb_EnumDescriptor_Getters},
{0, NULL}};
static PyType_Spec PyUpb_EnumDescriptor_Spec = {
PYUPB_MODULE_NAME ".EnumDescriptor", // tp_name
sizeof(PyUpb_DescriptorBase), // tp_basicsize
0, // tp_itemsize
Py_TPFLAGS_DEFAULT, // tp_flags
PyUpb_EnumDescriptor_Slots,
};
// -----------------------------------------------------------------------------
// EnumValueDescriptor
// -----------------------------------------------------------------------------
PyObject* PyUpb_EnumValueDescriptor_Get(const upb_EnumValueDef* ev) {
const upb_FileDef* file = upb_EnumDef_File(upb_EnumValueDef_Enum(ev));
return PyUpb_DescriptorBase_Get(kPyUpb_EnumValueDescriptor, ev, file);
}
static PyObject* PyUpb_EnumValueDescriptor_GetName(PyObject* self,
void* closure) {
PyUpb_DescriptorBase* base = (PyUpb_DescriptorBase*)self;
return PyUnicode_FromString(upb_EnumValueDef_Name(base->def));
}
static PyObject* PyUpb_EnumValueDescriptor_GetNumber(PyObject* self,
void* closure) {
PyUpb_DescriptorBase* base = (PyUpb_DescriptorBase*)self;
return PyLong_FromLong(upb_EnumValueDef_Number(base->def));
}
static PyObject* PyUpb_EnumValueDescriptor_GetIndex(PyObject* self,
void* closure) {
PyUpb_DescriptorBase* base = (PyUpb_DescriptorBase*)self;
return PyLong_FromLong(upb_EnumValueDef_Index(base->def));
}
static PyObject* PyUpb_EnumValueDescriptor_GetType(PyObject* self,
void* closure) {
PyUpb_DescriptorBase* base = (PyUpb_DescriptorBase*)self;
return PyUpb_EnumDescriptor_Get(upb_EnumValueDef_Enum(base->def));
}
static PyObject* PyUpb_EnumValueDescriptor_GetHasOptions(PyObject* _self,
void* closure) {
PyUpb_DescriptorBase* self = (void*)_self;
return PyBool_FromLong(upb_EnumValueDef_HasOptions(self->def));
}
static PyObject* PyUpb_EnumValueDescriptor_GetOptions(PyObject* _self,
PyObject* args) {
PyUpb_DescriptorBase* self = (void*)_self;
return PyUpb_DescriptorBase_GetOptions(
&self->options, UPB_UPCAST(upb_EnumValueDef_Options(self->def)),
&google__protobuf__EnumValueOptions_msg_init,
PYUPB_DESCRIPTOR_PROTO_PACKAGE ".EnumValueOptions");
}
static PyObject* PyUpb_EnumValueDescriptor_GetFeatures(PyObject* _self,
PyObject* args) {
PyUpb_DescriptorBase* self = (void*)_self;
return PyUpb_DescriptorBase_GetFeatures(
&self->features,
UPB_UPCAST(upb_EnumValueDef_ResolvedFeatures(self->def)));
}
static PyGetSetDef PyUpb_EnumValueDescriptor_Getters[] = {
{"name", PyUpb_EnumValueDescriptor_GetName, NULL, "name"},
{"number", PyUpb_EnumValueDescriptor_GetNumber, NULL, "number"},
{"index", PyUpb_EnumValueDescriptor_GetIndex, NULL, "index"},
{"type", PyUpb_EnumValueDescriptor_GetType, NULL, "index"},
{"has_options", PyUpb_EnumValueDescriptor_GetHasOptions, NULL,
"Has Options"},
{NULL}};
static PyMethodDef PyUpb_EnumValueDescriptor_Methods[] = {
{
"GetOptions",
PyUpb_EnumValueDescriptor_GetOptions,
METH_NOARGS,
},
{
"_GetFeatures",
PyUpb_EnumValueDescriptor_GetFeatures,
METH_NOARGS,
},
{NULL}};
static PyType_Slot PyUpb_EnumValueDescriptor_Slots[] = {
DESCRIPTOR_BASE_SLOTS,
{Py_tp_methods, PyUpb_EnumValueDescriptor_Methods},
{Py_tp_getset, PyUpb_EnumValueDescriptor_Getters},
{0, NULL}};
static PyType_Spec PyUpb_EnumValueDescriptor_Spec = {
PYUPB_MODULE_NAME ".EnumValueDescriptor", // tp_name
sizeof(PyUpb_DescriptorBase), // tp_basicsize
0, // tp_itemsize
Py_TPFLAGS_DEFAULT, // tp_flags
PyUpb_EnumValueDescriptor_Slots,
};
// -----------------------------------------------------------------------------
// FieldDescriptor
// -----------------------------------------------------------------------------
const upb_FieldDef* PyUpb_FieldDescriptor_GetDef(PyObject* _self) {
PyUpb_DescriptorBase* self =
PyUpb_DescriptorBase_Check(_self, kPyUpb_FieldDescriptor);
return self ? self->def : NULL;
}
PyObject* PyUpb_FieldDescriptor_Get(const upb_FieldDef* field) {
const upb_FileDef* file = upb_FieldDef_File(field);
return PyUpb_DescriptorBase_Get(kPyUpb_FieldDescriptor, field, file);
}
static PyObject* PyUpb_FieldDescriptor_GetFullName(PyUpb_DescriptorBase* self,
void* closure) {
return PyUnicode_FromString(upb_FieldDef_FullName(self->def));
}
static PyObject* PyUpb_FieldDescriptor_GetName(PyUpb_DescriptorBase* self,
void* closure) {
return PyUnicode_FromString(upb_FieldDef_Name(self->def));
}
static char PyUpb_AsciiIsUpper(char ch) { return ch >= 'A' && ch <= 'Z'; }
static char PyUpb_AsciiToLower(char ch) {
assert(PyUpb_AsciiIsUpper(ch));
return ch + ('a' - 'A');
}
static PyObject* PyUpb_FieldDescriptor_GetCamelCaseName(
PyUpb_DescriptorBase* self, void* closure) {
// Camelcase is equivalent to JSON name except for potentially the first
// character.
const char* name = upb_FieldDef_JsonName(self->def);
size_t size = strlen(name);
return size > 0 && PyUpb_AsciiIsUpper(name[0])
? PyUnicode_FromFormat("%c%s", PyUpb_AsciiToLower(name[0]),