diff --git a/python/repeated.c b/python/repeated.c index b063fcf15c..2c83080bd3 100644 --- a/python/repeated.c +++ b/python/repeated.c @@ -168,20 +168,20 @@ PyObject* PyUpb_RepeatedContainer_NewUnset(PyObject* parent, return &repeated->ob_base; } -PyObject* PyUpb_RepeatedContainer_GetOrCreateWrapper(upb_array* u_arr, +PyObject* PyUpb_RepeatedContainer_GetOrCreateWrapper(upb_array* arr, const upb_fielddef* f, PyObject* arena) { - PyObject* ret = PyUpb_ObjCache_Get(u_arr); + PyObject* ret = PyUpb_ObjCache_Get(arr); if (ret) return ret; PyTypeObject* cls = PyUpb_RepeatedContainer_GetClass(f); PyUpb_RepeatedContainer* repeated = (void*)PyType_GenericAlloc(cls, 0); repeated->arena = arena; repeated->field = (uintptr_t)PyUpb_FieldDescriptor_Get(f); - repeated->ptr.arr = u_arr; + repeated->ptr.arr = arr; ret = &repeated->ob_base; Py_INCREF(arena); - PyUpb_ObjCache_Add(u_arr, ret); + PyUpb_ObjCache_Add(arr, ret); return ret; } @@ -241,19 +241,12 @@ PyObject* PyUpb_RepeatedContainer_ToList(PyObject* _self) { const upb_fielddef* f = PyUpb_RepeatedContainer_GetField(self); size_t n = upb_array_size(arr); PyObject* list = PyList_New(n); - PyObject* ret = list; - if (!list) return NULL; for (size_t i = 0; i < n; i++) { PyObject* val = PyUpb_UpbToPy(upb_array_get(arr, i), f, self->arena); - if (!val) { - val = Py_None; - Py_INCREF(val); - ret = NULL; - } + if (!val) return NULL; PyList_SetItem(list, i, val); } - if (ret != list) Py_DECREF(list); - return ret; + return list; } static PyObject* PyUpb_RepeatedContainer_Repr(PyObject* _self) { @@ -296,19 +289,13 @@ static PyObject* PyUpb_RepeatedContainer_Subscript(PyObject* _self, return PyUpb_UpbToPy(upb_array_get(arr, idx), f, self->arena); } else { PyObject* list = PyList_New(count); - PyObject* ret = list; for (Py_ssize_t i = 0; i < count; i++, idx += step) { upb_msgval msgval = upb_array_get(self->ptr.arr, idx); PyObject* item = PyUpb_UpbToPy(msgval, f, self->arena); - if (!item) { - item = Py_None; - Py_INCREF(Py_None); - ret = NULL; - } + if (!item) return NULL; PyList_SetItem(list, i, item); } - if (ret != list) Py_DECREF(list); - return ret; + return list; } } diff --git a/python/repeated.h b/python/repeated.h index 9a364329ca..5e5f6f94ba 100644 --- a/python/repeated.h +++ b/python/repeated.h @@ -39,11 +39,11 @@ PyObject* PyUpb_RepeatedContainer_NewUnset(PyObject* parent, const upb_fielddef* f, PyObject* arena); -// Returns a repeated field object wrapping `u_arr`, of field type `f`, which +// Returns a repeated field object wrapping `arr`, of field type `f`, which // must be on `arena`. If an existing wrapper object exists, it will be // returned, otherwise a new object will be created. The caller always owns a // ref on the returned value. -PyObject* PyUpb_RepeatedContainer_GetOrCreateWrapper(upb_array* u_arr, +PyObject* PyUpb_RepeatedContainer_GetOrCreateWrapper(upb_array* arr, const upb_fielddef* f, PyObject* arena); @@ -51,7 +51,12 @@ PyObject* PyUpb_RepeatedContainer_GetOrCreateWrapper(upb_array* u_arr, // data being pointed to. void PyUpb_RepeatedContainer_Reify(PyObject* self, upb_array* arr); +// Implements repeated_field.extend(iterable). `_self` must be a repeated +// field (either repeated composite or repeated scalar). PyObject* PyUpb_RepeatedContainer_Extend(PyObject* _self, PyObject* value); + +// Implements repeated_field.add(initial_values). `_self` must be a repeated +// composite field. PyObject* PyUpb_RepeatedCompositeContainer_Add(PyObject* _self, PyObject* args, PyObject* kwargs);