店播爬取Python脚本

message_factory.cc 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. #include <unordered_map>
  31. #include <Python.h>
  32. #include <google/protobuf/dynamic_message.h>
  33. #include <google/protobuf/pyext/descriptor.h>
  34. #include <google/protobuf/pyext/message.h>
  35. #include <google/protobuf/pyext/message_factory.h>
  36. #include <google/protobuf/pyext/scoped_pyobject_ptr.h>
  37. #if PY_MAJOR_VERSION >= 3
  38. #if PY_VERSION_HEX < 0x03030000
  39. #error "Python 3.0 - 3.2 are not supported."
  40. #endif
  41. #define PyString_AsStringAndSize(ob, charpp, sizep) \
  42. (PyUnicode_Check(ob) ? ((*(charpp) = const_cast<char*>( \
  43. PyUnicode_AsUTF8AndSize(ob, (sizep)))) == NULL \
  44. ? -1 \
  45. : 0) \
  46. : PyBytes_AsStringAndSize(ob, (charpp), (sizep)))
  47. #endif
  48. namespace google {
  49. namespace protobuf {
  50. namespace python {
  51. namespace message_factory {
  52. PyMessageFactory* NewMessageFactory(PyTypeObject* type, PyDescriptorPool* pool) {
  53. PyMessageFactory* factory = reinterpret_cast<PyMessageFactory*>(
  54. PyType_GenericAlloc(type, 0));
  55. if (factory == NULL) {
  56. return NULL;
  57. }
  58. DynamicMessageFactory* message_factory = new DynamicMessageFactory();
  59. // This option might be the default some day.
  60. message_factory->SetDelegateToGeneratedFactory(true);
  61. factory->message_factory = message_factory;
  62. factory->pool = pool;
  63. Py_INCREF(pool);
  64. factory->classes_by_descriptor = new PyMessageFactory::ClassesByMessageMap();
  65. return factory;
  66. }
  67. PyObject* New(PyTypeObject* type, PyObject* args, PyObject* kwargs) {
  68. static const char* kwlist[] = {"pool", 0};
  69. PyObject* pool = NULL;
  70. if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O",
  71. const_cast<char**>(kwlist), &pool)) {
  72. return NULL;
  73. }
  74. ScopedPyObjectPtr owned_pool;
  75. if (pool == NULL || pool == Py_None) {
  76. owned_pool.reset(PyObject_CallFunction(
  77. reinterpret_cast<PyObject*>(&PyDescriptorPool_Type), NULL));
  78. if (owned_pool == NULL) {
  79. return NULL;
  80. }
  81. pool = owned_pool.get();
  82. } else {
  83. if (!PyObject_TypeCheck(pool, &PyDescriptorPool_Type)) {
  84. PyErr_Format(PyExc_TypeError, "Expected a DescriptorPool, got %s",
  85. pool->ob_type->tp_name);
  86. return NULL;
  87. }
  88. }
  89. return reinterpret_cast<PyObject*>(
  90. NewMessageFactory(type, reinterpret_cast<PyDescriptorPool*>(pool)));
  91. }
  92. static void Dealloc(PyObject* pself) {
  93. PyMessageFactory* self = reinterpret_cast<PyMessageFactory*>(pself);
  94. typedef PyMessageFactory::ClassesByMessageMap::iterator iterator;
  95. for (iterator it = self->classes_by_descriptor->begin();
  96. it != self->classes_by_descriptor->end(); ++it) {
  97. Py_CLEAR(it->second);
  98. }
  99. delete self->classes_by_descriptor;
  100. delete self->message_factory;
  101. Py_CLEAR(self->pool);
  102. Py_TYPE(self)->tp_free(pself);
  103. }
  104. static int GcTraverse(PyObject* pself, visitproc visit, void* arg) {
  105. PyMessageFactory* self = reinterpret_cast<PyMessageFactory*>(pself);
  106. Py_VISIT(self->pool);
  107. for (const auto& desc_and_class : *self->classes_by_descriptor) {
  108. Py_VISIT(desc_and_class.second);
  109. }
  110. return 0;
  111. }
  112. static int GcClear(PyObject* pself) {
  113. PyMessageFactory* self = reinterpret_cast<PyMessageFactory*>(pself);
  114. // Here it's important to not clear self->pool, so that the C++ DescriptorPool
  115. // is still alive when self->message_factory is destructed.
  116. for (auto& desc_and_class : *self->classes_by_descriptor) {
  117. Py_CLEAR(desc_and_class.second);
  118. }
  119. return 0;
  120. }
  121. // Add a message class to our database.
  122. int RegisterMessageClass(PyMessageFactory* self,
  123. const Descriptor* message_descriptor,
  124. CMessageClass* message_class) {
  125. Py_INCREF(message_class);
  126. typedef PyMessageFactory::ClassesByMessageMap::iterator iterator;
  127. std::pair<iterator, bool> ret = self->classes_by_descriptor->insert(
  128. std::make_pair(message_descriptor, message_class));
  129. if (!ret.second) {
  130. // Update case: DECREF the previous value.
  131. Py_DECREF(ret.first->second);
  132. ret.first->second = message_class;
  133. }
  134. return 0;
  135. }
  136. CMessageClass* GetOrCreateMessageClass(PyMessageFactory* self,
  137. const Descriptor* descriptor) {
  138. // This is the same implementation as MessageFactory.GetPrototype().
  139. // Do not create a MessageClass that already exists.
  140. std::unordered_map<const Descriptor*, CMessageClass*>::iterator it =
  141. self->classes_by_descriptor->find(descriptor);
  142. if (it != self->classes_by_descriptor->end()) {
  143. Py_INCREF(it->second);
  144. return it->second;
  145. }
  146. ScopedPyObjectPtr py_descriptor(
  147. PyMessageDescriptor_FromDescriptor(descriptor));
  148. if (py_descriptor == NULL) {
  149. return NULL;
  150. }
  151. // Create a new message class.
  152. ScopedPyObjectPtr args(Py_BuildValue(
  153. "s(){sOsOsO}", descriptor->name().c_str(),
  154. "DESCRIPTOR", py_descriptor.get(),
  155. "__module__", Py_None,
  156. "message_factory", self));
  157. if (args == NULL) {
  158. return NULL;
  159. }
  160. ScopedPyObjectPtr message_class(PyObject_CallObject(
  161. reinterpret_cast<PyObject*>(CMessageClass_Type), args.get()));
  162. if (message_class == NULL) {
  163. return NULL;
  164. }
  165. // Create messages class for the messages used by the fields, and registers
  166. // all extensions for these messages during the recursion.
  167. for (int field_idx = 0; field_idx < descriptor->field_count(); field_idx++) {
  168. const Descriptor* sub_descriptor =
  169. descriptor->field(field_idx)->message_type();
  170. // It is NULL if the field type is not a message.
  171. if (sub_descriptor != NULL) {
  172. CMessageClass* result = GetOrCreateMessageClass(self, sub_descriptor);
  173. if (result == NULL) {
  174. return NULL;
  175. }
  176. Py_DECREF(result);
  177. }
  178. }
  179. // Register extensions defined in this message.
  180. for (int ext_idx = 0 ; ext_idx < descriptor->extension_count() ; ext_idx++) {
  181. const FieldDescriptor* extension = descriptor->extension(ext_idx);
  182. ScopedPyObjectPtr py_extended_class(
  183. GetOrCreateMessageClass(self, extension->containing_type())
  184. ->AsPyObject());
  185. if (py_extended_class == NULL) {
  186. return NULL;
  187. }
  188. ScopedPyObjectPtr py_extension(PyFieldDescriptor_FromDescriptor(extension));
  189. if (py_extension == NULL) {
  190. return NULL;
  191. }
  192. ScopedPyObjectPtr result(cmessage::RegisterExtension(
  193. py_extended_class.get(), py_extension.get()));
  194. if (result == NULL) {
  195. return NULL;
  196. }
  197. }
  198. return reinterpret_cast<CMessageClass*>(message_class.release());
  199. }
  200. // Retrieve the message class added to our database.
  201. CMessageClass* GetMessageClass(PyMessageFactory* self,
  202. const Descriptor* message_descriptor) {
  203. typedef PyMessageFactory::ClassesByMessageMap::iterator iterator;
  204. iterator ret = self->classes_by_descriptor->find(message_descriptor);
  205. if (ret == self->classes_by_descriptor->end()) {
  206. PyErr_Format(PyExc_TypeError, "No message class registered for '%s'",
  207. message_descriptor->full_name().c_str());
  208. return NULL;
  209. } else {
  210. return ret->second;
  211. }
  212. }
  213. static PyMethodDef Methods[] = {
  214. {NULL}};
  215. static PyObject* GetPool(PyMessageFactory* self, void* closure) {
  216. Py_INCREF(self->pool);
  217. return reinterpret_cast<PyObject*>(self->pool);
  218. }
  219. static PyGetSetDef Getters[] = {
  220. {"pool", (getter)GetPool, NULL, "DescriptorPool"},
  221. {NULL}
  222. };
  223. } // namespace message_factory
  224. PyTypeObject PyMessageFactory_Type = {
  225. PyVarObject_HEAD_INIT(&PyType_Type, 0) FULL_MODULE_NAME
  226. ".MessageFactory", // tp_name
  227. sizeof(PyMessageFactory), // tp_basicsize
  228. 0, // tp_itemsize
  229. message_factory::Dealloc, // tp_dealloc
  230. 0, // tp_print
  231. 0, // tp_getattr
  232. 0, // tp_setattr
  233. 0, // tp_compare
  234. 0, // tp_repr
  235. 0, // tp_as_number
  236. 0, // tp_as_sequence
  237. 0, // tp_as_mapping
  238. 0, // tp_hash
  239. 0, // tp_call
  240. 0, // tp_str
  241. 0, // tp_getattro
  242. 0, // tp_setattro
  243. 0, // tp_as_buffer
  244. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, // tp_flags
  245. "A static Message Factory", // tp_doc
  246. message_factory::GcTraverse, // tp_traverse
  247. message_factory::GcClear, // tp_clear
  248. 0, // tp_richcompare
  249. 0, // tp_weaklistoffset
  250. 0, // tp_iter
  251. 0, // tp_iternext
  252. message_factory::Methods, // tp_methods
  253. 0, // tp_members
  254. message_factory::Getters, // tp_getset
  255. 0, // tp_base
  256. 0, // tp_dict
  257. 0, // tp_descr_get
  258. 0, // tp_descr_set
  259. 0, // tp_dictoffset
  260. 0, // tp_init
  261. 0, // tp_alloc
  262. message_factory::New, // tp_new
  263. PyObject_GC_Del, // tp_free
  264. };
  265. bool InitMessageFactory() {
  266. if (PyType_Ready(&PyMessageFactory_Type) < 0) {
  267. return false;
  268. }
  269. return true;
  270. }
  271. } // namespace python
  272. } // namespace protobuf
  273. } // namespace google