@@ -195,19 +195,19 @@ static PyObject* PyUpb_MapContainer_Subscript(PyObject* _self, PyObject* key) {
195195 return PyUpb_UpbToPy (u_val , val_f , self -> arena );
196196}
197197
198- static PyObject * PyUpb_MapContainer_Contains (PyObject * _self , PyObject * key ) {
198+ static int PyUpb_MapContainer_Contains (PyObject * _self , PyObject * key ) {
199199 PyUpb_MapContainer * self = (PyUpb_MapContainer * )_self ;
200200 upb_Map * map = PyUpb_MapContainer_GetIfReified (self );
201- if (!map ) Py_RETURN_FALSE ;
201+ if (!map ) return 0 ;
202202 const upb_FieldDef * f = PyUpb_MapContainer_GetField (self );
203203 const upb_MessageDef * entry_m = upb_FieldDef_MessageSubDef (f );
204204 const upb_FieldDef * key_f = upb_MessageDef_Field (entry_m , 0 );
205205 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 ;
207207 if (upb_Map_Get (map , u_key , NULL )) {
208- Py_RETURN_TRUE ;
208+ return 1 ;
209209 } else {
210- Py_RETURN_FALSE ;
210+ return 0 ;
211211 }
212212}
213213
@@ -339,8 +339,6 @@ PyObject* PyUpb_MapContainer_GetOrCreateWrapper(upb_Map* map,
339339// -----------------------------------------------------------------------------
340340
341341static PyMethodDef PyUpb_ScalarMapContainer_Methods [] = {
342- {"__contains__" , PyUpb_MapContainer_Contains , METH_O ,
343- "Tests whether a key is a member of the map." },
344342 {"clear" , PyUpb_MapContainer_Clear , METH_NOARGS ,
345343 "Removes all elements from the map." },
346344 {"get" , (PyCFunction )PyUpb_MapContainer_Get , METH_VARARGS | METH_KEYWORDS ,
@@ -363,6 +361,7 @@ static PyType_Slot PyUpb_ScalarMapContainer_Slots[] = {
363361 {Py_mp_length , PyUpb_MapContainer_Length },
364362 {Py_mp_subscript , PyUpb_MapContainer_Subscript },
365363 {Py_mp_ass_subscript , PyUpb_MapContainer_AssignSubscript },
364+ {Py_sq_contains , PyUpb_MapContainer_Contains },
366365 {Py_tp_methods , PyUpb_ScalarMapContainer_Methods },
367366 {Py_tp_iter , PyUpb_MapIterator_New },
368367 {Py_tp_repr , PyUpb_MapContainer_Repr },
@@ -382,8 +381,6 @@ static PyType_Spec PyUpb_ScalarMapContainer_Spec = {
382381// -----------------------------------------------------------------------------
383382
384383static PyMethodDef PyUpb_MessageMapContainer_Methods [] = {
385- {"__contains__" , PyUpb_MapContainer_Contains , METH_O ,
386- "Tests whether the map contains this element." },
387384 {"clear" , PyUpb_MapContainer_Clear , METH_NOARGS ,
388385 "Removes all elements from the map." },
389386 {"get" , (PyCFunction )PyUpb_MapContainer_Get , METH_VARARGS | METH_KEYWORDS ,
@@ -408,6 +405,7 @@ static PyType_Slot PyUpb_MessageMapContainer_Slots[] = {
408405 {Py_mp_length , PyUpb_MapContainer_Length },
409406 {Py_mp_subscript , PyUpb_MapContainer_Subscript },
410407 {Py_mp_ass_subscript , PyUpb_MapContainer_AssignSubscript },
408+ {Py_sq_contains , PyUpb_MapContainer_Contains },
411409 {Py_tp_methods , PyUpb_MessageMapContainer_Methods },
412410 {Py_tp_iter , PyUpb_MapIterator_New },
413411 {Py_tp_repr , PyUpb_MapContainer_Repr },
@@ -477,28 +475,38 @@ static PyType_Spec PyUpb_MapIterator_Spec = {
477475static PyObject * GetMutableMappingBase (void ) {
478476 PyObject * collections = NULL ;
479477 PyObject * mapping = NULL ;
480- PyObject * bases = NULL ;
478+ PyObject * base = NULL ;
481479 if ((collections = PyImport_ImportModule ("collections.abc" )) &&
482480 (mapping = PyObject_GetAttrString (collections , "MutableMapping" ))) {
483- bases = Py_BuildValue ("(O) " , mapping );
481+ base = Py_BuildValue ("O " , mapping );
484482 }
485483 Py_XDECREF (collections );
486484 Py_XDECREF (mapping );
487- return bases ;
485+ return base ;
488486}
489487
490488bool PyUpb_Map_Init (PyObject * m ) {
491489 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 };
494495
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+ }
499506 state -> map_iterator_type = PyUpb_AddClass (m , & PyUpb_MapIterator_Spec );
500507
501- Py_DECREF (bases );
508+ Py_DECREF (base );
509+ Py_DECREF (methods );
502510
503511 return state -> message_map_container_type &&
504512 state -> scalar_map_container_type && state -> map_iterator_type ;
0 commit comments