Skip to content

SympyFunction.to_sympy: an argument SymPy rejects leaves it unevaluated - #2043

Merged
rocky merged 3 commits into
Mathics3:masterfrom
tombouctou:audit-sympy-errors
Oct 18, 2026
Merged

rocky merged 3 commits into
Mathics3:masterfrom
tombouctou:audit-sympy-errors

Conversation

@tombouctou

Copy link
Copy Markdown
Collaborator

SympyFunction.to_sympy caught only TypeError from the SymPy call. SymPy
also answers a meaningless argument with AttributeError or ValueError,
e.g. asin(true): 'BooleanTrue' object has no attribute 'as_coefficient'.
These escaped as Python exceptions, so Sin[True], Log[True], ArcTan[False],
Erf[True], Conjugate[True], EuclideanDistance[True, 2] and the like
crashed the session. In our fuzzing this affected 46 builtins.

Such an expression now has no SymPy form and stays unevaluated, as for
a TypeError.

To reproduce on master:

Sin[True]   (* AttributeError: 'BooleanTrue' object has no attribute 'as_coefficient' *)
Log[True]   (* same kind *)

Tests: new cases in test/builtin/numbers/test_trig.py (test_trig). They fail on current master and pass with this change.

The full pytest suite and the doctests pass with this change (run together with the other fixes from the same audit; the only pytest failures, in test_main.py and test_returncode.py, are the same as on master and come from the mathics script not being on PATH in our environment).

Found in a correctness audit of Mathics3 (differential checks against SymPy/mpmath/exact computation, and calls of every builtin with edge-case arguments).

🤖 Generated with Claude Code

SympyFunction.to_sympy caught only TypeError from the SymPy call. SymPy
also answers a meaningless argument with AttributeError or ValueError,
e.g. asin(true): 'BooleanTrue' object has no attribute 'as_coefficient'.
These escaped as Python exceptions, so Sin[True], Log[True], ArcTan[False],
Erf[True], Conjugate[True], EuclideanDistance[True, 2] and the like
crashed the session. In our fuzzing this affected 46 builtins.

Such an expression now has no SymPy form and stays unevaluated, as for
a TypeError.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread test/builtin/numbers/test_trig.py Outdated
("N[Sin[1], 40]", None, "0.8414709848078965066525023216302989996226", None),
("Tan[0.5 Pi]", None, "1.63312×10^16", None),
# Arguments SymPy rejects leave the expression unevaluated.
("Sin[True]", [], "Sin[True]", None),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please enhance the failure messages.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in dbd9ca4: each new case now says what it checks and why (SymPy rejects the Boolean argument, so the expression stays unevaluated).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rocky

rocky commented Oct 18, 2026

Copy link
Copy Markdown
Member

LGTM

@rocky

rocky commented Oct 18, 2026

Copy link
Copy Markdown
Member

LGTM

@rocky
rocky merged commit 0e0aba0 into Mathics3:master Oct 18, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants