From 007bd83a2ab18efcf1aeace499674b4127ebeb7c Mon Sep 17 00:00:00 2001 From: rocky Date: Mon, 30 Sep 2024 22:14:06 -0400 Subject: [PATCH] Start CheckAbort[] builtin function Just basic implementation. Right now, we don't handle options. --- CHANGES.rst | 5 +++-- mathics/builtin/procedural.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index e8a99c4bc..e7b23fa00 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -6,6 +6,7 @@ CHANGES New Builtins ++++++++++++ +* ``CheckAbort`` * ``SetEnvironment`` Compatibility @@ -22,9 +23,9 @@ Operator precedence has been gone over and is picked up in tables from the Mathi Performance ----------- -* `Blank*` patterns without arguments are now singletons. +* ``Blank*`` patterns without arguments are now singletons. + - 7.0.0 ----- diff --git a/mathics/builtin/procedural.py b/mathics/builtin/procedural.py index 9d1eef551..daff081d8 100644 --- a/mathics/builtin/procedural.py +++ b/mathics/builtin/procedural.py @@ -152,6 +152,36 @@ def eval_with_form_and_fn(self, expr, form, f, evaluation): return ret +class CheckAbort(Builtin): + """ + :WMA link: + https://reference.wolfram.com/language/ref/CheckAbort.html + +
+
'CheckAbort[$expr$, $failexpr$]' +
evaluates $expr, returning $failexpr$ if an abort occurs. +
+ + >> CheckAbort[Abort[]; 1, 2] + x + = 2 + x + + >> CheckAbort[1, 2] + x + = 1 + x + """ + + attributes = A_HOLD_ALL | A_PROTECTED + + summary_text = "catch an Abort[] exception" + + def eval(self, expr, failexpr, evaluation): + "CheckAbort[expr_, failexpr_]" + + try: + return expr.evaluate(evaluation) + except AbortInterrupt: + return failexpr + + class CompoundExpression(BinaryOperator): """ :WMA link: