店播爬取Python脚本

descriptor.cc 71KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Author: petar@google.com (Petar Petrov)
  31. #include <google/protobuf/pyext/descriptor.h>
  32. #include <Python.h>
  33. #include <frameobject.h>
  34. #include <cstdint>
  35. #include <string>
  36. #include <unordered_map>
  37. #include <google/protobuf/io/coded_stream.h>
  38. #include <google/protobuf/descriptor.pb.h>
  39. #include <google/protobuf/dynamic_message.h>
  40. #include <google/protobuf/pyext/descriptor_containers.h>
  41. #include <google/protobuf/pyext/descriptor_pool.h>
  42. #include <google/protobuf/pyext/message.h>
  43. #include <google/protobuf/pyext/message_factory.h>
  44. #include <google/protobuf/pyext/scoped_pyobject_ptr.h>
  45. #include <google/protobuf/stubs/hash.h>
  46. #if PY_MAJOR_VERSION >= 3
  47. #define PyString_FromStringAndSize PyUnicode_FromStringAndSize
  48. #define PyString_Check PyUnicode_Check
  49. #define PyString_InternFromString PyUnicode_InternFromString
  50. #define PyInt_FromLong PyLong_FromLong
  51. #define PyInt_FromSize_t PyLong_FromSize_t
  52. #if PY_VERSION_HEX < 0x03030000
  53. #error "Python 3.0 - 3.2 are not supported."
  54. #endif
  55. #define PyString_AsStringAndSize(ob, charpp, sizep) \
  56. (PyUnicode_Check(ob) ? ((*(charpp) = const_cast<char*>( \
  57. PyUnicode_AsUTF8AndSize(ob, (sizep)))) == NULL \
  58. ? -1 \
  59. : 0) \
  60. : PyBytes_AsStringAndSize(ob, (charpp), (sizep)))
  61. #endif
  62. namespace google {
  63. namespace protobuf {
  64. namespace python {
  65. // Store interned descriptors, so that the same C++ descriptor yields the same
  66. // Python object. Objects are not immortal: this map does not own the
  67. // references, and items are deleted when the last reference to the object is
  68. // released.
  69. // This is enough to support the "is" operator on live objects.
  70. // All descriptors are stored here.
  71. std::unordered_map<const void*, PyObject*>* interned_descriptors;
  72. PyObject* PyString_FromCppString(const std::string& str) {
  73. return PyString_FromStringAndSize(str.c_str(), str.size());
  74. }
  75. // Check that the calling Python code is the global scope of a _pb2.py module.
  76. // This function is used to support the current code generated by the proto
  77. // compiler, which creates descriptors, then update some properties.
  78. // For example:
  79. // message_descriptor = Descriptor(
  80. // name='Message',
  81. // fields = [FieldDescriptor(name='field')]
  82. // message_descriptor.fields[0].containing_type = message_descriptor
  83. //
  84. // This code is still executed, but the descriptors now have no other storage
  85. // than the (const) C++ pointer, and are immutable.
  86. // So we let this code pass, by simply ignoring the new value.
  87. //
  88. // From user code, descriptors still look immutable.
  89. //
  90. // TODO(amauryfa): Change the proto2 compiler to remove the assignments, and
  91. // remove this hack.
  92. bool _CalledFromGeneratedFile(int stacklevel) {
  93. #ifndef PYPY_VERSION
  94. // This check is not critical and is somewhat difficult to implement correctly
  95. // in PyPy.
  96. PyFrameObject* frame = PyEval_GetFrame();
  97. if (frame == NULL) {
  98. return false;
  99. }
  100. while (stacklevel-- > 0) {
  101. frame = frame->f_back;
  102. if (frame == NULL) {
  103. return false;
  104. }
  105. }
  106. if (frame->f_code->co_filename == NULL) {
  107. return false;
  108. }
  109. char* filename;
  110. Py_ssize_t filename_size;
  111. if (PyString_AsStringAndSize(frame->f_code->co_filename,
  112. &filename, &filename_size) < 0) {
  113. // filename is not a string.
  114. PyErr_Clear();
  115. return false;
  116. }
  117. if ((filename_size < 3) ||
  118. (strcmp(&filename[filename_size - 3], ".py") != 0)) {
  119. // Cython's stack does not have .py file name and is not at global module
  120. // scope.
  121. return true;
  122. }
  123. if (filename_size < 7) {
  124. // filename is too short.
  125. return false;
  126. }
  127. if (strcmp(&filename[filename_size - 7], "_pb2.py") != 0) {
  128. // Filename is not ending with _pb2.
  129. return false;
  130. }
  131. if (frame->f_globals != frame->f_locals) {
  132. // Not at global module scope
  133. return false;
  134. }
  135. #endif
  136. return true;
  137. }
  138. // If the calling code is not a _pb2.py file, raise AttributeError.
  139. // To be used in attribute setters.
  140. static int CheckCalledFromGeneratedFile(const char* attr_name) {
  141. if (_CalledFromGeneratedFile(0)) {
  142. return 0;
  143. }
  144. PyErr_Format(PyExc_AttributeError,
  145. "attribute is not writable: %s", attr_name);
  146. return -1;
  147. }
  148. #ifndef PyVarObject_HEAD_INIT
  149. #define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size,
  150. #endif
  151. #ifndef Py_TYPE
  152. #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
  153. #endif
  154. // Helper functions for descriptor objects.
  155. // A set of templates to retrieve the C++ FileDescriptor of any descriptor.
  156. template<class DescriptorClass>
  157. const FileDescriptor* GetFileDescriptor(const DescriptorClass* descriptor) {
  158. return descriptor->file();
  159. }
  160. template<>
  161. const FileDescriptor* GetFileDescriptor(const FileDescriptor* descriptor) {
  162. return descriptor;
  163. }
  164. template<>
  165. const FileDescriptor* GetFileDescriptor(const EnumValueDescriptor* descriptor) {
  166. return descriptor->type()->file();
  167. }
  168. template<>
  169. const FileDescriptor* GetFileDescriptor(const OneofDescriptor* descriptor) {
  170. return descriptor->containing_type()->file();
  171. }
  172. template<>
  173. const FileDescriptor* GetFileDescriptor(const MethodDescriptor* descriptor) {
  174. return descriptor->service()->file();
  175. }
  176. bool Reparse(
  177. PyMessageFactory* message_factory, const Message& from, Message* to) {
  178. // Reparse message.
  179. std::string serialized;
  180. from.SerializeToString(&serialized);
  181. io::CodedInputStream input(
  182. reinterpret_cast<const uint8_t*>(serialized.c_str()), serialized.size());
  183. input.SetExtensionRegistry(message_factory->pool->pool,
  184. message_factory->message_factory);
  185. bool success = to->ParseFromCodedStream(&input);
  186. if (!success) {
  187. return false;
  188. }
  189. return true;
  190. }
  191. // Converts options into a Python protobuf, and cache the result.
  192. //
  193. // This is a bit tricky because options can contain extension fields defined in
  194. // the same proto file. In this case the options parsed from the serialized_pb
  195. // have unknown fields, and we need to parse them again.
  196. //
  197. // Always returns a new reference.
  198. template<class DescriptorClass>
  199. static PyObject* GetOrBuildOptions(const DescriptorClass *descriptor) {
  200. // Options are cached in the pool that owns the descriptor.
  201. // First search in the cache.
  202. PyDescriptorPool* caching_pool = GetDescriptorPool_FromPool(
  203. GetFileDescriptor(descriptor)->pool());
  204. std::unordered_map<const void*, PyObject*>* descriptor_options =
  205. caching_pool->descriptor_options;
  206. if (descriptor_options->find(descriptor) != descriptor_options->end()) {
  207. PyObject *value = (*descriptor_options)[descriptor];
  208. Py_INCREF(value);
  209. return value;
  210. }
  211. // Similar to the C++ implementation, we return an Options object from the
  212. // default (generated) factory, so that client code know that they can use
  213. // extensions from generated files:
  214. // d.GetOptions().Extensions[some_pb2.extension]
  215. //
  216. // The consequence is that extensions not defined in the default pool won't
  217. // be available. If needed, we could add an optional 'message_factory'
  218. // parameter to the GetOptions() function.
  219. PyMessageFactory* message_factory =
  220. GetDefaultDescriptorPool()->py_message_factory;
  221. // Build the Options object: get its Python class, and make a copy of the C++
  222. // read-only instance.
  223. const Message& options(descriptor->options());
  224. const Descriptor *message_type = options.GetDescriptor();
  225. CMessageClass* message_class = message_factory::GetOrCreateMessageClass(
  226. message_factory, message_type);
  227. if (message_class == NULL) {
  228. PyErr_Format(PyExc_TypeError, "Could not retrieve class for Options: %s",
  229. message_type->full_name().c_str());
  230. return NULL;
  231. }
  232. ScopedPyObjectPtr args(PyTuple_New(0));
  233. ScopedPyObjectPtr value(
  234. PyObject_Call(message_class->AsPyObject(), args.get(), NULL));
  235. Py_DECREF(message_class);
  236. if (value == NULL) {
  237. return NULL;
  238. }
  239. if (!PyObject_TypeCheck(value.get(), CMessage_Type)) {
  240. PyErr_Format(PyExc_TypeError, "Invalid class for %s: %s",
  241. message_type->full_name().c_str(),
  242. Py_TYPE(value.get())->tp_name);
  243. return NULL;
  244. }
  245. CMessage* cmsg = reinterpret_cast<CMessage*>(value.get());
  246. const Reflection* reflection = options.GetReflection();
  247. const UnknownFieldSet& unknown_fields(reflection->GetUnknownFields(options));
  248. if (unknown_fields.empty()) {
  249. cmsg->message->CopyFrom(options);
  250. } else {
  251. // Reparse options string! XXX call cmessage::MergeFromString
  252. if (!Reparse(message_factory, options, cmsg->message)) {
  253. PyErr_Format(PyExc_ValueError, "Error reparsing Options message");
  254. return NULL;
  255. }
  256. }
  257. // Cache the result.
  258. Py_INCREF(value.get());
  259. (*descriptor_options)[descriptor] = value.get();
  260. return value.release();
  261. }
  262. // Copy the C++ descriptor to a Python message.
  263. // The Python message is an instance of descriptor_pb2.DescriptorProto
  264. // or similar.
  265. template<class DescriptorProtoClass, class DescriptorClass>
  266. static PyObject* CopyToPythonProto(const DescriptorClass *descriptor,
  267. PyObject *target) {
  268. const Descriptor* self_descriptor =
  269. DescriptorProtoClass::default_instance().GetDescriptor();
  270. CMessage* message = reinterpret_cast<CMessage*>(target);
  271. if (!PyObject_TypeCheck(target, CMessage_Type) ||
  272. message->message->GetDescriptor() != self_descriptor) {
  273. PyErr_Format(PyExc_TypeError, "Not a %s message",
  274. self_descriptor->full_name().c_str());
  275. return NULL;
  276. }
  277. cmessage::AssureWritable(message);
  278. DescriptorProtoClass* descriptor_message =
  279. static_cast<DescriptorProtoClass*>(message->message);
  280. descriptor->CopyTo(descriptor_message);
  281. // Custom options might in unknown extensions. Reparse
  282. // the descriptor_message. Can't skip reparse when options unknown
  283. // fields is empty, because they might in sub descriptors' options.
  284. PyMessageFactory* message_factory =
  285. GetDefaultDescriptorPool()->py_message_factory;
  286. if (!Reparse(message_factory, *descriptor_message, descriptor_message)) {
  287. PyErr_Format(PyExc_ValueError, "Error reparsing descriptor message");
  288. return nullptr;
  289. }
  290. Py_RETURN_NONE;
  291. }
  292. // All Descriptors classes share the same memory layout.
  293. typedef struct PyBaseDescriptor {
  294. PyObject_HEAD
  295. // Pointer to the C++ proto2 descriptor.
  296. // Like all descriptors, it is owned by the global DescriptorPool.
  297. const void* descriptor;
  298. // Owned reference to the DescriptorPool, to ensure it is kept alive.
  299. PyDescriptorPool* pool;
  300. } PyBaseDescriptor;
  301. // FileDescriptor structure "inherits" from the base descriptor.
  302. typedef struct PyFileDescriptor {
  303. PyBaseDescriptor base;
  304. // The cached version of serialized pb. Either NULL, or a Bytes string.
  305. // We own the reference.
  306. PyObject *serialized_pb;
  307. } PyFileDescriptor;
  308. namespace descriptor {
  309. // Creates or retrieve a Python descriptor of the specified type.
  310. // Objects are interned: the same descriptor will return the same object if it
  311. // was kept alive.
  312. // 'was_created' is an optional pointer to a bool, and is set to true if a new
  313. // object was allocated.
  314. // Always return a new reference.
  315. template<class DescriptorClass>
  316. PyObject* NewInternedDescriptor(PyTypeObject* type,
  317. const DescriptorClass* descriptor,
  318. bool* was_created) {
  319. if (was_created) {
  320. *was_created = false;
  321. }
  322. if (descriptor == NULL) {
  323. PyErr_BadInternalCall();
  324. return NULL;
  325. }
  326. // See if the object is in the map of interned descriptors
  327. std::unordered_map<const void*, PyObject*>::iterator it =
  328. interned_descriptors->find(descriptor);
  329. if (it != interned_descriptors->end()) {
  330. GOOGLE_DCHECK(Py_TYPE(it->second) == type);
  331. Py_INCREF(it->second);
  332. return it->second;
  333. }
  334. // Create a new descriptor object
  335. PyBaseDescriptor* py_descriptor = PyObject_GC_New(
  336. PyBaseDescriptor, type);
  337. if (py_descriptor == NULL) {
  338. return NULL;
  339. }
  340. py_descriptor->descriptor = descriptor;
  341. // and cache it.
  342. interned_descriptors->insert(
  343. std::make_pair(descriptor, reinterpret_cast<PyObject*>(py_descriptor)));
  344. // Ensures that the DescriptorPool stays alive.
  345. PyDescriptorPool* pool = GetDescriptorPool_FromPool(
  346. GetFileDescriptor(descriptor)->pool());
  347. if (pool == NULL) {
  348. // Don't DECREF, the object is not fully initialized.
  349. PyObject_Del(py_descriptor);
  350. return NULL;
  351. }
  352. Py_INCREF(pool);
  353. py_descriptor->pool = pool;
  354. PyObject_GC_Track(py_descriptor);
  355. if (was_created) {
  356. *was_created = true;
  357. }
  358. return reinterpret_cast<PyObject*>(py_descriptor);
  359. }
  360. static void Dealloc(PyObject* pself) {
  361. PyBaseDescriptor* self = reinterpret_cast<PyBaseDescriptor*>(pself);
  362. // Remove from interned dictionary
  363. interned_descriptors->erase(self->descriptor);
  364. Py_CLEAR(self->pool);
  365. Py_TYPE(self)->tp_free(pself);
  366. }
  367. static int GcTraverse(PyObject* pself, visitproc visit, void* arg) {
  368. PyBaseDescriptor* self = reinterpret_cast<PyBaseDescriptor*>(pself);
  369. Py_VISIT(self->pool);
  370. return 0;
  371. }
  372. static int GcClear(PyObject* pself) {
  373. PyBaseDescriptor* self = reinterpret_cast<PyBaseDescriptor*>(pself);
  374. Py_CLEAR(self->pool);
  375. return 0;
  376. }
  377. static PyGetSetDef Getters[] = {
  378. {NULL}
  379. };
  380. PyTypeObject PyBaseDescriptor_Type = {
  381. PyVarObject_HEAD_INIT(&PyType_Type, 0) FULL_MODULE_NAME
  382. ".DescriptorBase", // tp_name
  383. sizeof(PyBaseDescriptor), // tp_basicsize
  384. 0, // tp_itemsize
  385. (destructor)Dealloc, // tp_dealloc
  386. 0, // tp_print
  387. 0, // tp_getattr
  388. 0, // tp_setattr
  389. 0, // tp_compare
  390. 0, // tp_repr
  391. 0, // tp_as_number
  392. 0, // tp_as_sequence
  393. 0, // tp_as_mapping
  394. 0, // tp_hash
  395. 0, // tp_call
  396. 0, // tp_str
  397. 0, // tp_getattro
  398. 0, // tp_setattro
  399. 0, // tp_as_buffer
  400. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, // tp_flags
  401. "Descriptors base class", // tp_doc
  402. GcTraverse, // tp_traverse
  403. GcClear, // tp_clear
  404. 0, // tp_richcompare
  405. 0, // tp_weaklistoffset
  406. 0, // tp_iter
  407. 0, // tp_iternext
  408. 0, // tp_methods
  409. 0, // tp_members
  410. Getters, // tp_getset
  411. };
  412. } // namespace descriptor
  413. const void* PyDescriptor_AsVoidPtr(PyObject* obj) {
  414. if (!PyObject_TypeCheck(obj, &descriptor::PyBaseDescriptor_Type)) {
  415. PyErr_SetString(PyExc_TypeError, "Not a BaseDescriptor");
  416. return NULL;
  417. }
  418. return reinterpret_cast<PyBaseDescriptor*>(obj)->descriptor;
  419. }
  420. namespace message_descriptor {
  421. // Unchecked accessor to the C++ pointer.
  422. static const Descriptor* _GetDescriptor(PyBaseDescriptor* self) {
  423. return reinterpret_cast<const Descriptor*>(self->descriptor);
  424. }
  425. static PyObject* GetName(PyBaseDescriptor* self, void *closure) {
  426. return PyString_FromCppString(_GetDescriptor(self)->name());
  427. }
  428. static PyObject* GetFullName(PyBaseDescriptor* self, void *closure) {
  429. return PyString_FromCppString(_GetDescriptor(self)->full_name());
  430. }
  431. static PyObject* GetFile(PyBaseDescriptor *self, void *closure) {
  432. return PyFileDescriptor_FromDescriptor(_GetDescriptor(self)->file());
  433. }
  434. static PyObject* GetConcreteClass(PyBaseDescriptor* self, void *closure) {
  435. // Returns the canonical class for the given descriptor.
  436. // This is the class that was registered with the primary descriptor pool
  437. // which contains this descriptor.
  438. // This might not be the one you expect! For example the returned object does
  439. // not know about extensions defined in a custom pool.
  440. CMessageClass* concrete_class(message_factory::GetMessageClass(
  441. GetDescriptorPool_FromPool(
  442. _GetDescriptor(self)->file()->pool())->py_message_factory,
  443. _GetDescriptor(self)));
  444. Py_XINCREF(concrete_class);
  445. return concrete_class->AsPyObject();
  446. }
  447. static PyObject* GetFieldsByName(PyBaseDescriptor* self, void *closure) {
  448. return NewMessageFieldsByName(_GetDescriptor(self));
  449. }
  450. static PyObject* GetFieldsByCamelcaseName(PyBaseDescriptor* self,
  451. void *closure) {
  452. return NewMessageFieldsByCamelcaseName(_GetDescriptor(self));
  453. }
  454. static PyObject* GetFieldsByNumber(PyBaseDescriptor* self, void *closure) {
  455. return NewMessageFieldsByNumber(_GetDescriptor(self));
  456. }
  457. static PyObject* GetFieldsSeq(PyBaseDescriptor* self, void *closure) {
  458. return NewMessageFieldsSeq(_GetDescriptor(self));
  459. }
  460. static PyObject* GetNestedTypesByName(PyBaseDescriptor* self, void *closure) {
  461. return NewMessageNestedTypesByName(_GetDescriptor(self));
  462. }
  463. static PyObject* GetNestedTypesSeq(PyBaseDescriptor* self, void *closure) {
  464. return NewMessageNestedTypesSeq(_GetDescriptor(self));
  465. }
  466. static PyObject* GetExtensionsByName(PyBaseDescriptor* self, void *closure) {
  467. return NewMessageExtensionsByName(_GetDescriptor(self));
  468. }
  469. static PyObject* GetExtensions(PyBaseDescriptor* self, void *closure) {
  470. return NewMessageExtensionsSeq(_GetDescriptor(self));
  471. }
  472. static PyObject* GetEnumsSeq(PyBaseDescriptor* self, void *closure) {
  473. return NewMessageEnumsSeq(_GetDescriptor(self));
  474. }
  475. static PyObject* GetEnumTypesByName(PyBaseDescriptor* self, void *closure) {
  476. return NewMessageEnumsByName(_GetDescriptor(self));
  477. }
  478. static PyObject* GetEnumValuesByName(PyBaseDescriptor* self, void *closure) {
  479. return NewMessageEnumValuesByName(_GetDescriptor(self));
  480. }
  481. static PyObject* GetOneofsByName(PyBaseDescriptor* self, void *closure) {
  482. return NewMessageOneofsByName(_GetDescriptor(self));
  483. }
  484. static PyObject* GetOneofsSeq(PyBaseDescriptor* self, void *closure) {
  485. return NewMessageOneofsSeq(_GetDescriptor(self));
  486. }
  487. static PyObject* IsExtendable(PyBaseDescriptor *self, void *closure) {
  488. if (_GetDescriptor(self)->extension_range_count() > 0) {
  489. Py_RETURN_TRUE;
  490. } else {
  491. Py_RETURN_FALSE;
  492. }
  493. }
  494. static PyObject* GetExtensionRanges(PyBaseDescriptor *self, void *closure) {
  495. const Descriptor* descriptor = _GetDescriptor(self);
  496. PyObject* range_list = PyList_New(descriptor->extension_range_count());
  497. for (int i = 0; i < descriptor->extension_range_count(); i++) {
  498. const Descriptor::ExtensionRange* range = descriptor->extension_range(i);
  499. PyObject* start = PyInt_FromLong(range->start);
  500. PyObject* end = PyInt_FromLong(range->end);
  501. PyList_SetItem(range_list, i, PyTuple_Pack(2, start, end));
  502. }
  503. return range_list;
  504. }
  505. static PyObject* GetContainingType(PyBaseDescriptor *self, void *closure) {
  506. const Descriptor* containing_type =
  507. _GetDescriptor(self)->containing_type();
  508. if (containing_type) {
  509. return PyMessageDescriptor_FromDescriptor(containing_type);
  510. } else {
  511. Py_RETURN_NONE;
  512. }
  513. }
  514. static int SetContainingType(PyBaseDescriptor *self, PyObject *value,
  515. void *closure) {
  516. return CheckCalledFromGeneratedFile("containing_type");
  517. }
  518. static PyObject* GetHasOptions(PyBaseDescriptor *self, void *closure) {
  519. const MessageOptions& options(_GetDescriptor(self)->options());
  520. if (&options != &MessageOptions::default_instance()) {
  521. Py_RETURN_TRUE;
  522. } else {
  523. Py_RETURN_FALSE;
  524. }
  525. }
  526. static int SetHasOptions(PyBaseDescriptor *self, PyObject *value,
  527. void *closure) {
  528. return CheckCalledFromGeneratedFile("has_options");
  529. }
  530. static PyObject* GetOptions(PyBaseDescriptor *self) {
  531. return GetOrBuildOptions(_GetDescriptor(self));
  532. }
  533. static int SetOptions(PyBaseDescriptor *self, PyObject *value,
  534. void *closure) {
  535. return CheckCalledFromGeneratedFile("_options");
  536. }
  537. static int SetSerializedOptions(PyBaseDescriptor *self, PyObject *value,
  538. void *closure) {
  539. return CheckCalledFromGeneratedFile("_serialized_options");
  540. }
  541. static PyObject* CopyToProto(PyBaseDescriptor *self, PyObject *target) {
  542. return CopyToPythonProto<DescriptorProto>(_GetDescriptor(self), target);
  543. }
  544. static PyObject* EnumValueName(PyBaseDescriptor *self, PyObject *args) {
  545. const char *enum_name;
  546. int number;
  547. if (!PyArg_ParseTuple(args, "si", &enum_name, &number))
  548. return NULL;
  549. const EnumDescriptor *enum_type =
  550. _GetDescriptor(self)->FindEnumTypeByName(enum_name);
  551. if (enum_type == NULL) {
  552. PyErr_SetString(PyExc_KeyError, enum_name);
  553. return NULL;
  554. }
  555. const EnumValueDescriptor *enum_value =
  556. enum_type->FindValueByNumber(number);
  557. if (enum_value == NULL) {
  558. PyErr_Format(PyExc_KeyError, "%d", number);
  559. return NULL;
  560. }
  561. return PyString_FromCppString(enum_value->name());
  562. }
  563. static PyObject* GetSyntax(PyBaseDescriptor *self, void *closure) {
  564. return PyString_InternFromString(
  565. FileDescriptor::SyntaxName(_GetDescriptor(self)->file()->syntax()));
  566. }
  567. static PyGetSetDef Getters[] = {
  568. { "name", (getter)GetName, NULL, "Last name"},
  569. { "full_name", (getter)GetFullName, NULL, "Full name"},
  570. { "_concrete_class", (getter)GetConcreteClass, NULL, "concrete class"},
  571. { "file", (getter)GetFile, NULL, "File descriptor"},
  572. { "fields", (getter)GetFieldsSeq, NULL, "Fields sequence"},
  573. { "fields_by_name", (getter)GetFieldsByName, NULL, "Fields by name"},
  574. { "fields_by_camelcase_name", (getter)GetFieldsByCamelcaseName, NULL,
  575. "Fields by camelCase name"},
  576. { "fields_by_number", (getter)GetFieldsByNumber, NULL, "Fields by number"},
  577. { "nested_types", (getter)GetNestedTypesSeq, NULL, "Nested types sequence"},
  578. { "nested_types_by_name", (getter)GetNestedTypesByName, NULL,
  579. "Nested types by name"},
  580. { "extensions", (getter)GetExtensions, NULL, "Extensions Sequence"},
  581. { "extensions_by_name", (getter)GetExtensionsByName, NULL,
  582. "Extensions by name"},
  583. { "extension_ranges", (getter)GetExtensionRanges, NULL, "Extension ranges"},
  584. { "enum_types", (getter)GetEnumsSeq, NULL, "Enum sequence"},
  585. { "enum_types_by_name", (getter)GetEnumTypesByName, NULL,
  586. "Enum types by name"},
  587. { "enum_values_by_name", (getter)GetEnumValuesByName, NULL,
  588. "Enum values by name"},
  589. { "oneofs_by_name", (getter)GetOneofsByName, NULL, "Oneofs by name"},
  590. { "oneofs", (getter)GetOneofsSeq, NULL, "Oneofs by name"},
  591. { "containing_type", (getter)GetContainingType, (setter)SetContainingType,
  592. "Containing type"},
  593. { "is_extendable", (getter)IsExtendable, (setter)NULL},
  594. { "has_options", (getter)GetHasOptions, (setter)SetHasOptions, "Has Options"},
  595. { "_options", (getter)NULL, (setter)SetOptions, "Options"},
  596. { "_serialized_options", (getter)NULL, (setter)SetSerializedOptions,
  597. "Serialized Options"},
  598. { "syntax", (getter)GetSyntax, (setter)NULL, "Syntax"},
  599. {NULL}
  600. };
  601. static PyMethodDef Methods[] = {
  602. { "GetOptions", (PyCFunction)GetOptions, METH_NOARGS, },
  603. { "CopyToProto", (PyCFunction)CopyToProto, METH_O, },
  604. { "EnumValueName", (PyCFunction)EnumValueName, METH_VARARGS, },
  605. {NULL}
  606. };
  607. } // namespace message_descriptor
  608. PyTypeObject PyMessageDescriptor_Type = {
  609. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  610. FULL_MODULE_NAME ".MessageDescriptor", // tp_name
  611. sizeof(PyBaseDescriptor), // tp_basicsize
  612. 0, // tp_itemsize
  613. 0, // tp_dealloc
  614. 0, // tp_print
  615. 0, // tp_getattr
  616. 0, // tp_setattr
  617. 0, // tp_compare
  618. 0, // tp_repr
  619. 0, // tp_as_number
  620. 0, // tp_as_sequence
  621. 0, // tp_as_mapping
  622. 0, // tp_hash
  623. 0, // tp_call
  624. 0, // tp_str
  625. 0, // tp_getattro
  626. 0, // tp_setattro
  627. 0, // tp_as_buffer
  628. Py_TPFLAGS_DEFAULT, // tp_flags
  629. "A Message Descriptor", // tp_doc
  630. 0, // tp_traverse
  631. 0, // tp_clear
  632. 0, // tp_richcompare
  633. 0, // tp_weaklistoffset
  634. 0, // tp_iter
  635. 0, // tp_iternext
  636. message_descriptor::Methods, // tp_methods
  637. 0, // tp_members
  638. message_descriptor::Getters, // tp_getset
  639. &descriptor::PyBaseDescriptor_Type, // tp_base
  640. };
  641. PyObject* PyMessageDescriptor_FromDescriptor(
  642. const Descriptor* message_descriptor) {
  643. return descriptor::NewInternedDescriptor(
  644. &PyMessageDescriptor_Type, message_descriptor, NULL);
  645. }
  646. const Descriptor* PyMessageDescriptor_AsDescriptor(PyObject* obj) {
  647. if (!PyObject_TypeCheck(obj, &PyMessageDescriptor_Type)) {
  648. PyErr_SetString(PyExc_TypeError, "Not a MessageDescriptor");
  649. return NULL;
  650. }
  651. return reinterpret_cast<const Descriptor*>(
  652. reinterpret_cast<PyBaseDescriptor*>(obj)->descriptor);
  653. }
  654. namespace field_descriptor {
  655. // Unchecked accessor to the C++ pointer.
  656. static const FieldDescriptor* _GetDescriptor(
  657. PyBaseDescriptor *self) {
  658. return reinterpret_cast<const FieldDescriptor*>(self->descriptor);
  659. }
  660. static PyObject* GetFullName(PyBaseDescriptor* self, void *closure) {
  661. return PyString_FromCppString(_GetDescriptor(self)->full_name());
  662. }
  663. static PyObject* GetName(PyBaseDescriptor *self, void *closure) {
  664. return PyString_FromCppString(_GetDescriptor(self)->name());
  665. }
  666. static PyObject* GetCamelcaseName(PyBaseDescriptor* self, void *closure) {
  667. return PyString_FromCppString(_GetDescriptor(self)->camelcase_name());
  668. }
  669. static PyObject* GetJsonName(PyBaseDescriptor* self, void *closure) {
  670. return PyString_FromCppString(_GetDescriptor(self)->json_name());
  671. }
  672. static PyObject* GetFile(PyBaseDescriptor *self, void *closure) {
  673. return PyFileDescriptor_FromDescriptor(_GetDescriptor(self)->file());
  674. }
  675. static PyObject* GetType(PyBaseDescriptor *self, void *closure) {
  676. return PyInt_FromLong(_GetDescriptor(self)->type());
  677. }
  678. static PyObject* GetCppType(PyBaseDescriptor *self, void *closure) {
  679. return PyInt_FromLong(_GetDescriptor(self)->cpp_type());
  680. }
  681. static PyObject* GetLabel(PyBaseDescriptor *self, void *closure) {
  682. return PyInt_FromLong(_GetDescriptor(self)->label());
  683. }
  684. static PyObject* GetNumber(PyBaseDescriptor *self, void *closure) {
  685. return PyInt_FromLong(_GetDescriptor(self)->number());
  686. }
  687. static PyObject* GetIndex(PyBaseDescriptor *self, void *closure) {
  688. return PyInt_FromLong(_GetDescriptor(self)->index());
  689. }
  690. static PyObject* GetID(PyBaseDescriptor *self, void *closure) {
  691. return PyLong_FromVoidPtr(self);
  692. }
  693. static PyObject* IsExtension(PyBaseDescriptor *self, void *closure) {
  694. return PyBool_FromLong(_GetDescriptor(self)->is_extension());
  695. }
  696. static PyObject* HasDefaultValue(PyBaseDescriptor *self, void *closure) {
  697. return PyBool_FromLong(_GetDescriptor(self)->has_default_value());
  698. }
  699. static PyObject* GetDefaultValue(PyBaseDescriptor *self, void *closure) {
  700. PyObject *result;
  701. if (_GetDescriptor(self)->is_repeated()) {
  702. return PyList_New(0);
  703. }
  704. switch (_GetDescriptor(self)->cpp_type()) {
  705. case FieldDescriptor::CPPTYPE_INT32: {
  706. int32_t value = _GetDescriptor(self)->default_value_int32();
  707. result = PyInt_FromLong(value);
  708. break;
  709. }
  710. case FieldDescriptor::CPPTYPE_INT64: {
  711. int64_t value = _GetDescriptor(self)->default_value_int64();
  712. result = PyLong_FromLongLong(value);
  713. break;
  714. }
  715. case FieldDescriptor::CPPTYPE_UINT32: {
  716. uint32_t value = _GetDescriptor(self)->default_value_uint32();
  717. result = PyInt_FromSize_t(value);
  718. break;
  719. }
  720. case FieldDescriptor::CPPTYPE_UINT64: {
  721. uint64_t value = _GetDescriptor(self)->default_value_uint64();
  722. result = PyLong_FromUnsignedLongLong(value);
  723. break;
  724. }
  725. case FieldDescriptor::CPPTYPE_FLOAT: {
  726. float value = _GetDescriptor(self)->default_value_float();
  727. result = PyFloat_FromDouble(value);
  728. break;
  729. }
  730. case FieldDescriptor::CPPTYPE_DOUBLE: {
  731. double value = _GetDescriptor(self)->default_value_double();
  732. result = PyFloat_FromDouble(value);
  733. break;
  734. }
  735. case FieldDescriptor::CPPTYPE_BOOL: {
  736. bool value = _GetDescriptor(self)->default_value_bool();
  737. result = PyBool_FromLong(value);
  738. break;
  739. }
  740. case FieldDescriptor::CPPTYPE_STRING: {
  741. const std::string& value = _GetDescriptor(self)->default_value_string();
  742. result = ToStringObject(_GetDescriptor(self), value);
  743. break;
  744. }
  745. case FieldDescriptor::CPPTYPE_ENUM: {
  746. const EnumValueDescriptor* value =
  747. _GetDescriptor(self)->default_value_enum();
  748. result = PyInt_FromLong(value->number());
  749. break;
  750. }
  751. case FieldDescriptor::CPPTYPE_MESSAGE: {
  752. Py_RETURN_NONE;
  753. break;
  754. }
  755. default:
  756. PyErr_Format(PyExc_NotImplementedError, "default value for %s",
  757. _GetDescriptor(self)->full_name().c_str());
  758. return NULL;
  759. }
  760. return result;
  761. }
  762. static PyObject* GetCDescriptor(PyObject *self, void *closure) {
  763. Py_INCREF(self);
  764. return self;
  765. }
  766. static PyObject *GetEnumType(PyBaseDescriptor *self, void *closure) {
  767. const EnumDescriptor* enum_type = _GetDescriptor(self)->enum_type();
  768. if (enum_type) {
  769. return PyEnumDescriptor_FromDescriptor(enum_type);
  770. } else {
  771. Py_RETURN_NONE;
  772. }
  773. }
  774. static int SetEnumType(PyBaseDescriptor *self, PyObject *value, void *closure) {
  775. return CheckCalledFromGeneratedFile("enum_type");
  776. }
  777. static PyObject *GetMessageType(PyBaseDescriptor *self, void *closure) {
  778. const Descriptor* message_type = _GetDescriptor(self)->message_type();
  779. if (message_type) {
  780. return PyMessageDescriptor_FromDescriptor(message_type);
  781. } else {
  782. Py_RETURN_NONE;
  783. }
  784. }
  785. static int SetMessageType(PyBaseDescriptor *self, PyObject *value,
  786. void *closure) {
  787. return CheckCalledFromGeneratedFile("message_type");
  788. }
  789. static PyObject* GetContainingType(PyBaseDescriptor *self, void *closure) {
  790. const Descriptor* containing_type =
  791. _GetDescriptor(self)->containing_type();
  792. if (containing_type) {
  793. return PyMessageDescriptor_FromDescriptor(containing_type);
  794. } else {
  795. Py_RETURN_NONE;
  796. }
  797. }
  798. static int SetContainingType(PyBaseDescriptor *self, PyObject *value,
  799. void *closure) {
  800. return CheckCalledFromGeneratedFile("containing_type");
  801. }
  802. static PyObject* GetExtensionScope(PyBaseDescriptor *self, void *closure) {
  803. const auto* desc = _GetDescriptor(self);
  804. const Descriptor* extension_scope =
  805. desc->is_extension() ? desc->extension_scope() : nullptr;
  806. if (extension_scope) {
  807. return PyMessageDescriptor_FromDescriptor(extension_scope);
  808. } else {
  809. Py_RETURN_NONE;
  810. }
  811. }
  812. static PyObject* GetContainingOneof(PyBaseDescriptor *self, void *closure) {
  813. const OneofDescriptor* containing_oneof =
  814. _GetDescriptor(self)->containing_oneof();
  815. if (containing_oneof) {
  816. return PyOneofDescriptor_FromDescriptor(containing_oneof);
  817. } else {
  818. Py_RETURN_NONE;
  819. }
  820. }
  821. static int SetContainingOneof(PyBaseDescriptor *self, PyObject *value,
  822. void *closure) {
  823. return CheckCalledFromGeneratedFile("containing_oneof");
  824. }
  825. static PyObject* GetHasOptions(PyBaseDescriptor *self, void *closure) {
  826. const FieldOptions& options(_GetDescriptor(self)->options());
  827. if (&options != &FieldOptions::default_instance()) {
  828. Py_RETURN_TRUE;
  829. } else {
  830. Py_RETURN_FALSE;
  831. }
  832. }
  833. static int SetHasOptions(PyBaseDescriptor *self, PyObject *value,
  834. void *closure) {
  835. return CheckCalledFromGeneratedFile("has_options");
  836. }
  837. static PyObject* GetOptions(PyBaseDescriptor *self) {
  838. return GetOrBuildOptions(_GetDescriptor(self));
  839. }
  840. static int SetOptions(PyBaseDescriptor *self, PyObject *value,
  841. void *closure) {
  842. return CheckCalledFromGeneratedFile("_options");
  843. }
  844. static int SetSerializedOptions(PyBaseDescriptor *self, PyObject *value,
  845. void *closure) {
  846. return CheckCalledFromGeneratedFile("_serialized_options");
  847. }
  848. static PyGetSetDef Getters[] = {
  849. { "full_name", (getter)GetFullName, NULL, "Full name"},
  850. { "name", (getter)GetName, NULL, "Unqualified name"},
  851. { "camelcase_name", (getter)GetCamelcaseName, NULL, "Camelcase name"},
  852. { "json_name", (getter)GetJsonName, NULL, "Json name"},
  853. { "file", (getter)GetFile, NULL, "File Descriptor"},
  854. { "type", (getter)GetType, NULL, "C++ Type"},
  855. { "cpp_type", (getter)GetCppType, NULL, "C++ Type"},
  856. { "label", (getter)GetLabel, NULL, "Label"},
  857. { "number", (getter)GetNumber, NULL, "Number"},
  858. { "index", (getter)GetIndex, NULL, "Index"},
  859. { "default_value", (getter)GetDefaultValue, NULL, "Default Value"},
  860. { "has_default_value", (getter)HasDefaultValue},
  861. { "is_extension", (getter)IsExtension, NULL, "ID"},
  862. { "id", (getter)GetID, NULL, "ID"},
  863. { "_cdescriptor", (getter)GetCDescriptor, NULL, "HAACK REMOVE ME"},
  864. { "message_type", (getter)GetMessageType, (setter)SetMessageType,
  865. "Message type"},
  866. { "enum_type", (getter)GetEnumType, (setter)SetEnumType, "Enum type"},
  867. { "containing_type", (getter)GetContainingType, (setter)SetContainingType,
  868. "Containing type"},
  869. { "extension_scope", (getter)GetExtensionScope, (setter)NULL,
  870. "Extension scope"},
  871. { "containing_oneof", (getter)GetContainingOneof, (setter)SetContainingOneof,
  872. "Containing oneof"},
  873. { "has_options", (getter)GetHasOptions, (setter)SetHasOptions, "Has Options"},
  874. { "_options", (getter)NULL, (setter)SetOptions, "Options"},
  875. { "_serialized_options", (getter)NULL, (setter)SetSerializedOptions,
  876. "Serialized Options"},
  877. {NULL}
  878. };
  879. static PyMethodDef Methods[] = {
  880. { "GetOptions", (PyCFunction)GetOptions, METH_NOARGS, },
  881. {NULL}
  882. };
  883. } // namespace field_descriptor
  884. PyTypeObject PyFieldDescriptor_Type = {
  885. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  886. FULL_MODULE_NAME ".FieldDescriptor", // tp_name
  887. sizeof(PyBaseDescriptor), // tp_basicsize
  888. 0, // tp_itemsize
  889. 0, // tp_dealloc
  890. 0, // tp_print
  891. 0, // tp_getattr
  892. 0, // tp_setattr
  893. 0, // tp_compare
  894. 0, // tp_repr
  895. 0, // tp_as_number
  896. 0, // tp_as_sequence
  897. 0, // tp_as_mapping
  898. 0, // tp_hash
  899. 0, // tp_call
  900. 0, // tp_str
  901. 0, // tp_getattro
  902. 0, // tp_setattro
  903. 0, // tp_as_buffer
  904. Py_TPFLAGS_DEFAULT, // tp_flags
  905. "A Field Descriptor", // tp_doc
  906. 0, // tp_traverse
  907. 0, // tp_clear
  908. 0, // tp_richcompare
  909. 0, // tp_weaklistoffset
  910. 0, // tp_iter
  911. 0, // tp_iternext
  912. field_descriptor::Methods, // tp_methods
  913. 0, // tp_members
  914. field_descriptor::Getters, // tp_getset
  915. &descriptor::PyBaseDescriptor_Type, // tp_base
  916. };
  917. PyObject* PyFieldDescriptor_FromDescriptor(
  918. const FieldDescriptor* field_descriptor) {
  919. return descriptor::NewInternedDescriptor(
  920. &PyFieldDescriptor_Type, field_descriptor, NULL);
  921. }
  922. const FieldDescriptor* PyFieldDescriptor_AsDescriptor(PyObject* obj) {
  923. if (!PyObject_TypeCheck(obj, &PyFieldDescriptor_Type)) {
  924. PyErr_SetString(PyExc_TypeError, "Not a FieldDescriptor");
  925. return NULL;
  926. }
  927. return reinterpret_cast<const FieldDescriptor*>(
  928. reinterpret_cast<PyBaseDescriptor*>(obj)->descriptor);
  929. }
  930. namespace enum_descriptor {
  931. // Unchecked accessor to the C++ pointer.
  932. static const EnumDescriptor* _GetDescriptor(
  933. PyBaseDescriptor *self) {
  934. return reinterpret_cast<const EnumDescriptor*>(self->descriptor);
  935. }
  936. static PyObject* GetFullName(PyBaseDescriptor* self, void *closure) {
  937. return PyString_FromCppString(_GetDescriptor(self)->full_name());
  938. }
  939. static PyObject* GetName(PyBaseDescriptor *self, void *closure) {
  940. return PyString_FromCppString(_GetDescriptor(self)->name());
  941. }
  942. static PyObject* GetFile(PyBaseDescriptor *self, void *closure) {
  943. return PyFileDescriptor_FromDescriptor(_GetDescriptor(self)->file());
  944. }
  945. static PyObject* GetEnumvaluesByName(PyBaseDescriptor* self, void *closure) {
  946. return NewEnumValuesByName(_GetDescriptor(self));
  947. }
  948. static PyObject* GetEnumvaluesByNumber(PyBaseDescriptor* self, void *closure) {
  949. return NewEnumValuesByNumber(_GetDescriptor(self));
  950. }
  951. static PyObject* GetEnumvaluesSeq(PyBaseDescriptor* self, void *closure) {
  952. return NewEnumValuesSeq(_GetDescriptor(self));
  953. }
  954. static PyObject* GetContainingType(PyBaseDescriptor *self, void *closure) {
  955. const Descriptor* containing_type =
  956. _GetDescriptor(self)->containing_type();
  957. if (containing_type) {
  958. return PyMessageDescriptor_FromDescriptor(containing_type);
  959. } else {
  960. Py_RETURN_NONE;
  961. }
  962. }
  963. static int SetContainingType(PyBaseDescriptor *self, PyObject *value,
  964. void *closure) {
  965. return CheckCalledFromGeneratedFile("containing_type");
  966. }
  967. static PyObject* GetHasOptions(PyBaseDescriptor *self, void *closure) {
  968. const EnumOptions& options(_GetDescriptor(self)->options());
  969. if (&options != &EnumOptions::default_instance()) {
  970. Py_RETURN_TRUE;
  971. } else {
  972. Py_RETURN_FALSE;
  973. }
  974. }
  975. static int SetHasOptions(PyBaseDescriptor *self, PyObject *value,
  976. void *closure) {
  977. return CheckCalledFromGeneratedFile("has_options");
  978. }
  979. static PyObject* GetOptions(PyBaseDescriptor *self) {
  980. return GetOrBuildOptions(_GetDescriptor(self));
  981. }
  982. static int SetOptions(PyBaseDescriptor *self, PyObject *value,
  983. void *closure) {
  984. return CheckCalledFromGeneratedFile("_options");
  985. }
  986. static int SetSerializedOptions(PyBaseDescriptor *self, PyObject *value,
  987. void *closure) {
  988. return CheckCalledFromGeneratedFile("_serialized_options");
  989. }
  990. static PyObject* CopyToProto(PyBaseDescriptor *self, PyObject *target) {
  991. return CopyToPythonProto<EnumDescriptorProto>(_GetDescriptor(self), target);
  992. }
  993. static PyMethodDef Methods[] = {
  994. { "GetOptions", (PyCFunction)GetOptions, METH_NOARGS, },
  995. { "CopyToProto", (PyCFunction)CopyToProto, METH_O, },
  996. {NULL}
  997. };
  998. static PyGetSetDef Getters[] = {
  999. { "full_name", (getter)GetFullName, NULL, "Full name"},
  1000. { "name", (getter)GetName, NULL, "last name"},
  1001. { "file", (getter)GetFile, NULL, "File descriptor"},
  1002. { "values", (getter)GetEnumvaluesSeq, NULL, "values"},
  1003. { "values_by_name", (getter)GetEnumvaluesByName, NULL,
  1004. "Enum values by name"},
  1005. { "values_by_number", (getter)GetEnumvaluesByNumber, NULL,
  1006. "Enum values by number"},
  1007. { "containing_type", (getter)GetContainingType, (setter)SetContainingType,
  1008. "Containing type"},
  1009. { "has_options", (getter)GetHasOptions, (setter)SetHasOptions, "Has Options"},
  1010. { "_options", (getter)NULL, (setter)SetOptions, "Options"},
  1011. { "_serialized_options", (getter)NULL, (setter)SetSerializedOptions,
  1012. "Serialized Options"},
  1013. {NULL}
  1014. };
  1015. } // namespace enum_descriptor
  1016. PyTypeObject PyEnumDescriptor_Type = {
  1017. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  1018. FULL_MODULE_NAME ".EnumDescriptor", // tp_name
  1019. sizeof(PyBaseDescriptor), // tp_basicsize
  1020. 0, // tp_itemsize
  1021. 0, // tp_dealloc
  1022. 0, // tp_print
  1023. 0, // tp_getattr
  1024. 0, // tp_setattr
  1025. 0, // tp_compare
  1026. 0, // tp_repr
  1027. 0, // tp_as_number
  1028. 0, // tp_as_sequence
  1029. 0, // tp_as_mapping
  1030. 0, // tp_hash
  1031. 0, // tp_call
  1032. 0, // tp_str
  1033. 0, // tp_getattro
  1034. 0, // tp_setattro
  1035. 0, // tp_as_buffer
  1036. Py_TPFLAGS_DEFAULT, // tp_flags
  1037. "A Enum Descriptor", // tp_doc
  1038. 0, // tp_traverse
  1039. 0, // tp_clear
  1040. 0, // tp_richcompare
  1041. 0, // tp_weaklistoffset
  1042. 0, // tp_iter
  1043. 0, // tp_iternext
  1044. enum_descriptor::Methods, // tp_methods
  1045. 0, // tp_members
  1046. enum_descriptor::Getters, // tp_getset
  1047. &descriptor::PyBaseDescriptor_Type, // tp_base
  1048. };
  1049. PyObject* PyEnumDescriptor_FromDescriptor(
  1050. const EnumDescriptor* enum_descriptor) {
  1051. return descriptor::NewInternedDescriptor(
  1052. &PyEnumDescriptor_Type, enum_descriptor, NULL);
  1053. }
  1054. const EnumDescriptor* PyEnumDescriptor_AsDescriptor(PyObject* obj) {
  1055. if (!PyObject_TypeCheck(obj, &PyEnumDescriptor_Type)) {
  1056. PyErr_SetString(PyExc_TypeError, "Not an EnumDescriptor");
  1057. return NULL;
  1058. }
  1059. return reinterpret_cast<const EnumDescriptor*>(
  1060. reinterpret_cast<PyBaseDescriptor*>(obj)->descriptor);
  1061. }
  1062. namespace enumvalue_descriptor {
  1063. // Unchecked accessor to the C++ pointer.
  1064. static const EnumValueDescriptor* _GetDescriptor(
  1065. PyBaseDescriptor *self) {
  1066. return reinterpret_cast<const EnumValueDescriptor*>(self->descriptor);
  1067. }
  1068. static PyObject* GetName(PyBaseDescriptor *self, void *closure) {
  1069. return PyString_FromCppString(_GetDescriptor(self)->name());
  1070. }
  1071. static PyObject* GetNumber(PyBaseDescriptor *self, void *closure) {
  1072. return PyInt_FromLong(_GetDescriptor(self)->number());
  1073. }
  1074. static PyObject* GetIndex(PyBaseDescriptor *self, void *closure) {
  1075. return PyInt_FromLong(_GetDescriptor(self)->index());
  1076. }
  1077. static PyObject* GetType(PyBaseDescriptor *self, void *closure) {
  1078. return PyEnumDescriptor_FromDescriptor(_GetDescriptor(self)->type());
  1079. }
  1080. static PyObject* GetHasOptions(PyBaseDescriptor *self, void *closure) {
  1081. const EnumValueOptions& options(_GetDescriptor(self)->options());
  1082. if (&options != &EnumValueOptions::default_instance()) {
  1083. Py_RETURN_TRUE;
  1084. } else {
  1085. Py_RETURN_FALSE;
  1086. }
  1087. }
  1088. static int SetHasOptions(PyBaseDescriptor *self, PyObject *value,
  1089. void *closure) {
  1090. return CheckCalledFromGeneratedFile("has_options");
  1091. }
  1092. static PyObject* GetOptions(PyBaseDescriptor *self) {
  1093. return GetOrBuildOptions(_GetDescriptor(self));
  1094. }
  1095. static int SetOptions(PyBaseDescriptor *self, PyObject *value,
  1096. void *closure) {
  1097. return CheckCalledFromGeneratedFile("_options");
  1098. }
  1099. static int SetSerializedOptions(PyBaseDescriptor *self, PyObject *value,
  1100. void *closure) {
  1101. return CheckCalledFromGeneratedFile("_serialized_options");
  1102. }
  1103. static PyGetSetDef Getters[] = {
  1104. { "name", (getter)GetName, NULL, "name"},
  1105. { "number", (getter)GetNumber, NULL, "number"},
  1106. { "index", (getter)GetIndex, NULL, "index"},
  1107. { "type", (getter)GetType, NULL, "index"},
  1108. { "has_options", (getter)GetHasOptions, (setter)SetHasOptions, "Has Options"},
  1109. { "_options", (getter)NULL, (setter)SetOptions, "Options"},
  1110. { "_serialized_options", (getter)NULL, (setter)SetSerializedOptions,
  1111. "Serialized Options"},
  1112. {NULL}
  1113. };
  1114. static PyMethodDef Methods[] = {
  1115. { "GetOptions", (PyCFunction)GetOptions, METH_NOARGS, },
  1116. {NULL}
  1117. };
  1118. } // namespace enumvalue_descriptor
  1119. PyTypeObject PyEnumValueDescriptor_Type = {
  1120. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  1121. FULL_MODULE_NAME ".EnumValueDescriptor", // tp_name
  1122. sizeof(PyBaseDescriptor), // tp_basicsize
  1123. 0, // tp_itemsize
  1124. 0, // tp_dealloc
  1125. 0, // tp_print
  1126. 0, // tp_getattr
  1127. 0, // tp_setattr
  1128. 0, // tp_compare
  1129. 0, // tp_repr
  1130. 0, // tp_as_number
  1131. 0, // tp_as_sequence
  1132. 0, // tp_as_mapping
  1133. 0, // tp_hash
  1134. 0, // tp_call
  1135. 0, // tp_str
  1136. 0, // tp_getattro
  1137. 0, // tp_setattro
  1138. 0, // tp_as_buffer
  1139. Py_TPFLAGS_DEFAULT, // tp_flags
  1140. "A EnumValue Descriptor", // tp_doc
  1141. 0, // tp_traverse
  1142. 0, // tp_clear
  1143. 0, // tp_richcompare
  1144. 0, // tp_weaklistoffset
  1145. 0, // tp_iter
  1146. 0, // tp_iternext
  1147. enumvalue_descriptor::Methods, // tp_methods
  1148. 0, // tp_members
  1149. enumvalue_descriptor::Getters, // tp_getset
  1150. &descriptor::PyBaseDescriptor_Type, // tp_base
  1151. };
  1152. PyObject* PyEnumValueDescriptor_FromDescriptor(
  1153. const EnumValueDescriptor* enumvalue_descriptor) {
  1154. return descriptor::NewInternedDescriptor(
  1155. &PyEnumValueDescriptor_Type, enumvalue_descriptor, NULL);
  1156. }
  1157. namespace file_descriptor {
  1158. // Unchecked accessor to the C++ pointer.
  1159. static const FileDescriptor* _GetDescriptor(PyFileDescriptor *self) {
  1160. return reinterpret_cast<const FileDescriptor*>(self->base.descriptor);
  1161. }
  1162. static void Dealloc(PyFileDescriptor* self) {
  1163. Py_XDECREF(self->serialized_pb);
  1164. descriptor::Dealloc(reinterpret_cast<PyObject*>(self));
  1165. }
  1166. static PyObject* GetPool(PyFileDescriptor *self, void *closure) {
  1167. PyObject* pool = reinterpret_cast<PyObject*>(
  1168. GetDescriptorPool_FromPool(_GetDescriptor(self)->pool()));
  1169. Py_XINCREF(pool);
  1170. return pool;
  1171. }
  1172. static PyObject* GetName(PyFileDescriptor *self, void *closure) {
  1173. return PyString_FromCppString(_GetDescriptor(self)->name());
  1174. }
  1175. static PyObject* GetPackage(PyFileDescriptor *self, void *closure) {
  1176. return PyString_FromCppString(_GetDescriptor(self)->package());
  1177. }
  1178. static PyObject* GetSerializedPb(PyFileDescriptor *self, void *closure) {
  1179. PyObject *serialized_pb = self->serialized_pb;
  1180. if (serialized_pb != NULL) {
  1181. Py_INCREF(serialized_pb);
  1182. return serialized_pb;
  1183. }
  1184. FileDescriptorProto file_proto;
  1185. _GetDescriptor(self)->CopyTo(&file_proto);
  1186. std::string contents;
  1187. file_proto.SerializePartialToString(&contents);
  1188. self->serialized_pb = PyBytes_FromStringAndSize(
  1189. contents.c_str(), contents.size());
  1190. if (self->serialized_pb == NULL) {
  1191. return NULL;
  1192. }
  1193. Py_INCREF(self->serialized_pb);
  1194. return self->serialized_pb;
  1195. }
  1196. static PyObject* GetMessageTypesByName(PyFileDescriptor* self, void *closure) {
  1197. return NewFileMessageTypesByName(_GetDescriptor(self));
  1198. }
  1199. static PyObject* GetEnumTypesByName(PyFileDescriptor* self, void *closure) {
  1200. return NewFileEnumTypesByName(_GetDescriptor(self));
  1201. }
  1202. static PyObject* GetExtensionsByName(PyFileDescriptor* self, void *closure) {
  1203. return NewFileExtensionsByName(_GetDescriptor(self));
  1204. }
  1205. static PyObject* GetServicesByName(PyFileDescriptor* self, void *closure) {
  1206. return NewFileServicesByName(_GetDescriptor(self));
  1207. }
  1208. static PyObject* GetDependencies(PyFileDescriptor* self, void *closure) {
  1209. return NewFileDependencies(_GetDescriptor(self));
  1210. }
  1211. static PyObject* GetPublicDependencies(PyFileDescriptor* self, void *closure) {
  1212. return NewFilePublicDependencies(_GetDescriptor(self));
  1213. }
  1214. static PyObject* GetHasOptions(PyFileDescriptor *self, void *closure) {
  1215. const FileOptions& options(_GetDescriptor(self)->options());
  1216. if (&options != &FileOptions::default_instance()) {
  1217. Py_RETURN_TRUE;
  1218. } else {
  1219. Py_RETURN_FALSE;
  1220. }
  1221. }
  1222. static int SetHasOptions(PyFileDescriptor *self, PyObject *value,
  1223. void *closure) {
  1224. return CheckCalledFromGeneratedFile("has_options");
  1225. }
  1226. static PyObject* GetOptions(PyFileDescriptor *self) {
  1227. return GetOrBuildOptions(_GetDescriptor(self));
  1228. }
  1229. static int SetOptions(PyFileDescriptor *self, PyObject *value,
  1230. void *closure) {
  1231. return CheckCalledFromGeneratedFile("_options");
  1232. }
  1233. static int SetSerializedOptions(PyFileDescriptor *self, PyObject *value,
  1234. void *closure) {
  1235. return CheckCalledFromGeneratedFile("_serialized_options");
  1236. }
  1237. static PyObject* GetSyntax(PyFileDescriptor *self, void *closure) {
  1238. return PyString_InternFromString(
  1239. FileDescriptor::SyntaxName(_GetDescriptor(self)->syntax()));
  1240. }
  1241. static PyObject* CopyToProto(PyFileDescriptor *self, PyObject *target) {
  1242. return CopyToPythonProto<FileDescriptorProto>(_GetDescriptor(self), target);
  1243. }
  1244. static PyGetSetDef Getters[] = {
  1245. { "pool", (getter)GetPool, NULL, "pool"},
  1246. { "name", (getter)GetName, NULL, "name"},
  1247. { "package", (getter)GetPackage, NULL, "package"},
  1248. { "serialized_pb", (getter)GetSerializedPb},
  1249. { "message_types_by_name", (getter)GetMessageTypesByName, NULL,
  1250. "Messages by name"},
  1251. { "enum_types_by_name", (getter)GetEnumTypesByName, NULL, "Enums by name"},
  1252. { "extensions_by_name", (getter)GetExtensionsByName, NULL,
  1253. "Extensions by name"},
  1254. { "services_by_name", (getter)GetServicesByName, NULL, "Services by name"},
  1255. { "dependencies", (getter)GetDependencies, NULL, "Dependencies"},
  1256. { "public_dependencies", (getter)GetPublicDependencies, NULL, "Dependencies"},
  1257. { "has_options", (getter)GetHasOptions, (setter)SetHasOptions, "Has Options"},
  1258. { "_options", (getter)NULL, (setter)SetOptions, "Options"},
  1259. { "_serialized_options", (getter)NULL, (setter)SetSerializedOptions,
  1260. "Serialized Options"},
  1261. { "syntax", (getter)GetSyntax, (setter)NULL, "Syntax"},
  1262. {NULL}
  1263. };
  1264. static PyMethodDef Methods[] = {
  1265. { "GetOptions", (PyCFunction)GetOptions, METH_NOARGS, },
  1266. { "CopyToProto", (PyCFunction)CopyToProto, METH_O, },
  1267. {NULL}
  1268. };
  1269. } // namespace file_descriptor
  1270. PyTypeObject PyFileDescriptor_Type = {
  1271. PyVarObject_HEAD_INIT(&PyType_Type, 0) FULL_MODULE_NAME
  1272. ".FileDescriptor", // tp_name
  1273. sizeof(PyFileDescriptor), // tp_basicsize
  1274. 0, // tp_itemsize
  1275. (destructor)file_descriptor::Dealloc, // tp_dealloc
  1276. 0, // tp_print
  1277. 0, // tp_getattr
  1278. 0, // tp_setattr
  1279. 0, // tp_compare
  1280. 0, // tp_repr
  1281. 0, // tp_as_number
  1282. 0, // tp_as_sequence
  1283. 0, // tp_as_mapping
  1284. 0, // tp_hash
  1285. 0, // tp_call
  1286. 0, // tp_str
  1287. 0, // tp_getattro
  1288. 0, // tp_setattro
  1289. 0, // tp_as_buffer
  1290. Py_TPFLAGS_DEFAULT, // tp_flags
  1291. "A File Descriptor", // tp_doc
  1292. 0, // tp_traverse
  1293. 0, // tp_clear
  1294. 0, // tp_richcompare
  1295. 0, // tp_weaklistoffset
  1296. 0, // tp_iter
  1297. 0, // tp_iternext
  1298. file_descriptor::Methods, // tp_methods
  1299. 0, // tp_members
  1300. file_descriptor::Getters, // tp_getset
  1301. &descriptor::PyBaseDescriptor_Type, // tp_base
  1302. 0, // tp_dict
  1303. 0, // tp_descr_get
  1304. 0, // tp_descr_set
  1305. 0, // tp_dictoffset
  1306. 0, // tp_init
  1307. 0, // tp_alloc
  1308. 0, // tp_new
  1309. PyObject_GC_Del, // tp_free
  1310. };
  1311. PyObject* PyFileDescriptor_FromDescriptor(
  1312. const FileDescriptor* file_descriptor) {
  1313. return PyFileDescriptor_FromDescriptorWithSerializedPb(file_descriptor,
  1314. NULL);
  1315. }
  1316. PyObject* PyFileDescriptor_FromDescriptorWithSerializedPb(
  1317. const FileDescriptor* file_descriptor, PyObject *serialized_pb) {
  1318. bool was_created;
  1319. PyObject* py_descriptor = descriptor::NewInternedDescriptor(
  1320. &PyFileDescriptor_Type, file_descriptor, &was_created);
  1321. if (py_descriptor == NULL) {
  1322. return NULL;
  1323. }
  1324. if (was_created) {
  1325. PyFileDescriptor* cfile_descriptor =
  1326. reinterpret_cast<PyFileDescriptor*>(py_descriptor);
  1327. Py_XINCREF(serialized_pb);
  1328. cfile_descriptor->serialized_pb = serialized_pb;
  1329. }
  1330. // TODO(amauryfa): In the case of a cached object, check that serialized_pb
  1331. // is the same as before.
  1332. return py_descriptor;
  1333. }
  1334. const FileDescriptor* PyFileDescriptor_AsDescriptor(PyObject* obj) {
  1335. if (!PyObject_TypeCheck(obj, &PyFileDescriptor_Type)) {
  1336. PyErr_SetString(PyExc_TypeError, "Not a FileDescriptor");
  1337. return NULL;
  1338. }
  1339. return reinterpret_cast<const FileDescriptor*>(
  1340. reinterpret_cast<PyBaseDescriptor*>(obj)->descriptor);
  1341. }
  1342. namespace oneof_descriptor {
  1343. // Unchecked accessor to the C++ pointer.
  1344. static const OneofDescriptor* _GetDescriptor(
  1345. PyBaseDescriptor *self) {
  1346. return reinterpret_cast<const OneofDescriptor*>(self->descriptor);
  1347. }
  1348. static PyObject* GetName(PyBaseDescriptor* self, void *closure) {
  1349. return PyString_FromCppString(_GetDescriptor(self)->name());
  1350. }
  1351. static PyObject* GetFullName(PyBaseDescriptor* self, void *closure) {
  1352. return PyString_FromCppString(_GetDescriptor(self)->full_name());
  1353. }
  1354. static PyObject* GetIndex(PyBaseDescriptor *self, void *closure) {
  1355. return PyInt_FromLong(_GetDescriptor(self)->index());
  1356. }
  1357. static PyObject* GetFields(PyBaseDescriptor* self, void *closure) {
  1358. return NewOneofFieldsSeq(_GetDescriptor(self));
  1359. }
  1360. static PyObject* GetContainingType(PyBaseDescriptor *self, void *closure) {
  1361. const Descriptor* containing_type =
  1362. _GetDescriptor(self)->containing_type();
  1363. if (containing_type) {
  1364. return PyMessageDescriptor_FromDescriptor(containing_type);
  1365. } else {
  1366. Py_RETURN_NONE;
  1367. }
  1368. }
  1369. static PyObject* GetHasOptions(PyBaseDescriptor *self, void *closure) {
  1370. const OneofOptions& options(_GetDescriptor(self)->options());
  1371. if (&options != &OneofOptions::default_instance()) {
  1372. Py_RETURN_TRUE;
  1373. } else {
  1374. Py_RETURN_FALSE;
  1375. }
  1376. }
  1377. static int SetHasOptions(PyBaseDescriptor *self, PyObject *value,
  1378. void *closure) {
  1379. return CheckCalledFromGeneratedFile("has_options");
  1380. }
  1381. static PyObject* GetOptions(PyBaseDescriptor *self) {
  1382. return GetOrBuildOptions(_GetDescriptor(self));
  1383. }
  1384. static int SetOptions(PyBaseDescriptor *self, PyObject *value,
  1385. void *closure) {
  1386. return CheckCalledFromGeneratedFile("_options");
  1387. }
  1388. static int SetSerializedOptions(PyBaseDescriptor *self, PyObject *value,
  1389. void *closure) {
  1390. return CheckCalledFromGeneratedFile("_serialized_options");
  1391. }
  1392. static PyGetSetDef Getters[] = {
  1393. { "name", (getter)GetName, NULL, "Name"},
  1394. { "full_name", (getter)GetFullName, NULL, "Full name"},
  1395. { "index", (getter)GetIndex, NULL, "Index"},
  1396. { "containing_type", (getter)GetContainingType, NULL, "Containing type"},
  1397. { "has_options", (getter)GetHasOptions, (setter)SetHasOptions, "Has Options"},
  1398. { "_options", (getter)NULL, (setter)SetOptions, "Options"},
  1399. { "_serialized_options", (getter)NULL, (setter)SetSerializedOptions,
  1400. "Serialized Options"},
  1401. { "fields", (getter)GetFields, NULL, "Fields"},
  1402. {NULL}
  1403. };
  1404. static PyMethodDef Methods[] = {
  1405. { "GetOptions", (PyCFunction)GetOptions, METH_NOARGS },
  1406. {NULL}
  1407. };
  1408. } // namespace oneof_descriptor
  1409. PyTypeObject PyOneofDescriptor_Type = {
  1410. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  1411. FULL_MODULE_NAME ".OneofDescriptor", // tp_name
  1412. sizeof(PyBaseDescriptor), // tp_basicsize
  1413. 0, // tp_itemsize
  1414. 0, // tp_dealloc
  1415. 0, // tp_print
  1416. 0, // tp_getattr
  1417. 0, // tp_setattr
  1418. 0, // tp_compare
  1419. 0, // tp_repr
  1420. 0, // tp_as_number
  1421. 0, // tp_as_sequence
  1422. 0, // tp_as_mapping
  1423. 0, // tp_hash
  1424. 0, // tp_call
  1425. 0, // tp_str
  1426. 0, // tp_getattro
  1427. 0, // tp_setattro
  1428. 0, // tp_as_buffer
  1429. Py_TPFLAGS_DEFAULT, // tp_flags
  1430. "A Oneof Descriptor", // tp_doc
  1431. 0, // tp_traverse
  1432. 0, // tp_clear
  1433. 0, // tp_richcompare
  1434. 0, // tp_weaklistoffset
  1435. 0, // tp_iter
  1436. 0, // tp_iternext
  1437. oneof_descriptor::Methods, // tp_methods
  1438. 0, // tp_members
  1439. oneof_descriptor::Getters, // tp_getset
  1440. &descriptor::PyBaseDescriptor_Type, // tp_base
  1441. };
  1442. PyObject* PyOneofDescriptor_FromDescriptor(
  1443. const OneofDescriptor* oneof_descriptor) {
  1444. return descriptor::NewInternedDescriptor(
  1445. &PyOneofDescriptor_Type, oneof_descriptor, NULL);
  1446. }
  1447. namespace service_descriptor {
  1448. // Unchecked accessor to the C++ pointer.
  1449. static const ServiceDescriptor* _GetDescriptor(
  1450. PyBaseDescriptor *self) {
  1451. return reinterpret_cast<const ServiceDescriptor*>(self->descriptor);
  1452. }
  1453. static PyObject* GetName(PyBaseDescriptor* self, void *closure) {
  1454. return PyString_FromCppString(_GetDescriptor(self)->name());
  1455. }
  1456. static PyObject* GetFullName(PyBaseDescriptor* self, void *closure) {
  1457. return PyString_FromCppString(_GetDescriptor(self)->full_name());
  1458. }
  1459. static PyObject* GetFile(PyBaseDescriptor *self, void *closure) {
  1460. return PyFileDescriptor_FromDescriptor(_GetDescriptor(self)->file());
  1461. }
  1462. static PyObject* GetIndex(PyBaseDescriptor *self, void *closure) {
  1463. return PyInt_FromLong(_GetDescriptor(self)->index());
  1464. }
  1465. static PyObject* GetMethods(PyBaseDescriptor* self, void *closure) {
  1466. return NewServiceMethodsSeq(_GetDescriptor(self));
  1467. }
  1468. static PyObject* GetMethodsByName(PyBaseDescriptor* self, void *closure) {
  1469. return NewServiceMethodsByName(_GetDescriptor(self));
  1470. }
  1471. static PyObject* FindMethodByName(PyBaseDescriptor *self, PyObject* arg) {
  1472. Py_ssize_t name_size;
  1473. char* name;
  1474. if (PyString_AsStringAndSize(arg, &name, &name_size) < 0) {
  1475. return NULL;
  1476. }
  1477. const MethodDescriptor* method_descriptor =
  1478. _GetDescriptor(self)->FindMethodByName(StringParam(name, name_size));
  1479. if (method_descriptor == NULL) {
  1480. PyErr_Format(PyExc_KeyError, "Couldn't find method %.200s", name);
  1481. return NULL;
  1482. }
  1483. return PyMethodDescriptor_FromDescriptor(method_descriptor);
  1484. }
  1485. static PyObject* GetOptions(PyBaseDescriptor *self) {
  1486. return GetOrBuildOptions(_GetDescriptor(self));
  1487. }
  1488. static PyObject* CopyToProto(PyBaseDescriptor *self, PyObject *target) {
  1489. return CopyToPythonProto<ServiceDescriptorProto>(_GetDescriptor(self),
  1490. target);
  1491. }
  1492. static PyGetSetDef Getters[] = {
  1493. { "name", (getter)GetName, NULL, "Name", NULL},
  1494. { "full_name", (getter)GetFullName, NULL, "Full name", NULL},
  1495. { "file", (getter)GetFile, NULL, "File descriptor"},
  1496. { "index", (getter)GetIndex, NULL, "Index", NULL},
  1497. { "methods", (getter)GetMethods, NULL, "Methods", NULL},
  1498. { "methods_by_name", (getter)GetMethodsByName, NULL, "Methods by name", NULL},
  1499. {NULL}
  1500. };
  1501. static PyMethodDef Methods[] = {
  1502. { "GetOptions", (PyCFunction)GetOptions, METH_NOARGS },
  1503. { "CopyToProto", (PyCFunction)CopyToProto, METH_O, },
  1504. { "FindMethodByName", (PyCFunction)FindMethodByName, METH_O },
  1505. {NULL}
  1506. };
  1507. } // namespace service_descriptor
  1508. PyTypeObject PyServiceDescriptor_Type = {
  1509. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  1510. FULL_MODULE_NAME ".ServiceDescriptor", // tp_name
  1511. sizeof(PyBaseDescriptor), // tp_basicsize
  1512. 0, // tp_itemsize
  1513. 0, // tp_dealloc
  1514. 0, // tp_print
  1515. 0, // tp_getattr
  1516. 0, // tp_setattr
  1517. 0, // tp_compare
  1518. 0, // tp_repr
  1519. 0, // tp_as_number
  1520. 0, // tp_as_sequence
  1521. 0, // tp_as_mapping
  1522. 0, // tp_hash
  1523. 0, // tp_call
  1524. 0, // tp_str
  1525. 0, // tp_getattro
  1526. 0, // tp_setattro
  1527. 0, // tp_as_buffer
  1528. Py_TPFLAGS_DEFAULT, // tp_flags
  1529. "A Service Descriptor", // tp_doc
  1530. 0, // tp_traverse
  1531. 0, // tp_clear
  1532. 0, // tp_richcompare
  1533. 0, // tp_weaklistoffset
  1534. 0, // tp_iter
  1535. 0, // tp_iternext
  1536. service_descriptor::Methods, // tp_methods
  1537. 0, // tp_members
  1538. service_descriptor::Getters, // tp_getset
  1539. &descriptor::PyBaseDescriptor_Type, // tp_base
  1540. };
  1541. PyObject* PyServiceDescriptor_FromDescriptor(
  1542. const ServiceDescriptor* service_descriptor) {
  1543. return descriptor::NewInternedDescriptor(
  1544. &PyServiceDescriptor_Type, service_descriptor, NULL);
  1545. }
  1546. const ServiceDescriptor* PyServiceDescriptor_AsDescriptor(PyObject* obj) {
  1547. if (!PyObject_TypeCheck(obj, &PyServiceDescriptor_Type)) {
  1548. PyErr_SetString(PyExc_TypeError, "Not a ServiceDescriptor");
  1549. return NULL;
  1550. }
  1551. return reinterpret_cast<const ServiceDescriptor*>(
  1552. reinterpret_cast<PyBaseDescriptor*>(obj)->descriptor);
  1553. }
  1554. namespace method_descriptor {
  1555. // Unchecked accessor to the C++ pointer.
  1556. static const MethodDescriptor* _GetDescriptor(
  1557. PyBaseDescriptor *self) {
  1558. return reinterpret_cast<const MethodDescriptor*>(self->descriptor);
  1559. }
  1560. static PyObject* GetName(PyBaseDescriptor* self, void *closure) {
  1561. return PyString_FromCppString(_GetDescriptor(self)->name());
  1562. }
  1563. static PyObject* GetFullName(PyBaseDescriptor* self, void *closure) {
  1564. return PyString_FromCppString(_GetDescriptor(self)->full_name());
  1565. }
  1566. static PyObject* GetIndex(PyBaseDescriptor *self, void *closure) {
  1567. return PyInt_FromLong(_GetDescriptor(self)->index());
  1568. }
  1569. static PyObject* GetContainingService(PyBaseDescriptor *self, void *closure) {
  1570. const ServiceDescriptor* containing_service =
  1571. _GetDescriptor(self)->service();
  1572. return PyServiceDescriptor_FromDescriptor(containing_service);
  1573. }
  1574. static PyObject* GetInputType(PyBaseDescriptor *self, void *closure) {
  1575. const Descriptor* input_type = _GetDescriptor(self)->input_type();
  1576. return PyMessageDescriptor_FromDescriptor(input_type);
  1577. }
  1578. static PyObject* GetOutputType(PyBaseDescriptor *self, void *closure) {
  1579. const Descriptor* output_type = _GetDescriptor(self)->output_type();
  1580. return PyMessageDescriptor_FromDescriptor(output_type);
  1581. }
  1582. static PyObject* GetOptions(PyBaseDescriptor *self) {
  1583. return GetOrBuildOptions(_GetDescriptor(self));
  1584. }
  1585. static PyObject* CopyToProto(PyBaseDescriptor *self, PyObject *target) {
  1586. return CopyToPythonProto<MethodDescriptorProto>(_GetDescriptor(self), target);
  1587. }
  1588. static PyGetSetDef Getters[] = {
  1589. { "name", (getter)GetName, NULL, "Name", NULL},
  1590. { "full_name", (getter)GetFullName, NULL, "Full name", NULL},
  1591. { "index", (getter)GetIndex, NULL, "Index", NULL},
  1592. { "containing_service", (getter)GetContainingService, NULL,
  1593. "Containing service", NULL},
  1594. { "input_type", (getter)GetInputType, NULL, "Input type", NULL},
  1595. { "output_type", (getter)GetOutputType, NULL, "Output type", NULL},
  1596. {NULL}
  1597. };
  1598. static PyMethodDef Methods[] = {
  1599. { "GetOptions", (PyCFunction)GetOptions, METH_NOARGS, },
  1600. { "CopyToProto", (PyCFunction)CopyToProto, METH_O, },
  1601. {NULL}
  1602. };
  1603. } // namespace method_descriptor
  1604. PyTypeObject PyMethodDescriptor_Type = {
  1605. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  1606. FULL_MODULE_NAME ".MethodDescriptor", // tp_name
  1607. sizeof(PyBaseDescriptor), // tp_basicsize
  1608. 0, // tp_itemsize
  1609. 0, // tp_dealloc
  1610. 0, // tp_print
  1611. 0, // tp_getattr
  1612. 0, // tp_setattr
  1613. 0, // tp_compare
  1614. 0, // tp_repr
  1615. 0, // tp_as_number
  1616. 0, // tp_as_sequence
  1617. 0, // tp_as_mapping
  1618. 0, // tp_hash
  1619. 0, // tp_call
  1620. 0, // tp_str
  1621. 0, // tp_getattro
  1622. 0, // tp_setattro
  1623. 0, // tp_as_buffer
  1624. Py_TPFLAGS_DEFAULT, // tp_flags
  1625. "A Method Descriptor", // tp_doc
  1626. 0, // tp_traverse
  1627. 0, // tp_clear
  1628. 0, // tp_richcompare
  1629. 0, // tp_weaklistoffset
  1630. 0, // tp_iter
  1631. 0, // tp_iternext
  1632. method_descriptor::Methods, // tp_methods
  1633. 0, // tp_members
  1634. method_descriptor::Getters, // tp_getset
  1635. &descriptor::PyBaseDescriptor_Type, // tp_base
  1636. };
  1637. PyObject* PyMethodDescriptor_FromDescriptor(
  1638. const MethodDescriptor* method_descriptor) {
  1639. return descriptor::NewInternedDescriptor(
  1640. &PyMethodDescriptor_Type, method_descriptor, NULL);
  1641. }
  1642. // Add a enum values to a type dictionary.
  1643. static bool AddEnumValues(PyTypeObject *type,
  1644. const EnumDescriptor* enum_descriptor) {
  1645. for (int i = 0; i < enum_descriptor->value_count(); ++i) {
  1646. const EnumValueDescriptor* value = enum_descriptor->value(i);
  1647. ScopedPyObjectPtr obj(PyInt_FromLong(value->number()));
  1648. if (obj == NULL) {
  1649. return false;
  1650. }
  1651. if (PyDict_SetItemString(type->tp_dict, value->name().c_str(), obj.get()) <
  1652. 0) {
  1653. return false;
  1654. }
  1655. }
  1656. return true;
  1657. }
  1658. static bool AddIntConstant(PyTypeObject *type, const char* name, int value) {
  1659. ScopedPyObjectPtr obj(PyInt_FromLong(value));
  1660. if (PyDict_SetItemString(type->tp_dict, name, obj.get()) < 0) {
  1661. return false;
  1662. }
  1663. return true;
  1664. }
  1665. bool InitDescriptor() {
  1666. if (PyType_Ready(&PyMessageDescriptor_Type) < 0)
  1667. return false;
  1668. if (PyType_Ready(&PyFieldDescriptor_Type) < 0)
  1669. return false;
  1670. if (!AddEnumValues(&PyFieldDescriptor_Type,
  1671. FieldDescriptorProto::Label_descriptor())) {
  1672. return false;
  1673. }
  1674. if (!AddEnumValues(&PyFieldDescriptor_Type,
  1675. FieldDescriptorProto::Type_descriptor())) {
  1676. return false;
  1677. }
  1678. #define ADD_FIELDDESC_CONSTANT(NAME) AddIntConstant( \
  1679. &PyFieldDescriptor_Type, #NAME, FieldDescriptor::NAME)
  1680. if (!ADD_FIELDDESC_CONSTANT(CPPTYPE_INT32) ||
  1681. !ADD_FIELDDESC_CONSTANT(CPPTYPE_INT64) ||
  1682. !ADD_FIELDDESC_CONSTANT(CPPTYPE_UINT32) ||
  1683. !ADD_FIELDDESC_CONSTANT(CPPTYPE_UINT64) ||
  1684. !ADD_FIELDDESC_CONSTANT(CPPTYPE_DOUBLE) ||
  1685. !ADD_FIELDDESC_CONSTANT(CPPTYPE_FLOAT) ||
  1686. !ADD_FIELDDESC_CONSTANT(CPPTYPE_BOOL) ||
  1687. !ADD_FIELDDESC_CONSTANT(CPPTYPE_ENUM) ||
  1688. !ADD_FIELDDESC_CONSTANT(CPPTYPE_STRING) ||
  1689. !ADD_FIELDDESC_CONSTANT(CPPTYPE_MESSAGE)) {
  1690. return false;
  1691. }
  1692. #undef ADD_FIELDDESC_CONSTANT
  1693. if (PyType_Ready(&PyEnumDescriptor_Type) < 0)
  1694. return false;
  1695. if (PyType_Ready(&PyEnumValueDescriptor_Type) < 0)
  1696. return false;
  1697. if (PyType_Ready(&PyFileDescriptor_Type) < 0)
  1698. return false;
  1699. if (PyType_Ready(&PyOneofDescriptor_Type) < 0)
  1700. return false;
  1701. if (PyType_Ready(&PyServiceDescriptor_Type) < 0)
  1702. return false;
  1703. if (PyType_Ready(&PyMethodDescriptor_Type) < 0)
  1704. return false;
  1705. if (!InitDescriptorMappingTypes())
  1706. return false;
  1707. // Initialize globals defined in this file.
  1708. interned_descriptors = new std::unordered_map<const void*, PyObject*>;
  1709. return true;
  1710. }
  1711. } // namespace python
  1712. } // namespace protobuf
  1713. } // namespace google