diff --git a/mathics/core/definitions.py b/mathics/core/definitions.py index 8213879e6..bd2d8d3bf 100644 --- a/mathics/core/definitions.py +++ b/mathics/core/definitions.py @@ -15,7 +15,7 @@ from mathics.core.convert.expression import to_mathics_list from mathics.core.element import fully_qualified_symbol_name from mathics.core.expression import Expression -from mathics.core.load_builtin import definition_contribute, modules +from mathics.core.load_builtin import definition_contribute, mathics3_builtins_modules from mathics.core.symbols import Atom, Symbol, strip_context from mathics.core.systemsymbols import SymbolGet from mathics.settings import ROOT_DIR @@ -139,10 +139,12 @@ def __init__( self.timing_trace_evaluation = False if add_builtin: - loaded = False if builtin_filename is not None: - builtin_dates = [get_file_time(module.__file__) for module in modules] + builtin_dates = [ + get_file_time(module.__file__) + for module in mathics3_builtins_modules + ] builtin_time = max(builtin_dates) if get_file_time(builtin_filename) > builtin_time: builtin_file = open(builtin_filename, "rb") @@ -769,7 +771,6 @@ def __init__( builtin=None, is_numeric=False, ) -> None: - super(Definition, self).__init__() self.name = name diff --git a/mathics/core/load_builtin.py b/mathics/core/load_builtin.py index a40399b84..a2a3453e2 100755 --- a/mathics/core/load_builtin.py +++ b/mathics/core/load_builtin.py @@ -12,6 +12,7 @@ import os.path as osp import pkgutil from glob import glob +from types import ModuleType from typing import List, Optional from mathics.core.pattern import pattern_objects @@ -19,14 +20,17 @@ from mathics.eval.makeboxes import builtins_precedence from mathics.settings import ENABLE_FILES_MODULE -# List of Mathics3 Builtin modules. -# This is initialized via below import_builtins modules -modules = [] +# List of Python modules contain Mathics3 Builtins. +# This list used outside to gather documentation, +# and test module consistency. It is +# is initialized via below import_builtins modules +mathics3_builtins_modules: List[ModuleType] = [] _builtins = {} builtins_by_module = {} display_operators_set = set() + # The fact that are importing inside here, suggests add_builtins # should get moved elsewhere. def add_builtins(new_builtins): @@ -56,7 +60,7 @@ def add_builtins(new_builtins): _builtins.update(dict(new_builtins)) -def add_builtins_from_builtin_modules(modules): +def add_builtins_from_builtin_modules(modules: List[ModuleType]): # This can be put at the top after mathics.builtin.__init__ # cleanup is done. from mathics.builtin.base import Builtin @@ -129,7 +133,7 @@ def import_and_load_builtins(): ) exclude_files = {"codetables", "base"} module_names = get_module_names(builtin_path, exclude_files) - import_builtins(module_names, modules) + import_builtins(module_names, mathics3_builtins_modules) # Get import modules in subdirectories of this directory of Python # modules that contain Mathics3 Builtin class definitions. @@ -140,16 +144,20 @@ def import_and_load_builtins(): disable_file_module_names = set() if ENABLE_FILES_MODULE else {"files_io"} subdirectories = next(os.walk(builtin_path))[1] - import_builtin_subdirectories(subdirectories, disable_file_module_names, modules) + import_builtin_subdirectories( + subdirectories, disable_file_module_names, mathics3_builtins_modules + ) - add_builtins_from_builtin_modules(modules) + add_builtins_from_builtin_modules(mathics3_builtins_modules) initialize_display_operators_set() # TODO: When we drop Python 3.7, # module_names can be a List[Literal] def import_builtins( - module_names: List[str], modules: list, submodule_name: Optional[str] = None + module_names: List[str], + modules: List[ModuleType], + submodule_name: Optional[str] = None, ): """ Imports the list of Mathics3 Built-in modules so that inside @@ -210,7 +218,7 @@ def initialize_display_operators_set(): display_operators_set.add(operator) -def name_is_builtin_symbol(module, name: str) -> Optional[type]: +def name_is_builtin_symbol(module: ModuleType, name: str) -> Optional[type]: """ Checks if ``name`` should be added to definitions, and return its associated Builtin class. diff --git a/mathics/doc/common_doc.py b/mathics/doc/common_doc.py index a5e4e59a5..617c43e27 100644 --- a/mathics/doc/common_doc.py +++ b/mathics/doc/common_doc.py @@ -36,7 +36,7 @@ from mathics.core.evaluation import Message, Print from mathics.core.load_builtin import ( builtins_by_module as global_builtins_by_module, - modules as mathics3_builtin_modules, + mathics3_builtins_modules, ) from mathics.core.util import IS_PYPY from mathics.doc.utils import slugify @@ -114,7 +114,8 @@ SUBSECTION_RE = re.compile('(?s)') TESTCASE_RE = re.compile( - r"""(?mx)^ # re.MULTILINE (multi-line match) and re.VERBOSE (readable regular expressions + r"""(?mx)^ # re.MULTILINE (multi-line match) + # and re.VERBOSE (readable regular expressions ((?:.|\n)*?) ^\s+([>#SX])>[ ](.*) # test-code indicator ((?:\n\s*(?:[:|=.][ ]|\.).*)*) # test-code results""" @@ -125,12 +126,6 @@ test_result_map = {} -def _replace_all(text, pairs): - for i, j in pairs: - text = text.replace(i, j) - return text - - def get_module_doc(module: ModuleType) -> tuple: doc = module.__doc__ if doc is not None: @@ -638,7 +633,7 @@ def gather_doctest_data(self): for title, modules, builtins_by_module, start in [ ( "Reference of Built-in Symbols", - mathics3_builtin_modules, + mathics3_builtins_modules, global_builtins_by_module, True, ) diff --git a/mathics/doc/latex_doc.py b/mathics/doc/latex_doc.py index d17f6f7cc..8dc6a53e0 100644 --- a/mathics/doc/latex_doc.py +++ b/mathics/doc/latex_doc.py @@ -35,7 +35,6 @@ DocText, Documentation, XMLDoc, - _replace_all, gather_tests, get_results_by_test, post_sub, @@ -104,7 +103,7 @@ def repl_python(match): text, post_substitutions = pre_sub(PYTHON_RE, text, repl_python) - text = _replace_all( + text = replace_all( text, [ ("\\", "\\\\"), @@ -120,7 +119,7 @@ def repl_python(match): def repl(match): text = match.group(1) if text: - text = _replace_all(text, [("\\'", "'"), ("^", "\\^")]) + text = replace_all(text, [("\\'", "'"), ("^", "\\^")]) escape_char = get_latex_escape_char(text) text = LATEX_RE.sub( lambda m: "%s%s\\codevar{\\textit{%s}}%s\\lstinline%s" @@ -163,7 +162,7 @@ def repl_list(match): text = LIST_RE.sub(repl_list, text) # FIXME: get this from MathicsScanner - text = _replace_all( + text = replace_all( text, [ ("$", r"\$"), @@ -313,7 +312,7 @@ def repl_subsection(match): def escape_latex_output(text) -> str: """Escape Mathics output""" - text = _replace_all( + text = replace_all( text, [ ("\\", "\\\\"), @@ -448,6 +447,12 @@ def repl_nonasy(match): return OUTSIDE_ASY_RE.sub(repl_nonasy, result) +def replace_all(text, pairs): + for i, j in pairs: + text = text.replace(i, j) + return text + + def strip_system_prefix(name): if name.startswith("System`"): stripped_name = name[len("System`") :] diff --git a/mathics/main.py b/mathics/main.py index 067be1bf4..51eb3efbd 100755 --- a/mathics/main.py +++ b/mathics/main.py @@ -32,6 +32,10 @@ from mathics.core.symbols import SymbolNull, strip_context from mathics.timing import show_lru_cache_statistics +# from mathics.timing import TimeitContextManager +# with TimeitContextManager("import_and_load_builtins()"): +# import_and_load_builtins() + import_and_load_builtins() diff --git a/mathics/version.py b/mathics/version.py index c36ca72bb..7b0a25141 100644 --- a/mathics/version.py +++ b/mathics/version.py @@ -5,4 +5,4 @@ # well as importing into Python. That's why there is no # space around "=" below. # fmt: off -__version__="6.1.0dev0" # noqa +__version__="7.0.0dev0" # noqa diff --git a/test/consistency-and-style/test_duplicate_builtins.py b/test/consistency-and-style/test_duplicate_builtins.py index 688310d4d..5ece63ccb 100644 --- a/test/consistency-and-style/test_duplicate_builtins.py +++ b/test/consistency-and-style/test_duplicate_builtins.py @@ -9,7 +9,7 @@ import pytest from mathics.builtin.base import Builtin -from mathics.core.load_builtin import modules, name_is_builtin_symbol +from mathics.core.load_builtin import mathics3_builtins_modules, name_is_builtin_symbol @pytest.mark.skipif( @@ -18,7 +18,7 @@ def test_check_duplicated(): msg = "" builtins_by_name = {} - for module in modules: + for module in mathics3_builtins_modules: vars = dir(module) for name in vars: var = name_is_builtin_symbol(module, name)