Skip to content

Commit c54808f

Browse files
committed
* Fix Pattern.create in a way that handles input expr that are not Atom or Expression, but have a to_expression method.
* Fix BoxExpression.sameQ to avoid infinite loops. * adds the pattern_sort parameter to the ``BuiltinExpression.get_sort_key`` method.
1 parent 2ffdf02 commit c54808f

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

CHANGES.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Bugs
5050

5151
# ``0`` with a given precision (like in ```0`3```) is now parsed as ``0``, an integer number.
5252
#. ``RandomSample`` with one list argument now returns a random ordering of the list items. Previously it would return just one item.
53-
53+
#. ``mathics.core.Pattern.create`` now catches the case when the input parameter is not an `Atom` or an `Expression`.
5454

5555
Enhancements
5656
++++++++++++

mathics/builtin/base.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -818,15 +818,20 @@ def get_head_name(self):
818818
def get_lookup_name(self):
819819
return self.get_name()
820820

821-
def get_sort_key(self) -> tuple:
821+
def get_sort_key(self, pattern_sort=False) -> tuple:
822822
return self.to_expression().get_sort_key()
823823

824824
def get_string_value(self):
825825
return "-@" + self.get_head_name() + "@-"
826826

827827
def sameQ(self, expr) -> bool:
828828
"""Mathics SameQ"""
829-
return expr.sameQ(self)
829+
if expr is self:
830+
return True
831+
# Otherwise, we need to convert
832+
# self into an to expression
833+
# to avoid an infinite loop...
834+
return self.to_expression().sameQ(expr)
830835

831836
def do_format(self, evaluation, format):
832837
return self

mathics/core/pattern.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,25 @@ class Pattern:
8080
def create(expr: BaseElement) -> "Pattern":
8181
"""
8282
If ``expr`` is listed in ``pattern_object`` return the pattern found there.
83-
Otherwise, if ``expr`` is an ``Atom``, create and return ``AtomPattern`` for ``expr``.
84-
Otherwise, create and return and ``ExpressionPattern`` for ``expr``.
83+
Otherwise, if ``expr`` is an ``Atom``, create and return ``AtomPattern`` for ``expr``;
84+
if it is an ``Expression`` create and return and ``ExpressionPattern`` for ``expr``.
85+
Finally, it tries to get one object of one of these classes
86+
by calling the method ``to_expression``, and then call the function
87+
again with this new object as input.
8588
"""
86-
8789
name = expr.get_head_name()
8890
pattern_object = pattern_objects.get(name)
8991
if pattern_object is not None:
9092
return pattern_object(expr)
9193
if isinstance(expr, Atom):
9294
return AtomPattern(expr)
93-
else:
95+
elif isinstance(expr, Expression):
9496
return ExpressionPattern(expr)
97+
# To handle Boxes and other possible objects that
98+
# can be converted into expressions, convert expr to
99+
# expression and call this function
100+
# again:
101+
return Pattern.create(expr.to_expression())
95102

96103
def match(
97104
self,

0 commit comments

Comments
 (0)