extension_dict.c
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 #include "python/extension_dict.h"
29 
30 #include "python/message.h"
31 #include "python/protobuf.h"
32 
33 // -----------------------------------------------------------------------------
34 // ExtensionDict
35 // -----------------------------------------------------------------------------
36 
37 typedef struct {
39  PyObject* msg; // Owning ref to our parent pessage.
41 
42 PyObject* PyUpb_ExtensionDict_New(PyObject* msg) {
44  PyUpb_ExtensionDict* ext_dict =
45  (void*)PyType_GenericAlloc(state->extension_dict_type, 0);
46  ext_dict->msg = msg;
47  Py_INCREF(ext_dict->msg);
48  return &ext_dict->ob_base;
49 }
50 
51 static PyObject* PyUpb_ExtensionDict_FindExtensionByName(PyObject* _self,
52  PyObject* key) {
54  const char* name = PyUpb_GetStrData(key);
59  if (ext) {
61  } else {
62  Py_RETURN_NONE;
63  }
64 }
65 
66 static PyObject* PyUpb_ExtensionDict_FindExtensionByNumber(PyObject* _self,
67  PyObject* arg) {
74  int64_t number = PyLong_AsLong(arg);
77  if (ext) {
80  } else {
81  Py_RETURN_NONE;
82  }
83 }
84 
87  Py_DECREF(self->msg);
88  PyUpb_Dealloc(self);
89 }
90 
91 static PyObject* PyUpb_ExtensionDict_RichCompare(PyObject* _self,
92  PyObject* _other, int opid) {
93  // Only equality comparisons are implemented.
94  if (opid != Py_EQ && opid != Py_NE) {
95  Py_INCREF(Py_NotImplemented);
96  return Py_NotImplemented;
97  }
99  bool equals = false;
100  if (PyObject_TypeCheck(_other, Py_TYPE(_self))) {
101  PyUpb_ExtensionDict* other = (PyUpb_ExtensionDict*)_other;
102  equals = self->msg == other->msg;
103  }
104  bool ret = opid == Py_EQ ? equals : !equals;
105  return PyBool_FromLong(ret);
106 }
107 
108 static int PyUpb_ExtensionDict_Contains(PyObject* _self, PyObject* key) {
111  if (!f) return -1;
113  if (!msg) return 0;
114  if (upb_FieldDef_IsRepeated(f)) {
116  return upb_Array_Size(val.array_val) > 0;
117  } else {
118  return upb_Message_Has(msg, f);
119  }
120 }
121 
122 static Py_ssize_t PyUpb_ExtensionDict_Length(PyObject* _self) {
125  return msg ? upb_Message_ExtensionCount(msg) : 0;
126 }
127 
128 static PyObject* PyUpb_ExtensionDict_Subscript(PyObject* _self, PyObject* key) {
131  if (!f) return NULL;
132  return PyUpb_CMessage_GetFieldValue(self->msg, f);
133 }
134 
135 static int PyUpb_ExtensionDict_AssignSubscript(PyObject* _self, PyObject* key,
136  PyObject* val) {
139  if (!f) return -1;
140  if (val) {
141  return PyUpb_CMessage_SetFieldValue(self->msg, f, val, PyExc_TypeError);
142  } else {
144  return 0;
145  }
146 }
147 
148 static PyObject* PyUpb_ExtensionIterator_New(PyObject* _ext_dict);
149 
150 static PyMethodDef PyUpb_ExtensionDict_Methods[] = {
151  {"_FindExtensionByName", PyUpb_ExtensionDict_FindExtensionByName, METH_O,
152  "Finds an extension by name."},
153  {"_FindExtensionByNumber", PyUpb_ExtensionDict_FindExtensionByNumber,
154  METH_O, "Finds an extension by number."},
155  {NULL, NULL},
156 };
157 
158 static PyType_Slot PyUpb_ExtensionDict_Slots[] = {
159  {Py_tp_dealloc, PyUpb_ExtensionDict_Dealloc},
160  {Py_tp_methods, PyUpb_ExtensionDict_Methods},
161  //{Py_tp_getset, PyUpb_ExtensionDict_Getters},
162  //{Py_tp_hash, PyObject_HashNotImplemented},
163  {Py_tp_richcompare, PyUpb_ExtensionDict_RichCompare},
164  {Py_tp_iter, PyUpb_ExtensionIterator_New},
165  {Py_sq_contains, PyUpb_ExtensionDict_Contains},
166  {Py_sq_length, PyUpb_ExtensionDict_Length},
167  {Py_mp_length, PyUpb_ExtensionDict_Length},
168  {Py_mp_subscript, PyUpb_ExtensionDict_Subscript},
169  {Py_mp_ass_subscript, PyUpb_ExtensionDict_AssignSubscript},
170  {0, NULL}};
171 
172 static PyType_Spec PyUpb_ExtensionDict_Spec = {
173  PYUPB_MODULE_NAME ".ExtensionDict", // tp_name
174  sizeof(PyUpb_ExtensionDict), // tp_basicsize
175  0, // tp_itemsize
176  Py_TPFLAGS_DEFAULT, // tp_flags
178 };
179 
180 // -----------------------------------------------------------------------------
181 // ExtensionIterator
182 // -----------------------------------------------------------------------------
183 
184 typedef struct {
186  PyObject* msg;
187  size_t iter;
189 
190 static PyObject* PyUpb_ExtensionIterator_New(PyObject* _ext_dict) {
191  PyUpb_ExtensionDict* ext_dict = (PyUpb_ExtensionDict*)_ext_dict;
194  (void*)PyType_GenericAlloc(state->extension_iterator_type, 0);
195  if (!iter) return NULL;
196  iter->msg = ext_dict->msg;
197  iter->iter = kUpb_Message_Begin;
198  Py_INCREF(iter->msg);
199  return &iter->ob_base;
200 }
201 
202 static void PyUpb_ExtensionIterator_Dealloc(void* _self) {
204  Py_DECREF(self->msg);
205  PyUpb_Dealloc(_self);
206 }
207 
208 PyObject* PyUpb_ExtensionIterator_IterNext(PyObject* _self) {
211  if (!msg) return NULL;
214  while (true) {
215  const upb_FieldDef* f;
216  upb_MessageValue val;
217  if (!upb_Message_Next(msg, m, symtab, &f, &val, &self->iter)) return NULL;
219  }
220 }
221 
222 static PyType_Slot PyUpb_ExtensionIterator_Slots[] = {
223  {Py_tp_dealloc, PyUpb_ExtensionIterator_Dealloc},
224  {Py_tp_iter, PyObject_SelfIter},
225  {Py_tp_iternext, PyUpb_ExtensionIterator_IterNext},
226  {0, NULL}};
227 
228 static PyType_Spec PyUpb_ExtensionIterator_Spec = {
229  PYUPB_MODULE_NAME ".ExtensionIterator", // tp_name
230  sizeof(PyUpb_ExtensionIterator), // tp_basicsize
231  0, // tp_itemsize
232  Py_TPFLAGS_DEFAULT, // tp_flags
234 };
235 
236 // -----------------------------------------------------------------------------
237 // Top Level
238 // -----------------------------------------------------------------------------
239 
240 bool PyUpb_InitExtensionDict(PyObject* m) {
242 
243  s->extension_dict_type = PyUpb_AddClass(m, &PyUpb_ExtensionDict_Spec);
244  s->extension_iterator_type = PyUpb_AddClass(m, &PyUpb_ExtensionIterator_Spec);
245 
246  return s->extension_dict_type && s->extension_iterator_type;
247 }
PyUpb_ExtensionIterator_Spec
static PyType_Spec PyUpb_ExtensionIterator_Spec
Definition: extension_dict.c:228
PyUpb_GetStrData
const char * PyUpb_GetStrData(PyObject *obj)
Definition: upb/python/protobuf.c:295
upb_Message_ExtensionCount
size_t upb_Message_ExtensionCount(const upb_Message *msg)
Definition: msg.c:170
PyUpb_InitExtensionDict
bool PyUpb_InitExtensionDict(PyObject *m)
Definition: extension_dict.c:240
PyUpb_CMessage_DoClearField
void PyUpb_CMessage_DoClearField(PyObject *_self, const upb_FieldDef *f)
Definition: upb/python/message.c:1220
PyUpb_ExtensionDict_Slots
static PyType_Slot PyUpb_ExtensionDict_Slots[]
Definition: extension_dict.c:158
PyUpb_ExtensionIterator::PyObject_HEAD
PyObject_HEAD
Definition: extension_dict.c:185
upb_MessageDef_MiniTable
const upb_MiniTable * upb_MessageDef_MiniTable(const upb_MessageDef *m)
Definition: upb/upb/def.c:818
upb_MessageDef
Definition: upb/upb/def.c:100
PyUpb_ExtensionIterator_IterNext
PyObject * PyUpb_ExtensionIterator_IterNext(PyObject *_self)
Definition: extension_dict.c:208
ext
void * ext
Definition: x509v3.h:87
upb_DefPool_ExtensionRegistry
const upb_ExtensionRegistry * upb_DefPool_ExtensionRegistry(const upb_DefPool *s)
Definition: upb/upb/def.c:3231
PyUpb_ExtensionDict_RichCompare
static PyObject * PyUpb_ExtensionDict_RichCompare(PyObject *_self, PyObject *_other, int opid)
Definition: extension_dict.c:91
upb_MiniTable_Extension
Definition: msg_internal.h:202
file
Definition: bloaty/third_party/zlib/examples/gzappend.c:170
_upb_DefPool_FindExtensionByMiniTable
const upb_FieldDef * _upb_DefPool_FindExtensionByMiniTable(const upb_DefPool *s, const upb_MiniTable_Extension *ext)
Definition: upb/upb/def.c:3207
PyUpb_ModuleState_GetFromModule
PyUpb_ModuleState * PyUpb_ModuleState_GetFromModule(PyObject *module)
Definition: upb/python/protobuf.c:82
PyUpb_CMessage_GetMsgdef
const upb_MessageDef * PyUpb_CMessage_GetMsgdef(PyObject *self)
Definition: upb/python/message.c:220
upb_MiniTable
Definition: msg_internal.h:185
setup.name
name
Definition: setup.py:542
upb_MessageValue::array_val
const upb_Array * array_val
Definition: upb/upb/reflection.h:50
PyUpb_ExtensionDict_Methods
static PyMethodDef PyUpb_ExtensionDict_Methods[]
Definition: extension_dict.c:150
upb_MessageValue
Definition: upb/upb/reflection.h:40
PyUpb_ExtensionIterator_Dealloc
static void PyUpb_ExtensionIterator_Dealloc(void *_self)
Definition: extension_dict.c:202
PyUpb_ExtensionIterator::msg
PyObject * msg
Definition: extension_dict.c:186
PyUpb_ExtensionIterator
Definition: extension_dict.c:184
autogen_x86imm.f
f
Definition: autogen_x86imm.py:9
kUpb_Message_Begin
#define kUpb_Message_Begin
Definition: upb/upb/reflection.h:113
PyUpb_Dealloc
static void PyUpb_Dealloc(void *self)
Definition: upb/python/protobuf.h:208
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
PyUpb_ExtensionDict_Dealloc
static void PyUpb_ExtensionDict_Dealloc(PyUpb_ExtensionDict *self)
Definition: extension_dict.c:85
PyUpb_ExtensionDict::msg
PyObject * msg
Definition: extension_dict.c:39
upb_FileDef_Pool
const upb_DefPool * upb_FileDef_Pool(const upb_FileDef *f)
Definition: upb/upb/def.c:993
PyUpb_ExtensionDict_Length
static Py_ssize_t PyUpb_ExtensionDict_Length(PyObject *_self)
Definition: extension_dict.c:122
upb_FieldDef_IsExtension
bool upb_FieldDef_IsExtension(const upb_FieldDef *f)
Definition: upb/upb/def.c:543
extension_dict.h
arg
Definition: cmdline.cc:40
number
int32_t number
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:850
PyUpb_AddClass
PyTypeObject * PyUpb_AddClass(PyObject *m, PyType_Spec *spec)
Definition: upb/python/protobuf.c:274
upb_Message_Next
bool upb_Message_Next(const upb_Message *msg, const upb_MessageDef *m, const upb_DefPool *ext_pool, const upb_FieldDef **out_f, upb_MessageValue *out_val, size_t *iter)
Definition: reflection.c:245
PyUpb_FieldDescriptor_Get
PyObject * PyUpb_FieldDescriptor_Get(const upb_FieldDef *field)
Definition: descriptor.c:870
PyUpb_ExtensionDict_New
PyObject * PyUpb_ExtensionDict_New(PyObject *msg)
Definition: extension_dict.c:42
PyUpb_ExtensionDict_FindExtensionByNumber
static PyObject * PyUpb_ExtensionDict_FindExtensionByNumber(PyObject *_self, PyObject *arg)
Definition: extension_dict.c:66
PyUpb_ExtensionDict_Contains
static int PyUpb_ExtensionDict_Contains(PyObject *_self, PyObject *key)
Definition: extension_dict.c:108
PyUpb_ExtensionIterator_New
static PyObject * PyUpb_ExtensionIterator_New(PyObject *_ext_dict)
Definition: extension_dict.c:190
protobuf.h
upb_Message
void upb_Message
Definition: msg.h:49
PyUpb_ExtensionIterator_Slots
static PyType_Slot PyUpb_ExtensionIterator_Slots[]
Definition: extension_dict.c:222
msg
std::string msg
Definition: client_interceptors_end2end_test.cc:372
upb_FieldDef
Definition: upb/upb/def.c:56
upb_Array_Size
size_t upb_Array_Size(const upb_Array *arr)
Definition: reflection.c:362
upb_Message_Has
bool upb_Message_Has(const upb_Message *msg, const upb_FieldDef *f)
Definition: reflection.c:112
symtab
upb_symtab * symtab
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:774
PyUpb_ModuleState_Get
PyUpb_ModuleState * PyUpb_ModuleState_Get(void)
Definition: upb/python/protobuf.c:89
message.h
PyUpb_ExtensionIterator::iter
size_t iter
Definition: extension_dict.c:187
PyUpb_CMessage_GetExtensionDef
const upb_FieldDef * PyUpb_CMessage_GetExtensionDef(PyObject *_self, PyObject *key)
Definition: upb/python/message.c:1342
key
const char * key
Definition: hpack_parser_table.cc:164
PYUPB_MODULE_NAME
#define PYUPB_MODULE_NAME
Definition: upb/python/protobuf.h:43
PyUpb_ExtensionDict_Subscript
static PyObject * PyUpb_ExtensionDict_Subscript(PyObject *_self, PyObject *key)
Definition: extension_dict.c:128
upb_FileDef
Definition: upb/upb/def.c:171
upb_Message_Get
upb_MessageValue upb_Message_Get(const upb_Message *msg, const upb_FieldDef *f)
Definition: reflection.c:146
upb_FieldDef_IsRepeated
bool upb_FieldDef_IsRepeated(const upb_FieldDef *f)
Definition: upb/upb/def.c:651
PyUpb_ModuleState
Definition: upb/python/protobuf.h:66
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
PyUpb_CMessage_ClearExtensionDict
void PyUpb_CMessage_ClearExtensionDict(PyObject *_self)
Definition: upb/python/message.c:1475
state
Definition: bloaty/third_party/zlib/contrib/blast/blast.c:41
PyUpb_ExtensionDict
Definition: extension_dict.c:37
upb_DefPool_FindExtensionByName
const upb_FieldDef * upb_DefPool_FindExtensionByName(const upb_DefPool *s, const char *sym)
Definition: upb/upb/def.c:1181
upb_MessageDef_File
const upb_FileDef * upb_MessageDef_File(const upb_MessageDef *m)
Definition: upb/upb/def.c:705
PyUpb_CMessage_GetFieldValue
PyObject * PyUpb_CMessage_GetFieldValue(PyObject *_self, const upb_FieldDef *field)
Definition: upb/python/message.c:854
_upb_extreg_get
const upb_msglayout_field * _upb_extreg_get(const upb_extreg *r, const upb_msglayout *l, uint32_t num)
Definition: php-upb.c:1835
iter
Definition: test_winkernel.cpp:47
Py_TYPE
#define Py_TYPE(ob)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/descriptor.cc:164
PyUpb_ExtensionDict_Spec
static PyType_Spec PyUpb_ExtensionDict_Spec
Definition: extension_dict.c:172
run_grpclb_interop_tests.l
dictionary l
Definition: run_grpclb_interop_tests.py:410
self
PHP_PROTO_OBJECT_FREE_END PHP_PROTO_OBJECT_DTOR_END intern self
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/map.c:543
regress.m
m
Definition: regress/regress.py:25
PyUpb_CMessage_SetFieldValue
int PyUpb_CMessage_SetFieldValue(PyObject *_self, const upb_FieldDef *field, PyObject *value, PyObject *exc)
Definition: upb/python/message.c:871
PyUpb_CMessage_GetIfReified
upb_Message * PyUpb_CMessage_GetIfReified(PyObject *_self)
Definition: upb/python/message.c:246
PyUpb_ExtensionDict_AssignSubscript
static int PyUpb_ExtensionDict_AssignSubscript(PyObject *_self, PyObject *key, PyObject *val)
Definition: extension_dict.c:135
upb_DefPool
Definition: upb/upb/def.c:217
PyUpb_ExtensionDict::PyObject_HEAD
PyObject_HEAD
Definition: extension_dict.c:38
upb_ExtensionRegistry
Definition: msg.c:372
PyUpb_ExtensionDict_FindExtensionByName
static PyObject * PyUpb_ExtensionDict_FindExtensionByName(PyObject *_self, PyObject *key)
Definition: extension_dict.c:51


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:20