Skip to content
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
8 changes: 4 additions & 4 deletions mathics/builtin/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ class RecursionLimit(Predefined):

Calculations terminated by '\$RecursionLimit' return '\$Aborted':
>> a = a + a
: Recursion depth of 200 exceeded.
: Recursion depth of 1024 exceeded.
= $Aborted
>> $RecursionLimit
= 200
= 1024

>> $RecursionLimit = x;
: Cannot set $RecursionLimit to x; value must be an integer between 20 and 512; use the MATHICS_MAX_RECURSION_DEPTH environment variable to allow higher limits.
: Cannot set $RecursionLimit to x; value must be an integer between 20 and 1024; use the MATHICS_MAX_RECURSION_DEPTH environment variable to allow higher limits.

>> $RecursionLimit = 512
= 512
Expand All @@ -47,7 +47,7 @@ class RecursionLimit(Predefined):
"""

name = "$RecursionLimit"
value = 200
value = 1024

set_python_recursion_limit(value)

Expand Down
8 changes: 7 additions & 1 deletion mathics/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from mathics.core.atoms import String
from mathics.core.definitions import Definitions
from mathics.core.evaluation import Evaluation, Result
from mathics.core.evaluation import Evaluation, Result, set_python_recursion_limit
from mathics.core.parser import MathicsSingleLineFeeder, parse
from mathics.core.symbols import SymbolNull

Expand Down Expand Up @@ -207,6 +207,12 @@ def reset(self, add_builtin=True, catch_interrupt=False):
definitions.set_line_no(1)
self.definitions = definitions

# An assignment to $RecursionLimit also set Python's limit. The new
# definitions have the default $RecursionLimit, so go back to its limit.
from mathics.builtin.evaluation import RecursionLimit

set_python_recursion_limit(RecursionLimit.value)

def evaluate(self, str_expression, timeout=None, form=None):
"""Parse str_expression and evaluate using the `evaluate` method of the Expression"""
self.evaluation.out.clear()
Expand Down
26 changes: 13 additions & 13 deletions mathics/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ def get_srcdir():

# Maximum recursion depth is safe for all Python environments
# without setting a custom thread stack size.
DEFAULT_MAX_RECURSION_DEPTH = 512
DEFAULT_MAX_RECURSION_DEPTH = 1024

# How deep $RecursionLimit = Infinity, as in Block[{$RecursionLimit = Infinity}, ...],
# lets an evaluation go. It is not bounded by the 512 above because an
# lets an evaluation go. It is not bounded by the 1024 above because an
# evaluation runs on a thread with a stack sized for it (see
# mathics.core.evaluation), or on a Python that checks its own C recursion.
DEFAULT_MAX_INFINITE_RECURSION_DEPTH = 100000

# Maximum number of digits allows in a string representation of a string number.
# Maximum number of digits allowed in a string representation of a string number.
# We picked this to be able to handle 1989 ^ 1989.
DEFAULT_MAX_STR_DIGITS = 7000
str_digits: str = os.environ.get("MATHICS_MAX_STR_DIGITS", str(DEFAULT_MAX_STR_DIGITS))
Expand All @@ -52,7 +52,7 @@ def get_srcdir():
TIMEOUT = None

# max pickle.dumps() size for storing results in DB
# historically 10000 was used on public mathics servers
# Historically, 10000 was used on public mathics servers
MAX_STORED_SIZE = 10000

ROOT_DIR = osp.dirname(__file__)
Expand All @@ -67,23 +67,23 @@ def get_srcdir():
# In contrast to ROOT_DIR, LOCAL_ROOT_DIR is used in building
# LaTeX documentation. When Mathics3 is installed, we don't want LaTeX file documentation.tex
# to get put in the installation directory, but instead we build documentation
# from checked-out source and that is where this should be put.
# from checked-out source, and that is where this should be put.
LOCAL_ROOT_DIR = get_srcdir()

# Location of doctests and test results formatted for LaTeX. This data
# is stoared as a Python Pickle format, but storing this in JSON if
# is stored as a Python Pickle format, but storing this in JSON if
# possible would be preferable and faster

# We need two versions of doctest data, one is in the user space which is updated with
# local packages installed and is user writable.
# We need two versions of doctest data; one is in the user space which is updated with
# local packages installed and is writable by the user.


DOCTEST_LATEX_DATA_PCL = os.environ.get(
"DOCTEST_LATEX_DATA_PCL", osp.join(DATA_DIR, "doctest_latex_data.pcl")
)

# We need another version of doctest data as a fallback, and that is distributed with the
# package. It is note user writable and not in the user space.
# package. It is not writable by the user and is not in the user space.

DOCTEST_SYSTEM_LATEX_DATA_PCL = os.environ.get(
"DOCTEST_SYSTEM_LATEX_DATA_PCL",
Expand All @@ -94,14 +94,14 @@ def get_srcdir():
DOC_LATEX_DIR = osp.join(LOCAL_ROOT_DIR, "doc", "latex")
DOC_LATEX_FILE = osp.join(DOC_LATEX_DIR, "documentation.tex")

# Set this True if you prefer 12 hour time to be the default
# Set this True if you prefer 12-hour time to be the default
TIME_12HOUR = False

# Leave this True unless you have specific reason for not permitting
# Leave this True unless you have a specific reason for not permitting
# users to access local files.
ENABLE_FILES_MODULE = True

# Leave this True unless you have specific reason for not permitting
# Leave this True unless you have a specific reason for not permitting
# users to execute system commands.
# If MATHICS3_SANDBOX environment variable is set, this defaults to False.
ENABLE_SYSTEM_COMMANDS = (
Expand Down Expand Up @@ -141,7 +141,7 @@ def get_doctest_latex_data_path(should_be_readable=False, create_parent=False) -
"""Returns a string path where we can find Python Pickle doctest data for LaTeX
processing.

If `should_be_readable` is True, the we will check to see whether
If `should_be_readable` is True, then we will check to see whether
this file is readable (which also means it exists). If not, we'll
return the `DOCTEST_SYSTEM_DATA_PATH`.

Expand Down
2 changes: 1 addition & 1 deletion test/builtin/assignments/test_assign_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def test_process_assign_other():
for kind, suffix in (
(
"Recursion",
"512; use the MATHICS_MAX_RECURSION_DEPTH environment variable to allow higher limits",
"1024; use the MATHICS_MAX_RECURSION_DEPTH environment variable to allow higher limits",
),
("Iteration", "Infinity"),
):
Expand Down
25 changes: 23 additions & 2 deletions test/builtin/test_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
),
("$RecursionLimit = 20", None, "20", None),
("a = a + a", ("Recursion depth of 20 exceeded.",), "$Aborted", None),
("$RecursionLimit = 200", None, "200", None),
("$RecursionLimit = 1024", None, "1024", None),
(
"ClearAll[f]; f[0] = 0; f[n_] := 1 + f[n - 1]; Block[{$RecursionLimit = Infinity}, f[300]]",
None,
Expand All @@ -35,6 +35,12 @@
"150",
"a Block value is evaluated before the variable is reset",
),
(
"ClearAll[f]; f[n_] := 1 + f[n + 1]; Block[{$RecursionLimit = Infinity}, f[0]]",
("Recursion depth of 100000 exceeded.",),
"$Aborted",
"Infinity goes as deep as the Python stack is set up for, then stops",
),
(
"ClearAll[f]; f[0] = 0; f[n_] := 1 + f[n - 1]; Block[{$RecursionLimit = Infinity}, f[2000]]",
None,
Expand Down Expand Up @@ -163,7 +169,22 @@ def test_python_recursion_error_is_reclim():
assert [m.text for m in result.out] == ["Recursion depth of 512 exceeded."]
finally:
sys.setrecursionlimit(python_limit)
session.evaluate("ClearAll[deep]; $RecursionLimit = 200")
session.evaluate("ClearAll[deep]; $RecursionLimit = 1024")


def test_session_reset_restores_python_recursion_limit():
"""Resetting a session gives the default $RecursionLimit back, and with it
the Python recursion limit that goes with it."""
from test.helper import reset_session, session

from mathics.core.evaluation import python_recursion_depth

default_limit = session.evaluate("$RecursionLimit").value
session.evaluate("$RecursionLimit = 100")
assert sys.getrecursionlimit() == python_recursion_depth(100)
reset_session()
assert session.evaluate("$RecursionLimit").value == default_limit
assert sys.getrecursionlimit() == python_recursion_depth(default_limit)


@pytest.mark.parametrize(
Expand Down
Loading