}
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;
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
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 */
pakfire_packagelist_unref;
# problem
- pakfire_problem_get_solutions;
+ pakfire_problem_next_solution;
pakfire_problem_ref;
pakfire_problem_to_string;
pakfire_problem_unref;
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;