Debian Codegraph @

third_party/upb/python/map.c

raw
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/map.h"
29
30 #include "python/convert.h"
31 #include "python/message.h"
32 #include "python/protobuf.h"
33 #include "upb/map.h"
34
35 // -----------------------------------------------------------------------------
36 // MapContainer
37 // -----------------------------------------------------------------------------
38
39 typedef struct {
40 PyObject_HEAD;
41 PyObject* arena;
42 // The field descriptor (upb_FieldDef*).
43 // The low bit indicates whether the container is reified (see ptr below).
44 // - low bit set: repeated field is a stub (empty map, no underlying data).
45 // - low bit clear: repeated field is reified (points to upb_Array).
46 uintptr_t field;
47 union {
48 PyObject* parent; // stub: owning pointer to parent message.
49 upb_Map* map; // reified: the data for this array.
50 } ptr;
51 int version;
52 } PyUpb_MapContainer;
53
54 static PyObject* PyUpb_MapIterator_New(PyUpb_MapContainer* map);
55
56 static bool PyUpb_MapContainer_IsStub(PyUpb_MapContainer* self) {
57 return self->field & 1;
58 }
59
60 // If the map is reified, returns it. Otherwise, returns NULL.
61 // If NULL is returned, the object is empty and has no underlying data.
62 static upb_Map* PyUpb_MapContainer_GetIfReified(PyUpb_MapContainer* self) {
63 return PyUpb_MapContainer_IsStub(self) ? NULL : self->ptr.map;
64 }
65
66 static const upb_FieldDef* PyUpb_MapContainer_GetField(
67 PyUpb_MapContainer* self) {
68 return (const upb_FieldDef*)(self->field & ~(uintptr_t)1);
69 }
70
71 static void PyUpb_MapContainer_Dealloc(void* _self) {
72 PyUpb_MapContainer* self = _self;
73 Py_DECREF(self->arena);
74 if (PyUpb_MapContainer_IsStub(self)) {
75 PyUpb_Message_CacheDelete(self->ptr.parent,
76 PyUpb_MapContainer_GetField(self));
77 Py_DECREF(self->ptr.parent);
78 } else {
79 PyUpb_ObjCache_Delete(self->ptr.map);
80 }
81 PyUpb_Dealloc(_self);
82 }
83
84 PyTypeObject* PyUpb_MapContainer_GetClass(const upb_FieldDef* f) {
85 assert(upb_FieldDef_IsMap(f));
86 PyUpb_ModuleState* state = PyUpb_ModuleState_Get();
87 return upb_FieldDef_IsSubMessage(f) ? state->message_map_container_type
88 : state->scalar_map_container_type;
89 }
90
91 PyObject* PyUpb_MapContainer_NewStub(PyObject* parent, const upb_FieldDef* f,
92 PyObject* arena) {
93 // We only create stubs when the parent is reified, by convention. However
94 // this is not an invariant: the parent could become reified at any time.
95 assert(PyUpb_Message_GetIfReified(parent) == NULL);
96 PyTypeObject* cls = PyUpb_MapContainer_GetClass(f);
97 PyUpb_MapContainer* map = (void*)PyType_GenericAlloc(cls, 0);
98 map->arena = arena;
99 map->field = (uintptr_t)f | 1;
100 map->ptr.parent = parent;
101 map->version = 0;
102 Py_INCREF(arena);
103 Py_INCREF(parent);
104 return &map->ob_base;
105 }
106
107 void PyUpb_MapContainer_Reify(PyObject* _self, upb_Map* map) {
108 PyUpb_MapContainer* self = (PyUpb_MapContainer*)_self;
109 if (!map) {
110 const upb_FieldDef* f = PyUpb_MapContainer_GetField(self);
111 upb_Arena* arena = PyUpb_Arena_Get(self->arena);
112 const upb_MessageDef* entry_m = upb_FieldDef_MessageSubDef(f);
113 const upb_FieldDef* key_f = upb_MessageDef_Field(entry_m, 0);
114 const upb_FieldDef* val_f = upb_MessageDef_Field(entry_m, 1);
115 map = upb_Map_New(arena, upb_FieldDef_CType(key_f),
116 upb_FieldDef_CType(val_f));
117 }
118 PyUpb_ObjCache_Add(map, &self->ob_base);
119 Py_DECREF(self->ptr.parent);
120 self->ptr.map = map; // Overwrites self->ptr.parent.
121 self->field &= ~(uintptr_t)1;
122 assert(!PyUpb_MapContainer_IsStub(self));
123 }
124
125 void PyUpb_MapContainer_Invalidate(PyObject* obj) {
126 PyUpb_MapContainer* self = (PyUpb_MapContainer*)obj;
127 self->version++;
128 }
129
130 upb_Map* PyUpb_MapContainer_EnsureReified(PyObject* _self) {
131 PyUpb_MapContainer* self = (PyUpb_MapContainer*)_self;
132 self->version++;
133 upb_Map* map = PyUpb_MapContainer_GetIfReified(self);
134 if (map) return map; // Already writable.
135
136 const upb_FieldDef* f = PyUpb_MapContainer_GetField(self);
137 upb_Arena* arena = PyUpb_Arena_Get(self->arena);
138 const upb_MessageDef* entry_m = upb_FieldDef_MessageSubDef(f);
139 const upb_FieldDef* key_f = upb_MessageDef_Field(entry_m, 0);
140 const upb_FieldDef* val_f = upb_MessageDef_Field(entry_m, 1);
141 map =
142 upb_Map_New(arena, upb_FieldDef_CType(key_f), upb_FieldDef_CType(val_f));
143 upb_MessageValue msgval = {.map_val = map};
144 PyUpb_Message_SetConcreteSubobj(self->ptr.parent, f, msgval);
145 PyUpb_MapContainer_Reify((PyObject*)self, map);
146 return map;
147 }
148
149 bool PyUpb_MapContainer_Set(PyUpb_MapContainer* self, upb_Map* map,
150 upb_MessageValue key, upb_MessageValue val,
151 upb_Arena* arena) {
152 switch (upb_Map_Insert(map, key, val, arena)) {
153 case kUpb_MapInsertStatus_Inserted:
154 return true;
155 case kUpb_MapInsertStatus_Replaced:
156 // We did not insert a new key, undo the previous invalidate.
157 self->version--;
158 return true;
159 case kUpb_MapInsertStatus_OutOfMemory:
160 return false;
161 }
162 return false; // Unreachable, silence compiler warning.
163 }
164
165 int PyUpb_MapContainer_AssignSubscript(PyObject* _self, PyObject* key,
166 PyObject* val) {
167 PyUpb_MapContainer* self = (PyUpb_MapContainer*)_self;
168 upb_Map* map = PyUpb_MapContainer_EnsureReified(_self);
169 const upb_FieldDef* f = PyUpb_MapContainer_GetField(self);
170 const upb_MessageDef* entry_m = upb_FieldDef_MessageSubDef(f);
171 const upb_FieldDef* key_f = upb_MessageDef_Field(entry_m, 0);
172 const upb_FieldDef* val_f = upb_MessageDef_Field(entry_m, 1);
173 upb_Arena* arena = PyUpb_Arena_Get(self->arena);
174 upb_MessageValue u_key, u_val;
175 if (!PyUpb_PyToUpb(key, key_f, &u_key, arena)) return -1;
176
177 if (val) {
178 if (!PyUpb_PyToUpb(val, val_f, &u_val, arena)) return -1;
179 if (!PyUpb_MapContainer_Set(self, map, u_key, u_val, arena)) return -1;
180 } else {
181 if (!upb_Map_Delete(map, u_key)) {
182 PyErr_Format(PyExc_KeyError, "Key not present in map");
183 return -1;
184 }
185 }
186 return 0;
187 }
188
189 PyObject* PyUpb_MapContainer_Subscript(PyObject* _self, PyObject* key) {
190 PyUpb_MapContainer* self = (PyUpb_MapContainer*)_self;
191 upb_Map* map = PyUpb_MapContainer_GetIfReified(self);
192 const upb_FieldDef* f = PyUpb_MapContainer_GetField(self);
193 const upb_MessageDef* entry_m = upb_FieldDef_MessageSubDef(f);
194 const upb_FieldDef* key_f = upb_MessageDef_Field(entry_m, 0);
195 const upb_FieldDef* val_f = upb_MessageDef_Field(entry_m, 1);
196 upb_Arena* arena = PyUpb_Arena_Get(self->arena);
197 upb_MessageValue u_key, u_val;
198 if (!PyUpb_PyToUpb(key, key_f, &u_key, arena)) return NULL;
199 if (!map || !upb_Map_Get(map, u_key, &u_val)) {
200 map = PyUpb_MapContainer_EnsureReified(_self);
201 upb_Arena* arena = PyUpb_Arena_Get(self->arena);
202 if (upb_FieldDef_IsSubMessage(val_f)) {
203 u_val.msg_val = upb_Message_New(upb_FieldDef_MessageSubDef(val_f), arena);
204 } else {
205 memset(&u_val, 0, sizeof(u_val));
206 }
207 if (!PyUpb_MapContainer_Set(self, map, u_key, u_val, arena)) return false;
208 }
209 return PyUpb_UpbToPy(u_val, val_f, self->arena);
210 }
211
212 PyObject* PyUpb_MapContainer_Contains(PyObject* _self, PyObject* key) {
213 PyUpb_MapContainer* self = (PyUpb_MapContainer*)_self;
214 upb_Map* map = PyUpb_MapContainer_GetIfReified(self);
215 if (!map) Py_RETURN_FALSE;
216 const upb_FieldDef* f = PyUpb_MapContainer_GetField(self);
217 const upb_MessageDef* entry_m = upb_FieldDef_MessageSubDef(f);
218 const upb_FieldDef* key_f = upb_MessageDef_Field(entry_m, 0);
219 upb_MessageValue u_key;
220 if (!PyUpb_PyToUpb(key, key_f, &u_key, NULL)) return NULL;
221 if (upb_Map_Get(map, u_key, NULL)) {
222 Py_RETURN_TRUE;
223 } else {
224 Py_RETURN_FALSE;
225 }
226 }
227
228 PyObject* PyUpb_MapContainer_Clear(PyObject* _self, PyObject* key) {
229 upb_Map* map = PyUpb_MapContainer_EnsureReified(_self);
230 upb_Map_Clear(map);
231 Py_RETURN_NONE;
232 }
233
234 static PyObject* PyUpb_MapContainer_Get(PyObject* _self, PyObject* args,
235 PyObject* kwargs) {
236 PyUpb_MapContainer* self = (PyUpb_MapContainer*)_self;
237 static const char* kwlist[] = {"key", "default", NULL};
238 PyObject* key;
239 PyObject* default_value = NULL;
240 upb_Map* map = PyUpb_MapContainer_GetIfReified(self);
241 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O", (char**)kwlist, &key,
242 &default_value)) {
243 return NULL;
244 }
245
246 const upb_FieldDef* f = PyUpb_MapContainer_GetField(self);
247 const upb_MessageDef* entry_m = upb_FieldDef_MessageSubDef(f);
248 const upb_FieldDef* key_f = upb_MessageDef_Field(entry_m, 0);
249 const upb_FieldDef* val_f = upb_MessageDef_Field(entry_m, 1);
250 upb_Arena* arena = PyUpb_Arena_Get(self->arena);
251 upb_MessageValue u_key, u_val;
252 if (!PyUpb_PyToUpb(key, key_f, &u_key, arena)) return NULL;
253 if (map && upb_Map_Get(map, u_key, &u_val)) {
254 return PyUpb_UpbToPy(u_val, val_f, self->arena);
255 }
256 if (default_value) {
257 Py_INCREF(default_value);
258 return default_value;
259 }
260 Py_RETURN_NONE;
261 }
262
263 static PyObject* PyUpb_MapContainer_GetEntryClass(PyObject* _self,
264 PyObject* arg) {
265 PyUpb_MapContainer* self = (PyUpb_MapContainer*)_self;
266 const upb_FieldDef* f = PyUpb_MapContainer_GetField(self);
267 const upb_MessageDef* entry_m = upb_FieldDef_MessageSubDef(f);
268 return PyUpb_Descriptor_GetClass(entry_m);
269 }
270
271 Py_ssize_t PyUpb_MapContainer_Length(PyObject* _self) {
272 PyUpb_MapContainer* self = (PyUpb_MapContainer*)_self;
273 upb_Map* map = PyUpb_MapContainer_GetIfReified(self);
274 return map ? upb_Map_Size(map) : 0;
275 }
276
277 PyUpb_MapContainer* PyUpb_MapContainer_Check(PyObject* _self) {
278 PyUpb_ModuleState* state = PyUpb_ModuleState_Get();
279 if (!PyObject_TypeCheck(_self, state->message_map_container_type) &&
280 !PyObject_TypeCheck(_self, state->scalar_map_container_type)) {
281 PyErr_Format(PyExc_TypeError, "Expected protobuf map, but got %R", _self);
282 return NULL;
283 }
284 return (PyUpb_MapContainer*)_self;
285 }
286
287 int PyUpb_Message_InitMapAttributes(PyObject* map, PyObject* value,
288 const upb_FieldDef* f);
289
290 static PyObject* PyUpb_MapContainer_MergeFrom(PyObject* _self, PyObject* _arg) {
291 PyUpb_MapContainer* self = (PyUpb_MapContainer*)_self;
292 const upb_FieldDef* f = PyUpb_MapContainer_GetField(self);
293
294 if (PyDict_Check(_arg)) {
295 return PyErr_Format(PyExc_AttributeError, "Merging of dict is not allowed");
296 }
297
298 if (PyUpb_Message_InitMapAttributes(_self, _arg, f) < 0) {
299 return NULL;
300 }
301
302 Py_RETURN_NONE;
303 }
304
305 static PyObject* PyUpb_MapContainer_Repr(PyObject* _self) {
306 PyUpb_MapContainer* self = (PyUpb_MapContainer*)_self;
307 upb_Map* map = PyUpb_MapContainer_GetIfReified(self);
308 PyObject* dict = PyDict_New();
309 if (map) {
310 const upb_FieldDef* f = PyUpb_MapContainer_GetField(self);
311 const upb_MessageDef* entry_m = upb_FieldDef_MessageSubDef(f);
312 const upb_FieldDef* key_f = upb_MessageDef_Field(entry_m, 0);
313 const upb_FieldDef* val_f = upb_MessageDef_Field(entry_m, 1);
314 size_t iter = kUpb_Map_Begin;
315 while (upb_MapIterator_Next(map, &iter)) {
316 PyObject* key =
317 PyUpb_UpbToPy(upb_MapIterator_Key(map, iter), key_f, self->arena);
318 PyObject* val =
319 PyUpb_UpbToPy(upb_MapIterator_Value(map, iter), val_f, self->arena);
320 if (!key || !val) {
321 Py_XDECREF(key);
322 Py_XDECREF(val);
323 Py_DECREF(dict);
324 return NULL;
325 }
326 PyDict_SetItem(dict, key, val);
327 Py_DECREF(key);
328 Py_DECREF(val);
329 }
330 }
331 PyObject* repr = PyObject_Repr(dict);
332 Py_DECREF(dict);
333 return repr;
334 }
335
336 PyObject* PyUpb_MapContainer_GetOrCreateWrapper(upb_Map* map,
337 const upb_FieldDef* f,
338 PyObject* arena) {
339 PyUpb_MapContainer* ret = (void*)PyUpb_ObjCache_Get(map);
340 if (ret) return &ret->ob_base;
341
342 PyTypeObject* cls = PyUpb_MapContainer_GetClass(f);
343 ret = (void*)PyType_GenericAlloc(cls, 0);
344 ret->arena = arena;
345 ret->field = (uintptr_t)f;
346 ret->ptr.map = map;
347 ret->version = 0;
348 Py_INCREF(arena);
349 PyUpb_ObjCache_Add(map, &ret->ob_base);
350 return &ret->ob_base;
351 }
352
353 // -----------------------------------------------------------------------------
354 // ScalarMapContainer
355 // -----------------------------------------------------------------------------
356
357 static PyMethodDef PyUpb_ScalarMapContainer_Methods[] = {
358 {"__contains__", PyUpb_MapContainer_Contains, METH_O,
359 "Tests whether a key is a member of the map."},
360 {"clear", PyUpb_MapContainer_Clear, METH_NOARGS,
361 "Removes all elements from the map."},
362 {"get", (PyCFunction)PyUpb_MapContainer_Get, METH_VARARGS | METH_KEYWORDS,
363 "Gets the value for the given key if present, or otherwise a default"},
364 {"GetEntryClass", PyUpb_MapContainer_GetEntryClass, METH_NOARGS,
365 "Return the class used to build Entries of (key, value) pairs."},
366 {"MergeFrom", PyUpb_MapContainer_MergeFrom, METH_O,
367 "Merges a map into the current map."},
368 /*
369 { "__deepcopy__", (PyCFunction)DeepCopy, METH_VARARGS,
370 "Makes a deep copy of the class." },
371 { "__reduce__", (PyCFunction)Reduce, METH_NOARGS,
372 "Outputs picklable representation of the repeated field." },
373 */
374 {NULL, NULL},
375 };
376
377 static PyType_Slot PyUpb_ScalarMapContainer_Slots[] = {
378 {Py_tp_dealloc, PyUpb_MapContainer_Dealloc},
379 {Py_mp_length, PyUpb_MapContainer_Length},
380 {Py_mp_subscript, PyUpb_MapContainer_Subscript},
381 {Py_mp_ass_subscript, PyUpb_MapContainer_AssignSubscript},
382 {Py_tp_methods, PyUpb_ScalarMapContainer_Methods},
383 {Py_tp_iter, PyUpb_MapIterator_New},
384 {Py_tp_repr, PyUpb_MapContainer_Repr},
385 {Py_tp_hash, PyObject_HashNotImplemented},
386 {0, NULL},
387 };
388
389 static PyType_Spec PyUpb_ScalarMapContainer_Spec = {
390 PYUPB_MODULE_NAME ".ScalarMapContainer",
391 sizeof(PyUpb_MapContainer),
392 0,
393 Py_TPFLAGS_DEFAULT,
394 PyUpb_ScalarMapContainer_Slots,
395 };
396
397 // -----------------------------------------------------------------------------
398 // MessageMapContainer
399 // -----------------------------------------------------------------------------
400
401 static PyMethodDef PyUpb_MessageMapContainer_Methods[] = {
402 {"__contains__", PyUpb_MapContainer_Contains, METH_O,
403 "Tests whether the map contains this element."},
404 {"clear", PyUpb_MapContainer_Clear, METH_NOARGS,
405 "Removes all elements from the map."},
406 {"get", (PyCFunction)PyUpb_MapContainer_Get, METH_VARARGS | METH_KEYWORDS,
407 "Gets the value for the given key if present, or otherwise a default"},
408 {"get_or_create", PyUpb_MapContainer_Subscript, METH_O,
409 "Alias for getitem, useful to make explicit that the map is mutated."},
410 {"GetEntryClass", PyUpb_MapContainer_GetEntryClass, METH_NOARGS,
411 "Return the class used to build Entries of (key, value) pairs."},
412 {"MergeFrom", PyUpb_MapContainer_MergeFrom, METH_O,
413 "Merges a map into the current map."},
414 /*
415 { "__deepcopy__", (PyCFunction)DeepCopy, METH_VARARGS,
416 "Makes a deep copy of the class." },
417 { "__reduce__", (PyCFunction)Reduce, METH_NOARGS,
418 "Outputs picklable representation of the repeated field." },
419 */
420 {NULL, NULL},
421 };
422
423 static PyType_Slot PyUpb_MessageMapContainer_Slots[] = {
424 {Py_tp_dealloc, PyUpb_MapContainer_Dealloc},
425 {Py_mp_length, PyUpb_MapContainer_Length},
426 {Py_mp_subscript, PyUpb_MapContainer_Subscript},
427 {Py_mp_ass_subscript, PyUpb_MapContainer_AssignSubscript},
428 {Py_tp_methods, PyUpb_MessageMapContainer_Methods},
429 {Py_tp_iter, PyUpb_MapIterator_New},
430 {Py_tp_repr, PyUpb_MapContainer_Repr},
431 {Py_tp_hash, PyObject_HashNotImplemented},
432 {0, NULL}};
433
434 static PyType_Spec PyUpb_MessageMapContainer_Spec = {
435 PYUPB_MODULE_NAME ".MessageMapContainer", sizeof(PyUpb_MapContainer), 0,
436 Py_TPFLAGS_DEFAULT, PyUpb_MessageMapContainer_Slots};
437
438 // -----------------------------------------------------------------------------
439 // MapIterator
440 // -----------------------------------------------------------------------------
441
442 typedef struct {
443 PyObject_HEAD;
444 PyUpb_MapContainer* map; // We own a reference.
445 size_t iter;
446 int version;
447 } PyUpb_MapIterator;
448
449 static PyObject* PyUpb_MapIterator_New(PyUpb_MapContainer* map) {
450 PyUpb_ModuleState* state = PyUpb_ModuleState_Get();
451 PyUpb_MapIterator* iter =
452 (void*)PyType_GenericAlloc(state->map_iterator_type, 0);
453 iter->map = map;
454 iter->iter = kUpb_Map_Begin;
455 iter->version = map->version;
456 Py_INCREF(map);
457 return &iter->ob_base;
458 }
459
460 static void PyUpb_MapIterator_Dealloc(void* _self) {
461 PyUpb_MapIterator* self = (PyUpb_MapIterator*)_self;
462 Py_DECREF(&self->map->ob_base);
463 PyUpb_Dealloc(_self);
464 }
465
466 PyObject* PyUpb_MapIterator_IterNext(PyObject* _self) {
467 PyUpb_MapIterator* self = (PyUpb_MapIterator*)_self;
468 if (self->version != self->map->version) {
469 return PyErr_Format(PyExc_RuntimeError, "Map modified during iteration.");
470 }
471 upb_Map* map = PyUpb_MapContainer_GetIfReified(self->map);
472 if (!map) return NULL;
473 if (!upb_MapIterator_Next(map, &self->iter)) return NULL;
474 upb_MessageValue key = upb_MapIterator_Key(map, self->iter);
475 const upb_FieldDef* f = PyUpb_MapContainer_GetField(self->map);
476 const upb_MessageDef* entry_m = upb_FieldDef_MessageSubDef(f);
477 const upb_FieldDef* key_f = upb_MessageDef_Field(entry_m, 0);
478 return PyUpb_UpbToPy(key, key_f, self->map->arena);
479 }
480
481 static PyType_Slot PyUpb_MapIterator_Slots[] = {
482 {Py_tp_dealloc, PyUpb_MapIterator_Dealloc},
483 {Py_tp_iter, PyObject_SelfIter},
484 {Py_tp_iternext, PyUpb_MapIterator_IterNext},
485 {0, NULL}};
486
487 static PyType_Spec PyUpb_MapIterator_Spec = {
488 PYUPB_MODULE_NAME ".MapIterator", sizeof(PyUpb_MapIterator), 0,
489 Py_TPFLAGS_DEFAULT, PyUpb_MapIterator_Slots};
490
491 // -----------------------------------------------------------------------------
492 // Top Level
493 // -----------------------------------------------------------------------------
494
495 static PyObject* GetMutableMappingBase(void) {
496 PyObject* collections = NULL;
497 PyObject* mapping = NULL;
498 PyObject* bases = NULL;
499 if ((collections = PyImport_ImportModule("collections.abc")) &&
500 (mapping = PyObject_GetAttrString(collections, "MutableMapping"))) {
501 bases = Py_BuildValue("(O)", mapping);
502 }
503 Py_XDECREF(collections);
504 Py_XDECREF(mapping);
505 return bases;
506 }
507
508 bool PyUpb_Map_Init(PyObject* m) {
509 PyUpb_ModuleState* state = PyUpb_ModuleState_GetFromModule(m);
510 PyObject* bases = GetMutableMappingBase();
511 if (!bases) return false;
512
513 state->message_map_container_type =
514 PyUpb_AddClassWithBases(m, &PyUpb_MessageMapContainer_Spec, bases);
515 state->scalar_map_container_type =
516 PyUpb_AddClassWithBases(m, &PyUpb_ScalarMapContainer_Spec, bases);
517 state->map_iterator_type = PyUpb_AddClass(m, &PyUpb_MapIterator_Spec);
518
519 Py_DECREF(bases);
520
521 return state->message_map_container_type &&
522 state->scalar_map_container_type && state->map_iterator_type;
523 }