Skip to content

Commit

Permalink
Get trace/debug tweaks and fixes...
Browse files Browse the repository at this point in the history
Get[xx, Trace->True] was not showing all lines read. And we did not
properly hook into a changeable print function.

Using:
   import a.b.c as e

reduces unchangeable lookup (a.b.c) does not change and simplifies and
use -- e.GET_PRINT_FN as opposed to a.b.c.GET_PRINT_FN
  • Loading branch information
rocky committed Sep 27, 2024
1 parent 27796f8 commit 4ce56a0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
10 changes: 6 additions & 4 deletions mathics/builtin/files_io/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import tempfile
from io import BytesIO

import mathics.eval.files_io.files
# We use the below import for access to variables that may change
# at runtime.
import mathics.eval.files_io.files as io_files
from mathics.core.atoms import Integer, String, SymbolString
from mathics.core.attributes import A_PROTECTED, A_READ_PROTECTED
from mathics.core.builtin import (
Expand Down Expand Up @@ -63,7 +65,7 @@ class Input_(Predefined):
summary_text = "the name of the current input stream"

def evaluate(self, evaluation: Evaluation) -> String:
return String(mathics.eval.files_io.files.INPUT_VAR)
return String(io_files.INPUT_VAR)


class _OpenAction(Builtin):
Expand Down Expand Up @@ -365,14 +367,14 @@ def eval(self, path: String, evaluation: Evaluation, options: dict):
# Make sure to pick up copy from module each time instead of using
# use "from ... import DEFAULT_TRACE_FN" which will not pick
# up run-time changes made to the module function.
trace_fn = mathics.eval.files_io.files.DEFAULT_TRACE_FN
trace_fn = io_files.DEFAULT_TRACE_FN

trace_get = evaluation.parse("Settings`$TraceGet")
if (
options["System`Trace"].to_python()
or trace_get.evaluate(evaluation) is SymbolTrue
):
trace_fn = print_line_number_and_text
trace_fn = io_files.GET_PRINT_FN

# perform the actual evaluation
return eval_Get(path.value, evaluation, trace_fn)
Expand Down
8 changes: 7 additions & 1 deletion mathics/eval/files_io/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ def print_line_number_and_text(line_number: int, text: str):
"""Prints a line number an text on that line with it.
This is used as the default trace function in Get[]
"""
print(f"%5d: {text}" % line_number, end="")
if line_number == 0:
print(f"Reading file: {text}")
else:
print("%5d: %s" % (line_number, text.rstrip()))


GET_PRINT_FN: Callable = print_line_number_and_text


def set_input_var(input_string: str):
Expand Down

0 comments on commit 4ce56a0

Please sign in to comment.