From d8915b3174b43c60f08a6aad3f65ca7153d0ce0c Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Sun, 9 Jan 2022 23:04:44 -0800 Subject: [PATCH] Cleaned up IsInitialized(). --- python/extension_dict.c | 6 +----- python/message.c | 27 +++++++++++++++------------ 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/python/extension_dict.c b/python/extension_dict.c index d466ba9791..0d3da1e7b8 100644 --- a/python/extension_dict.c +++ b/python/extension_dict.c @@ -175,6 +175,7 @@ static PyObject* PyUpb_ExtensionIterator_New(PyObject* _ext_dict) { PyUpb_ModuleState* state = PyUpb_ModuleState_Get(); PyUpb_ExtensionIterator* iter = (void*)PyType_GenericAlloc(state->extension_iterator_type, 0); + if (!iter) return NULL; iter->msg = ext_dict->msg; iter->iter = UPB_MSG_BEGIN; Py_INCREF(iter->msg); @@ -189,11 +190,6 @@ static void PyUpb_ExtensionIterator_Dealloc(void* _self) { PyObject* PyUpb_ExtensionIterator_IterNext(PyObject* _self) { PyUpb_ExtensionIterator* self = (PyUpb_ExtensionIterator*)_self; - /* - if (self->version != self->map->version) { - return PyErr_Format(PyExc_RuntimeError, "Map modified during iteration."); - } - */ upb_msg* msg = PyUpb_CMessage_GetIfReified(self->msg); if (!msg) return NULL; const upb_msgdef* m = PyUpb_CMessage_GetMsgdef(self->msg); diff --git a/python/message.c b/python/message.c index 19cc862b48..ba83c8c095 100644 --- a/python/message.c +++ b/python/message.c @@ -931,6 +931,16 @@ static PyObject* PyUpb_CMessage_HasField(PyObject* _self, PyObject* arg) { static PyObject* PyUpb_CMessage_FindInitializationErrors(PyObject* _self, PyObject* arg); +static PyObject* PyUpb_CMessage_IsInitializedAppendErrors(PyObject* _self, + PyObject* errors) { + PyObject* list = PyUpb_CMessage_FindInitializationErrors(_self, NULL); + if (!list) return NULL; + bool ret = PyList_Size(list) == 0; + if (!ret) PyObject_CallMethod(errors, "extend", "O", list); + Py_XDECREF(list); + return PyBool_FromLong(ret); +} + static PyObject* PyUpb_CMessage_IsInitialized(PyObject* _self, PyObject* args) { PyObject* errors = NULL; if (!PyArg_ParseTuple(args, "|O", &errors)) { @@ -939,19 +949,12 @@ static PyObject* PyUpb_CMessage_IsInitialized(PyObject* _self, PyObject* args) { upb_msg* msg = PyUpb_CMessage_GetIfReified(_self); if (!msg) Py_RETURN_NONE; // TODO if (errors) { - PyObject* list = PyUpb_CMessage_FindInitializationErrors(_self, NULL); - if (!list) return NULL; - if (PyList_Size(list) == 0) { - return PyBool_FromLong(true); - } - PyObject* extend_name = PyUnicode_FromString("extend"); - if (extend_name == NULL) { - Py_DECREF(list); - return NULL; - } - PyObject_CallMethodObjArgs(errors, extend_name, list, NULL); - return PyBool_FromLong(false); + // We need to collect a list of unset required fields and append it to + // `errors`. + return PyUpb_CMessage_IsInitializedAppendErrors(_self, errors); } else { + // We just need to return a boolean "true" or "false" for whether all + // required fields are set. const upb_msgdef* m = PyUpb_CMessage_GetMsgdef(_self); const upb_symtab* symtab = upb_filedef_symtab(upb_msgdef_file(m)); bool initialized = !upb_util_HasUnsetRequired(msg, m, symtab, NULL);