descriptor.c 67 KB

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