1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2023 Google LLC. All rights reserved. 3 // 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file or at 6 // https://developers.google.com/open-source/licenses/bsd 7 8 #ifndef PYPB_MESSAGE_H__ 9 #define PYPB_MESSAGE_H__ 10 11 #include <stdbool.h> 12 13 #include "python/protobuf.h" 14 #include "upb/reflection/message.h" 15 16 // Removes the wrapper object for this field from the unset subobject cache. 17 void PyUpb_Message_CacheDelete(PyObject* _self, const upb_FieldDef* f); 18 19 // Sets the field value for `f` to `subobj`, evicting the wrapper object from 20 // the "unset subobject" cache now that real data exists for it. The caller 21 // must also update the wrapper associated with `f` to point to `subobj` also. 22 void PyUpb_Message_SetConcreteSubobj(PyObject* _self, const upb_FieldDef* f, 23 upb_MessageValue subobj); 24 25 // Gets a Python wrapper object for message `u_msg` of type `m`, returning a 26 // cached wrapper if one was previously created. If a new object is created, 27 // it will reference `arena`, which must own `u_msg`. 28 PyObject* PyUpb_Message_Get(upb_Message* u_msg, const upb_MessageDef* m, 29 PyObject* arena); 30 31 // Verifies that a Python object is a message. Sets a TypeError exception and 32 // returns false on failure. 33 bool PyUpb_Message_Verify(PyObject* self); 34 35 // Gets the upb_Message* for this message object if the message is reified. 36 // Otherwise returns NULL. 37 upb_Message* PyUpb_Message_GetIfReified(PyObject* _self); 38 39 // Returns the `upb_MessageDef` for a given Message. 40 const upb_MessageDef* PyUpb_Message_GetMsgdef(PyObject* self); 41 42 // Functions that match the corresponding methods on the message object. 43 PyObject* PyUpb_Message_MergeFrom(PyObject* self, PyObject* arg); 44 PyObject* PyUpb_Message_MergeFromString(PyObject* self, PyObject* arg); 45 PyObject* PyUpb_Message_SerializeToString(PyObject* self, PyObject* args, 46 PyObject* kwargs); 47 PyObject* PyUpb_Message_SerializePartialToString(PyObject* self, PyObject* args, 48 PyObject* kwargs); 49 50 // Sets fields of the message according to the attribuges in `kwargs`. 51 int PyUpb_Message_InitAttributes(PyObject* _self, PyObject* args, 52 PyObject* kwargs); 53 54 // Checks that `key` is a field descriptor for an extension type, and that the 55 // extendee is this message. Otherwise returns NULL and sets a KeyError. 56 const upb_FieldDef* PyUpb_Message_GetExtensionDef(PyObject* _self, 57 PyObject* key); 58 59 // Clears the given field in this message. 60 void PyUpb_Message_DoClearField(PyObject* _self, const upb_FieldDef* f); 61 62 // Clears the ExtensionDict from the message. The message must have an 63 // ExtensionDict set. 64 void PyUpb_Message_ClearExtensionDict(PyObject* _self); 65 66 // Implements the equivalent of getattr(msg, field), once `field` has 67 // already been resolved to a `upb_FieldDef*`. 68 PyObject* PyUpb_Message_GetFieldValue(PyObject* _self, 69 const upb_FieldDef* field); 70 71 // Implements the equivalent of setattr(msg, field, value), once `field` has 72 // already been resolved to a `upb_FieldDef*`. 73 int PyUpb_Message_SetFieldValue(PyObject* _self, const upb_FieldDef* field, 74 PyObject* value, PyObject* exc); 75 76 // Creates message meta class. 77 PyObject* PyUpb_MessageMeta_DoCreateClass(PyObject* py_descriptor, 78 const char* name, PyObject* dict); 79 80 // Returns the version associated with this message. The version will be 81 // incremented when the message changes. 82 int PyUpb_Message_GetVersion(PyObject* _self); 83 84 // Module-level init. 85 bool PyUpb_InitMessage(PyObject* m); 86 87 #endif // PYPB_MESSAGE_H__ 88