Skip to content

Commit 4be33d2

Browse files
authored
Merge pull request #1172 from mathics/sanitizeloadmodules
Check fails in loading modules.
2 parents 32dcf61 + 7138976 commit 4be33d2

47 files changed

Lines changed: 1269 additions & 1210 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

mathics/builtin/__init__.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
#!/usr/bin/env python3
21
# -*- coding: utf-8 -*-
3-
from mathics.core.expression import ensure_context
42

53
import glob
64
import importlib
75
import re
86
import os.path as osp
97
from mathics.settings import ENABLE_FILES_MODULE
8+
from mathics.version import __version__ # noqa used in loading to check consistency.
109

1110
# Get a list of file in this directory. We'll exclude from the start
1211
# files with leading characters we don't want like __init__ with its leading underscore.
@@ -23,8 +22,6 @@
2322
PatternObject,
2423
)
2524

26-
from mathics.settings import ENABLE_FILES_MODULE
27-
2825
exclude_files = set(("files", "codetables", "base", "importexport", "colors"))
2926
module_names = [
3027
f for f in __py_files__ if re.match("^[a-z0-9]+$", f) if f not in exclude_files
@@ -38,9 +35,17 @@
3835
for module_name in module_names:
3936
try:
4037
module = importlib.import_module("mathics.builtin." + module_name)
41-
except:
42-
# print("XXX", module_name)
38+
except Exception as e:
39+
print(e)
40+
print(f" Not able to load {module_name}. Check your installation.")
41+
print(f" mathics.builtin loads from {__file__[:-11]}")
4342
continue
43+
44+
if __version__ != module.__version__:
45+
print(
46+
f"Version {module.__version__} in the module do not match with {__version__}"
47+
)
48+
4449
modules.append(module)
4550

4651
_builtins = []
@@ -75,7 +80,7 @@ def is_builtin(var):
7580
# This set the default context for symbols in mathics.builtins
7681
if not type(instance).context:
7782
type(instance).context = "System`"
78-
_builtins.append( (instance.get_name(), instance))
83+
_builtins.append((instance.get_name(), instance))
7984
builtins_by_module[module.__name__].append(instance)
8085

8186

@@ -113,9 +118,11 @@ def add_builtins(new_builtins):
113118

114119

115120
def builtins_dict():
116-
return { builtin.get_name() : builtin
117-
for modname, builtins in builtins_by_module.items()
118-
for builtin in builtins}
121+
return {
122+
builtin.get_name(): builtin
123+
for modname, builtins in builtins_by_module.items()
124+
for builtin in builtins
125+
}
119126

120127

121128
def get_module_doc(module):

mathics/builtin/algebra.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
#!/usr/bin/env python3
21
# -*- coding: utf-8 -*-
32
"""
43
Algebraic Manipulation
54
"""
65

6+
from mathics.version import __version__ # noqa used in loading to check consistency.
77

88
from mathics.builtin.base import Builtin
99
from mathics.core.expression import Expression, Integer, Symbol, Atom, Number

mathics/builtin/arithmetic.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/usr/bin/env python3
21
# -*- coding: utf-8 -*-
32
# cython: language_level=3
43

@@ -8,7 +7,7 @@
87
Basic arithmetic functions, including complex number arithmetic.
98
"""
109

11-
10+
from mathics.version import __version__ # noqa used in loading to check consistency.
1211
import sympy
1312
import mpmath
1413

mathics/builtin/assignment.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# -*- coding: utf-8 -*-
22

3+
from mathics.version import __version__ # noqa used in loading to check consistency.
4+
35
import mathics.builtin
46
from mathics.builtin.base import (
57
Builtin, BinaryOperator, PostfixOperator, PrefixOperator)

mathics/builtin/attributes.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/usr/bin/env python3
21
# -*- coding: utf-8 -*-
32

43
r"""
@@ -8,9 +7,9 @@
87
However, you can set any symbol as an attribute, in contrast to \Mathematica.
98
"""
109

10+
from mathics.version import __version__ # noqa used in loading to check consistency.
1111

1212
from mathics.builtin.base import Predefined, Builtin
13-
from mathics.builtin.evaluation import Sequence
1413
from mathics.core.expression import Expression, Symbol, SymbolNull, String
1514
from mathics.builtin.assignment import get_symbol_list
1615

mathics/builtin/base.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/usr/bin/env python3
21
# -*- coding: utf-8 -*-
32

43
import re
@@ -10,6 +9,8 @@
109
import typing
1110
from typing import Any, cast
1211

12+
from mathics.version import __version__ # noqa used in loading to check consistency.
13+
1314
from mathics.core.definitions import Definition
1415
from mathics.core.parser.util import SystemDefinitions, PyMathicsDefinitions
1516
from mathics.core.rules import Rule, BuiltinRule, Pattern
@@ -26,6 +27,7 @@
2627
)
2728
from mathics.core.numbers import get_precision, PrecisionValueError
2829

30+
2931
def get_option(options, name, evaluation, pop=False, evaluate=True):
3032
# we do not care whether an option X is given as System`X,
3133
# Global`X, or with any prefix from $ContextPath for that
@@ -82,6 +84,7 @@ def __init__(self, *args, **kwargs):
8284

8385
def contribute(self, definitions, is_pymodule=False):
8486
from mathics.core.parser import parse_builtin_rule
87+
8588
# Set the default context
8689
if not self.context:
8790
self.context = "Pymathics`" if is_pymodule else "System`"
@@ -102,7 +105,8 @@ def contribute(self, definitions, is_pymodule=False):
102105
# used, so it won't work.
103106
if option not in definitions.builtin:
104107
definitions.builtin[option] = Definition(
105-
name=name, attributes=set())
108+
name=name, attributes=set()
109+
)
106110

107111
# Check if the given options are actually supported by the Builtin.
108112
# If not, we might issue an optx error and abort. Using '$OptionSyntax'
@@ -144,9 +148,7 @@ def check_options(options_to_check, evaluation):
144148

145149
for pattern, function in self.get_functions(is_pymodule=is_pymodule):
146150
rules.append(
147-
BuiltinRule(
148-
name, pattern, function, check_options, system=True
149-
)
151+
BuiltinRule(name, pattern, function, check_options, system=True)
150152
)
151153
for pattern, replace in self.rules.items():
152154
if not isinstance(pattern, BaseExpression):
@@ -246,7 +248,8 @@ def contextify_form_name(f):
246248
# used, so it won't work.
247249
if option not in definitions.builtin:
248250
definitions.builtin[option] = Definition(
249-
name=name, attributes=set())
251+
name=name, attributes=set()
252+
)
250253
defaults = []
251254
for spec, value in self.defaults.items():
252255
value = parse_builtin_rule(value)
@@ -266,7 +269,7 @@ def contextify_form_name(f):
266269
attributes=attributes,
267270
options=options,
268271
defaultvalues=defaults,
269-
builtin=self
272+
builtin=self,
270273
)
271274
if is_pymodule:
272275
definitions.pymathics[name] = definition
@@ -537,7 +540,7 @@ def get_constant(self, precision, evaluation, have_mpmath=False):
537540

538541
sympy_fn = self.to_sympy()
539542
if d is None:
540-
result = self.get_mpmath_function()if have_mpmath else sympy_fn()
543+
result = self.get_mpmath_function() if have_mpmath else sympy_fn()
541544
return MachineReal(result)
542545
else:
543546
return PrecisionReal(sympy_fn.n(d))
@@ -630,15 +633,15 @@ def head(self):
630633

631634
@head.setter
632635
def head(self, value):
633-
raise ValueError('BoxConstruct.head is write protected.')
636+
raise ValueError("BoxConstruct.head is write protected.")
634637

635638
@property
636639
def leaves(self):
637640
return self._leaves
638641

639642
@leaves.setter
640643
def leaves(self, value):
641-
raise ValueError('BoxConstruct.leaves is write protected.')
644+
raise ValueError("BoxConstruct.leaves is write protected.")
642645

643646
# I need to repeat this, because this is not
644647
# an expression...
@@ -663,14 +666,17 @@ def has_form(self, heads, *leaf_counts):
663666
if leaf_counts and leaf_counts[0] is not None:
664667
count = len(self._leaves)
665668
if count not in leaf_counts:
666-
if (len(leaf_counts) == 2 and # noqa
667-
leaf_counts[1] is None and count >= leaf_counts[0]):
669+
if (
670+
len(leaf_counts) == 2
671+
and leaf_counts[1] is None # noqa
672+
and count >= leaf_counts[0]
673+
):
668674
return True
669675
else:
670676
return False
671677
return True
672678

673-
def flatten_pattern_sequence(self, evaluation) -> 'BoxConstruct':
679+
def flatten_pattern_sequence(self, evaluation) -> "BoxConstruct":
674680
return self
675681

676682
def get_option_values(self, leaves, **options):
@@ -681,6 +687,7 @@ def get_option_values(self, leaves, **options):
681687
default.update(options)
682688
else:
683689
from mathics.core.parser import parse_builtin_rule
690+
684691
default = {}
685692
for option, value in self.options.items():
686693
option = ensure_context(option)

0 commit comments

Comments
 (0)