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


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:57