店播爬取Python脚本

unknown_fields.cc 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 <google/protobuf/pyext/unknown_fields.h>
  31. #include <Python.h>
  32. #include <set>
  33. #include <memory>
  34. #include <google/protobuf/message.h>
  35. #include <google/protobuf/pyext/message.h>
  36. #include <google/protobuf/pyext/scoped_pyobject_ptr.h>
  37. #include <google/protobuf/unknown_field_set.h>
  38. #include <google/protobuf/wire_format_lite.h>
  39. #if PY_MAJOR_VERSION >= 3
  40. #define PyInt_FromLong PyLong_FromLong
  41. #endif
  42. namespace google {
  43. namespace protobuf {
  44. namespace python {
  45. namespace unknown_fields {
  46. static Py_ssize_t Len(PyObject* pself) {
  47. PyUnknownFields* self =
  48. reinterpret_cast<PyUnknownFields*>(pself);
  49. if (self->fields == NULL) {
  50. PyErr_Format(PyExc_ValueError,
  51. "UnknownFields does not exist. "
  52. "The parent message might be cleared.");
  53. return -1;
  54. }
  55. return self->fields->field_count();
  56. }
  57. void Clear(PyUnknownFields* self) {
  58. for (std::set<PyUnknownFields*>::iterator it =
  59. self->sub_unknown_fields.begin();
  60. it != self->sub_unknown_fields.end(); it++) {
  61. Clear(*it);
  62. }
  63. self->fields = NULL;
  64. self->sub_unknown_fields.clear();
  65. }
  66. PyObject* NewPyUnknownFieldRef(PyUnknownFields* parent,
  67. Py_ssize_t index);
  68. static PyObject* Item(PyObject* pself, Py_ssize_t index) {
  69. PyUnknownFields* self =
  70. reinterpret_cast<PyUnknownFields*>(pself);
  71. if (self->fields == NULL) {
  72. PyErr_Format(PyExc_ValueError,
  73. "UnknownFields does not exist. "
  74. "The parent message might be cleared.");
  75. return NULL;
  76. }
  77. Py_ssize_t total_size = self->fields->field_count();
  78. if (index < 0) {
  79. index = total_size + index;
  80. }
  81. if (index < 0 || index >= total_size) {
  82. PyErr_Format(PyExc_IndexError,
  83. "index (%zd) out of range",
  84. index);
  85. return NULL;
  86. }
  87. return unknown_fields::NewPyUnknownFieldRef(self, index);
  88. }
  89. PyObject* NewPyUnknownFields(CMessage* c_message) {
  90. PyUnknownFields* self = reinterpret_cast<PyUnknownFields*>(
  91. PyType_GenericAlloc(&PyUnknownFields_Type, 0));
  92. if (self == NULL) {
  93. return NULL;
  94. }
  95. // Call "placement new" to initialize PyUnknownFields.
  96. new (self) PyUnknownFields;
  97. Py_INCREF(c_message);
  98. self->parent = reinterpret_cast<PyObject*>(c_message);
  99. Message* message = c_message->message;
  100. const Reflection* reflection = message->GetReflection();
  101. self->fields = &reflection->GetUnknownFields(*message);
  102. return reinterpret_cast<PyObject*>(self);
  103. }
  104. PyObject* NewPyUnknownFieldRef(PyUnknownFields* parent,
  105. Py_ssize_t index) {
  106. PyUnknownFieldRef* self = reinterpret_cast<PyUnknownFieldRef*>(
  107. PyType_GenericAlloc(&PyUnknownFieldRef_Type, 0));
  108. if (self == NULL) {
  109. return NULL;
  110. }
  111. Py_INCREF(parent);
  112. self->parent = parent;
  113. self->index = index;
  114. return reinterpret_cast<PyObject*>(self);
  115. }
  116. static void Dealloc(PyObject* pself) {
  117. PyUnknownFields* self =
  118. reinterpret_cast<PyUnknownFields*>(pself);
  119. if (PyObject_TypeCheck(self->parent, &PyUnknownFields_Type)) {
  120. reinterpret_cast<PyUnknownFields*>(
  121. self->parent)->sub_unknown_fields.erase(self);
  122. } else {
  123. reinterpret_cast<CMessage*>(self->parent)->unknown_field_set = nullptr;
  124. }
  125. Py_CLEAR(self->parent);
  126. self->~PyUnknownFields();
  127. Py_TYPE(pself)->tp_free(pself);
  128. }
  129. static PySequenceMethods SqMethods = {
  130. Len, /* sq_length */
  131. 0, /* sq_concat */
  132. 0, /* sq_repeat */
  133. Item, /* sq_item */
  134. 0, /* sq_slice */
  135. 0, /* sq_ass_item */
  136. };
  137. } // namespace unknown_fields
  138. PyTypeObject PyUnknownFields_Type = {
  139. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  140. FULL_MODULE_NAME ".PyUnknownFields", // tp_name
  141. sizeof(PyUnknownFields), // tp_basicsize
  142. 0, // tp_itemsize
  143. unknown_fields::Dealloc, // tp_dealloc
  144. 0, // tp_print
  145. 0, // tp_getattr
  146. 0, // tp_setattr
  147. 0, // tp_compare
  148. 0, // tp_repr
  149. 0, // tp_as_number
  150. &unknown_fields::SqMethods, // tp_as_sequence
  151. 0, // tp_as_mapping
  152. PyObject_HashNotImplemented, // tp_hash
  153. 0, // tp_call
  154. 0, // tp_str
  155. 0, // tp_getattro
  156. 0, // tp_setattro
  157. 0, // tp_as_buffer
  158. Py_TPFLAGS_DEFAULT, // tp_flags
  159. "unknown field set", // tp_doc
  160. 0, // tp_traverse
  161. 0, // tp_clear
  162. 0, // tp_richcompare
  163. 0, // tp_weaklistoffset
  164. 0, // tp_iter
  165. 0, // tp_iternext
  166. 0, // tp_methods
  167. 0, // tp_members
  168. 0, // tp_getset
  169. 0, // tp_base
  170. 0, // tp_dict
  171. 0, // tp_descr_get
  172. 0, // tp_descr_set
  173. 0, // tp_dictoffset
  174. 0, // tp_init
  175. };
  176. namespace unknown_field {
  177. static PyObject* PyUnknownFields_FromUnknownFieldSet(
  178. PyUnknownFields* parent, const UnknownFieldSet& fields) {
  179. PyUnknownFields* self = reinterpret_cast<PyUnknownFields*>(
  180. PyType_GenericAlloc(&PyUnknownFields_Type, 0));
  181. if (self == NULL) {
  182. return NULL;
  183. }
  184. // Call "placement new" to initialize PyUnknownFields.
  185. new (self) PyUnknownFields;
  186. Py_INCREF(parent);
  187. self->parent = reinterpret_cast<PyObject*>(parent);
  188. self->fields = &fields;
  189. parent->sub_unknown_fields.emplace(self);
  190. return reinterpret_cast<PyObject*>(self);
  191. }
  192. const UnknownField* GetUnknownField(PyUnknownFieldRef* self) {
  193. const UnknownFieldSet* fields = self->parent->fields;
  194. if (fields == NULL) {
  195. PyErr_Format(PyExc_ValueError,
  196. "UnknownField does not exist. "
  197. "The parent message might be cleared.");
  198. return NULL;
  199. }
  200. ssize_t total_size = fields->field_count();
  201. if (self->index >= total_size) {
  202. PyErr_Format(PyExc_ValueError,
  203. "UnknownField does not exist. "
  204. "The parent message might be cleared.");
  205. return NULL;
  206. }
  207. return &fields->field(self->index);
  208. }
  209. static PyObject* GetFieldNumber(PyUnknownFieldRef* self, void *closure) {
  210. const UnknownField* unknown_field = GetUnknownField(self);
  211. if (unknown_field == NULL) {
  212. return NULL;
  213. }
  214. return PyInt_FromLong(unknown_field->number());
  215. }
  216. using internal::WireFormatLite;
  217. static PyObject* GetWireType(PyUnknownFieldRef* self, void *closure) {
  218. const UnknownField* unknown_field = GetUnknownField(self);
  219. if (unknown_field == NULL) {
  220. return NULL;
  221. }
  222. // Assign a default value to suppress may-uninitialized warnings (errors
  223. // when built in some places).
  224. WireFormatLite::WireType wire_type = WireFormatLite::WIRETYPE_VARINT;
  225. switch (unknown_field->type()) {
  226. case UnknownField::TYPE_VARINT:
  227. wire_type = WireFormatLite::WIRETYPE_VARINT;
  228. break;
  229. case UnknownField::TYPE_FIXED32:
  230. wire_type = WireFormatLite::WIRETYPE_FIXED32;
  231. break;
  232. case UnknownField::TYPE_FIXED64:
  233. wire_type = WireFormatLite::WIRETYPE_FIXED64;
  234. break;
  235. case UnknownField::TYPE_LENGTH_DELIMITED:
  236. wire_type = WireFormatLite::WIRETYPE_LENGTH_DELIMITED;
  237. break;
  238. case UnknownField::TYPE_GROUP:
  239. wire_type = WireFormatLite::WIRETYPE_START_GROUP;
  240. break;
  241. }
  242. return PyInt_FromLong(wire_type);
  243. }
  244. static PyObject* GetData(PyUnknownFieldRef* self, void *closure) {
  245. const UnknownField* field = GetUnknownField(self);
  246. if (field == NULL) {
  247. return NULL;
  248. }
  249. PyObject* data = NULL;
  250. switch (field->type()) {
  251. case UnknownField::TYPE_VARINT:
  252. data = PyInt_FromLong(field->varint());
  253. break;
  254. case UnknownField::TYPE_FIXED32:
  255. data = PyInt_FromLong(field->fixed32());
  256. break;
  257. case UnknownField::TYPE_FIXED64:
  258. data = PyInt_FromLong(field->fixed64());
  259. break;
  260. case UnknownField::TYPE_LENGTH_DELIMITED:
  261. data = PyBytes_FromStringAndSize(field->length_delimited().data(),
  262. field->GetLengthDelimitedSize());
  263. break;
  264. case UnknownField::TYPE_GROUP:
  265. data = PyUnknownFields_FromUnknownFieldSet(
  266. self->parent, field->group());
  267. break;
  268. }
  269. return data;
  270. }
  271. static void Dealloc(PyObject* pself) {
  272. PyUnknownFieldRef* self =
  273. reinterpret_cast<PyUnknownFieldRef*>(pself);
  274. Py_CLEAR(self->parent);
  275. }
  276. static PyGetSetDef Getters[] = {
  277. {"field_number", (getter)GetFieldNumber, NULL},
  278. {"wire_type", (getter)GetWireType, NULL},
  279. {"data", (getter)GetData, NULL},
  280. {NULL}
  281. };
  282. } // namespace unknown_field
  283. PyTypeObject PyUnknownFieldRef_Type = {
  284. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  285. FULL_MODULE_NAME ".PyUnknownFieldRef", // tp_name
  286. sizeof(PyUnknownFieldRef), // tp_basicsize
  287. 0, // tp_itemsize
  288. unknown_field::Dealloc, // tp_dealloc
  289. 0, // tp_print
  290. 0, // tp_getattr
  291. 0, // tp_setattr
  292. 0, // tp_compare
  293. 0, // tp_repr
  294. 0, // tp_as_number
  295. 0, // tp_as_sequence
  296. 0, // tp_as_mapping
  297. PyObject_HashNotImplemented, // tp_hash
  298. 0, // tp_call
  299. 0, // tp_str
  300. 0, // tp_getattro
  301. 0, // tp_setattro
  302. 0, // tp_as_buffer
  303. Py_TPFLAGS_DEFAULT, // tp_flags
  304. "unknown field", // tp_doc
  305. 0, // tp_traverse
  306. 0, // tp_clear
  307. 0, // tp_richcompare
  308. 0, // tp_weaklistoffset
  309. 0, // tp_iter
  310. 0, // tp_iternext
  311. 0, // tp_methods
  312. 0, // tp_members
  313. unknown_field::Getters, // tp_getset
  314. 0, // tp_base
  315. 0, // tp_dict
  316. 0, // tp_descr_get
  317. 0, // tp_descr_set
  318. 0, // tp_dictoffset
  319. 0, // tp_init
  320. };
  321. } // namespace python
  322. } // namespace protobuf
  323. } // namespace google