店播爬取Python脚本

message.h 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. // Author: anuraag@google.com (Anuraag Agrawal)
  31. // Author: tibell@google.com (Johan Tibell)
  32. #ifndef GOOGLE_PROTOBUF_PYTHON_CPP_MESSAGE_H__
  33. #define GOOGLE_PROTOBUF_PYTHON_CPP_MESSAGE_H__
  34. #include <Python.h>
  35. #include <cstdint>
  36. #include <memory>
  37. #include <string>
  38. #include <unordered_map>
  39. #include <google/protobuf/stubs/common.h>
  40. namespace google {
  41. namespace protobuf {
  42. class Message;
  43. class Reflection;
  44. class FieldDescriptor;
  45. class Descriptor;
  46. class DescriptorPool;
  47. class MessageFactory;
  48. namespace python {
  49. struct ExtensionDict;
  50. struct PyMessageFactory;
  51. struct CMessageClass;
  52. // Most of the complexity of the Message class comes from the "Release"
  53. // behavior:
  54. //
  55. // When a field is cleared, it is only detached from its message. Existing
  56. // references to submessages, to repeated container etc. won't see any change,
  57. // as if the data was effectively managed by these containers.
  58. //
  59. // ExtensionDicts and UnknownFields containers do NOT follow this rule. They
  60. // don't store any data, and always refer to their parent message.
  61. struct ContainerBase {
  62. PyObject_HEAD;
  63. // Strong reference to a parent message object. For a CMessage there are three
  64. // cases:
  65. // - For a top-level message, this pointer is NULL.
  66. // - For a sub-message, this points to the parent message.
  67. // - For a message managed externally, this is a owned reference to Py_None.
  68. //
  69. // For all other types: repeated containers, maps, it always point to a
  70. // valid parent CMessage.
  71. struct CMessage* parent;
  72. // If this object belongs to a parent message, describes which field it comes
  73. // from.
  74. // The pointer is owned by the DescriptorPool (which is kept alive
  75. // through the message's Python class)
  76. const FieldDescriptor* parent_field_descriptor;
  77. PyObject* AsPyObject() { return reinterpret_cast<PyObject*>(this); }
  78. // The Three methods below are only used by Repeated containers, and Maps.
  79. // This implementation works for all containers which have a parent.
  80. PyObject* DeepCopy();
  81. // Delete this container object from its parent. Does not work for messages.
  82. void RemoveFromParentCache();
  83. };
  84. typedef struct CMessage : public ContainerBase {
  85. // Pointer to the C++ Message object for this CMessage.
  86. // - If this object has no parent, we own this pointer.
  87. // - If this object has a parent message, the parent owns this pointer.
  88. Message* message;
  89. // Indicates this submessage is pointing to a default instance of a message.
  90. // Submessages are always first created as read only messages and are then
  91. // made writable, at which point this field is set to false.
  92. bool read_only;
  93. // A mapping indexed by field, containing weak references to contained objects
  94. // which need to implement the "Release" mechanism:
  95. // direct submessages, RepeatedCompositeContainer, RepeatedScalarContainer
  96. // and MapContainer.
  97. typedef std::unordered_map<const FieldDescriptor*, ContainerBase*>
  98. CompositeFieldsMap;
  99. CompositeFieldsMap* composite_fields;
  100. // A mapping containing weak references to indirect child messages, accessed
  101. // through containers: repeated messages, and values of message maps.
  102. // This avoid the creation of similar maps in each of those containers.
  103. typedef std::unordered_map<const Message*, CMessage*> SubMessagesMap;
  104. SubMessagesMap* child_submessages;
  105. // A reference to PyUnknownFields.
  106. PyObject* unknown_field_set;
  107. // Implements the "weakref" protocol for this object.
  108. PyObject* weakreflist;
  109. // Return a *borrowed* reference to the message class.
  110. CMessageClass* GetMessageClass() {
  111. return reinterpret_cast<CMessageClass*>(Py_TYPE(this));
  112. }
  113. // For container containing messages, return a Python object for the given
  114. // pointer to a message.
  115. CMessage* BuildSubMessageFromPointer(const FieldDescriptor* field_descriptor,
  116. Message* sub_message,
  117. CMessageClass* message_class);
  118. CMessage* MaybeReleaseSubMessage(Message* sub_message);
  119. } CMessage;
  120. // The (meta) type of all Messages classes.
  121. // It allows us to cache some C++ pointers in the class object itself, they are
  122. // faster to extract than from the type's dictionary.
  123. struct CMessageClass {
  124. // This is how CPython subclasses C structures: the base structure must be
  125. // the first member of the object.
  126. PyHeapTypeObject super;
  127. // C++ descriptor of this message.
  128. const Descriptor* message_descriptor;
  129. // Owned reference, used to keep the pointer above alive.
  130. // This reference must stay alive until all message pointers are destructed.
  131. PyObject* py_message_descriptor;
  132. // The Python MessageFactory used to create the class. It is needed to resolve
  133. // fields descriptors, including extensions fields; its C++ MessageFactory is
  134. // used to instantiate submessages.
  135. // This reference must stay alive until all message pointers are destructed.
  136. PyMessageFactory* py_message_factory;
  137. PyObject* AsPyObject() {
  138. return reinterpret_cast<PyObject*>(this);
  139. }
  140. };
  141. extern PyTypeObject* CMessageClass_Type;
  142. extern PyTypeObject* CMessage_Type;
  143. namespace cmessage {
  144. // Internal function to create a new empty Message Python object, but with empty
  145. // pointers to the C++ objects.
  146. // The caller must fill self->message, self->owner and eventually self->parent.
  147. CMessage* NewEmptyMessage(CMessageClass* type);
  148. // Retrieves the C++ descriptor of a Python Extension descriptor.
  149. // On error, return NULL with an exception set.
  150. const FieldDescriptor* GetExtensionDescriptor(PyObject* extension);
  151. // Initializes a new CMessage instance for a submessage. Only called once per
  152. // submessage as the result is cached in composite_fields.
  153. //
  154. // Corresponds to reflection api method GetMessage.
  155. CMessage* InternalGetSubMessage(
  156. CMessage* self, const FieldDescriptor* field_descriptor);
  157. // Deletes a range of items in a repeated field (following a
  158. // removal in a RepeatedCompositeContainer).
  159. //
  160. // Corresponds to reflection api method RemoveLast.
  161. int DeleteRepeatedField(CMessage* self,
  162. const FieldDescriptor* field_descriptor,
  163. PyObject* slice);
  164. // Sets the specified scalar value to the message.
  165. int InternalSetScalar(CMessage* self,
  166. const FieldDescriptor* field_descriptor,
  167. PyObject* value);
  168. // Sets the specified scalar value to the message. Requires it is not a Oneof.
  169. int InternalSetNonOneofScalar(Message* message,
  170. const FieldDescriptor* field_descriptor,
  171. PyObject* arg);
  172. // Retrieves the specified scalar value from the message.
  173. //
  174. // Returns a new python reference.
  175. PyObject* InternalGetScalar(const Message* message,
  176. const FieldDescriptor* field_descriptor);
  177. bool SetCompositeField(CMessage* self, const FieldDescriptor* field,
  178. ContainerBase* value);
  179. bool SetSubmessage(CMessage* self, CMessage* submessage);
  180. // Clears the message, removing all contained data. Extension dictionary and
  181. // submessages are released first if there are remaining external references.
  182. //
  183. // Corresponds to message api method Clear.
  184. PyObject* Clear(CMessage* self);
  185. // Clears the data described by the given descriptor.
  186. // Returns -1 on error.
  187. //
  188. // Corresponds to reflection api method ClearField.
  189. int ClearFieldByDescriptor(CMessage* self, const FieldDescriptor* descriptor);
  190. // Checks if the message has the field described by the descriptor. Used for
  191. // extensions (which have no name).
  192. // Returns 1 if true, 0 if false, and -1 on error.
  193. //
  194. // Corresponds to reflection api method HasField
  195. int HasFieldByDescriptor(CMessage* self,
  196. const FieldDescriptor* field_descriptor);
  197. // Checks if the message has the named field.
  198. //
  199. // Corresponds to reflection api method HasField.
  200. PyObject* HasField(CMessage* self, PyObject* arg);
  201. // Initializes values of fields on a newly constructed message.
  202. // Note that positional arguments are disallowed: 'args' must be NULL or the
  203. // empty tuple.
  204. int InitAttributes(CMessage* self, PyObject* args, PyObject* kwargs);
  205. PyObject* MergeFrom(CMessage* self, PyObject* arg);
  206. // This method does not do anything beyond checking that no other extension
  207. // has been registered with the same field number on this class.
  208. PyObject* RegisterExtension(PyObject* cls, PyObject* extension_handle);
  209. // Get a field from a message.
  210. PyObject* GetFieldValue(CMessage* self,
  211. const FieldDescriptor* field_descriptor);
  212. // Sets the value of a scalar field in a message.
  213. // On error, return -1 with an extension set.
  214. int SetFieldValue(CMessage* self, const FieldDescriptor* field_descriptor,
  215. PyObject* value);
  216. PyObject* FindInitializationErrors(CMessage* self);
  217. int AssureWritable(CMessage* self);
  218. // Returns the message factory for the given message.
  219. // This is equivalent to message.MESSAGE_FACTORY
  220. //
  221. // The returned factory is suitable for finding fields and building submessages,
  222. // even in the case of extensions.
  223. // Returns a *borrowed* reference, and never fails because we pass a CMessage.
  224. PyMessageFactory* GetFactoryForMessage(CMessage* message);
  225. PyObject* SetAllowOversizeProtos(PyObject* m, PyObject* arg);
  226. } // namespace cmessage
  227. /* Is 64bit */
  228. #define IS_64BIT (SIZEOF_LONG == 8)
  229. #define FIELD_IS_REPEATED(field_descriptor) \
  230. ((field_descriptor)->label() == FieldDescriptor::LABEL_REPEATED)
  231. #define GOOGLE_CHECK_GET_INT32(arg, value, err) \
  232. int32_t value; \
  233. if (!CheckAndGetInteger(arg, &value)) { \
  234. return err; \
  235. }
  236. #define GOOGLE_CHECK_GET_INT64(arg, value, err) \
  237. int64_t value; \
  238. if (!CheckAndGetInteger(arg, &value)) { \
  239. return err; \
  240. }
  241. #define GOOGLE_CHECK_GET_UINT32(arg, value, err) \
  242. uint32_t value; \
  243. if (!CheckAndGetInteger(arg, &value)) { \
  244. return err; \
  245. }
  246. #define GOOGLE_CHECK_GET_UINT64(arg, value, err) \
  247. uint64_t value; \
  248. if (!CheckAndGetInteger(arg, &value)) { \
  249. return err; \
  250. }
  251. #define GOOGLE_CHECK_GET_FLOAT(arg, value, err) \
  252. float value; \
  253. if (!CheckAndGetFloat(arg, &value)) { \
  254. return err; \
  255. }
  256. #define GOOGLE_CHECK_GET_DOUBLE(arg, value, err) \
  257. double value; \
  258. if (!CheckAndGetDouble(arg, &value)) { \
  259. return err; \
  260. }
  261. #define GOOGLE_CHECK_GET_BOOL(arg, value, err) \
  262. bool value; \
  263. if (!CheckAndGetBool(arg, &value)) { \
  264. return err; \
  265. }
  266. #define FULL_MODULE_NAME "google.protobuf.pyext._message"
  267. void FormatTypeError(PyObject* arg, const char* expected_types);
  268. template<class T>
  269. bool CheckAndGetInteger(PyObject* arg, T* value);
  270. bool CheckAndGetDouble(PyObject* arg, double* value);
  271. bool CheckAndGetFloat(PyObject* arg, float* value);
  272. bool CheckAndGetBool(PyObject* arg, bool* value);
  273. PyObject* CheckString(PyObject* arg, const FieldDescriptor* descriptor);
  274. bool CheckAndSetString(
  275. PyObject* arg, Message* message,
  276. const FieldDescriptor* descriptor,
  277. const Reflection* reflection,
  278. bool append,
  279. int index);
  280. PyObject* ToStringObject(const FieldDescriptor* descriptor,
  281. const std::string& value);
  282. // Check if the passed field descriptor belongs to the given message.
  283. // If not, return false and set a Python exception (a KeyError)
  284. bool CheckFieldBelongsToMessage(const FieldDescriptor* field_descriptor,
  285. const Message* message);
  286. extern PyObject* PickleError_class;
  287. PyObject* PyMessage_New(const Descriptor* descriptor,
  288. PyObject* py_message_factory);
  289. const Message* PyMessage_GetMessagePointer(PyObject* msg);
  290. Message* PyMessage_GetMutableMessagePointer(PyObject* msg);
  291. PyObject* PyMessage_NewMessageOwnedExternally(Message* message,
  292. PyObject* py_message_factory);
  293. bool InitProto2MessageModule(PyObject *m);
  294. // These are referenced by repeated_scalar_container, and must
  295. // be explicitly instantiated.
  296. extern template bool CheckAndGetInteger<int32>(PyObject*, int32*);
  297. extern template bool CheckAndGetInteger<int64>(PyObject*, int64*);
  298. extern template bool CheckAndGetInteger<uint32>(PyObject*, uint32*);
  299. extern template bool CheckAndGetInteger<uint64>(PyObject*, uint64*);
  300. } // namespace python
  301. } // namespace protobuf
  302. } // namespace google
  303. #endif // GOOGLE_PROTOBUF_PYTHON_CPP_MESSAGE_H__