Implemented the behavior of ignoring duplicate added filenames.
diff --git a/python/descriptor_pool.c b/python/descriptor_pool.c index 26ab7e6..3ee6946 100644 --- a/python/descriptor_pool.c +++ b/python/descriptor_pool.c
@@ -27,10 +27,13 @@ #include "python/descriptor_pool.h" +#include "python/convert.h" #include "python/descriptor.h" #include "python/message.h" #include "python/protobuf.h" #include "upb/def.h" +#include "upb/util/def_to_proto.h" +#include "google/protobuf/descriptor.upbdefs.h" // ----------------------------------------------------------------------------- // DescriptorPool @@ -47,6 +50,14 @@ return s->default_pool; } +const upb_msgdef* PyUpb_DescriptorPool_GetFileProtoDef(void) { + PyUpb_ModuleState* s = PyUpb_ModuleState_Get(); + if (!s->c_descriptor_symtab) { + s->c_descriptor_symtab = upb_symtab_new(); + } + return google_protobuf_FileDescriptorProto_getmsgdef(s->c_descriptor_symtab); +} + static PyObject* PyUpb_DescriptorPool_DoCreateWithCache( PyTypeObject* type, PyObject* db, PyUpb_WeakMap* obj_cache) { PyUpb_DescriptorPool* pool = (void*)PyType_GenericAlloc(type, 0); @@ -146,6 +157,27 @@ goto done; } + upb_strview name = google_protobuf_FileDescriptorProto_name(proto); + const upb_filedef* file = + upb_symtab_lookupfile2(self->symtab, name.data, name.size); + + if (file) { + // If the existing file is equal to the new file, then silently ignore the + // duplicate add. + google_protobuf_FileDescriptorProto* existing = + upb_FileDef_ToProto(file, arena); + if (!existing) { + PyErr_SetNone(PyExc_MemoryError); + goto done; + } + const upb_msgdef* m = PyUpb_DescriptorPool_GetFileProtoDef(); + if (PyUpb_Message_IsEqual(proto, existing, m)) { + Py_INCREF(Py_None); + result = Py_None; + goto done; + } + } + upb_status status; upb_status_clear(&status);