descriptor_database.cc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. // This file defines a C++ DescriptorDatabase, which wraps a Python Database
  31. // and delegate all its operations to Python methods.
  32. #include <google/protobuf/pyext/descriptor_database.h>
  33. #include <google/protobuf/stubs/logging.h>
  34. #include <google/protobuf/stubs/common.h>
  35. #include <google/protobuf/descriptor.pb.h>
  36. #include <google/protobuf/pyext/message.h>
  37. #include <google/protobuf/pyext/scoped_pyobject_ptr.h>
  38. namespace google {
  39. namespace protobuf {
  40. namespace python {
  41. PyDescriptorDatabase::PyDescriptorDatabase(PyObject* py_database)
  42. : py_database_(py_database) {
  43. Py_INCREF(py_database_);
  44. }
  45. PyDescriptorDatabase::~PyDescriptorDatabase() { Py_DECREF(py_database_); }
  46. // Convert a Python object to a FileDescriptorProto pointer.
  47. // Handles all kinds of Python errors, which are simply logged.
  48. static bool GetFileDescriptorProto(PyObject* py_descriptor,
  49. FileDescriptorProto* output) {
  50. if (py_descriptor == NULL) {
  51. if (PyErr_ExceptionMatches(PyExc_KeyError)) {
  52. // Expected error: item was simply not found.
  53. PyErr_Clear();
  54. } else {
  55. GOOGLE_LOG(ERROR) << "DescriptorDatabase method raised an error";
  56. PyErr_Print();
  57. }
  58. return false;
  59. }
  60. if (py_descriptor == Py_None) {
  61. return false;
  62. }
  63. const Descriptor* filedescriptor_descriptor =
  64. FileDescriptorProto::default_instance().GetDescriptor();
  65. CMessage* message = reinterpret_cast<CMessage*>(py_descriptor);
  66. if (PyObject_TypeCheck(py_descriptor, CMessage_Type) &&
  67. message->message->GetDescriptor() == filedescriptor_descriptor) {
  68. // Fast path: Just use the pointer.
  69. FileDescriptorProto* file_proto =
  70. static_cast<FileDescriptorProto*>(message->message);
  71. *output = *file_proto;
  72. return true;
  73. } else {
  74. // Slow path: serialize the message. This allows to use databases which
  75. // use a different implementation of FileDescriptorProto.
  76. ScopedPyObjectPtr serialized_pb(
  77. PyObject_CallMethod(py_descriptor, "SerializeToString", NULL));
  78. if (serialized_pb == NULL) {
  79. GOOGLE_LOG(ERROR)
  80. << "DescriptorDatabase method did not return a FileDescriptorProto";
  81. PyErr_Print();
  82. return false;
  83. }
  84. char* str;
  85. Py_ssize_t len;
  86. if (PyBytes_AsStringAndSize(serialized_pb.get(), &str, &len) < 0) {
  87. GOOGLE_LOG(ERROR)
  88. << "DescriptorDatabase method did not return a FileDescriptorProto";
  89. PyErr_Print();
  90. return false;
  91. }
  92. FileDescriptorProto file_proto;
  93. if (!file_proto.ParseFromArray(str, len)) {
  94. GOOGLE_LOG(ERROR)
  95. << "DescriptorDatabase method did not return a FileDescriptorProto";
  96. return false;
  97. }
  98. *output = file_proto;
  99. return true;
  100. }
  101. }
  102. // Find a file by file name.
  103. bool PyDescriptorDatabase::FindFileByName(const std::string& filename,
  104. FileDescriptorProto* output) {
  105. ScopedPyObjectPtr py_descriptor(PyObject_CallMethod(
  106. py_database_, "FindFileByName", "s#", filename.c_str(), filename.size()));
  107. return GetFileDescriptorProto(py_descriptor.get(), output);
  108. }
  109. // Find the file that declares the given fully-qualified symbol name.
  110. bool PyDescriptorDatabase::FindFileContainingSymbol(
  111. const std::string& symbol_name, FileDescriptorProto* output) {
  112. ScopedPyObjectPtr py_descriptor(
  113. PyObject_CallMethod(py_database_, "FindFileContainingSymbol", "s#",
  114. symbol_name.c_str(), symbol_name.size()));
  115. return GetFileDescriptorProto(py_descriptor.get(), output);
  116. }
  117. // Find the file which defines an extension extending the given message type
  118. // with the given field number.
  119. // Python DescriptorDatabases are not required to implement this method.
  120. bool PyDescriptorDatabase::FindFileContainingExtension(
  121. const std::string& containing_type, int field_number,
  122. FileDescriptorProto* output) {
  123. ScopedPyObjectPtr py_method(
  124. PyObject_GetAttrString(py_database_, "FindFileContainingExtension"));
  125. if (py_method == NULL) {
  126. // This method is not implemented, returns without error.
  127. PyErr_Clear();
  128. return false;
  129. }
  130. ScopedPyObjectPtr py_descriptor(
  131. PyObject_CallFunction(py_method.get(), "s#i", containing_type.c_str(),
  132. containing_type.size(), field_number));
  133. return GetFileDescriptorProto(py_descriptor.get(), output);
  134. }
  135. // Finds the tag numbers used by all known extensions of
  136. // containing_type, and appends them to output in an undefined
  137. // order.
  138. // Python DescriptorDatabases are not required to implement this method.
  139. bool PyDescriptorDatabase::FindAllExtensionNumbers(
  140. const std::string& containing_type, std::vector<int>* output) {
  141. ScopedPyObjectPtr py_method(
  142. PyObject_GetAttrString(py_database_, "FindAllExtensionNumbers"));
  143. if (py_method == NULL) {
  144. // This method is not implemented, returns without error.
  145. PyErr_Clear();
  146. return false;
  147. }
  148. ScopedPyObjectPtr py_list(
  149. PyObject_CallFunction(py_method.get(), "s#", containing_type.c_str(),
  150. containing_type.size()));
  151. if (py_list == NULL) {
  152. PyErr_Print();
  153. return false;
  154. }
  155. Py_ssize_t size = PyList_Size(py_list.get());
  156. int64 item_value;
  157. for (Py_ssize_t i = 0 ; i < size; ++i) {
  158. ScopedPyObjectPtr item(PySequence_GetItem(py_list.get(), i));
  159. item_value = PyLong_AsLong(item.get());
  160. if (item_value < 0) {
  161. GOOGLE_LOG(ERROR)
  162. << "FindAllExtensionNumbers method did not return "
  163. << "valid extension numbers.";
  164. PyErr_Print();
  165. return false;
  166. }
  167. output->push_back(item_value);
  168. }
  169. return true;
  170. }
  171. } // namespace python
  172. } // namespace protobuf
  173. } // namespace google