Skip to content

Commit

Permalink
fix: increase recursion limit (#65)
Browse files Browse the repository at this point in the history
* fix: fix recursion limit

* skip on win 3.10
  • Loading branch information
tlambert03 authored Oct 25, 2024
1 parent 404820f commit 1cb1994
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
40 changes: 32 additions & 8 deletions ilpy/expressions.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
from __future__ import annotations

import ast
from collections.abc import Sequence
import sys
from collections.abc import Iterator, Sequence
from contextlib import contextmanager
from typing import Any, ClassVar, Union

from ilpy.wrapper import Constraint, Objective, Relation, Sense

Number = Union[float, int]


@contextmanager
def recursion_limit_raised_by(N: int = 5000) -> Iterator[None]:
"""Temporarily increase the recursion limit by N."""
old_limit = sys.getrecursionlimit()
sys.setrecursionlimit(old_limit + N)
try:
yield
finally:
sys.setrecursionlimit(old_limit)


class Expression(ast.AST):
"""Base class for all expression nodes.
Expand Down Expand Up @@ -266,13 +279,24 @@ def _get_coeff_indices(
l_coeffs: dict[int, float] = {}
q_coeffs: dict[tuple[int, int], float] = {}
constant = 0.0
for var, coefficient in _get_coefficients(expr).items():
if var is None:
constant = coefficient
elif isinstance(var, tuple):
q_coeffs[(_ensure_index(var[0]), _ensure_index(var[1]))] = coefficient
elif coefficient != 0:
l_coeffs[_ensure_index(var)] = coefficient
try:
with recursion_limit_raised_by(5000):
for var, coefficient in _get_coefficients(expr).items():
if var is None:
constant = coefficient
elif isinstance(var, tuple):
q_coeffs[(_ensure_index(var[0]), _ensure_index(var[1]))] = (
coefficient
)
elif coefficient != 0:
l_coeffs[_ensure_index(var)] = coefficient
except RecursionError as e:
raise RecursionError(
"RecursionError when casting an ilpy.Expression to a Constraint or "
"Objective. If you really want an expression this large, you may raise the "
"limit temporarily with `ilpy.expressions.recursion_limit_raised_by` (or "
"manually with `sys.setrecursionlimit`)"
) from e
return l_coeffs, q_coeffs, constant


Expand Down
19 changes: 19 additions & 0 deletions tests/test_expressions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import operator
import os
import sys

import pytest
from ilpy.expressions import Expression, Variable, _get_coefficients
Expand Down Expand Up @@ -161,3 +163,20 @@ def test_adding() -> None:
solver.set_objective(u)
solver.add_constraint(u >= 0)
solver.set_constraints(constraints)


@pytest.mark.skipif(
sys.version_info < (3, 11) and os.name == "nt",
reason="fails too often on windows 3.10",
)
def test_recursion() -> None:
from ilpy.expressions import recursion_limit_raised_by

reclimit = sys.getrecursionlimit()
s = sum(Variable(str(x), index=x) for x in range(reclimit + 5001))
SOME_MAX = 1000
expr = s <= SOME_MAX
with pytest.raises(RecursionError):
expr.as_constraint()
with recursion_limit_raised_by():
assert isinstance(expr.as_constraint(), Constraint)

0 comments on commit 1cb1994

Please sign in to comment.