店播爬取Python脚本

message_module.cc 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 <Python.h>
  31. #include <google/protobuf/message_lite.h>
  32. #include <google/protobuf/pyext/descriptor.h>
  33. #include <google/protobuf/pyext/descriptor_pool.h>
  34. #include <google/protobuf/pyext/message.h>
  35. #include <google/protobuf/pyext/message_factory.h>
  36. #include <google/protobuf/proto_api.h>
  37. namespace {
  38. // C++ API. Clients get at this via proto_api.h
  39. struct ApiImplementation : google::protobuf::python::PyProto_API {
  40. const google::protobuf::Message* GetMessagePointer(PyObject* msg) const override {
  41. return google::protobuf::python::PyMessage_GetMessagePointer(msg);
  42. }
  43. google::protobuf::Message* GetMutableMessagePointer(PyObject* msg) const override {
  44. return google::protobuf::python::PyMessage_GetMutableMessagePointer(msg);
  45. }
  46. const google::protobuf::Descriptor* MessageDescriptor_AsDescriptor(
  47. PyObject* desc) const override {
  48. return google::protobuf::python::PyMessageDescriptor_AsDescriptor(desc);
  49. }
  50. const google::protobuf::EnumDescriptor* EnumDescriptor_AsDescriptor(
  51. PyObject* enum_desc) const override {
  52. return google::protobuf::python::PyEnumDescriptor_AsDescriptor(enum_desc);
  53. }
  54. const google::protobuf::DescriptorPool* GetDefaultDescriptorPool() const override {
  55. return google::protobuf::python::GetDefaultDescriptorPool()->pool;
  56. }
  57. google::protobuf::MessageFactory* GetDefaultMessageFactory() const override {
  58. return google::protobuf::python::GetDefaultDescriptorPool()
  59. ->py_message_factory->message_factory;
  60. }
  61. PyObject* NewMessage(const google::protobuf::Descriptor* descriptor,
  62. PyObject* py_message_factory) const override {
  63. return google::protobuf::python::PyMessage_New(descriptor, py_message_factory);
  64. }
  65. PyObject* NewMessageOwnedExternally(
  66. google::protobuf::Message* msg, PyObject* py_message_factory) const override {
  67. return google::protobuf::python::PyMessage_NewMessageOwnedExternally(
  68. msg, py_message_factory);
  69. }
  70. PyObject* DescriptorPool_FromPool(
  71. const google::protobuf::DescriptorPool* pool) const override {
  72. return google::protobuf::python::PyDescriptorPool_FromPool(pool);
  73. }
  74. };
  75. } // namespace
  76. static const char module_docstring[] =
  77. "python-proto2 is a module that can be used to enhance proto2 Python API\n"
  78. "performance.\n"
  79. "\n"
  80. "It provides access to the protocol buffers C++ reflection API that\n"
  81. "implements the basic protocol buffer functions.";
  82. static PyMethodDef ModuleMethods[] = {
  83. {"SetAllowOversizeProtos",
  84. (PyCFunction)google::protobuf::python::cmessage::SetAllowOversizeProtos, METH_O,
  85. "Enable/disable oversize proto parsing."},
  86. // DO NOT USE: For migration and testing only.
  87. {NULL, NULL}};
  88. #if PY_MAJOR_VERSION >= 3
  89. static struct PyModuleDef _module = {PyModuleDef_HEAD_INIT,
  90. "_message",
  91. module_docstring,
  92. -1,
  93. ModuleMethods, /* m_methods */
  94. NULL,
  95. NULL,
  96. NULL,
  97. NULL};
  98. #define INITFUNC PyInit__message
  99. #define INITFUNC_ERRORVAL NULL
  100. #else // Python 2
  101. #define INITFUNC init_message
  102. #define INITFUNC_ERRORVAL
  103. #endif
  104. PyMODINIT_FUNC INITFUNC() {
  105. PyObject* m;
  106. #if PY_MAJOR_VERSION >= 3
  107. m = PyModule_Create(&_module);
  108. #else
  109. m = Py_InitModule3("_message", ModuleMethods, module_docstring);
  110. #endif
  111. if (m == NULL) {
  112. return INITFUNC_ERRORVAL;
  113. }
  114. if (!google::protobuf::python::InitProto2MessageModule(m)) {
  115. Py_DECREF(m);
  116. return INITFUNC_ERRORVAL;
  117. }
  118. // Adds the C++ API
  119. if (PyObject* api = PyCapsule_New(
  120. new ApiImplementation(), google::protobuf::python::PyProtoAPICapsuleName(),
  121. [](PyObject* o) {
  122. delete (ApiImplementation*)PyCapsule_GetPointer(
  123. o, google::protobuf::python::PyProtoAPICapsuleName());
  124. })) {
  125. PyModule_AddObject(m, "proto_API", api);
  126. } else {
  127. return INITFUNC_ERRORVAL;
  128. }
  129. #if PY_MAJOR_VERSION >= 3
  130. return m;
  131. #endif
  132. }