Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: increase recursion limit #65

Merged
merged 3 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading