Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More on Builtin.contribute and Builtin.get_functions. #1298

Merged
merged 6 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions mathics/core/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,16 @@ def __init__(self, *args, **kwargs):
def contribute(self, definitions: Definitions, is_pymodule=False):
from mathics.core.parser import parse_builtin_rule

# Set the default context
if not self.context:
self.context = "Pymathics`" if is_pymodule else "System`"
name = self.get_name()
attributes = self.attributes
options = {}
# Set the default context
if not self.context:
self.context = "Pymathics`" if is_pymodule else "System`"
# get_name takes the context from the class, not from the
# instance, so even if we set the context here,
# self.get_name() does not includes the context.
name = self.context + name

# - 'Strict': warn and fail with unsupported options
# - 'Warn': warn about unsupported options, but continue
Expand Down Expand Up @@ -468,7 +472,15 @@ def get_functions(self, prefix="eval", is_pymodule=False):
if pattern is None: # Fixes PyPy bug
continue
else:
m = re.match(r"[(]([\w,]+),[)]\:\s*(.*)", pattern)
# TODO: consider to use a more sophisticated
# regular expression, which handles breaklines
# more properly, that supports format names
# with contexts (context`name) and be less
# fragile against leaving spaces between the
# elements.
m = re.match(
r"[(]([\w,]+),[ ]*[)]\:\s*(.*)", pattern.replace("\n", " ")
rocky marked this conversation as resolved.
Show resolved Hide resolved
)
if m is not None:
attrs = m.group(1).split(",")
pattern = m.group(2)
Expand Down
63 changes: 63 additions & 0 deletions test/core/test_builtin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""
Test how builtins are loaded
"""

from mathics.builtin.makeboxes import MakeBoxes
from mathics.core.builtin import Builtin
from mathics.core.definitions import Definitions


def test_contribute_builtin():
"""Test for Builtin.contribute."""

definitions = Definitions()
MakeBoxes.context = "System`"
MakeBoxes(expression=False).contribute(definitions)

class TestBuiltin(Builtin):
"""
<dl>
<dt>'TestBuiltin'[$x$]
<dd>nothing
</dl>
"""

messages = {
"nomsg": "Test message `1`.",
}

def eval_downvalue(self, expr, evaluation):
"""expr: TestBuiltin[_:Symbol]"""
return

def eval_upvalue(self, expr, x, evaluation):
"""expr: G[TestBuiltin[x_:Symbol]]"""
return

def format_parm1(self, expr, x, evaluation):
"""(CustomForm,): expr: F[x_:Symbol]"""
return

def format_parm2(self, expr, x, y, evaluation):
"""(MakeBoxes, ):expr: G[x_:Symbol,
rocky marked this conversation as resolved.
Show resolved Hide resolved
y_]
"""
return

def format_parmb(self, expr, x, y, evaluation):
"""(OutputForm,): expr: G[x_:Symbol,
y:P|Q]
"""
return

TestBuiltin(expression=False).contribute(definitions)
assert "System`TestBuiltin" in definitions.builtin.keys()
definition = definitions.get_definition("System`TestBuiltin")
# Check that the formats are loaded into the right places.
assert "System`MakeBoxes" in definition.formatvalues
assert "System`CustomForm" in definition.formatvalues
assert "System`OutputForm" in definition.formatvalues
# Test if the rules are loaded into the right place.
assert definition.upvalues
assert definition.downvalues
assert definition.messages
Loading