descriptor_pool.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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_pool.h>
  34. #include <google/protobuf/pyext/descriptor.h>
  35. #include <google/protobuf/pyext/message.h>
  36. #include <google/protobuf/pyext/scoped_pyobject_ptr.h>
  37. #if PY_MAJOR_VERSION >= 3
  38. #define PyString_FromStringAndSize PyUnicode_FromStringAndSize
  39. #if PY_VERSION_HEX < 0x03030000
  40. #error "Python 3.0 - 3.2 are not supported."
  41. #endif
  42. #define PyString_AsStringAndSize(ob, charpp, sizep) \
  43. (PyUnicode_Check(ob)? \
  44. ((*(charpp) = PyUnicode_AsUTF8AndSize(ob, (sizep))) == NULL? -1: 0): \
  45. PyBytes_AsStringAndSize(ob, (charpp), (sizep)))
  46. #endif
  47. namespace google {
  48. namespace protobuf {
  49. namespace python {
  50. namespace cdescriptor_pool {
  51. PyDescriptorPool* NewDescriptorPool() {
  52. PyDescriptorPool* cdescriptor_pool = PyObject_New(
  53. PyDescriptorPool, &PyDescriptorPool_Type);
  54. if (cdescriptor_pool == NULL) {
  55. return NULL;
  56. }
  57. // Build a DescriptorPool for messages only declared in Python libraries.
  58. // generated_pool() contains all messages linked in C++ libraries, and is used
  59. // as underlay.
  60. cdescriptor_pool->pool = new DescriptorPool(DescriptorPool::generated_pool());
  61. // TODO(amauryfa): Rewrite the SymbolDatabase in C so that it uses the same
  62. // storage.
  63. cdescriptor_pool->classes_by_descriptor =
  64. new PyDescriptorPool::ClassesByMessageMap();
  65. cdescriptor_pool->interned_descriptors =
  66. new hash_map<const void*, PyObject *>();
  67. cdescriptor_pool->descriptor_options =
  68. new hash_map<const void*, PyObject *>();
  69. return cdescriptor_pool;
  70. }
  71. static void Dealloc(PyDescriptorPool* self) {
  72. typedef PyDescriptorPool::ClassesByMessageMap::iterator iterator;
  73. for (iterator it = self->classes_by_descriptor->begin();
  74. it != self->classes_by_descriptor->end(); ++it) {
  75. Py_DECREF(it->second);
  76. }
  77. delete self->classes_by_descriptor;
  78. delete self->interned_descriptors; // its references were borrowed.
  79. for (hash_map<const void*, PyObject*>::iterator it =
  80. self->descriptor_options->begin();
  81. it != self->descriptor_options->end(); ++it) {
  82. Py_DECREF(it->second);
  83. }
  84. delete self->descriptor_options;
  85. Py_TYPE(self)->tp_free(reinterpret_cast<PyObject*>(self));
  86. }
  87. PyObject* FindMessageByName(PyDescriptorPool* self, PyObject* arg) {
  88. Py_ssize_t name_size;
  89. char* name;
  90. if (PyString_AsStringAndSize(arg, &name, &name_size) < 0) {
  91. return NULL;
  92. }
  93. const Descriptor* message_descriptor =
  94. self->pool->FindMessageTypeByName(string(name, name_size));
  95. if (message_descriptor == NULL) {
  96. PyErr_Format(PyExc_KeyError, "Couldn't find message %.200s", name);
  97. return NULL;
  98. }
  99. return PyMessageDescriptor_FromDescriptor(message_descriptor);
  100. }
  101. // Add a message class to our database.
  102. const Descriptor* RegisterMessageClass(
  103. PyDescriptorPool* self, PyObject *message_class, PyObject* descriptor) {
  104. ScopedPyObjectPtr full_message_name(
  105. PyObject_GetAttrString(descriptor, "full_name"));
  106. Py_ssize_t name_size;
  107. char* name;
  108. if (PyString_AsStringAndSize(full_message_name, &name, &name_size) < 0) {
  109. return NULL;
  110. }
  111. const Descriptor *message_descriptor =
  112. self->pool->FindMessageTypeByName(string(name, name_size));
  113. if (!message_descriptor) {
  114. PyErr_Format(PyExc_TypeError, "Could not find C++ descriptor for '%s'",
  115. name);
  116. return NULL;
  117. }
  118. Py_INCREF(message_class);
  119. typedef PyDescriptorPool::ClassesByMessageMap::iterator iterator;
  120. std::pair<iterator, bool> ret = self->classes_by_descriptor->insert(
  121. std::make_pair(message_descriptor, message_class));
  122. if (!ret.second) {
  123. // Update case: DECREF the previous value.
  124. Py_DECREF(ret.first->second);
  125. ret.first->second = message_class;
  126. }
  127. return message_descriptor;
  128. }
  129. // Retrieve the message class added to our database.
  130. PyObject *GetMessageClass(PyDescriptorPool* self,
  131. const Descriptor *message_descriptor) {
  132. typedef PyDescriptorPool::ClassesByMessageMap::iterator iterator;
  133. iterator ret = self->classes_by_descriptor->find(message_descriptor);
  134. if (ret == self->classes_by_descriptor->end()) {
  135. PyErr_Format(PyExc_TypeError, "No message class registered for '%s'",
  136. message_descriptor->full_name().c_str());
  137. return NULL;
  138. } else {
  139. return ret->second;
  140. }
  141. }
  142. PyObject* FindFileByName(PyDescriptorPool* self, PyObject* arg) {
  143. Py_ssize_t name_size;
  144. char* name;
  145. if (PyString_AsStringAndSize(arg, &name, &name_size) < 0) {
  146. return NULL;
  147. }
  148. const FileDescriptor* file_descriptor =
  149. self->pool->FindFileByName(string(name, name_size));
  150. if (file_descriptor == NULL) {
  151. PyErr_Format(PyExc_KeyError, "Couldn't find file %.200s",
  152. name);
  153. return NULL;
  154. }
  155. return PyFileDescriptor_FromDescriptor(file_descriptor);
  156. }
  157. PyObject* FindFieldByName(PyDescriptorPool* self, PyObject* arg) {
  158. Py_ssize_t name_size;
  159. char* name;
  160. if (PyString_AsStringAndSize(arg, &name, &name_size) < 0) {
  161. return NULL;
  162. }
  163. const FieldDescriptor* field_descriptor =
  164. self->pool->FindFieldByName(string(name, name_size));
  165. if (field_descriptor == NULL) {
  166. PyErr_Format(PyExc_KeyError, "Couldn't find field %.200s",
  167. name);
  168. return NULL;
  169. }
  170. return PyFieldDescriptor_FromDescriptor(field_descriptor);
  171. }
  172. PyObject* FindExtensionByName(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->FindExtensionByName(string(name, name_size));
  180. if (field_descriptor == NULL) {
  181. PyErr_Format(PyExc_KeyError, "Couldn't find extension field %.200s", name);
  182. return NULL;
  183. }
  184. return PyFieldDescriptor_FromDescriptor(field_descriptor);
  185. }
  186. PyObject* FindEnumTypeByName(PyDescriptorPool* self, PyObject* arg) {
  187. Py_ssize_t name_size;
  188. char* name;
  189. if (PyString_AsStringAndSize(arg, &name, &name_size) < 0) {
  190. return NULL;
  191. }
  192. const EnumDescriptor* enum_descriptor =
  193. self->pool->FindEnumTypeByName(string(name, name_size));
  194. if (enum_descriptor == NULL) {
  195. PyErr_Format(PyExc_KeyError, "Couldn't find enum %.200s", name);
  196. return NULL;
  197. }
  198. return PyEnumDescriptor_FromDescriptor(enum_descriptor);
  199. }
  200. PyObject* FindOneofByName(PyDescriptorPool* self, PyObject* arg) {
  201. Py_ssize_t name_size;
  202. char* name;
  203. if (PyString_AsStringAndSize(arg, &name, &name_size) < 0) {
  204. return NULL;
  205. }
  206. const OneofDescriptor* oneof_descriptor =
  207. self->pool->FindOneofByName(string(name, name_size));
  208. if (oneof_descriptor == NULL) {
  209. PyErr_Format(PyExc_KeyError, "Couldn't find oneof %.200s", name);
  210. return NULL;
  211. }
  212. return PyOneofDescriptor_FromDescriptor(oneof_descriptor);
  213. }
  214. // The code below loads new Descriptors from a serialized FileDescriptorProto.
  215. // Collects errors that occur during proto file building to allow them to be
  216. // propagated in the python exception instead of only living in ERROR logs.
  217. class BuildFileErrorCollector : public DescriptorPool::ErrorCollector {
  218. public:
  219. BuildFileErrorCollector() : error_message(""), had_errors(false) {}
  220. void AddError(const string& filename, const string& element_name,
  221. const Message* descriptor, ErrorLocation location,
  222. const string& message) {
  223. // Replicates the logging behavior that happens in the C++ implementation
  224. // when an error collector is not passed in.
  225. if (!had_errors) {
  226. error_message +=
  227. ("Invalid proto descriptor for file \"" + filename + "\":\n");
  228. had_errors = true;
  229. }
  230. // As this only happens on failure and will result in the program not
  231. // running at all, no effort is made to optimize this string manipulation.
  232. error_message += (" " + element_name + ": " + message + "\n");
  233. }
  234. string error_message;
  235. bool had_errors;
  236. };
  237. PyObject* AddSerializedFile(PyDescriptorPool* self, PyObject* serialized_pb) {
  238. char* message_type;
  239. Py_ssize_t message_len;
  240. if (PyBytes_AsStringAndSize(serialized_pb, &message_type, &message_len) < 0) {
  241. return NULL;
  242. }
  243. FileDescriptorProto file_proto;
  244. if (!file_proto.ParseFromArray(message_type, message_len)) {
  245. PyErr_SetString(PyExc_TypeError, "Couldn't parse file content!");
  246. return NULL;
  247. }
  248. // If the file was already part of a C++ library, all its descriptors are in
  249. // the underlying pool. No need to do anything else.
  250. const FileDescriptor* generated_file =
  251. DescriptorPool::generated_pool()->FindFileByName(file_proto.name());
  252. if (generated_file != NULL) {
  253. return PyFileDescriptor_FromDescriptorWithSerializedPb(
  254. generated_file, serialized_pb);
  255. }
  256. BuildFileErrorCollector error_collector;
  257. const FileDescriptor* descriptor =
  258. self->pool->BuildFileCollectingErrors(file_proto,
  259. &error_collector);
  260. if (descriptor == NULL) {
  261. PyErr_Format(PyExc_TypeError,
  262. "Couldn't build proto file into descriptor pool!\n%s",
  263. error_collector.error_message.c_str());
  264. return NULL;
  265. }
  266. return PyFileDescriptor_FromDescriptorWithSerializedPb(
  267. descriptor, serialized_pb);
  268. }
  269. PyObject* Add(PyDescriptorPool* self, PyObject* file_descriptor_proto) {
  270. ScopedPyObjectPtr serialized_pb(
  271. PyObject_CallMethod(file_descriptor_proto, "SerializeToString", NULL));
  272. if (serialized_pb == NULL) {
  273. return NULL;
  274. }
  275. return AddSerializedFile(self, serialized_pb);
  276. }
  277. static PyMethodDef Methods[] = {
  278. { "Add", (PyCFunction)Add, METH_O,
  279. "Adds the FileDescriptorProto and its types to this pool." },
  280. { "AddSerializedFile", (PyCFunction)AddSerializedFile, METH_O,
  281. "Adds a serialized FileDescriptorProto to this pool." },
  282. { "FindFileByName", (PyCFunction)FindFileByName, METH_O,
  283. "Searches for a file descriptor by its .proto name." },
  284. { "FindMessageTypeByName", (PyCFunction)FindMessageByName, METH_O,
  285. "Searches for a message descriptor by full name." },
  286. { "FindFieldByName", (PyCFunction)FindFieldByName, METH_O,
  287. "Searches for a field descriptor by full name." },
  288. { "FindExtensionByName", (PyCFunction)FindExtensionByName, METH_O,
  289. "Searches for extension descriptor by full name." },
  290. { "FindEnumTypeByName", (PyCFunction)FindEnumTypeByName, METH_O,
  291. "Searches for enum type descriptor by full name." },
  292. { "FindOneofByName", (PyCFunction)FindOneofByName, METH_O,
  293. "Searches for oneof descriptor by full name." },
  294. {NULL}
  295. };
  296. } // namespace cdescriptor_pool
  297. PyTypeObject PyDescriptorPool_Type = {
  298. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  299. FULL_MODULE_NAME ".DescriptorPool", // tp_name
  300. sizeof(PyDescriptorPool), // tp_basicsize
  301. 0, // tp_itemsize
  302. (destructor)cdescriptor_pool::Dealloc, // tp_dealloc
  303. 0, // tp_print
  304. 0, // tp_getattr
  305. 0, // tp_setattr
  306. 0, // tp_compare
  307. 0, // tp_repr
  308. 0, // tp_as_number
  309. 0, // tp_as_sequence
  310. 0, // tp_as_mapping
  311. 0, // tp_hash
  312. 0, // tp_call
  313. 0, // tp_str
  314. 0, // tp_getattro
  315. 0, // tp_setattro
  316. 0, // tp_as_buffer
  317. Py_TPFLAGS_DEFAULT, // tp_flags
  318. "A Descriptor Pool", // tp_doc
  319. 0, // tp_traverse
  320. 0, // tp_clear
  321. 0, // tp_richcompare
  322. 0, // tp_weaklistoffset
  323. 0, // tp_iter
  324. 0, // tp_iternext
  325. cdescriptor_pool::Methods, // tp_methods
  326. 0, // tp_members
  327. 0, // tp_getset
  328. 0, // tp_base
  329. 0, // tp_dict
  330. 0, // tp_descr_get
  331. 0, // tp_descr_set
  332. 0, // tp_dictoffset
  333. 0, // tp_init
  334. 0, // tp_alloc
  335. 0, // tp_new
  336. PyObject_Del, // tp_free
  337. };
  338. static PyDescriptorPool* global_cdescriptor_pool = NULL;
  339. bool InitDescriptorPool() {
  340. if (PyType_Ready(&PyDescriptorPool_Type) < 0)
  341. return false;
  342. global_cdescriptor_pool = cdescriptor_pool::NewDescriptorPool();
  343. if (global_cdescriptor_pool == NULL) {
  344. return false;
  345. }
  346. return true;
  347. }
  348. PyDescriptorPool* GetDescriptorPool() {
  349. return global_cdescriptor_pool;
  350. }
  351. } // namespace python
  352. } // namespace protobuf
  353. } // namespace google