店播爬取Python脚本

proto_api.h 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 can be included by other C++ libraries, typically extension modules
  31. // which want to interact with the Python Messages coming from the "cpp"
  32. // implementation of protocol buffers.
  33. //
  34. // Usage:
  35. // Declare a (probably static) variable to hold the API:
  36. // const PyProto_API* py_proto_api;
  37. // In some initialization function, write:
  38. // py_proto_api = static_cast<const PyProto_API*>(PyCapsule_Import(
  39. // PyProtoAPICapsuleName(), 0));
  40. // if (!py_proto_api) { ...handle ImportError... }
  41. // Then use the methods of the returned class:
  42. // py_proto_api->GetMessagePointer(...);
  43. #ifndef GOOGLE_PROTOBUF_PYTHON_PROTO_API_H__
  44. #define GOOGLE_PROTOBUF_PYTHON_PROTO_API_H__
  45. #include <Python.h>
  46. #include <google/protobuf/descriptor_database.h>
  47. #include <google/protobuf/message.h>
  48. namespace google {
  49. namespace protobuf {
  50. namespace python {
  51. // Note on the implementation:
  52. // This API is designed after
  53. // https://docs.python.org/3/extending/extending.html#providing-a-c-api-for-an-extension-module
  54. // The class below contains no mutable state, and all methods are "const";
  55. // we use a C++ class instead of a C struct with functions pointers just because
  56. // the code looks more readable.
  57. struct PyProto_API {
  58. // The API object is created at initialization time and never freed.
  59. // This destructor is never called.
  60. virtual ~PyProto_API() {}
  61. // Operations on Messages.
  62. // If the passed object is a Python Message, returns its internal pointer.
  63. // Otherwise, returns NULL with an exception set.
  64. virtual const Message* GetMessagePointer(PyObject* msg) const = 0;
  65. // If the passed object is a Python Message, returns a mutable pointer.
  66. // Otherwise, returns NULL with an exception set.
  67. // This function will succeed only if there are no other Python objects
  68. // pointing to the message, like submessages or repeated containers.
  69. // With the current implementation, only empty messages are in this case.
  70. virtual Message* GetMutableMessagePointer(PyObject* msg) const = 0;
  71. // If the passed object is a Python Message Descriptor, returns its internal
  72. // pointer.
  73. // Otherwise, returns NULL with an exception set.
  74. virtual const Descriptor* MessageDescriptor_AsDescriptor(
  75. PyObject* desc) const = 0;
  76. // If the passed object is a Python Enum Descriptor, returns its internal
  77. // pointer.
  78. // Otherwise, returns NULL with an exception set.
  79. virtual const EnumDescriptor* EnumDescriptor_AsDescriptor(
  80. PyObject* enum_desc) const = 0;
  81. // Expose the underlying DescriptorPool and MessageFactory to enable C++ code
  82. // to create Python-compatible message.
  83. virtual const DescriptorPool* GetDefaultDescriptorPool() const = 0;
  84. virtual MessageFactory* GetDefaultMessageFactory() const = 0;
  85. // Allocate a new protocol buffer as a python object for the provided
  86. // descriptor. This function works even if no Python module has been imported
  87. // for the corresponding protocol buffer class.
  88. // The factory is usually null; when provided, it is the MessageFactory which
  89. // owns the Python class, and will be used to find and create Extensions for
  90. // this message.
  91. // When null is returned, a python error has already been set.
  92. virtual PyObject* NewMessage(const Descriptor* descriptor,
  93. PyObject* py_message_factory) const = 0;
  94. // Allocate a new protocol buffer where the underlying object is owned by C++.
  95. // The factory must currently be null. This function works even if no Python
  96. // module has been imported for the corresponding protocol buffer class.
  97. // When null is returned, a python error has already been set.
  98. //
  99. // Since this call returns a python object owned by C++, some operations
  100. // are risky, and it must be used carefully. In particular:
  101. // * Avoid modifying the returned object from the C++ side while there are
  102. // existing python references to it or it's subobjects.
  103. // * Avoid using python references to this object or any subobjects after the
  104. // C++ object has been freed.
  105. // * Calling this with the same C++ pointer will result in multiple distinct
  106. // python objects referencing the same C++ object.
  107. virtual PyObject* NewMessageOwnedExternally(
  108. Message* msg, PyObject* py_message_factory) const = 0;
  109. // Returns a new reference for the given DescriptorPool.
  110. // The returned object does not manage the C++ DescriptorPool: it is the
  111. // responsibility of the caller to keep it alive.
  112. // As long as the returned Python DescriptorPool object is kept alive,
  113. // functions that process C++ descriptors or messages created from this pool
  114. // can work and return their Python counterparts.
  115. virtual PyObject* DescriptorPool_FromPool(
  116. const google::protobuf::DescriptorPool* pool) const = 0;
  117. };
  118. inline const char* PyProtoAPICapsuleName() {
  119. static const char kCapsuleName[] =
  120. "google.protobuf.pyext._message.proto_API";
  121. return kCapsuleName;
  122. }
  123. } // namespace python
  124. } // namespace protobuf
  125. } // namespace google
  126. #endif // GOOGLE_PROTOBUF_PYTHON_PROTO_API_H__