Skip to content

Commit

Permalink
Move more code
Browse files Browse the repository at this point in the history
  • Loading branch information
rocky committed Dec 4, 2022
1 parent 8383f92 commit 2eed518
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 43 deletions.
33 changes: 2 additions & 31 deletions mathics/builtin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
among other things.
"""

import glob
import importlib
import os.path as osp
import pkgutil
import re
from typing import List
Expand All @@ -34,16 +32,11 @@
SympyObject,
mathics_to_python,
)
from mathics.builtin.system_init import get_builtin_pyfiles, name_is_builtin_symbol
from mathics.core.pattern import pattern_objects
from mathics.settings import ENABLE_FILES_MODULE

# Get a list of files in this directory. We'll exclude from the start
# files with leading characters we don't want like __init__ with its leading underscore.
__py_files__ = [
osp.basename(f[0:-3])
for f in glob.glob(osp.join(osp.dirname(__file__), "[a-z]*.py"))
]

__py_files__ = get_builtin_pyfiles()

mathics_to_sympy = {} # here we have: name -> sympy object
sympy_to_mathics = {}
Expand Down Expand Up @@ -73,26 +66,6 @@ def add_builtins(new_builtins):
system_builtins.update(dict(new_builtins))


def contribute(definitions):
# let MakeBoxes contribute first
system_builtins["System`MakeBoxes"].contribute(definitions)
for name, item in system_builtins.items():
if name != "System`MakeBoxes":
item.contribute(definitions)

from mathics.core.definitions import Definition
from mathics.core.expression import ensure_context
from mathics.core.parser import all_operator_names

# All builtins are loaded. Create dummy builtin definitions for
# any remaining operators that don't have them. This allows
# operators like \[Cup] to behave correctly.
for operator in all_operator_names:
if not definitions.have_definition(ensure_context(operator)):
op = ensure_context(operator)
definitions.builtin[op] = Definition(name=op)


def import_builtins(module_names: List[str], submodule_name=None) -> None:
"""
Imports the list of Mathics Built-in modules so that inside
Expand Down Expand Up @@ -175,8 +148,6 @@ def import_module(module_name: str, import_name: str):
# print("XXX3", submodule_names)
import_builtins(submodule_names, subdir)

from mathics.builtin.system_init import name_is_builtin_symbol

for module in modules:
builtins_by_module[module.__name__] = []
module_vars = dir(module)
Expand Down
49 changes: 38 additions & 11 deletions mathics/builtin/system_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@
# list of Builtin classes.
builtins_by_module = {}
builtins_list = []

# Get a list of files in this directory. We'll exclude from the start
# files with leading characters we don't want like __init__ with its leading underscore.
__py_files__ = [
osp.basename(f[0:-3])
for f in glob.glob(osp.join(osp.dirname(__file__), "[a-z]*.py"))
]

builtins_precedence = {}

system_builtins = {}
Expand Down Expand Up @@ -65,6 +57,26 @@ def builtins_dict():
}


def contribute(definitions):
# let MakeBoxes contribute first
system_builtins["System`MakeBoxes"].contribute(definitions)
for name, item in system_builtins.items():
if name != "System`MakeBoxes":
item.contribute(definitions)

from mathics.core.definitions import Definition
from mathics.core.expression import ensure_context
from mathics.core.parser import all_operator_names

# All builtins are loaded. Create dummy builtin definitions for
# any remaining operators that don't have them. This allows
# operators like \[Cup] to behave correctly.
for operator in all_operator_names:
if not definitions.have_definition(ensure_context(operator)):
op = ensure_context(operator)
definitions.builtin[op] = Definition(name=op)


def create_builtins_by_module():
from mathics.builtin import modules

Expand All @@ -90,6 +102,17 @@ def create_builtins_by_module():
add_builtins(builtins_list)


def get_builtin_pyfiles() -> tuple:
"""
Return a list of files in this directory. We'll exclude from the start
files with leading characters we don't want like __init__ with its leading underscore.
"""
return tuple(
osp.basename(f[0:-3])
for f in glob.glob(osp.join(osp.dirname(__file__), "[a-z]*.py"))
)


display_operators_set = set()

# TODO: after doing more, e.g. moving Builtin class processing,
Expand All @@ -103,9 +126,6 @@ def initialize_system():
Not much here but more may be added.
"""
create_builtins_by_module()
from mathics.builtin import __py_files__ as old_py_files

assert __py_files__ == old_py_files
for modname, builtins in builtins_by_module.items():
for builtin in builtins:
# name = builtin.get_name()
Expand Down Expand Up @@ -156,6 +176,9 @@ def name_is_builtin_symbol(module, name: str) -> Optional[type]:
return module_object


# FIXME: Checking can be done as an outside
# utility program. Think for example, "flake8", "black", or "isort".

# Set this to True to print all the builtins that do not have
# a summary_text. In the future, we can set this to True
# and raise an error if a new builtin is added without
Expand All @@ -164,6 +187,10 @@ def name_is_builtin_symbol(module, name: str) -> Optional[type]:


def sanity_check(cls, module):
"""
Look for Builtin classes that do not have a ``summary_text``
and do not have a class attribute "context" set to "Internal"
"""
if not RUN_SANITY_TEST:
return True

Expand Down
3 changes: 2 additions & 1 deletion mathics/core/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ def __init__(
self.timing_trace_evaluation = False

if add_builtin:
from mathics.builtin import modules, contribute
from mathics.builtin import modules
from mathics.builtin.system_init import contribute
from mathics.settings import ROOT_DIR

loaded = False
Expand Down

0 comments on commit 2eed518

Please sign in to comment.