descriptor.cc 69 KB

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