店播爬取Python脚本

descriptor_database.cc 7.2KB

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