| 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 { |
| 38 |
PyObject_HEAD; |
| 39 |
PyObject* msg; // Owning ref to our parent pessage. |
| 40 |
} PyUpb_ExtensionDict; |
| 41 |
|
| 42 |
PyObject* PyUpb_ExtensionDict_New(PyObject* msg) { |
| 43 |
PyUpb_ModuleState* state = PyUpb_ModuleState_Get(); |
| 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) { |
| 53 |
PyUpb_ExtensionDict* self = (PyUpb_ExtensionDict*)_self; |
| 54 |
const char* name = PyUpb_GetStrData(key); |
| 55 |
const upb_MessageDef* m = PyUpb_Message_GetMsgdef(self->msg); |
| 56 |
const upb_FileDef* file = upb_MessageDef_File(m); |
| 57 |
const upb_DefPool* symtab = upb_FileDef_Pool(file); |
| 58 |
const upb_FieldDef* ext = upb_DefPool_FindExtensionByName(symtab, name); |
| 59 |
if (ext) { |
| 60 |
return PyUpb_FieldDescriptor_Get(ext); |
| 61 |
} else { |
| 62 |
Py_RETURN_NONE; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
static PyObject* PyUpb_ExtensionDict_FindExtensionByNumber(PyObject* _self, |
| 67 |
PyObject* arg) { |
| 68 |
PyUpb_ExtensionDict* self = (PyUpb_ExtensionDict*)_self; |
| 69 |
const upb_MessageDef* m = PyUpb_Message_GetMsgdef(self->msg); |
| 70 |
const upb_MiniTable* l = upb_MessageDef_MiniTable(m); |
| 71 |
const upb_FileDef* file = upb_MessageDef_File(m); |
| 72 |
const upb_DefPool* symtab = upb_FileDef_Pool(file); |
| 73 |
const upb_ExtensionRegistry* reg = upb_DefPool_ExtensionRegistry(symtab); |
| 74 |
int64_t number = PyLong_AsLong(arg); |
| 75 |
const upb_MiniTable_Extension* ext = |
| 76 |
(upb_MiniTable_Extension*)_upb_extreg_get(reg, l, number); |
| 77 |
if (ext) { |
| 78 |
const upb_FieldDef* f = _upb_DefPool_FindExtensionByMiniTable(symtab, ext); |
| 79 |
return PyUpb_FieldDescriptor_Get(f); |
| 80 |
} else { |
| 81 |
Py_RETURN_NONE; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
static void PyUpb_ExtensionDict_Dealloc(PyUpb_ExtensionDict* self) { |
| 86 |
PyUpb_Message_ClearExtensionDict(self->msg); |
| 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 |
} |
| 98 |
PyUpb_ExtensionDict* self = (PyUpb_ExtensionDict*)_self; |
| 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) { |
| 109 |
PyUpb_ExtensionDict* self = (PyUpb_ExtensionDict*)_self; |
| 110 |
const upb_FieldDef* f = PyUpb_Message_GetExtensionDef(self->msg, key); |
| 111 |
if (!f) return -1; |
| 112 |
upb_Message* msg = PyUpb_Message_GetIfReified(self->msg); |
| 113 |
if (!msg) return 0; |
| 114 |
if (upb_FieldDef_IsRepeated(f)) { |
| 115 |
upb_MessageValue val = upb_Message_Get(msg, 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) { |
| 123 |
PyUpb_ExtensionDict* self = (PyUpb_ExtensionDict*)_self; |
| 124 |
upb_Message* msg = PyUpb_Message_GetIfReified(self->msg); |
| 125 |
return msg ? upb_Message_ExtensionCount(msg) : 0; |
| 126 |
} |
| 127 |
|
| 128 |
static PyObject* PyUpb_ExtensionDict_Subscript(PyObject* _self, PyObject* key) { |
| 129 |
PyUpb_ExtensionDict* self = (PyUpb_ExtensionDict*)_self; |
| 130 |
const upb_FieldDef* f = PyUpb_Message_GetExtensionDef(self->msg, key); |
| 131 |
if (!f) return NULL; |
| 132 |
return PyUpb_Message_GetFieldValue(self->msg, f); |
| 133 |
} |
| 134 |
|
| 135 |
static int PyUpb_ExtensionDict_AssignSubscript(PyObject* _self, PyObject* key, |
| 136 |
PyObject* val) { |
| 137 |
PyUpb_ExtensionDict* self = (PyUpb_ExtensionDict*)_self; |
| 138 |
const upb_FieldDef* f = PyUpb_Message_GetExtensionDef(self->msg, key); |
| 139 |
if (!f) return -1; |
| 140 |
if (val) { |
| 141 |
return PyUpb_Message_SetFieldValue(self->msg, f, val, PyExc_TypeError); |
| 142 |
} else { |
| 143 |
PyUpb_Message_DoClearField(self->msg, f); |
| 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 |
| 177 |
PyUpb_ExtensionDict_Slots, |
| 178 |
}; |
| 179 |
|
| 180 |
// ----------------------------------------------------------------------------- |
| 181 |
// ExtensionIterator |
| 182 |
// ----------------------------------------------------------------------------- |
| 183 |
|
| 184 |
typedef struct { |
| 185 |
PyObject_HEAD; |
| 186 |
PyObject* msg; |
| 187 |
size_t iter; |
| 188 |
} PyUpb_ExtensionIterator; |
| 189 |
|
| 190 |
static PyObject* PyUpb_ExtensionIterator_New(PyObject* _ext_dict) { |
| 191 |
PyUpb_ExtensionDict* ext_dict = (PyUpb_ExtensionDict*)_ext_dict; |
| 192 |
PyUpb_ModuleState* state = PyUpb_ModuleState_Get(); |
| 193 |
PyUpb_ExtensionIterator* iter = |
| 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) { |
| 203 |
PyUpb_ExtensionIterator* self = (PyUpb_ExtensionIterator*)_self; |
| 204 |
Py_DECREF(self->msg); |
| 205 |
PyUpb_Dealloc(_self); |
| 206 |
} |
| 207 |
|
| 208 |
PyObject* PyUpb_ExtensionIterator_IterNext(PyObject* _self) { |
| 209 |
PyUpb_ExtensionIterator* self = (PyUpb_ExtensionIterator*)_self; |
| 210 |
upb_Message* msg = PyUpb_Message_GetIfReified(self->msg); |
| 211 |
if (!msg) return NULL; |
| 212 |
const upb_MessageDef* m = PyUpb_Message_GetMsgdef(self->msg); |
| 213 |
const upb_DefPool* symtab = upb_FileDef_Pool(upb_MessageDef_File(m)); |
| 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; |
| 218 |
if (upb_FieldDef_IsExtension(f)) return PyUpb_FieldDescriptor_Get(f); |
| 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 |
| 233 |
PyUpb_ExtensionIterator_Slots, |
| 234 |
}; |
| 235 |
|
| 236 |
// ----------------------------------------------------------------------------- |
| 237 |
// Top Level |
| 238 |
// ----------------------------------------------------------------------------- |
| 239 |
|
| 240 |
bool PyUpb_InitExtensionDict(PyObject* m) { |
| 241 |
PyUpb_ModuleState* s = PyUpb_ModuleState_GetFromModule(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 |
} |