From 58b26ad849701068ddcb8496202b152eba7b1806 Mon Sep 17 00:00:00 2001 From: Stanislav Date: Fri, 16 Oct 2026 02:57:26 +0400 Subject: [PATCH 1/2] Get: go on reading the file after a syntax error, as in WMA eval_Get returned at the first syntax error, so everything after it in the file was silently skipped. WMA documents: "Get continues attempting to read a file even after a syntax error has been detected." Report the error and continue with the next expression; stop only if the parser made no progress. Co-Authored-By: Claude Opus 5.5 --- mathics/eval/files_io/files.py | 7 ++++++- test/builtin/files_io/test_files.py | 12 ++++++++++++ test/data/syntax-error-bug.m | 5 +++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 test/data/syntax-error-bug.m diff --git a/mathics/eval/files_io/files.py b/mathics/eval/files_io/files.py index 03fb2d6c50..2aca568740 100644 --- a/mathics/eval/files_io/files.py +++ b/mathics/eval/files_io/files.py @@ -206,12 +206,17 @@ def eval_Get( with Mathics3Open(resolved_path, "r", encoding=encoding) as f: feeder = MathicsFileLineFeeder(f, trace_fn) while not feeder.empty(): + lineno = feeder.lineno try: # Note: we use mathics.core.parser.parse # so that tracing/debugging can intercept parse() query = mathics.core.parser.parse(definitions, feeder) except SyntaxError: - return SymbolNull + # As in WMA, report the error and go on reading the + # rest of the file, unless no input was consumed. + if feeder.lineno == lineno: + return SymbolNull + continue finally: feeder.send_messages(evaluation) if query is None: # blank line / comment diff --git a/test/builtin/files_io/test_files.py b/test/builtin/files_io/test_files.py index 25eb9a5660..e499581bb3 100644 --- a/test/builtin/files_io/test_files.py +++ b/test/builtin/files_io/test_files.py @@ -67,6 +67,18 @@ def test_get_evaluates_queries_after_error(): check_evaluation(f'Get["{script_path}"]', "OK!", hold_expected=True) +def test_get_continues_after_syntax_error(): + # As in WMA, Get reports a syntax error and goes on reading the file. + script_path = canonic_filename( + osp.normpath( + osp.join(osp.dirname(__file__), "..", "..", "data", "syntax-error-bug.m") + ) + ) + # The syntax message itself is not checked here: its text comes from + # Mathics3-scanner. + check_evaluation(f'Get["{script_path}"]', "{1, getSyntaxB, 3}") + + @pytest.mark.skipif( sys.platform in ("win32",), reason="$Path does not work on Windows?" ) diff --git a/test/data/syntax-error-bug.m b/test/data/syntax-error-bug.m new file mode 100644 index 0000000000..8b1c055924 --- /dev/null +++ b/test/data/syntax-error-bug.m @@ -0,0 +1,5 @@ +(* Get must go on reading the file after a syntax error, as in WMA. *) +getSyntaxA = 1; +getSyntaxB = \[NoSuchCharacterName]; +getSyntaxC = 3; +{getSyntaxA, getSyntaxB, getSyntaxC} From 1f544acc203ead902b7ef767a3a90d5292639f06 Mon Sep 17 00:00:00 2001 From: Stanislav Date: Fri, 16 Oct 2026 21:45:15 +0400 Subject: [PATCH 2/2] Test data for Get after a syntax error: .wl rather than .m .m is also the extension of Objective-C sources; .wl is unambiguous. Co-Authored-By: Claude Opus 5.5 --- test/builtin/files_io/test_files.py | 2 +- test/data/{syntax-error-bug.m => syntax-error-bug.wl} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename test/data/{syntax-error-bug.m => syntax-error-bug.wl} (100%) diff --git a/test/builtin/files_io/test_files.py b/test/builtin/files_io/test_files.py index e499581bb3..74cecc68d2 100644 --- a/test/builtin/files_io/test_files.py +++ b/test/builtin/files_io/test_files.py @@ -71,7 +71,7 @@ def test_get_continues_after_syntax_error(): # As in WMA, Get reports a syntax error and goes on reading the file. script_path = canonic_filename( osp.normpath( - osp.join(osp.dirname(__file__), "..", "..", "data", "syntax-error-bug.m") + osp.join(osp.dirname(__file__), "..", "..", "data", "syntax-error-bug.wl") ) ) # The syntax message itself is not checked here: its text comes from diff --git a/test/data/syntax-error-bug.m b/test/data/syntax-error-bug.wl similarity index 100% rename from test/data/syntax-error-bug.m rename to test/data/syntax-error-bug.wl