From 772aa1a8568b193cbc3563998ce61dc0edde6b04 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Tue, 1 Nov 2022 14:28:32 +0000 Subject: [PATCH] problems: Drop pakfire_problem_get_solutions in favour of *_next_solution Signed-off-by: Michael Tremer --- src/_pakfire/problem.c | 40 ++++++++++++-------- src/libpakfire/include/pakfire/problem.h | 8 ++-- src/libpakfire/libpakfire.sym | 2 +- src/libpakfire/problem.c | 48 +----------------------- 4 files changed, 29 insertions(+), 69 deletions(-) diff --git a/src/_pakfire/problem.c b/src/_pakfire/problem.c index 66a123a48..15a0f2bb1 100644 --- a/src/_pakfire/problem.c +++ b/src/_pakfire/problem.c @@ -82,30 +82,38 @@ static PyObject* Problem_string(ProblemObject* self) { } static PyObject* Problem_get_solutions(ProblemObject* self) { - struct pakfire_solution** solutions = NULL; - - // Fetch all solutions - int r = pakfire_problem_get_solutions(self->problem, &solutions); - if (r) { - PyErr_SetFromErrno(PyExc_OSError); - return NULL; - } + struct pakfire_solution* solution = NULL; + PyObject* solution_obj = NULL; + int r; PyObject* list = PyList_New(0); if (!list) return NULL; - // Return the empty list if there are no solutions - if (!solutions) - return list; + for (;;) { + r = pakfire_problem_next_solution(self->problem, &solution); + if (r) + goto ERROR; + + // No more solutions + if (!solution) + break; + + // Create a new Solution object + solution_obj = new_solution(solution); + if (!solution_obj) { + pakfire_solution_unref(solution); + goto ERROR; + } - for (struct pakfire_solution** solution = solutions; *solution; solution++) { - PyObject* s = new_solution(*solution); - if (!s) + // Append the solution to the list + r = PyList_Append(list, solution_obj); + if (r) { + pakfire_solution_unref(solution); goto ERROR; + } - PyList_Append(list, s); - Py_DECREF(s); + Py_DECREF(solution_obj); } return list; diff --git a/src/libpakfire/include/pakfire/problem.h b/src/libpakfire/include/pakfire/problem.h index 046d0b1e4..b9f7e6844 100644 --- a/src/libpakfire/include/pakfire/problem.h +++ b/src/libpakfire/include/pakfire/problem.h @@ -31,8 +31,9 @@ struct pakfire_problem* pakfire_problem_unref(struct pakfire_problem* problem); const char* pakfire_problem_to_string(struct pakfire_problem* problem); struct pakfire_request* pakfire_problem_get_request(struct pakfire_problem* problem); -int pakfire_problem_get_solutions(struct pakfire_problem* problem, - struct pakfire_solution*** solutions); + +int pakfire_problem_next_solution( + struct pakfire_problem* problem, struct pakfire_solution** solution); #ifdef PAKFIRE_PRIVATE @@ -44,9 +45,6 @@ int pakfire_problem_create(struct pakfire_problem** problem, struct pakfire* pak struct pakfire* pakfire_problem_get_pakfire(struct pakfire_problem* problem); Id pakfire_problem_get_id(struct pakfire_problem* problem); -int pakfire_problem_next_solution( - struct pakfire_problem* problem, struct pakfire_solution** solution); - #endif #endif /* PAKFIRE_PROBLEM_H */ diff --git a/src/libpakfire/libpakfire.sym b/src/libpakfire/libpakfire.sym index 757db5115..513399571 100644 --- a/src/libpakfire/libpakfire.sym +++ b/src/libpakfire/libpakfire.sym @@ -198,7 +198,7 @@ global: pakfire_packagelist_unref; # problem - pakfire_problem_get_solutions; + pakfire_problem_next_solution; pakfire_problem_ref; pakfire_problem_to_string; pakfire_problem_unref; diff --git a/src/libpakfire/problem.c b/src/libpakfire/problem.c index 4bc7ebffb..67d403f1e 100644 --- a/src/libpakfire/problem.c +++ b/src/libpakfire/problem.c @@ -246,53 +246,7 @@ PAKFIRE_EXPORT struct pakfire_request* pakfire_problem_get_request(struct pakfir return pakfire_request_ref(problem->request); } -PAKFIRE_EXPORT int pakfire_problem_get_solutions(struct pakfire_problem* problem, - struct pakfire_solution*** solutions) { - Solver* solver = pakfire_request_get_solver(problem->request); - int r; - - // How many solutions are there? - unsigned int count = solver_solution_count(solver, problem->id); - if (!count) { - *solutions = NULL; - return 0; - } - - // Allocate a solutions array - *solutions = calloc(count + 1, sizeof(**solutions)); - if (!*solutions) - return 1; - - struct pakfire_solution* solution = NULL; - - Id s = 0; - for (unsigned int i = 0; i < count; i++) { - s = solver_next_solution(solver, problem->id, s); - if (!s) - break; - - // Create a new solution object - r = pakfire_solution_create(&solution, problem, s); - if (r) - goto ERROR; - - // Append solution to array - (*solutions)[i] = solution; - } - - return 0; - -ERROR: - if (*solutions) { - for (struct pakfire_solution** _solution = *solutions; *_solution; _solution++) - pakfire_solution_unref(*_solution); - free(*solutions); - } - - return r; -} - -int pakfire_problem_next_solution( +PAKFIRE_EXPORT int pakfire_problem_next_solution( struct pakfire_problem* problem, struct pakfire_solution** solution) { Solver* solver = pakfire_request_get_solver(problem->request); Id id = 0; -- 2.39.5