Skip to content

Commit

Permalink
the true fix:
Browse files Browse the repository at this point in the history
* adding comments
* moving _initialize_system_definitions to a better place, and hide it.
* fixing contribute in order to make work the pymathics loading mechanism without overwrite builtins
  • Loading branch information
mmatera committed Nov 22, 2022
1 parent f1ccc6d commit 53300f5
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 30 deletions.
30 changes: 28 additions & 2 deletions mathics/builtin/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import importlib
import re

from functools import lru_cache, total_ordering
from itertools import chain
from typing import Any, Callable, Dict, Iterable, List, Optional, Union, cast
Expand Down Expand Up @@ -291,7 +292,13 @@ def check_options(options_to_check, evaluation):
)

box_rules = []
# FIXME: Why a special case for System`MakeBoxes? Remove this

# System`MakeBoxes comes first because all the other symbols contribute to it.
# Then, a definition of this symbol must be available from the begining.
# Probably, this is not a good approach, since it makes that MakeBoxes has
# a very large number of inespecific rules. A better approach would imply at least
# to store the rules as upvalues of the symbols associated to these rules.
#
if name != "System`MakeBoxes":
new_rules = []
for rule in rules:
Expand Down Expand Up @@ -389,13 +396,32 @@ def contextify_form_name(f):
)
if is_pymodule:
definitions.pymathics[name] = definition
try:
makeboxes_def = definitions.pymathics["System`MakeBoxes"]
except KeyError:
builtin_mb_def = definitions.builtin["System`MakeBoxes"]
makeboxes_def = Definition(
"System`MakeBoxes",
builtin=builtin_mb_def.builtin,
attributes=builtin_mb_def.attributes,
is_numeric=builtin_mb_def.is_numeric,
)
definitions.pymathics["System`MakeBoxes"] = makeboxes_def
else:
definitions.builtin[name] = definition
makeboxes_def = definitions.builtin["System`MakeBoxes"]

makeboxes_def = definitions.builtin["System`MakeBoxes"]
for rule in box_rules:
makeboxes_def.add_rule(rule)

# If a pymathics module was loaded, then there are at least two
# definitions for MakeBoxes, the builtin, the pymathics, and the
# cached from combining the former two. Rules are added to one of
# both, which are not going to be in the cached version that
# Definitions.get_definition provides. To make the new rules
# accesible, we need to clear the definitions cache.
definitions.clear_cache("System`MakeBoxes")

@classmethod
def get_name(cls, short=False) -> str:
if cls.name is None:
Expand Down
4 changes: 2 additions & 2 deletions mathics/docpipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import mathics
import mathics.settings

from mathics.core.definitions import Definitions, initialize_system_definitions
from mathics.core.definitions import Definitions
from mathics.core.evaluation import Evaluation, Output
from mathics.core.parser import MathicsSingleLineFeeder
from mathics.builtin.system_init import builtins_dict
Expand Down Expand Up @@ -446,7 +446,7 @@ def main():
global definitions
global logfile
global check_partial_enlapsed_time
initialize_system_definitions()

definitions = Definitions(add_builtin=True)

parser = ArgumentParser(description="Mathics test suite.", add_help=False)
Expand Down
7 changes: 1 addition & 6 deletions mathics/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from mathics.builtin.system_init import autoload_files
from mathics.core.parser import parse, MathicsSingleLineFeeder
from mathics.core.definitions import Definitions, initialize_system_definitions
from mathics.core.definitions import Definitions
from mathics.core.evaluation import Evaluation
import mathics.settings

Expand Down Expand Up @@ -72,11 +72,6 @@ def reset(self, add_builtin=True, catch_interrupt=False):
"""
reset the definitions and the evaluation objects.
"""
# TODO: Due to a fail in the Definitions __init__ method,
# we need to initialize this each time Definitions is
# instantiated. When it gets fixed, the next line
# can be removed.
initialize_system_definitions()
self.definitions = Definitions(add_builtin)
self.evaluation = Evaluation(
definitions=self.definitions, catch_interrupt=catch_interrupt
Expand Down
1 change: 0 additions & 1 deletion test/format/test_asy.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from mathics.session import MathicsSession
from mathics.builtin.makeboxes import MakeBoxes


session = MathicsSession(add_builtin=True, catch_interrupt=False)
evaluation = Evaluation(session.definitions)

Expand Down
2 changes: 0 additions & 2 deletions test/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
from typing import Optional

from mathics.session import MathicsSession
from mathics.core.definitions import initialize_system_definitions

initialize_system_definitions()

# Set up a Mathics session with definitions.
# For consistency set the character encoding ASCII which is
Expand Down
34 changes: 17 additions & 17 deletions test/test_custom_boxexpression.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ def __init__(self, evaluation):
super().__init__(evaluation=evaluation)
self._elements = [1, 2, 3]

def boxes_to_text(self, leaves=None, **options):
if not leaves:
leaves = self.elements
def boxes_to_text(self, elements=None, **options):
if not elements:
elements = self.elements
return "CustomBoxExpression<<" + self.elements.__str__() + ">>"

def boxes_to_mathml(self, leaves=None, **options):
if not leaves:
leaves = self.elements
def boxes_to_mathml(self, elements=None, **options):
if not elements:
elements = self.elements
return "CustomBoxExpression<<" + self.elements.__str__() + ">>"

def boxes_to_tex(self, leaves=None, **options):
if not leaves:
leaves = self.elements
def boxes_to_tex(self, elements=None, **options):
if not elements:
elements = self.elements
return "CustomBoxExpression<<" + int(self.elements) + ">>"


Expand Down Expand Up @@ -59,25 +59,25 @@ def init(self, *elems, **options):
def to_expression(self):
return Expression(SymbolCustomGraphicsBox, *self.elements)

def apply_box(self, elems, evaluation, options):
"""System`MakeBoxes[System`Graphics[elems_, System`OptionsPattern[System`Graphics]],
def apply_box(self, expr, evaluation, options):
"""System`MakeBoxes[System`Graphics[System`expr_, System`OptionsPattern[System`Graphics]],
System`StandardForm|System`TraditionalForm|System`OutputForm]"""
instance = CustomGraphicsBox(*(elems.elements), evaluation=evaluation)
instance = CustomGraphicsBox(*(expr.elements), evaluation=evaluation)
return instance

def boxes_to_text(self, leaves=None, **options):
if leaves:
self._elements = leaves
def boxes_to_text(self, elements=None, **options):
if elements:
self._elements = elements
return (
"--custom graphics--: I should plot " + self.elements.__str__() + " items"
)

def boxes_to_tex(self, leaves=None, **options):
def boxes_to_tex(self, elements=None, **options):
return (
"--custom graphics--: I should plot " + self.elements.__str__() + " items"
)

def boxes_to_mathml(self, leaves=None, **options):
def boxes_to_mathml(self, elements=None, **options):
return (
"--custom graphics--: I should plot " + self.elements.__str__() + " items"
)
Expand Down

0 comments on commit 53300f5

Please sign in to comment.