Skip to content

Commit ba45361

Browse files
committed
Miscellaneous small fixes...
* Syntax Errors showing more properly by removing weird SymbolForm handling. Also get ready for an "error" kind of format * Remove GNU Readline termshell style error when no style given * Better error checking on giving a file argument for an invalid file
1 parent e03cba7 commit ba45361

File tree

4 files changed

+42
-25
lines changed

4 files changed

+42
-25
lines changed

mathicsscript/__main__.py

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -469,29 +469,40 @@ def main(
469469

470470
load_settings_file(shell)
471471
style_from_settings_file = definitions.get_ownvalue("Settings`$PygmentsStyle")
472-
if style_from_settings_file is SymbolNull and style is None:
472+
if style_from_settings_file is not SymbolNull and style is None:
473473
style = style_from_settings_file
474474
shell.setup_pygments_style(style)
475475

476476
if file:
477-
with open(file, "r") as ifile:
478-
feeder = MathicsFileLineFeeder(ifile)
477+
if not os.path.exists(file):
478+
print(f"\nFile {file} does not exist; skipping reading.")
479+
file = None
480+
elif os.path.isdir(file):
481+
print(f"\nFile {file} does is a directory; skipping reading.")
482+
file = None
483+
else:
479484
try:
480-
while not feeder.empty():
481-
evaluation = Evaluation(
482-
shell.definitions,
483-
output=TerminalOutput(shell),
484-
catch_interrupt=False,
485-
format="text",
486-
)
487-
query = evaluation.parse_feeder(feeder)
488-
if query is None:
489-
continue
490-
evaluation.evaluate(query, timeout=settings.TIMEOUT)
491-
except KeyboardInterrupt:
492-
print("\nKeyboardInterrupt")
493-
494-
definitions.set_line_no(0)
485+
with open(file, "r") as ifile:
486+
feeder = MathicsFileLineFeeder(ifile)
487+
try:
488+
while not feeder.empty():
489+
evaluation = Evaluation(
490+
shell.definitions,
491+
output=TerminalOutput(shell),
492+
catch_interrupt=False,
493+
format="text",
494+
)
495+
query = evaluation.parse_feeder(feeder)
496+
if query is None:
497+
continue
498+
evaluation.evaluate(query, timeout=settings.TIMEOUT)
499+
except KeyboardInterrupt:
500+
print("\nKeyboardInterrupt")
501+
except Exception as e:
502+
print(f"\nError reading {file}: {e}; skipping reading.")
503+
file = None
504+
else:
505+
definitions.set_line_no(0)
495506

496507
if code:
497508
for expr in code:

mathicsscript/format.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,18 @@ def eval_boxes(result, fn: Callable, obj, **options):
168168
obj.last_eval = SymbolAborted
169169
return "$Aborted"
170170
if format == "text":
171-
if expr_head is SymbolStringForm:
172-
return expr.elements[0].value
173-
elif isinstance(expr, String):
171+
if isinstance(expr, String):
174172
return expr.value
175173
result = expr.format(obj, SymbolOutputForm)
176174
elif format == "xml":
177175
result = Expression(SymbolStandardForm, expr).format(obj, SymbolMathMLForm)
178176
elif format == "tex":
179177
result = Expression(SymbolStandardForm, expr).format(obj, SymbolTeXForm)
178+
elif format == "error":
179+
# We may do fancier things in the future.
180+
if isinstance(expr, String):
181+
return expr.value
182+
result = expr.format(obj, SymbolOutputForm)
180183
elif format == "unformatted":
181184
if expr_head is PyMathicsGraph and hasattr(expr, "G"):
182185
return format_graph(expr.G)

mathicsscript/termshell.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import mathics_scanner.location
1111

1212
from columnize import columnize
13-
from mathics.core.atoms import Symbol
13+
from mathics.core.atoms import String, Symbol
1414
from mathics.core.attributes import attribute_string_to_number
1515
from mathics.core.expression import Expression, from_python # strip_context,
1616
from mathics.core.rules import Rule
@@ -238,7 +238,8 @@ def print_result(
238238
out_str = str(result.result)
239239
use_highlight = True
240240
if eval_type == "System`String":
241-
if strict_wl_output: # exact-wl-compatibility
241+
# Use exact-wl-compatibility?
242+
if strict_wl_output:
242243
out_str = (
243244
format(
244245
[(MToken.STRING, out_str.rstrip())], self.terminal_formatter
@@ -263,6 +264,7 @@ def print_result(
263264

264265
if eval_type == "System`Graph":
265266
out_str = "-Graph-"
267+
266268
elif self.terminal_formatter: # pygmentize
267269
show_pygments_tokens = get_settings_value(
268270
self.definitions, "Settings`$PygmentsShowTokens"

mathicsscript/termshell_prompt.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ def print_result(
164164
out_str = str(result.result)
165165
use_highlight = True
166166
if eval_type == "System`String":
167-
if strict_wl_output: # exact-wl-compatibility
167+
# Use exact-wl-compatibility?
168+
if strict_wl_output:
168169
out_str = (
169170
format(
170171
[(MToken.STRING, out_str.rstrip())], self.terminal_formatter
@@ -189,8 +190,8 @@ def print_result(
189190

190191
if eval_type == "System`Graph":
191192
out_str = "-Graph-"
192-
elif self.terminal_formatter: # pygmentize
193193

194+
elif self.terminal_formatter: # pygmentize
194195
if show_pygments_tokens:
195196
print(list(lex(out_str, mma_lexer)))
196197
if use_highlight:

0 commit comments

Comments
 (0)