diff --git a/mathics/builtin/files_io/files.py b/mathics/builtin/files_io/files.py index 45a3c4082..a36f1a3b1 100644 --- a/mathics/builtin/files_io/files.py +++ b/mathics/builtin/files_io/files.py @@ -13,7 +13,6 @@ from mathics_scanner import TranslateError import mathics -from mathics.core import read from mathics.core.atoms import Integer, String, SymbolString from mathics.core.attributes import A_PROTECTED, A_READ_PROTECTED from mathics.core.builtin import ( @@ -465,7 +464,7 @@ class InputFileName_(Predefined): name = "$InputFileName" def evaluate(self, evaluation): - return String(read.INPUTFILE_VAR) + return String(evaluation.definitions.get_inputfile()) class InputStream(Builtin): diff --git a/mathics/core/definitions.py b/mathics/core/definitions.py index bd2d8d3bf..1d1cdb26c 100644 --- a/mathics/core/definitions.py +++ b/mathics/core/definitions.py @@ -121,6 +121,7 @@ def __init__( "System`", "Global`", ) + self.inputfile = "" # Importing "mathics.format" populates the Symbol of the # PrintForms and OutputForms sets. @@ -243,6 +244,9 @@ def get_current_context(self): def get_context_path(self): return self.context_path + def get_inputfile(self): + return self.inputfile if hasattr(self, "inputfile") else "" + def set_current_context(self, context) -> None: assert isinstance(context, str) self.set_ownvalue("System`$Context", String(context)) @@ -259,6 +263,9 @@ def set_context_path(self, context_path) -> None: self.context_path = context_path self.clear_cache() + def set_inputfile(self, dir) -> None: + self.inputfile = dir + def get_builtin_names(self): return set(self.builtin) diff --git a/mathics/core/read.py b/mathics/core/read.py index 7755584c9..08c77f2f8 100644 --- a/mathics/core/read.py +++ b/mathics/core/read.py @@ -3,7 +3,6 @@ """ import io -import os.path as osp from mathics.builtin.atomic.strings import to_python_encoding from mathics.core.atoms import Integer, String @@ -13,9 +12,6 @@ from mathics.core.streams import Stream, path_search, stream_manager from mathics.core.symbols import Symbol -# FIXME: don't use a module-level path -INPUTFILE_VAR = "" - SymbolInputStream = Symbol("InputStream") SymbolOutputStream = Symbol("OutputStream") SymbolEndOfFile = Symbol("EndOfFile") @@ -83,8 +79,6 @@ def __enter__(self, is_temporary_file=False): # Open the file self.fp = io.open(path, self.mode, encoding=self.encoding) - global INPUTFILE_VAR - INPUTFILE_VAR = osp.abspath(path) # Add to our internal list of streams self.stream = stream_manager.add( @@ -100,8 +94,6 @@ def __enter__(self, is_temporary_file=False): return self.fp def __exit__(self, type, value, traceback): - global INPUTFILE_VAR - INPUTFILE_VAR = self.old_inputfile_var or "" self.fp.close() stream_manager.delete_stream(self.stream) super().__exit__(type, value, traceback) @@ -316,8 +308,3 @@ def read_from_stream(stream, word_separators, msgfn, accepted=None): break word += tmp - - -def set_inputfile(filename): - global INPUTFILE_VAR - INPUTFILE_VAR = filename diff --git a/mathics/main.py b/mathics/main.py index 2c2a36792..e4d1270f7 100755 --- a/mathics/main.py +++ b/mathics/main.py @@ -26,7 +26,7 @@ from mathics.core.expression import Expression from mathics.core.load_builtin import import_and_load_builtins from mathics.core.parser import MathicsFileLineFeeder, MathicsLineFeeder -from mathics.core.read import channel_to_stream, set_inputfile +from mathics.core.read import channel_to_stream from mathics.core.rules import BuiltinRule from mathics.core.streams import stream_manager from mathics.core.symbols import SymbolNull, strip_context @@ -423,7 +423,7 @@ def dump_tracing_stats(): definitions.set_line_no(0) if args.FILE is not None: - set_inputfile(args.FILE.name) + definitions.set_inputfile(args.FILE.name) feeder = MathicsFileLineFeeder(args.FILE) try: while not feeder.empty():