}
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;
+ }
+
PyObject* list = PyList_New(0);
+ if (!list)
+ return NULL;
+
+ // Return the empty list if there are no solutions
+ if (!solutions)
+ return list;
+
+ for (struct pakfire_solution** solution = solutions; *solution; solution++) {
+ PyObject* s = new_solution(*solution);
+ if (!s)
+ goto ERROR;
- struct pakfire_solution* solution = pakfire_problem_get_solutions(self->problem);
- while (solution) {
- PyObject* s = new_solution(solution);
PyList_Append(list, s);
Py_DECREF(s);
-
- solution = pakfire_solution_next(solution);
}
return list;
+
+ERROR:
+ Py_DECREF(list);
+ return NULL;
}
static struct PyMethodDef Problem_methods[] = {
const char* pakfire_problem_to_string(struct pakfire_problem* problem);
struct pakfire_request* pakfire_problem_get_request(struct pakfire_problem* problem);
-struct pakfire_solution* pakfire_problem_get_solutions(struct pakfire_problem* problem);
+int pakfire_problem_get_solutions(struct pakfire_problem* problem,
+ struct pakfire_solution*** solutions);
#ifdef PAKFIRE_PRIVATE
return pakfire_request_ref(problem->request);
}
-PAKFIRE_EXPORT struct pakfire_solution* pakfire_problem_get_solutions(struct pakfire_problem* problem) {
- struct pakfire_solution* s = NULL;
- struct pakfire_solution* ret = NULL;
+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;
- Id solution = 0;
- while ((solution = solver_next_solution(solver, problem->id, solution)) != 0) {
- pakfire_solution_create(&s, problem, solution);
+ // 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;
- if (ret)
- pakfire_solution_append(ret, s);
- else
- ret = s;
+ERROR:
+ if (*solutions) {
+ for (struct pakfire_solution** solution = *solutions; *solution; solution++)
+ pakfire_solution_unref(*solution);
+ free(*solutions);
}
- return ret;
+ return r;
}