Skip to content

Commit

Permalink
Moved INPUTFILE_VAR as attribute in Definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessandro Piras committed Feb 8, 2024
1 parent 132be71 commit c6157a4
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 17 deletions.
3 changes: 1 addition & 2 deletions mathics/builtin/files_io/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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):
Expand Down
7 changes: 7 additions & 0 deletions mathics/core/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def __init__(
"System`",
"Global`",
)
self.inputfile = ""

# Importing "mathics.format" populates the Symbol of the
# PrintForms and OutputForms sets.
Expand Down Expand Up @@ -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))
Expand All @@ -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)

Expand Down
13 changes: 0 additions & 13 deletions mathics/core/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down Expand Up @@ -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(
Expand All @@ -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)
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions mathics/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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():
Expand Down

0 comments on commit c6157a4

Please sign in to comment.