| 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 PYUPB_PROTOBUF_H__ |
| 29 |
#define PYUPB_PROTOBUF_H__ |
| 30 |
|
| 31 |
#include <stdbool.h> |
| 32 |
|
| 33 |
#include "python/descriptor.h" |
| 34 |
#include "python/python_api.h" |
| 35 |
|
| 36 |
// begin:github_only |
| 37 |
#define PYUPB_PROTOBUF_PUBLIC_PACKAGE "google.protobuf" |
| 38 |
#define PYUPB_PROTOBUF_INTERNAL_PACKAGE "google.protobuf.internal" |
| 39 |
#define PYUPB_DESCRIPTOR_PROTO_PACKAGE "google.protobuf" |
| 40 |
#define PYUPB_DESCRIPTOR_MODULE "google.protobuf.descriptor_pb2" |
| 41 |
#define PYUPB_MODULE_NAME "google._upb._message" |
| 42 |
// end:github_only |
| 43 |
|
| 44 |
// begin:google_only |
| 45 |
// #define PYUPB_PROTOBUF_PUBLIC_PACKAGE "google3.net.proto2.python.public" |
| 46 |
// #define PYUPB_PROTOBUF_INTERNAL_PACKAGE "google3.net.proto2.python.internal" |
| 47 |
// #define PYUPB_DESCRIPTOR_PROTO_PACKAGE "proto2" |
| 48 |
// #define PYUPB_DESCRIPTOR_MODULE "google3.net.proto2.proto.descriptor_pb2" |
| 49 |
// #define PYUPB_MODULE_NAME "google3.third_party.upb.python._message" |
| 50 |
// end:google_only |
| 51 |
|
| 52 |
#define PYUPB_RETURN_OOM return PyErr_SetNone(PyExc_MemoryError), NULL |
| 53 |
|
| 54 |
struct PyUpb_WeakMap; |
| 55 |
typedef struct PyUpb_WeakMap PyUpb_WeakMap; |
| 56 |
|
| 57 |
// ----------------------------------------------------------------------------- |
| 58 |
// ModuleState |
| 59 |
// ----------------------------------------------------------------------------- |
| 60 |
|
| 61 |
// We store all "global" state in this struct instead of using (C) global |
| 62 |
// variables. This makes this extension compatible with sub-interpreters. |
| 63 |
|
| 64 |
typedef struct { |
| 65 |
// From descriptor.c |
| 66 |
PyTypeObject* descriptor_types[kPyUpb_Descriptor_Count]; |
| 67 |
|
| 68 |
// From descriptor_containers.c |
| 69 |
PyTypeObject* by_name_map_type; |
| 70 |
PyTypeObject* by_number_map_type; |
| 71 |
PyTypeObject* descriptor_iterator_type; |
| 72 |
PyTypeObject* generic_sequence_type; |
| 73 |
|
| 74 |
// From descriptor_pool.c |
| 75 |
PyObject* default_pool; |
| 76 |
|
| 77 |
// From descriptor_pool.c |
| 78 |
PyTypeObject* descriptor_pool_type; |
| 79 |
upb_DefPool* c_descriptor_symtab; |
| 80 |
|
| 81 |
// From extension_dict.c |
| 82 |
PyTypeObject* extension_dict_type; |
| 83 |
PyTypeObject* extension_iterator_type; |
| 84 |
|
| 85 |
// From map.c |
| 86 |
PyTypeObject* map_iterator_type; |
| 87 |
PyTypeObject* message_map_container_type; |
| 88 |
PyTypeObject* scalar_map_container_type; |
| 89 |
|
| 90 |
// From message.c |
| 91 |
PyObject* decode_error_class; |
| 92 |
PyObject* descriptor_string; |
| 93 |
PyObject* encode_error_class; |
| 94 |
PyObject* enum_type_wrapper_class; |
| 95 |
PyObject* message_class; |
| 96 |
PyTypeObject* cmessage_type; |
| 97 |
PyTypeObject* message_meta_type; |
| 98 |
PyObject* listfields_item_key; |
| 99 |
|
| 100 |
// From protobuf.c |
| 101 |
bool allow_oversize_protos; |
| 102 |
PyObject* wkt_bases; |
| 103 |
PyTypeObject* arena_type; |
| 104 |
PyUpb_WeakMap* obj_cache; |
| 105 |
|
| 106 |
// From repeated.c |
| 107 |
PyTypeObject* repeated_composite_container_type; |
| 108 |
PyTypeObject* repeated_scalar_container_type; |
| 109 |
|
| 110 |
// From unknown_fields.c |
| 111 |
PyTypeObject* unknown_fields_type; |
| 112 |
PyObject* unknown_field_type; |
| 113 |
} PyUpb_ModuleState; |
| 114 |
|
| 115 |
// Returns the global state object from the current interpreter. The current |
| 116 |
// interpreter is looked up from thread-local state. |
| 117 |
PyUpb_ModuleState* PyUpb_ModuleState_Get(void); |
| 118 |
PyUpb_ModuleState* PyUpb_ModuleState_GetFromModule(PyObject* module); |
| 119 |
|
| 120 |
// Returns NULL if module state is not yet available (during startup). |
| 121 |
// Any use of the module state during startup needs to be passed explicitly. |
| 122 |
PyUpb_ModuleState* PyUpb_ModuleState_MaybeGet(void); |
| 123 |
|
| 124 |
// Returns: |
| 125 |
// from google.protobuf.internal.well_known_types import WKTBASES |
| 126 |
// |
| 127 |
// This has to be imported lazily rather than at module load time, because |
| 128 |
// otherwise it would cause a circular import. |
| 129 |
PyObject* PyUpb_GetWktBases(PyUpb_ModuleState* state); |
| 130 |
|
| 131 |
// ----------------------------------------------------------------------------- |
| 132 |
// WeakMap |
| 133 |
// ----------------------------------------------------------------------------- |
| 134 |
|
| 135 |
// A WeakMap maps C pointers to the corresponding Python wrapper object. We |
| 136 |
// want a consistent Python wrapper object for each C object, both to save |
| 137 |
// memory and to provide object stability (ie. x is x). |
| 138 |
// |
| 139 |
// Each wrapped object should add itself to the map when it is constructed and |
| 140 |
// remove itself from the map when it is destroyed. The map is weak so it does |
| 141 |
// not take references to the cached objects. |
| 142 |
|
| 143 |
PyUpb_WeakMap* PyUpb_WeakMap_New(void); |
| 144 |
void PyUpb_WeakMap_Free(PyUpb_WeakMap* map); |
| 145 |
|
| 146 |
// Adds the given object to the map, indexed by the given key. |
| 147 |
void PyUpb_WeakMap_Add(PyUpb_WeakMap* map, const void* key, PyObject* py_obj); |
| 148 |
|
| 149 |
// Removes the given key from the cache. It must exist in the cache currently. |
| 150 |
void PyUpb_WeakMap_Delete(PyUpb_WeakMap* map, const void* key); |
| 151 |
void PyUpb_WeakMap_TryDelete(PyUpb_WeakMap* map, const void* key); |
| 152 |
|
| 153 |
// Returns a new reference to an object if it exists, otherwise returns NULL. |
| 154 |
PyObject* PyUpb_WeakMap_Get(PyUpb_WeakMap* map, const void* key); |
| 155 |
|
| 156 |
#define PYUPB_WEAKMAP_BEGIN UPB_INTTABLE_BEGIN |
| 157 |
|
| 158 |
// Iteration over the weak map, eg. |
| 159 |
// |
| 160 |
// intptr_t it = PYUPB_WEAKMAP_BEGIN; |
| 161 |
// while (PyUpb_WeakMap_Next(map, &key, &obj, &it)) { |
| 162 |
// // ... |
| 163 |
// } |
| 164 |
// |
| 165 |
// Note that the callee does not own a ref on the returned `obj`. |
| 166 |
bool PyUpb_WeakMap_Next(PyUpb_WeakMap* map, const void** key, PyObject** obj, |
| 167 |
intptr_t* iter); |
| 168 |
void PyUpb_WeakMap_DeleteIter(PyUpb_WeakMap* map, intptr_t* iter); |
| 169 |
|
| 170 |
// ----------------------------------------------------------------------------- |
| 171 |
// ObjCache |
| 172 |
// ----------------------------------------------------------------------------- |
| 173 |
|
| 174 |
// The object cache is a global WeakMap for mapping upb objects to the |
| 175 |
// corresponding wrapper. |
| 176 |
void PyUpb_ObjCache_Add(const void* key, PyObject* py_obj); |
| 177 |
void PyUpb_ObjCache_Delete(const void* key); |
| 178 |
PyObject* PyUpb_ObjCache_Get(const void* key); // returns NULL if not present. |
| 179 |
PyUpb_WeakMap* PyUpb_ObjCache_Instance(void); |
| 180 |
|
| 181 |
// ----------------------------------------------------------------------------- |
| 182 |
// Arena |
| 183 |
// ----------------------------------------------------------------------------- |
| 184 |
|
| 185 |
PyObject* PyUpb_Arena_New(void); |
| 186 |
upb_Arena* PyUpb_Arena_Get(PyObject* arena); |
| 187 |
|
| 188 |
// ----------------------------------------------------------------------------- |
| 189 |
// Utilities |
| 190 |
// ----------------------------------------------------------------------------- |
| 191 |
|
| 192 |
PyTypeObject* AddObject(PyObject* m, const char* name, PyType_Spec* spec); |
| 193 |
|
| 194 |
// Creates a Python type from `spec` and adds it to the given module `m`. |
| 195 |
PyTypeObject* PyUpb_AddClass(PyObject* m, PyType_Spec* spec); |
| 196 |
|
| 197 |
// Like PyUpb_AddClass(), but allows you to specify a tuple of base classes |
| 198 |
// in `bases`. |
| 199 |
PyTypeObject* PyUpb_AddClassWithBases(PyObject* m, PyType_Spec* spec, |
| 200 |
PyObject* bases); |
| 201 |
|
| 202 |
// A function that implements the tp_new slot for types that we do not allow |
| 203 |
// users to create directly. This will immediately fail with an error message. |
| 204 |
PyObject* PyUpb_Forbidden_New(PyObject* cls, PyObject* args, PyObject* kwds); |
| 205 |
|
| 206 |
// Our standard dealloc func. It follows the guidance defined in: |
| 207 |
// https://docs.python.org/3/c-api/typeobj.html#c.PyTypeObject.tp_dealloc |
| 208 |
// However it tests Py_TPFLAGS_HEAPTYPE dynamically so that a single dealloc |
| 209 |
// function can work for any type. |
| 210 |
static inline void PyUpb_Dealloc(void* self) { |
| 211 |
PyTypeObject* tp = Py_TYPE(self); |
| 212 |
assert(PyType_GetFlags(tp) & Py_TPFLAGS_HEAPTYPE); |
| 213 |
freefunc tp_free = (freefunc)PyType_GetSlot(tp, Py_tp_free); |
| 214 |
tp_free(self); |
| 215 |
Py_DECREF(tp); |
| 216 |
} |
| 217 |
|
| 218 |
// Equivalent to the Py_NewRef() function introduced in Python 3.10. If/when we |
| 219 |
// drop support for Python <3.10, we can remove this function and replace all |
| 220 |
// callers with Py_NewRef(). |
| 221 |
static inline PyObject* PyUpb_NewRef(PyObject* obj) { |
| 222 |
Py_INCREF(obj); |
| 223 |
return obj; |
| 224 |
} |
| 225 |
|
| 226 |
const char* PyUpb_GetStrData(PyObject* obj); |
| 227 |
const char* PyUpb_VerifyStrData(PyObject* obj); |
| 228 |
|
| 229 |
#endif // PYUPB_PROTOBUF_H__ |