Cleaned up IsInitialized().

pull/13171/head
Joshua Haberman 3 years ago
parent cfeb1ad685
commit d8915b3174
  1. 6
      python/extension_dict.c
  2. 27
      python/message.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);

@ -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);

Loading…
Cancel
Save