descriptor_pool.cc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  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. // Implements the DescriptorPool, which collects all descriptors.
  31. #include <Python.h>
  32. #include <google/protobuf/descriptor.pb.h>
  33. #include <google/protobuf/pyext/descriptor.h>
  34. #include <google/protobuf/pyext/descriptor_database.h>
  35. #include <google/protobuf/pyext/descriptor_pool.h>
  36. #include <google/protobuf/pyext/message.h>
  37. #include <google/protobuf/pyext/message_factory.h>
  38. #include <google/protobuf/pyext/scoped_pyobject_ptr.h>
  39. #if PY_MAJOR_VERSION >= 3
  40. #define PyString_FromStringAndSize PyUnicode_FromStringAndSize
  41. #if PY_VERSION_HEX < 0x03030000
  42. #error "Python 3.0 - 3.2 are not supported."
  43. #endif
  44. #define PyString_AsStringAndSize(ob, charpp, sizep) \
  45. (PyUnicode_Check(ob)? \
  46. ((*(charpp) = PyUnicode_AsUTF8AndSize(ob, (sizep))) == NULL? -1: 0): \
  47. PyBytes_AsStringAndSize(ob, (charpp), (sizep)))
  48. #endif
  49. namespace google {
  50. namespace protobuf {
  51. namespace python {
  52. // A map to cache Python Pools per C++ pointer.
  53. // Pointers are not owned here, and belong to the PyDescriptorPool.
  54. static hash_map<const DescriptorPool*, PyDescriptorPool*> descriptor_pool_map;
  55. namespace cdescriptor_pool {
  56. // Create a Python DescriptorPool object, but does not fill the "pool"
  57. // attribute.
  58. static PyDescriptorPool* _CreateDescriptorPool() {
  59. PyDescriptorPool* cpool = PyObject_New(
  60. PyDescriptorPool, &PyDescriptorPool_Type);
  61. if (cpool == NULL) {
  62. return NULL;
  63. }
  64. cpool->underlay = NULL;
  65. cpool->database = NULL;
  66. cpool->descriptor_options =
  67. new hash_map<const void*, PyObject *>();
  68. cpool->py_message_factory = message_factory::NewMessageFactory(
  69. &PyMessageFactory_Type, cpool);
  70. if (cpool->py_message_factory == NULL) {
  71. Py_DECREF(cpool);
  72. return NULL;
  73. }
  74. return cpool;
  75. }
  76. // Create a Python DescriptorPool, using the given pool as an underlay:
  77. // new messages will be added to a custom pool, not to the underlay.
  78. //
  79. // Ownership of the underlay is not transferred, its pointer should
  80. // stay alive.
  81. static PyDescriptorPool* PyDescriptorPool_NewWithUnderlay(
  82. const DescriptorPool* underlay) {
  83. PyDescriptorPool* cpool = _CreateDescriptorPool();
  84. if (cpool == NULL) {
  85. return NULL;
  86. }
  87. cpool->pool = new DescriptorPool(underlay);
  88. cpool->underlay = underlay;
  89. if (!descriptor_pool_map.insert(
  90. std::make_pair(cpool->pool, cpool)).second) {
  91. // Should never happen -- would indicate an internal error / bug.
  92. PyErr_SetString(PyExc_ValueError, "DescriptorPool already registered");
  93. return NULL;
  94. }
  95. return cpool;
  96. }
  97. static PyDescriptorPool* PyDescriptorPool_NewWithDatabase(
  98. DescriptorDatabase* database) {
  99. PyDescriptorPool* cpool = _CreateDescriptorPool();
  100. if (cpool == NULL) {
  101. return NULL;
  102. }
  103. if (database != NULL) {
  104. cpool->pool = new DescriptorPool(database);
  105. cpool->database = database;
  106. } else {
  107. cpool->pool = new DescriptorPool();
  108. }
  109. if (!descriptor_pool_map.insert(std::make_pair(cpool->pool, cpool)).second) {
  110. // Should never happen -- would indicate an internal error / bug.
  111. PyErr_SetString(PyExc_ValueError, "DescriptorPool already registered");
  112. return NULL;
  113. }
  114. return cpool;
  115. }
  116. // The public DescriptorPool constructor.
  117. static PyObject* New(PyTypeObject* type,
  118. PyObject* args, PyObject* kwargs) {
  119. static char* kwlist[] = {"descriptor_db", 0};
  120. PyObject* py_database = NULL;
  121. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &py_database)) {
  122. return NULL;
  123. }
  124. DescriptorDatabase* database = NULL;
  125. if (py_database && py_database != Py_None) {
  126. database = new PyDescriptorDatabase(py_database);
  127. }
  128. return reinterpret_cast<PyObject*>(
  129. PyDescriptorPool_NewWithDatabase(database));
  130. }
  131. static void Dealloc(PyDescriptorPool* self) {
  132. descriptor_pool_map.erase(self->pool);
  133. Py_CLEAR(self->py_message_factory);
  134. for (hash_map<const void*, PyObject*>::iterator it =
  135. self->descriptor_options->begin();
  136. it != self->descriptor_options->end(); ++it) {
  137. Py_DECREF(it->second);
  138. }
  139. delete self->descriptor_options;
  140. delete self->database;
  141. delete self->pool;
  142. Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self));
  143. }
  144. PyObject* FindMessageByName(PyDescriptorPool* self, PyObject* arg) {
  145. Py_ssize_t name_size;
  146. char* name;
  147. if (PyString_AsStringAndSize(arg, &name, &name_size) < 0) {
  148. return NULL;
  149. }
  150. const Descriptor* message_descriptor =
  151. self->pool->FindMessageTypeByName(string(name, name_size));
  152. if (message_descriptor == NULL) {
  153. PyErr_Format(PyExc_KeyError, "Couldn't find message %.200s", name);
  154. return NULL;
  155. }
  156. return PyMessageDescriptor_FromDescriptor(message_descriptor);
  157. }
  158. PyObject* FindFileByName(PyDescriptorPool* self, PyObject* arg) {
  159. Py_ssize_t name_size;
  160. char* name;
  161. if (PyString_AsStringAndSize(arg, &name, &name_size) < 0) {
  162. return NULL;
  163. }
  164. const FileDescriptor* file_descriptor =
  165. self->pool->FindFileByName(string(name, name_size));
  166. if (file_descriptor == NULL) {
  167. PyErr_Format(PyExc_KeyError, "Couldn't find file %.200s", name);
  168. return NULL;
  169. }
  170. return PyFileDescriptor_FromDescriptor(file_descriptor);
  171. }
  172. PyObject* FindFieldByName(PyDescriptorPool* self, PyObject* arg) {
  173. Py_ssize_t name_size;
  174. char* name;
  175. if (PyString_AsStringAndSize(arg, &name, &name_size) < 0) {
  176. return NULL;
  177. }
  178. const FieldDescriptor* field_descriptor =
  179. self->pool->FindFieldByName(string(name, name_size));
  180. if (field_descriptor == NULL) {
  181. PyErr_Format(PyExc_KeyError, "Couldn't find field %.200s",
  182. name);
  183. return NULL;
  184. }
  185. return PyFieldDescriptor_FromDescriptor(field_descriptor);
  186. }
  187. PyObject* FindExtensionByName(PyDescriptorPool* self, PyObject* arg) {
  188. Py_ssize_t name_size;
  189. char* name;
  190. if (PyString_AsStringAndSize(arg, &name, &name_size) < 0) {
  191. return NULL;
  192. }
  193. const FieldDescriptor* field_descriptor =
  194. self->pool->FindExtensionByName(string(name, name_size));
  195. if (field_descriptor == NULL) {
  196. PyErr_Format(PyExc_KeyError, "Couldn't find extension field %.200s", name);
  197. return NULL;
  198. }
  199. return PyFieldDescriptor_FromDescriptor(field_descriptor);
  200. }
  201. PyObject* FindEnumTypeByName(PyDescriptorPool* self, PyObject* arg) {
  202. Py_ssize_t name_size;
  203. char* name;
  204. if (PyString_AsStringAndSize(arg, &name, &name_size) < 0) {
  205. return NULL;
  206. }
  207. const EnumDescriptor* enum_descriptor =
  208. self->pool->FindEnumTypeByName(string(name, name_size));
  209. if (enum_descriptor == NULL) {
  210. PyErr_Format(PyExc_KeyError, "Couldn't find enum %.200s", name);
  211. return NULL;
  212. }
  213. return PyEnumDescriptor_FromDescriptor(enum_descriptor);
  214. }
  215. PyObject* FindOneofByName(PyDescriptorPool* self, PyObject* arg) {
  216. Py_ssize_t name_size;
  217. char* name;
  218. if (PyString_AsStringAndSize(arg, &name, &name_size) < 0) {
  219. return NULL;
  220. }
  221. const OneofDescriptor* oneof_descriptor =
  222. self->pool->FindOneofByName(string(name, name_size));
  223. if (oneof_descriptor == NULL) {
  224. PyErr_Format(PyExc_KeyError, "Couldn't find oneof %.200s", name);
  225. return NULL;
  226. }
  227. return PyOneofDescriptor_FromDescriptor(oneof_descriptor);
  228. }
  229. PyObject* FindServiceByName(PyDescriptorPool* self, PyObject* arg) {
  230. Py_ssize_t name_size;
  231. char* name;
  232. if (PyString_AsStringAndSize(arg, &name, &name_size) < 0) {
  233. return NULL;
  234. }
  235. const ServiceDescriptor* service_descriptor =
  236. self->pool->FindServiceByName(string(name, name_size));
  237. if (service_descriptor == NULL) {
  238. PyErr_Format(PyExc_KeyError, "Couldn't find service %.200s", name);
  239. return NULL;
  240. }
  241. return PyServiceDescriptor_FromDescriptor(service_descriptor);
  242. }
  243. PyObject* FindMethodByName(PyDescriptorPool* self, PyObject* arg) {
  244. Py_ssize_t name_size;
  245. char* name;
  246. if (PyString_AsStringAndSize(arg, &name, &name_size) < 0) {
  247. return NULL;
  248. }
  249. const MethodDescriptor* method_descriptor =
  250. self->pool->FindMethodByName(string(name, name_size));
  251. if (method_descriptor == NULL) {
  252. PyErr_Format(PyExc_KeyError, "Couldn't find method %.200s", name);
  253. return NULL;
  254. }
  255. return PyMethodDescriptor_FromDescriptor(method_descriptor);
  256. }
  257. PyObject* FindFileContainingSymbol(PyDescriptorPool* self, PyObject* arg) {
  258. Py_ssize_t name_size;
  259. char* name;
  260. if (PyString_AsStringAndSize(arg, &name, &name_size) < 0) {
  261. return NULL;
  262. }
  263. const FileDescriptor* file_descriptor =
  264. self->pool->FindFileContainingSymbol(string(name, name_size));
  265. if (file_descriptor == NULL) {
  266. PyErr_Format(PyExc_KeyError, "Couldn't find symbol %.200s", name);
  267. return NULL;
  268. }
  269. return PyFileDescriptor_FromDescriptor(file_descriptor);
  270. }
  271. PyObject* FindExtensionByNumber(PyDescriptorPool* self, PyObject* args) {
  272. PyObject* message_descriptor;
  273. int number;
  274. if (!PyArg_ParseTuple(args, "Oi", &message_descriptor, &number)) {
  275. return NULL;
  276. }
  277. const Descriptor* descriptor = PyMessageDescriptor_AsDescriptor(
  278. message_descriptor);
  279. if (descriptor == NULL) {
  280. return NULL;
  281. }
  282. const FieldDescriptor* extension_descriptor =
  283. self->pool->FindExtensionByNumber(descriptor, number);
  284. if (extension_descriptor == NULL) {
  285. PyErr_Format(PyExc_KeyError, "Couldn't find extension %d", number);
  286. return NULL;
  287. }
  288. return PyFieldDescriptor_FromDescriptor(extension_descriptor);
  289. }
  290. PyObject* FindAllExtensions(PyDescriptorPool* self, PyObject* arg) {
  291. const Descriptor* descriptor = PyMessageDescriptor_AsDescriptor(arg);
  292. if (descriptor == NULL) {
  293. return NULL;
  294. }
  295. std::vector<const FieldDescriptor*> extensions;
  296. self->pool->FindAllExtensions(descriptor, &extensions);
  297. ScopedPyObjectPtr result(PyList_New(extensions.size()));
  298. if (result == NULL) {
  299. return NULL;
  300. }
  301. for (int i = 0; i < extensions.size(); i++) {
  302. PyObject* extension = PyFieldDescriptor_FromDescriptor(extensions[i]);
  303. if (extension == NULL) {
  304. return NULL;
  305. }
  306. PyList_SET_ITEM(result.get(), i, extension); // Steals the reference.
  307. }
  308. return result.release();
  309. }
  310. // These functions should not exist -- the only valid way to create
  311. // descriptors is to call Add() or AddSerializedFile().
  312. // But these AddDescriptor() functions were created in Python and some people
  313. // call them, so we support them for now for compatibility.
  314. // However we do check that the existing descriptor already exists in the pool,
  315. // which appears to always be true for existing calls -- but then why do people
  316. // call a function that will just be a no-op?
  317. // TODO(amauryfa): Need to investigate further.
  318. PyObject* AddFileDescriptor(PyDescriptorPool* self, PyObject* descriptor) {
  319. const FileDescriptor* file_descriptor =
  320. PyFileDescriptor_AsDescriptor(descriptor);
  321. if (!file_descriptor) {
  322. return NULL;
  323. }
  324. if (file_descriptor !=
  325. self->pool->FindFileByName(file_descriptor->name())) {
  326. PyErr_Format(PyExc_ValueError,
  327. "The file descriptor %s does not belong to this pool",
  328. file_descriptor->name().c_str());
  329. return NULL;
  330. }
  331. Py_RETURN_NONE;
  332. }
  333. PyObject* AddDescriptor(PyDescriptorPool* self, PyObject* descriptor) {
  334. const Descriptor* message_descriptor =
  335. PyMessageDescriptor_AsDescriptor(descriptor);
  336. if (!message_descriptor) {
  337. return NULL;
  338. }
  339. if (message_descriptor !=
  340. self->pool->FindMessageTypeByName(message_descriptor->full_name())) {
  341. PyErr_Format(PyExc_ValueError,
  342. "The message descriptor %s does not belong to this pool",
  343. message_descriptor->full_name().c_str());
  344. return NULL;
  345. }
  346. Py_RETURN_NONE;
  347. }
  348. PyObject* AddEnumDescriptor(PyDescriptorPool* self, PyObject* descriptor) {
  349. const EnumDescriptor* enum_descriptor =
  350. PyEnumDescriptor_AsDescriptor(descriptor);
  351. if (!enum_descriptor) {
  352. return NULL;
  353. }
  354. if (enum_descriptor !=
  355. self->pool->FindEnumTypeByName(enum_descriptor->full_name())) {
  356. PyErr_Format(PyExc_ValueError,
  357. "The enum descriptor %s does not belong to this pool",
  358. enum_descriptor->full_name().c_str());
  359. return NULL;
  360. }
  361. Py_RETURN_NONE;
  362. }
  363. PyObject* AddExtensionDescriptor(PyDescriptorPool* self, PyObject* descriptor) {
  364. const FieldDescriptor* extension_descriptor =
  365. PyFieldDescriptor_AsDescriptor(descriptor);
  366. if (!extension_descriptor) {
  367. return NULL;
  368. }
  369. if (extension_descriptor !=
  370. self->pool->FindExtensionByName(extension_descriptor->full_name())) {
  371. PyErr_Format(PyExc_ValueError,
  372. "The extension descriptor %s does not belong to this pool",
  373. extension_descriptor->full_name().c_str());
  374. return NULL;
  375. }
  376. Py_RETURN_NONE;
  377. }
  378. // The code below loads new Descriptors from a serialized FileDescriptorProto.
  379. // Collects errors that occur during proto file building to allow them to be
  380. // propagated in the python exception instead of only living in ERROR logs.
  381. class BuildFileErrorCollector : public DescriptorPool::ErrorCollector {
  382. public:
  383. BuildFileErrorCollector() : error_message(""), had_errors(false) {}
  384. void AddError(const string& filename, const string& element_name,
  385. const Message* descriptor, ErrorLocation location,
  386. const string& message) {
  387. // Replicates the logging behavior that happens in the C++ implementation
  388. // when an error collector is not passed in.
  389. if (!had_errors) {
  390. error_message +=
  391. ("Invalid proto descriptor for file \"" + filename + "\":\n");
  392. had_errors = true;
  393. }
  394. // As this only happens on failure and will result in the program not
  395. // running at all, no effort is made to optimize this string manipulation.
  396. error_message += (" " + element_name + ": " + message + "\n");
  397. }
  398. string error_message;
  399. bool had_errors;
  400. };
  401. PyObject* AddSerializedFile(PyDescriptorPool* self, PyObject* serialized_pb) {
  402. char* message_type;
  403. Py_ssize_t message_len;
  404. if (self->database != NULL) {
  405. PyErr_SetString(
  406. PyExc_ValueError,
  407. "Cannot call Add on a DescriptorPool that uses a DescriptorDatabase. "
  408. "Add your file to the underlying database.");
  409. return NULL;
  410. }
  411. if (PyBytes_AsStringAndSize(serialized_pb, &message_type, &message_len) < 0) {
  412. return NULL;
  413. }
  414. FileDescriptorProto file_proto;
  415. if (!file_proto.ParseFromArray(message_type, message_len)) {
  416. PyErr_SetString(PyExc_TypeError, "Couldn't parse file content!");
  417. return NULL;
  418. }
  419. // If the file was already part of a C++ library, all its descriptors are in
  420. // the underlying pool. No need to do anything else.
  421. const FileDescriptor* generated_file = NULL;
  422. if (self->underlay) {
  423. generated_file = self->underlay->FindFileByName(file_proto.name());
  424. }
  425. if (generated_file != NULL) {
  426. return PyFileDescriptor_FromDescriptorWithSerializedPb(
  427. generated_file, serialized_pb);
  428. }
  429. BuildFileErrorCollector error_collector;
  430. const FileDescriptor* descriptor =
  431. self->pool->BuildFileCollectingErrors(file_proto,
  432. &error_collector);
  433. if (descriptor == NULL) {
  434. PyErr_Format(PyExc_TypeError,
  435. "Couldn't build proto file into descriptor pool!\n%s",
  436. error_collector.error_message.c_str());
  437. return NULL;
  438. }
  439. return PyFileDescriptor_FromDescriptorWithSerializedPb(
  440. descriptor, serialized_pb);
  441. }
  442. PyObject* Add(PyDescriptorPool* self, PyObject* file_descriptor_proto) {
  443. ScopedPyObjectPtr serialized_pb(
  444. PyObject_CallMethod(file_descriptor_proto, "SerializeToString", NULL));
  445. if (serialized_pb == NULL) {
  446. return NULL;
  447. }
  448. return AddSerializedFile(self, serialized_pb.get());
  449. }
  450. static PyMethodDef Methods[] = {
  451. { "Add", (PyCFunction)Add, METH_O,
  452. "Adds the FileDescriptorProto and its types to this pool." },
  453. { "AddSerializedFile", (PyCFunction)AddSerializedFile, METH_O,
  454. "Adds a serialized FileDescriptorProto to this pool." },
  455. // TODO(amauryfa): Understand why the Python implementation differs from
  456. // this one, ask users to use another API and deprecate these functions.
  457. { "AddFileDescriptor", (PyCFunction)AddFileDescriptor, METH_O,
  458. "No-op. Add() must have been called before." },
  459. { "AddDescriptor", (PyCFunction)AddDescriptor, METH_O,
  460. "No-op. Add() must have been called before." },
  461. { "AddEnumDescriptor", (PyCFunction)AddEnumDescriptor, METH_O,
  462. "No-op. Add() must have been called before." },
  463. { "AddExtensionDescriptor", (PyCFunction)AddExtensionDescriptor, METH_O,
  464. "No-op. Add() must have been called before." },
  465. { "FindFileByName", (PyCFunction)FindFileByName, METH_O,
  466. "Searches for a file descriptor by its .proto name." },
  467. { "FindMessageTypeByName", (PyCFunction)FindMessageByName, METH_O,
  468. "Searches for a message descriptor by full name." },
  469. { "FindFieldByName", (PyCFunction)FindFieldByName, METH_O,
  470. "Searches for a field descriptor by full name." },
  471. { "FindExtensionByName", (PyCFunction)FindExtensionByName, METH_O,
  472. "Searches for extension descriptor by full name." },
  473. { "FindEnumTypeByName", (PyCFunction)FindEnumTypeByName, METH_O,
  474. "Searches for enum type descriptor by full name." },
  475. { "FindOneofByName", (PyCFunction)FindOneofByName, METH_O,
  476. "Searches for oneof descriptor by full name." },
  477. { "FindServiceByName", (PyCFunction)FindServiceByName, METH_O,
  478. "Searches for service descriptor by full name." },
  479. { "FindMethodByName", (PyCFunction)FindMethodByName, METH_O,
  480. "Searches for method descriptor by full name." },
  481. { "FindFileContainingSymbol", (PyCFunction)FindFileContainingSymbol, METH_O,
  482. "Gets the FileDescriptor containing the specified symbol." },
  483. { "FindExtensionByNumber", (PyCFunction)FindExtensionByNumber, METH_VARARGS,
  484. "Gets the extension descriptor for the given number." },
  485. { "FindAllExtensions", (PyCFunction)FindAllExtensions, METH_O,
  486. "Gets all known extensions of the given message descriptor." },
  487. {NULL}
  488. };
  489. } // namespace cdescriptor_pool
  490. PyTypeObject PyDescriptorPool_Type = {
  491. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  492. FULL_MODULE_NAME ".DescriptorPool", // tp_name
  493. sizeof(PyDescriptorPool), // tp_basicsize
  494. 0, // tp_itemsize
  495. (destructor)cdescriptor_pool::Dealloc, // tp_dealloc
  496. 0, // tp_print
  497. 0, // tp_getattr
  498. 0, // tp_setattr
  499. 0, // tp_compare
  500. 0, // tp_repr
  501. 0, // tp_as_number
  502. 0, // tp_as_sequence
  503. 0, // tp_as_mapping
  504. 0, // tp_hash
  505. 0, // tp_call
  506. 0, // tp_str
  507. 0, // tp_getattro
  508. 0, // tp_setattro
  509. 0, // tp_as_buffer
  510. Py_TPFLAGS_DEFAULT, // tp_flags
  511. "A Descriptor Pool", // tp_doc
  512. 0, // tp_traverse
  513. 0, // tp_clear
  514. 0, // tp_richcompare
  515. 0, // tp_weaklistoffset
  516. 0, // tp_iter
  517. 0, // tp_iternext
  518. cdescriptor_pool::Methods, // tp_methods
  519. 0, // tp_members
  520. 0, // tp_getset
  521. 0, // tp_base
  522. 0, // tp_dict
  523. 0, // tp_descr_get
  524. 0, // tp_descr_set
  525. 0, // tp_dictoffset
  526. 0, // tp_init
  527. 0, // tp_alloc
  528. cdescriptor_pool::New, // tp_new
  529. PyObject_Del, // tp_free
  530. };
  531. // This is the DescriptorPool which contains all the definitions from the
  532. // generated _pb2.py modules.
  533. static PyDescriptorPool* python_generated_pool = NULL;
  534. bool InitDescriptorPool() {
  535. if (PyType_Ready(&PyDescriptorPool_Type) < 0)
  536. return false;
  537. // The Pool of messages declared in Python libraries.
  538. // generated_pool() contains all messages already linked in C++ libraries, and
  539. // is used as underlay.
  540. python_generated_pool = cdescriptor_pool::PyDescriptorPool_NewWithUnderlay(
  541. DescriptorPool::generated_pool());
  542. if (python_generated_pool == NULL) {
  543. return false;
  544. }
  545. // Register this pool to be found for C++-generated descriptors.
  546. descriptor_pool_map.insert(
  547. std::make_pair(DescriptorPool::generated_pool(),
  548. python_generated_pool));
  549. return true;
  550. }
  551. // The default DescriptorPool used everywhere in this module.
  552. // Today it's the python_generated_pool.
  553. // TODO(amauryfa): Remove all usages of this function: the pool should be
  554. // derived from the context.
  555. PyDescriptorPool* GetDefaultDescriptorPool() {
  556. return python_generated_pool;
  557. }
  558. PyDescriptorPool* GetDescriptorPool_FromPool(const DescriptorPool* pool) {
  559. // Fast path for standard descriptors.
  560. if (pool == python_generated_pool->pool ||
  561. pool == DescriptorPool::generated_pool()) {
  562. return python_generated_pool;
  563. }
  564. hash_map<const DescriptorPool*, PyDescriptorPool*>::iterator it =
  565. descriptor_pool_map.find(pool);
  566. if (it == descriptor_pool_map.end()) {
  567. PyErr_SetString(PyExc_KeyError, "Unknown descriptor pool");
  568. return NULL;
  569. }
  570. return it->second;
  571. }
  572. } // namespace python
  573. } // namespace protobuf
  574. } // namespace google