diff --git a/mathics/builtin/evaluation.py b/mathics/builtin/evaluation.py index 9b15cf08f..2b65c24f4 100644 --- a/mathics/builtin/evaluation.py +++ b/mathics/builtin/evaluation.py @@ -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 @@ -47,7 +47,7 @@ class RecursionLimit(Predefined): """ name = "$RecursionLimit" - value = 200 + value = 1024 set_python_recursion_limit(value) diff --git a/mathics/session.py b/mathics/session.py index e54c09b5f..18c77d8f2 100644 --- a/mathics/session.py +++ b/mathics/session.py @@ -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 @@ -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() diff --git a/mathics/settings.py b/mathics/settings.py index 8ea3f7f78..20c176820 100644 --- a/mathics/settings.py +++ b/mathics/settings.py @@ -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)) @@ -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__) @@ -67,15 +67,15 @@ 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( @@ -83,7 +83,7 @@ def get_srcdir(): ) # 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", @@ -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 = ( @@ -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`. diff --git a/test/builtin/assignments/test_assign_messages.py b/test/builtin/assignments/test_assign_messages.py index 17b635682..5ed28baa4 100644 --- a/test/builtin/assignments/test_assign_messages.py +++ b/test/builtin/assignments/test_assign_messages.py @@ -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"), ): diff --git a/test/builtin/test_evaluation.py b/test/builtin/test_evaluation.py index 20207f618..8f9c32872 100644 --- a/test/builtin/test_evaluation.py +++ b/test/builtin/test_evaluation.py @@ -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, @@ -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, @@ -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(