1 /* 2 * Copyright (c) 2009-2021, Google LLC 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * * Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * * Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * * Neither the name of Google LLC nor the 13 * names of its contributors may be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT, 20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #ifndef PYPB_MESSAGE_H__ 29 #define PYPB_MESSAGE_H__ 30 31 #include <stdbool.h> 32 33 #include "python/protobuf.h" 34 #include "upb/reflection/message.h" 35 36 // Removes the wrapper object for this field from the unset subobject cache. 37 void PyUpb_Message_CacheDelete(PyObject* _self, const upb_FieldDef* f); 38 39 // Sets the field value for `f` to `subobj`, evicting the wrapper object from 40 // the "unset subobject" cache now that real data exists for it. The caller 41 // must also update the wrapper associated with `f` to point to `subobj` also. 42 void PyUpb_Message_SetConcreteSubobj(PyObject* _self, const upb_FieldDef* f, 43 upb_MessageValue subobj); 44 45 // Gets a Python wrapper object for message `u_msg` of type `m`, returning a 46 // cached wrapper if one was previously created. If a new object is created, 47 // it will reference `arena`, which must own `u_msg`. 48 PyObject* PyUpb_Message_Get(upb_Message* u_msg, const upb_MessageDef* m, 49 PyObject* arena); 50 51 // Verifies that a Python object is a message. Sets a TypeError exception and 52 // returns false on failure. 53 bool PyUpb_Message_Verify(PyObject* self); 54 55 // Gets the upb_Message* for this message object if the message is reified. 56 // Otherwise returns NULL. 57 upb_Message* PyUpb_Message_GetIfReified(PyObject* _self); 58 59 // Returns the `upb_MessageDef` for a given Message. 60 const upb_MessageDef* PyUpb_Message_GetMsgdef(PyObject* self); 61 62 // Functions that match the corresponding methods on the message object. 63 PyObject* PyUpb_Message_MergeFrom(PyObject* self, PyObject* arg); 64 PyObject* PyUpb_Message_MergeFromString(PyObject* self, PyObject* arg); 65 PyObject* PyUpb_Message_SerializeToString(PyObject* self, PyObject* args, 66 PyObject* kwargs); 67 PyObject* PyUpb_Message_SerializePartialToString(PyObject* self, PyObject* args, 68 PyObject* kwargs); 69 70 // Sets fields of the message according to the attribuges in `kwargs`. 71 int PyUpb_Message_InitAttributes(PyObject* _self, PyObject* args, 72 PyObject* kwargs); 73 74 // Checks that `key` is a field descriptor for an extension type, and that the 75 // extendee is this message. Otherwise returns NULL and sets a KeyError. 76 const upb_FieldDef* PyUpb_Message_GetExtensionDef(PyObject* _self, 77 PyObject* key); 78 79 // Clears the given field in this message. 80 void PyUpb_Message_DoClearField(PyObject* _self, const upb_FieldDef* f); 81 82 // Clears the ExtensionDict from the message. The message must have an 83 // ExtensionDict set. 84 void PyUpb_Message_ClearExtensionDict(PyObject* _self); 85 86 // Implements the equivalent of getattr(msg, field), once `field` has 87 // already been resolved to a `upb_FieldDef*`. 88 PyObject* PyUpb_Message_GetFieldValue(PyObject* _self, 89 const upb_FieldDef* field); 90 91 // Implements the equivalent of setattr(msg, field, value), once `field` has 92 // already been resolved to a `upb_FieldDef*`. 93 int PyUpb_Message_SetFieldValue(PyObject* _self, const upb_FieldDef* field, 94 PyObject* value, PyObject* exc); 95 96 // Returns the version associated with this message. The version will be 97 // incremented when the message changes. 98 int PyUpb_Message_GetVersion(PyObject* _self); 99 100 // Module-level init. 101 bool PyUpb_InitMessage(PyObject* m); 102 103 #endif // PYPB_MESSAGE_H__ 104