店播爬取Python脚本

field.cc 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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/field.h>
  31. #include <google/protobuf/descriptor.h>
  32. #include <google/protobuf/pyext/descriptor.h>
  33. #include <google/protobuf/pyext/message.h>
  34. #if PY_MAJOR_VERSION >= 3
  35. #define PyString_FromFormat PyUnicode_FromFormat
  36. #endif
  37. namespace google {
  38. namespace protobuf {
  39. namespace python {
  40. namespace field {
  41. static PyObject* Repr(PyMessageFieldProperty* self) {
  42. return PyString_FromFormat("<field property '%s'>",
  43. self->field_descriptor->full_name().c_str());
  44. }
  45. static PyObject* DescrGet(PyMessageFieldProperty* self, PyObject* obj,
  46. PyObject* type) {
  47. if (obj == NULL) {
  48. Py_INCREF(self);
  49. return reinterpret_cast<PyObject*>(self);
  50. }
  51. return cmessage::GetFieldValue(reinterpret_cast<CMessage*>(obj),
  52. self->field_descriptor);
  53. }
  54. static int DescrSet(PyMessageFieldProperty* self, PyObject* obj,
  55. PyObject* value) {
  56. if (value == NULL) {
  57. PyErr_SetString(PyExc_AttributeError, "Cannot delete field attribute");
  58. return -1;
  59. }
  60. return cmessage::SetFieldValue(reinterpret_cast<CMessage*>(obj),
  61. self->field_descriptor, value);
  62. }
  63. static PyObject* GetDescriptor(PyMessageFieldProperty* self, void* closure) {
  64. return PyFieldDescriptor_FromDescriptor(self->field_descriptor);
  65. }
  66. static PyObject* GetDoc(PyMessageFieldProperty* self, void* closure) {
  67. return PyString_FromFormat("Field %s",
  68. self->field_descriptor->full_name().c_str());
  69. }
  70. static PyGetSetDef Getters[] = {
  71. {"DESCRIPTOR", (getter)GetDescriptor, NULL, "Field descriptor"},
  72. {"__doc__", (getter)GetDoc, NULL, NULL},
  73. {NULL}};
  74. } // namespace field
  75. static PyTypeObject _CFieldProperty_Type = {
  76. PyVarObject_HEAD_INIT(&PyType_Type, 0) // head
  77. FULL_MODULE_NAME ".FieldProperty", // tp_name
  78. sizeof(PyMessageFieldProperty), // tp_basicsize
  79. 0, // tp_itemsize
  80. 0, // tp_dealloc
  81. 0, // tp_print
  82. 0, // tp_getattr
  83. 0, // tp_setattr
  84. 0, // tp_compare
  85. (reprfunc)field::Repr, // tp_repr
  86. 0, // tp_as_number
  87. 0, // tp_as_sequence
  88. 0, // tp_as_mapping
  89. 0, // tp_hash
  90. 0, // tp_call
  91. 0, // tp_str
  92. 0, // tp_getattro
  93. 0, // tp_setattro
  94. 0, // tp_as_buffer
  95. Py_TPFLAGS_DEFAULT, // tp_flags
  96. "Field property of a Message", // tp_doc
  97. 0, // tp_traverse
  98. 0, // tp_clear
  99. 0, // tp_richcompare
  100. 0, // tp_weaklistoffset
  101. 0, // tp_iter
  102. 0, // tp_iternext
  103. 0, // tp_methods
  104. 0, // tp_members
  105. field::Getters, // tp_getset
  106. 0, // tp_base
  107. 0, // tp_dict
  108. (descrgetfunc)field::DescrGet, // tp_descr_get
  109. (descrsetfunc)field::DescrSet, // tp_descr_set
  110. 0, // tp_dictoffset
  111. 0, // tp_init
  112. 0, // tp_alloc
  113. 0, // tp_new
  114. };
  115. PyTypeObject* CFieldProperty_Type = &_CFieldProperty_Type;
  116. PyObject* NewFieldProperty(const FieldDescriptor* field_descriptor) {
  117. // Create a new descriptor object
  118. PyMessageFieldProperty* property =
  119. PyObject_New(PyMessageFieldProperty, CFieldProperty_Type);
  120. if (property == NULL) {
  121. return NULL;
  122. }
  123. property->field_descriptor = field_descriptor;
  124. return reinterpret_cast<PyObject*>(property);
  125. }
  126. } // namespace python
  127. } // namespace protobuf
  128. } // namespace google