From: Michael Tremer Date: Fri, 19 Aug 2022 13:58:04 +0000 (+0000) Subject: python: Add function to generate list from filelist X-Git-Tag: 0.9.28~423 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=907c00a16a238973c000477d9a8b23f99ac945a2;p=pakfire.git python: Add function to generate list from filelist Signed-off-by: Michael Tremer --- diff --git a/src/_pakfire/archive.c b/src/_pakfire/archive.c index 058a32975..c0d5e7109 100644 --- a/src/_pakfire/archive.c +++ b/src/_pakfire/archive.c @@ -21,15 +21,16 @@ #include #include +#include #include #include #include #include "archive.h" #include "errors.h" -#include "file.h" #include "key.h" #include "package.h" +#include "util.h" PyObject* new_archive(PyTypeObject* type, struct pakfire_archive* archive) { ArchiveObject* self = (ArchiveObject *)type->tp_alloc(type, 0); @@ -204,8 +205,6 @@ static PyObject* Archive_get_path(ArchiveObject* self) { } static PyObject* Archive_get_filelist(ArchiveObject* self) { - int r; - // Fetch the filelist struct pakfire_filelist* filelist = pakfire_archive_get_filelist(self->archive); if (!filelist) { @@ -213,48 +212,13 @@ static PyObject* Archive_get_filelist(ArchiveObject* self) { return NULL; } - const size_t size = pakfire_filelist_size(filelist); - - // Create a new Python list - PyObject* list = PyList_New(0); - if (!list) - goto ERROR; - - for (unsigned int i = 0; i < size; i++) { - struct pakfire_file* file = pakfire_filelist_get(filelist, i); - if (!file) - goto ERROR; - - // Create a new File object - PyObject* obj = new_file(file); - if (!obj) - goto ERROR; - - // Append the new object to the list - r = PyList_Append(list, obj); - Py_DECREF(obj); - - // If we could not append to the list, we will break - if (r) - goto ERROR; - - // Free file - pakfire_file_unref(file); - } - - goto OUT; + // Convert to filelist + PyObject* _filelist = PyList_FromFileList(filelist); -ERROR: - if (list) { - Py_DECREF(list); - list = NULL; - } - -OUT: - if (filelist) - pakfire_filelist_unref(filelist); + // Cleanup + pakfire_filelist_unref(filelist); - return list; + return _filelist; } static struct PyMethodDef Archive_methods[] = { diff --git a/src/_pakfire/util.c b/src/_pakfire/util.c index 7e24e6e06..e269cf09a 100644 --- a/src/_pakfire/util.c +++ b/src/_pakfire/util.c @@ -24,9 +24,11 @@ #include #include +#include #include #include +#include "file.h" #include "package.h" #include "util.h" @@ -98,3 +100,44 @@ PyObject* PyList_FromPackageList(struct pakfire_packagelist* packagelist) { return list; } + +PyObject* PyList_FromFileList(struct pakfire_filelist* filelist) { + struct pakfire_file* file = NULL; + PyObject* f = NULL; + int r; + + PyObject* list = PyList_New(0); + if (!list) + goto ERROR; + + // Fetch size + const size_t size = pakfire_filelist_size(filelist); + + for (unsigned int i = 0; i < size; i++) { + file = pakfire_filelist_get(filelist, i); + if (!file) + goto ERROR; + + // Create a new File object + f = new_file(file); + if (!f) + goto ERROR; + + // Append the new object to the list + r = PyList_Append(list, f); + Py_DECREF(f); + + // If we could not append to the list, we will break + if (r) + goto ERROR; + + // Free file + pakfire_file_unref(file); + } + + return list; + +ERROR: + Py_XDECREF(list); + return NULL; +} diff --git a/src/_pakfire/util.h b/src/_pakfire/util.h index 7669acf46..a8113cb06 100644 --- a/src/_pakfire/util.h +++ b/src/_pakfire/util.h @@ -23,10 +23,12 @@ #include +#include #include extern PyObject* performance_index(PyObject* self, PyObject* args); PyObject* PyList_FromPackageList(struct pakfire_packagelist* packagelist); +PyObject* PyList_FromFileList(struct pakfire_filelist* filelist); #endif /* PYTHON_PAKFIRE_UTIL_H */