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
62 changes: 29 additions & 33 deletions mathicsscript/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def load_settings(shell):
if query is None:
continue
evaluation.evaluate(query)
except (KeyboardInterrupt):
except KeyboardInterrupt:
print("\nKeyboardInterrupt")
return True

Expand Down Expand Up @@ -223,7 +223,7 @@ def fmt_fun(query: Any) -> Any:
# FIXME add this... when in Mathics core updated
# shell.definitions.increment_line(1)

except (KeyboardInterrupt):
except KeyboardInterrupt:
print("\nKeyboardInterrupt")
except EOFError:
if prompt:
Expand All @@ -240,13 +240,10 @@ def fmt_fun(query: Any) -> Any:
shell.reset_lineno()


if click.__version__ >= "7.":
case_sensitive = {"case_sensitive": False}
else:
case_sensitive = {}
case_sensitive = {"case_sensitive": False}


@click.command()
@click.command(context_settings=dict(help_option_names=["-h", "-help", "--help"]))
@click.option(
"--edit-mode",
"-e",
Expand All @@ -256,7 +253,7 @@ def fmt_fun(query: Any) -> Any:
@click.version_option(version=__version__)
@click.option(
"--full-form",
"-f",
"-F",
"full_form",
flag_value="full_form",
default=False,
Expand Down Expand Up @@ -296,10 +293,11 @@ def fmt_fun(query: Any) -> Any:
),
)
@click.option(
"--unicode/--no-unicode",
"-charset",
metavar="ENCODING",
default=sys.getdefaultencoding() == "utf-8",
show_default=True,
help="Accept Unicode operators in input and show unicode in output.",
help="Use encoding for output. Encodings can be any entry in $CharacterEncodings.",
)
@click.option(
"--post-mortem/--no-post-mortem",
Expand All @@ -321,20 +319,19 @@ def fmt_fun(query: Any) -> Any:
)
@click.option(
"-c",
"-e",
"--execute",
help="evaluate EXPR before processing any input files (may be given "
"multiple times). Sets --quiet and --no-completion",
"-code",
"--code",
help="Give Mathics3 source code to execute. This may be given "
"multiple times. Sets --quiet and --no-completion",
multiple=True,
required=False,
)
@click.option(
"--run",
"-f",
"-file",
"--file",
type=click.Path(readable=True),
help=(
"go to interactive shell after evaluating PATH but leave "
"history empty and set $Line to 1"
),
help=("Give a file containing Mathics3 source code to execute."),
)
@click.option(
"-s",
Expand Down Expand Up @@ -383,18 +380,17 @@ def main(
quiet,
readline,
completion,
unicode,
charset,
post_mortem,
prompt,
pyextensions,
execute,
run,
code,
file,
style,
pygments_tokens,
strict_wl_output,
asymptote,
matplotlib,
file,
) -> int:
"""A command-line interface to Mathics.

Expand Down Expand Up @@ -433,20 +429,20 @@ def main(
else:
sys.excepthook = post_mortem_excepthook

readline = "none" if (execute or file and not persist) else readline.lower()
readline = "none" if (code or file and not persist) else readline.lower()
if readline == "prompt":
shell = TerminalShellPromptToolKit(
definitions, style, completion, unicode, prompt, edit_mode
definitions, style, completion, charset, prompt, edit_mode
)
else:
want_readline = readline == "gnu"
shell = TerminalShellGNUReadline(
definitions, style, want_readline, completion, unicode, prompt
definitions, style, want_readline, completion, charset, prompt
)

load_settings(shell)
if run:
with open(run, "r") as ifile:
if file:
with open(file, "r") as ifile:
feeder = MathicsFileLineFeeder(ifile)
try:
while not feeder.empty():
Expand All @@ -460,13 +456,13 @@ def main(
if query is None:
continue
evaluation.evaluate(query, timeout=settings.TIMEOUT)
except (KeyboardInterrupt):
except KeyboardInterrupt:
print("\nKeyboardInterrupt")

definitions.set_line_no(0)

if execute:
for expr in execute:
if code:
for expr in code:
evaluation = Evaluation(
shell.definitions, output=TerminalOutput(shell), format="text"
)
Expand Down Expand Up @@ -503,7 +499,7 @@ def main(
if query is None:
continue
evaluation.evaluate(query, timeout=settings.TIMEOUT)
except (KeyboardInterrupt):
except KeyboardInterrupt:
print("\nKeyboardInterrupt")

if not persist:
Expand Down Expand Up @@ -531,7 +527,7 @@ def main(

definitions.set_line_no(0)
interactive_eval_loop(
shell, unicode, prompt, asymptote, matplotlib, strict_wl_output
shell, charset, prompt, asymptote, matplotlib, strict_wl_output
)
return exit_rc

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ description = "Command-line interface to Mathics3"
dependencies = [
"Mathics_Scanner>1.4.1",
"Mathics3 >= 8.0.1",
"click",
"click >= 8.0.0",
"colorama",
"columnize",
"networkx",
Expand Down
16 changes: 12 additions & 4 deletions test/test_returncode.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@ def get_testdir():


def test_returncode():
assert subprocess.run(["mathicsscript", "-e", "Quit[5]"]).returncode == 5
assert subprocess.run(["mathicsscript", "-e", "1 + 2'"]).returncode == 0
assert subprocess.run(["mathicsscript", "-e", "Quit[0]"]).returncode == 0
assert subprocess.run(["mathicsscript", "-c", "Quit[5]"]).returncode == 5
assert subprocess.run(["mathicsscript", "-c", "1 + 2'"]).returncode == 0
assert subprocess.run(["mathicsscript", "-c", "Quit[0]"]).returncode == 0

gcd_file = osp.join(get_testdir(), "data", "recursive-gcd.m")
assert subprocess.run(["mathicsscript", "-f", gcd_file]).returncode == 0
# We add --readline None for OSX and Windows which don't interact well
# with tty's in a CI environment. TODO: separate out Ubuntu and
# use prompt_readline for that?
assert (
subprocess.run(
["mathicsscript", "--readline", "None", "-f", gcd_file]
).returncode
== 0
)


if __name__ == "__main__":
Expand Down
Loading