Skip to content

Commit 37911b1

Browse files
authored
Merge pull request #1369 from mathics/general-autoload
General autoload
2 parents 28530c4 + 8f7be52 commit 37911b1

File tree

7 files changed

+51
-44
lines changed

7 files changed

+51
-44
lines changed

CHANGES.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Enhancements
4141
* ``FileNames`` returns a sorted list (#1250).
4242
* ``FindRoot`` now receives several optional parameters like ``Method`` and ``MaxIterations``.
4343
* ``FixedPoint`` now supports the ``SameTest`` option.
44+
* ``mathics`` CLI now uses its own Mathics ``settings.m`` file
4445
* ``Prepend`` works with ``DownValues`` Issue #1251
4546
* ``Prime`` and ``PrimePi`` now accept a list parameter and have the ``NumericFunction`` attribute.
4647
* ``Read`` with ``Hold[Expression]`` now supported. (#1242)
@@ -66,10 +67,16 @@ Bug fixes
6667
* Functions gone over to ensure the ``Listable`` and ``NumericFunction`` properties are correct.
6768

6869

70+
Incompatible changes
71+
--------------------
72+
73+
``System`$UseSansSerif`` moved from core and is sent front-ends using ``Settings`$UseSansSerif``.
74+
6975
Internal changes
7076
----------------
7177

72-
* doctest accepts the option `-d` to show how long it takes to parse, evaluate and compare each individual test.
78+
* doctest accepts the option ``-d`` to show how long it takes to parse, evaluate and compare each individual test.
79+
``-x`` option (akin to ``pytests -x`` is a short-hand for stop on first error
7380

7481

7582
2.1.0

mathics/autoload-cli/settings.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
(***********************************)
2+
(* Settings for Mathics CLI script *)
3+
(***********************************)
4+
5+
System`$Notebooks::usage = "Set True if the Mathics is being used with a notebook-based front end.";
6+
System`$Notebooks = False;

mathics/autoload/settings.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
Settings`$PreferredBackendMethod = "sympy"
1212
Unprotect[Settings`$PreferredBackendMethod]
1313

14+
(* FIXME: remove this when all front-ends are adjusted to set this. *)
1415
(* Some packages like Feyncalc, test for whether a they are being used
1516
inside a notbook. *)
1617
System`$Notebooks::usage = "Set True if the Mathics is being used with a notebook-based front end.";

mathics/builtin/inout.py

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
BoxConstruct,
1919
BoxConstructError,
2020
Operator,
21-
Predefined,
2221
)
2322
from mathics.builtin.tensors import get_dimensions
2423
from mathics.builtin.comparison import expr_min
@@ -51,35 +50,6 @@
5150
MULTI_NEWLINE_RE = re.compile(r"\n{2,}")
5251

5352

54-
class UseSansSerif(Predefined):
55-
"""
56-
<dl>
57-
<dt>'$UseSansSerif'
58-
<dd>controls whether the Web interfaces use a Sans-Serif font.
59-
</dl>
60-
61-
When set True, the output in MathMLForm uses SansSerif fonts instead
62-
of the standard fonts.
63-
64-
X> $UseSansSerif
65-
= True
66-
X> $UseSansSerif = False
67-
68-
"""
69-
70-
context = "System`"
71-
name = "$UseSansSerif"
72-
attributes = ("Unprotected",)
73-
value = True
74-
75-
rules = {"$UseSansSerif": str(value)}
76-
77-
messages = {}
78-
79-
def evaluate(self, evaluation):
80-
return Integer(self.value)
81-
82-
8353
class Format(Builtin):
8454
"""
8555
<dl>
@@ -2105,10 +2075,10 @@ def apply_mathml(self, expr, evaluation) -> Expression:
21052075

21062076
# mathml = '<math><mstyle displaystyle="true">%s</mstyle></math>' % xml
21072077
# #convert_box(boxes)
2108-
query = evaluation.parse("System`$UseSansSerif")
2078+
query = evaluation.parse("Settings`$UseSansSerif")
21092079
usesansserif = query.evaluate(evaluation).to_python()
21102080
if not is_a_picture:
2111-
if usesansserif:
2081+
if isinstance(usesansserif, bool) and usesansserif:
21122082
xml = '<mstyle mathvariant="sans-serif">%s</mstyle>' % xml
21132083

21142084
mathml = '<math display="block">%s</math>' % xml # convert_box(boxes)

mathics/core/definitions.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,27 @@ def valuesname(name) -> str:
4141
return name[7:-6].lower()
4242

4343

44+
def autoload_files(defs, root_dir_path: str, autoload_dir):
45+
from mathics.core.evaluation import Evaluation
46+
47+
# Load symbols from the autoload folder
48+
for root, dirs, files in os.walk(os.path.join(root_dir_path, autoload_dir)):
49+
for path in [os.path.join(root, f) for f in files if f.endswith(".m")]:
50+
Expression("Get", String(path)).evaluate(Evaluation(defs))
51+
52+
# Move any user definitions created by autoloaded files to
53+
# builtins, and clear out the user definitions list. This
54+
# means that any autoloaded definitions become shared
55+
# between users and no longer disappear after a Quit[].
56+
#
57+
# Autoloads that accidentally define a name in Global`
58+
# could cause confusion, so check for this.
59+
#
60+
for name in defs.user:
61+
if name.startswith("Global`"):
62+
raise ValueError("autoload defined %s." % name)
63+
64+
4465
class PyMathicsLoadException(Exception):
4566
def __init__(self, module):
4667
self.name = module + " is not a valid pymathics module"
@@ -78,22 +99,17 @@ def __init__(
7899
contribute(self)
79100
for module in extension_modules:
80101
try:
81-
loaded_module = self.load_pymathics_module(
82-
module, remove_on_quit=False
83-
)
84-
except PyMathicsLoadException as e:
102+
self.load_pymathics_module(module, remove_on_quit=False)
103+
except PyMathicsLoadException:
85104
raise
86-
except ImportError as e:
105+
except ImportError:
87106
raise
88107

89108
if builtin_filename is not None:
90109
builtin_file = open(builtin_filename, "wb")
91110
pickle.dump(self.builtin, builtin_file, -1)
92111

93-
# Load symbols from the autoload folder
94-
for root, dirs, files in os.walk(os.path.join(ROOT_DIR, "autoload")):
95-
for path in [os.path.join(root, f) for f in files if f.endswith(".m")]:
96-
Expression("Get", String(path)).evaluate(Evaluation(self))
112+
autoload_files(self, ROOT_DIR, "autoload")
97113

98114
# Move any user definitions created by autoloaded files to
99115
# builtins, and clear out the user definitions list. This
@@ -106,6 +122,7 @@ def __init__(
106122
for name in self.user:
107123
if name.startswith("Global`"):
108124
raise ValueError("autoload defined %s." % name)
125+
109126
self.builtin.update(self.user)
110127
self.user = {}
111128
self.clear_cache()
@@ -475,7 +492,7 @@ def get_definition(self, name, only_if_exists=False) -> "Definition":
475492
self.lookup_cache[original_name] = name
476493
elif not only_if_exists:
477494
definition = Definition(name=name)
478-
if name[-1] != '`':
495+
if name[-1] != "`":
479496
self.user[name] = definition
480497

481498
return definition

mathics/main.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@
88
import subprocess
99
import sys
1010

11+
import os.path as osp
1112
from mathics.core.parser import MathicsFileLineFeeder, MathicsLineFeeder
1213

13-
from mathics.core.definitions import Definitions, Symbol
14+
from mathics.core.definitions import autoload_files, Definitions, Symbol
1415
from mathics.core.expression import strip_context
1516
from mathics.core.evaluation import Evaluation, Output
1617
from mathics import version_string, license_string, __version__
1718
from mathics import settings
1819

20+
def get_srcdir():
21+
filename = osp.normcase(osp.dirname(osp.abspath(__file__)))
22+
return osp.realpath(filename)
1923

2024
class TerminalShell(MathicsLineFeeder):
2125
def __init__(self, definitions, colors, want_readline, want_completion):
@@ -82,6 +86,7 @@ def __init__(self, definitions, colors, want_readline, want_completion):
8286

8387
self.incolors, self.outcolors = term_colors
8488
self.definitions = definitions
89+
autoload_files(definitions, get_srcdir(), "autoload-cli")
8590

8691
def get_last_line_number(self):
8792
return self.definitions.get_line_no()

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ def subdirs(root, file="*.*", depth=10):
144144
"doc/xml/data",
145145
"doc/tex/data",
146146
"autoload/*.m",
147+
"autoload-cli/*.m",
147148
"autoload/formats/*/Import.m",
148149
"autoload/formats/*/Export.m",
149150
"packages/*/*.m",

0 commit comments

Comments
 (0)