@@ -195,19 +195,19 @@ static PyObject* PyUpb_MapContainer_Subscript(PyObject* _self, PyObject* key) {
195
195
return PyUpb_UpbToPy (u_val , val_f , self -> arena );
196
196
}
197
197
198
- static PyObject * PyUpb_MapContainer_Contains (PyObject * _self , PyObject * key ) {
198
+ static int PyUpb_MapContainer_Contains (PyObject * _self , PyObject * key ) {
199
199
PyUpb_MapContainer * self = (PyUpb_MapContainer * )_self ;
200
200
upb_Map * map = PyUpb_MapContainer_GetIfReified (self );
201
- if (!map ) Py_RETURN_FALSE ;
201
+ if (!map ) return 0 ;
202
202
const upb_FieldDef * f = PyUpb_MapContainer_GetField (self );
203
203
const upb_MessageDef * entry_m = upb_FieldDef_MessageSubDef (f );
204
204
const upb_FieldDef * key_f = upb_MessageDef_Field (entry_m , 0 );
205
205
upb_MessageValue u_key ;
206
- if (!PyUpb_PyToUpb (key , key_f , & u_key , NULL )) return NULL ;
206
+ if (!PyUpb_PyToUpb (key , key_f , & u_key , NULL )) return -1 ;
207
207
if (upb_Map_Get (map , u_key , NULL )) {
208
- Py_RETURN_TRUE ;
208
+ return 1 ;
209
209
} else {
210
- Py_RETURN_FALSE ;
210
+ return 0 ;
211
211
}
212
212
}
213
213
@@ -339,8 +339,6 @@ PyObject* PyUpb_MapContainer_GetOrCreateWrapper(upb_Map* map,
339
339
// -----------------------------------------------------------------------------
340
340
341
341
static PyMethodDef PyUpb_ScalarMapContainer_Methods [] = {
342
- {"__contains__" , PyUpb_MapContainer_Contains , METH_O ,
343
- "Tests whether a key is a member of the map." },
344
342
{"clear" , PyUpb_MapContainer_Clear , METH_NOARGS ,
345
343
"Removes all elements from the map." },
346
344
{"get" , (PyCFunction )PyUpb_MapContainer_Get , METH_VARARGS | METH_KEYWORDS ,
@@ -363,6 +361,7 @@ static PyType_Slot PyUpb_ScalarMapContainer_Slots[] = {
363
361
{Py_mp_length , PyUpb_MapContainer_Length },
364
362
{Py_mp_subscript , PyUpb_MapContainer_Subscript },
365
363
{Py_mp_ass_subscript , PyUpb_MapContainer_AssignSubscript },
364
+ {Py_sq_contains , PyUpb_MapContainer_Contains },
366
365
{Py_tp_methods , PyUpb_ScalarMapContainer_Methods },
367
366
{Py_tp_iter , PyUpb_MapIterator_New },
368
367
{Py_tp_repr , PyUpb_MapContainer_Repr },
@@ -382,8 +381,6 @@ static PyType_Spec PyUpb_ScalarMapContainer_Spec = {
382
381
// -----------------------------------------------------------------------------
383
382
384
383
static PyMethodDef PyUpb_MessageMapContainer_Methods [] = {
385
- {"__contains__" , PyUpb_MapContainer_Contains , METH_O ,
386
- "Tests whether the map contains this element." },
387
384
{"clear" , PyUpb_MapContainer_Clear , METH_NOARGS ,
388
385
"Removes all elements from the map." },
389
386
{"get" , (PyCFunction )PyUpb_MapContainer_Get , METH_VARARGS | METH_KEYWORDS ,
@@ -408,6 +405,7 @@ static PyType_Slot PyUpb_MessageMapContainer_Slots[] = {
408
405
{Py_mp_length , PyUpb_MapContainer_Length },
409
406
{Py_mp_subscript , PyUpb_MapContainer_Subscript },
410
407
{Py_mp_ass_subscript , PyUpb_MapContainer_AssignSubscript },
408
+ {Py_sq_contains , PyUpb_MapContainer_Contains },
411
409
{Py_tp_methods , PyUpb_MessageMapContainer_Methods },
412
410
{Py_tp_iter , PyUpb_MapIterator_New },
413
411
{Py_tp_repr , PyUpb_MapContainer_Repr },
@@ -477,28 +475,38 @@ static PyType_Spec PyUpb_MapIterator_Spec = {
477
475
static PyObject * GetMutableMappingBase (void ) {
478
476
PyObject * collections = NULL ;
479
477
PyObject * mapping = NULL ;
480
- PyObject * bases = NULL ;
478
+ PyObject * base = NULL ;
481
479
if ((collections = PyImport_ImportModule ("collections.abc" )) &&
482
480
(mapping = PyObject_GetAttrString (collections , "MutableMapping" ))) {
483
- bases = Py_BuildValue ("(O) " , mapping );
481
+ base = Py_BuildValue ("O " , mapping );
484
482
}
485
483
Py_XDECREF (collections );
486
484
Py_XDECREF (mapping );
487
- return bases ;
485
+ return base ;
488
486
}
489
487
490
488
bool PyUpb_Map_Init (PyObject * m ) {
491
489
PyUpb_ModuleState * state = PyUpb_ModuleState_GetFromModule (m );
492
- PyObject * bases = GetMutableMappingBase ();
493
- if (!bases ) return false;
490
+ PyObject * base = GetMutableMappingBase ();
491
+ if (!base ) return false;
492
+
493
+ const char * methods [] = {"keys" , "items" , "values" , "__eq__" , "__ne__" ,
494
+ "pop" , "popitem" , "update" , "setdefault" , NULL };
494
495
495
- state -> message_map_container_type =
496
- PyUpb_AddClassWithBases (m , & PyUpb_MessageMapContainer_Spec , bases );
497
- state -> scalar_map_container_type =
498
- PyUpb_AddClassWithBases (m , & PyUpb_ScalarMapContainer_Spec , bases );
496
+ state -> message_map_container_type = PyUpb_AddClassWithRegister (
497
+ m , & PyUpb_MessageMapContainer_Spec , base , methods );
498
+ if (!state -> message_map_container_type ) {
499
+ return false;
500
+ }
501
+ state -> scalar_map_container_type = PyUpb_AddClassWithRegister (
502
+ m , & PyUpb_ScalarMapContainer_Spec , base , methods );
503
+ if (!state -> scalar_map_container_type ) {
504
+ return false;
505
+ }
499
506
state -> map_iterator_type = PyUpb_AddClass (m , & PyUpb_MapIterator_Spec );
500
507
501
- Py_DECREF (bases );
508
+ Py_DECREF (base );
509
+ Py_DECREF (methods );
502
510
503
511
return state -> message_map_container_type &&
504
512
state -> scalar_map_container_type && state -> map_iterator_type ;
0 commit comments