def solve(self):
if self.status != SolverStatus.pending:<EOL><INDENT>return self<EOL><DEDENT>scopes = self.scopes[:]<EOL>failure_reason = None<EOL>extractions = {}<EOL>changed_scopes_i = self.changed_scopes_i.copy()<EOL>def _create_phase(status=None):<EOL><INDENT>phase = copy.copy(self)<EOL>phase.scopes = scopes<EOL>phase.failure_reason = failure_reason<EOL>phase.extractions = extractions<EOL>phase.changed_scopes_i = set()<EOL>if status is None:<EOL><INDENT>phase.status = (SolverStatus.solved if phase._is_solved()<EOL>else SolverStatus.exhausted)<EOL><DEDENT>else:<EOL><INDENT>phase.status = status<EOL><DEDENT>return phase<EOL><DEDENT>while True:<EOL><INDENT>prev_num_scopes = len(scopes)<EOL>widened_scopes_i = set()<EOL>while True:<EOL><INDENT>self.pr.subheader("<STR_LIT>")<EOL>extracted_requests = []<EOL>with self.solver.timed(self.solver.extraction_time):<EOL><INDENT>for i in range(len(scopes)):<EOL><INDENT>while True:<EOL><INDENT>scope_, extracted_request = scopes[i].extract()<EOL>if extracted_request:<EOL><INDENT>extracted_requests.append(extracted_request)<EOL>k = (scopes[i].package_name, extracted_request.name)<EOL>extractions[k] = extracted_request<EOL>self.solver.extractions_count += <NUM_LIT:1><EOL>scopes[i] = scope_<EOL><DEDENT>else:<EOL><INDENT>break<EOL><DEDENT><DEDENT><DEDENT><DEDENT>if not extracted_requests:<EOL><INDENT>break<EOL><DEDENT>self.pr.subheader("<STR_LIT>")<EOL>extracted_requests = RequirementList(extracted_requests)<EOL>if extracted_requests.conflict:  <EOL><INDENT>req1, req2 = extracted_requests.conflict<EOL>conflict = DependencyConflict(req1, req2)<EOL>failure_reason = DependencyConflicts([conflict])<EOL>return _create_phase(SolverStatus.failed)<EOL><DEDENT>elif self.pr:<EOL><INDENT>self.pr("<STR_LIT>", extracted_requests)<EOL><DEDENT>self.pr.subheader("<STR_LIT>")<EOL>req_fams = []<EOL>with self.solver.timed(self.solver.intersection_test_time):<EOL><INDENT>for i, scope in enumerate(scopes):<EOL><INDENT>extracted_req = extracted_requests.get(scope.package_name)<EOL>if extracted_req is None:<EOL><INDENT>continue<EOL><DEDENT>scope_ = scope.intersect(extracted_req.range)<EOL>req_fams.append(extracted_req.name)<EOL>if scope_ is None:<EOL><INDENT>conflict = DependencyConflict(<EOL>extracted_req, scope.package_request)<EOL>failure_reason = DependencyConflicts([conflict])<EOL>return _create_phase(SolverStatus.failed)<EOL><DEDENT>if scope_ is not scope:<EOL><INDENT>scopes[i] = scope_<EOL>changed_scopes_i.add(i)<EOL>self.solver.intersections_count += <NUM_LIT:1><EOL>if scope.is_conflict and not scope_.is_conflict:<EOL><INDENT>widened_scopes_i.add(i)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>new_extracted_reqs = [<EOL>x for x in extracted_requests.requirements<EOL>if x.name not in req_fams]<EOL>if new_extracted_reqs:<EOL><INDENT>self.pr.subheader("<STR_LIT>")<EOL>for req in new_extracted_reqs:<EOL><INDENT>scope = _PackageScope(req, solver=self.solver)<EOL>scopes.append(scope)<EOL>if self.pr:<EOL><INDENT>self.pr("<STR_LIT>", scope)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>num_scopes = len(scopes)<EOL>if (num_scopes == prev_num_scopes)and not changed_scopes_iand not widened_scopes_i:<EOL><INDENT>break<EOL><DEDENT>self.pr.subheader("<STR_LIT>")<EOL>if not self.solver.optimised:<EOL><INDENT>changed_scopes_i = set(range(num_scopes))<EOL>prev_num_scopes = num_scopes<EOL><DEDENT>pending_reducts = SortedSet()<EOL>all_scopes_i = list(range(num_scopes))<EOL>added_scopes_i = list(range(prev_num_scopes, num_scopes))<EOL>for x in range(prev_num_scopes):<EOL><INDENT>for y in changed_scopes_i:<EOL><INDENT>if x != y:<EOL><INDENT>pending_reducts.add((x, y))<EOL><DEDENT><DEDENT>for y in added_scopes_i:<EOL><INDENT>pending_reducts.add((x, y))<EOL><DEDENT><DEDENT>for x in added_scopes_i:<EOL><INDENT>for y in all_scopes_i:<EOL><INDENT>if x != y:<EOL><INDENT>pending_reducts.add((x, y))<EOL><DEDENT><DEDENT><DEDENT>for x in widened_scopes_i:<EOL><INDENT>for y in all_scopes_i:<EOL><INDENT>if x != y:<EOL><INDENT>pending_reducts.add((x, y))<EOL><DEDENT><DEDENT><DEDENT>with self.solver.timed(self.solver.reduction_test_time):<EOL><INDENT>while pending_reducts:<EOL><INDENT>x, y = pending_reducts.pop()<EOL>new_scope, reductions = scopes[x].reduce_by(<EOL>scopes[y].package_request)<EOL>if new_scope is None:<EOL><INDENT>failure_reason = TotalReduction(reductions)<EOL>return _create_phase(SolverStatus.failed)<EOL><DEDENT>elif new_scope is not scopes[x]:<EOL><INDENT>scopes[x] = new_scope<EOL>for j in all_scopes_i:<EOL><INDENT>if j != x:<EOL><INDENT>pending_reducts.add((j, x))<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>changed_scopes_i = set()<EOL><DEDENT>return _create_phase()<EOL>
Attempt to solve the phase.