Skip to content

Commit 4af8a3e

Browse files
authored
Merge pull request #1455 from TiagoCavalcanteTrindade/fix-712
Fix weird results when combining -script and -e
2 parents 6508671 + 4bf30cd commit 4af8a3e

File tree

3 files changed

+51
-20
lines changed

3 files changed

+51
-20
lines changed

mathics/main.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -340,25 +340,6 @@ def main() -> int:
340340

341341
definitions.set_line_no(0)
342342

343-
if args.execute:
344-
for expr in args.execute:
345-
evaluation = Evaluation(shell.definitions, output=TerminalOutput(shell))
346-
result = evaluation.parse_evaluate(expr, timeout=settings.TIMEOUT)
347-
shell.print_result(
348-
result, no_out_prompt=True, strict_wl_output=args.strict_wl_output
349-
)
350-
if evaluation.exc_result == Symbol("Null"):
351-
exit_rc = 0
352-
elif evaluation.exc_result == Symbol("$Aborted"):
353-
exit_rc = -1
354-
elif evaluation.exc_result == Symbol("Overflow"):
355-
exit_rc = -2
356-
else:
357-
exit_rc = -3
358-
359-
if not args.persist:
360-
return exit_rc
361-
362343
if args.FILE is not None:
363344
feeder = MathicsFileLineFeeder(args.FILE)
364345
try:
@@ -377,7 +358,26 @@ def main() -> int:
377358

378359
if args.persist:
379360
definitions.set_line_no(0)
380-
else:
361+
elif not args.execute:
362+
return exit_rc
363+
364+
if args.execute:
365+
for expr in args.execute:
366+
evaluation = Evaluation(shell.definitions, output=TerminalOutput(shell))
367+
result = evaluation.parse_evaluate(expr, timeout=settings.TIMEOUT)
368+
shell.print_result(
369+
result, no_out_prompt=True, strict_wl_output=args.strict_wl_output
370+
)
371+
if evaluation.exc_result == Symbol("Null"):
372+
exit_rc = 0
373+
elif evaluation.exc_result == Symbol("$Aborted"):
374+
exit_rc = -1
375+
elif evaluation.exc_result == Symbol("Overflow"):
376+
exit_rc = -2
377+
else:
378+
exit_rc = -3
379+
380+
if not args.persist:
381381
return exit_rc
382382

383383
if not args.quiet:

test/data/script.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Print["Hello"];

test/test_cli.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# -*- coding: utf-8 -*-
2+
import subprocess
3+
4+
import os.path as osp
5+
import re
6+
import pytest
7+
import sys
8+
9+
10+
def get_testdir():
11+
filename = osp.normcase(osp.dirname(osp.abspath(__file__)))
12+
return osp.realpath(filename)
13+
14+
15+
@pytest.mark.skipif(sys.version_info < (3, 7), reason="requires Python 3.7 or higher")
16+
def test_cli():
17+
script_file = osp.join(get_testdir(), "data", "script.m")
18+
19+
# asserts output contains 'Hello' and '2'
20+
assert re.match(
21+
r"Hello\s+2",
22+
subprocess.run(
23+
["mathics", "-e", "Print[1+1];", "-script", script_file],
24+
capture_output=True,
25+
).stdout.decode("utf-8"),
26+
)
27+
28+
29+
if __name__ == "__main__":
30+
test_cli()

0 commit comments

Comments
 (0)