Skip to content

Commit a1f3fe5

Browse files
committed
merge useasyforxmlgraphics
2 parents b905fc9 + 9079351 commit a1f3fe5

5 files changed

Lines changed: 65 additions & 11 deletions

File tree

CHANGES.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,12 @@ Document updates
8989
Enhancements and Bug fixes:
9090
+++++++++++++++++++++++++++
9191

92-
- Fix evaluation timeouts
92+
- Fix evaluation timeouts.
9393
- ``Sum``'s lower and upper bounds values can now be Mathics expressions
94-
94+
- Partial fix for MathML 2D Graphics. Requires the installation of the package Asymptote, and to set
95+
the variable ``Settings`UseAsyForGraphics2D`` to ``System`True``.
96+
- Set the numeric threshold in PossibleZeroQ to 10^(-10), for compatibility with ``System`Chop``.
97+
- Catch possible exceptions generated by ``$UserName``.
9598

9699

97100
Miscelanea

mathics/builtin/arithmetic.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,14 +1343,13 @@ class PossibleZeroQ(SympyFunction):
13431343
def apply(self, expr, evaluation):
13441344
"%(name)s[expr_]"
13451345
from sympy.matrices.utilities import _iszero
1346-
13471346
sympy_expr = expr.to_sympy()
13481347
result = _iszero(sympy_expr)
13491348
if result is None:
13501349
# try expanding the expression
13511350
exprexp = Expression("ExpandAll", expr).evaluate(evaluation)
13521351
exprexp = exprexp.to_sympy()
1353-
result = _iszero(exprexp)
1352+
result = _iszero(exprexp)
13541353
if result is None:
13551354
# Can't get exact answer, so try approximate equal
13561355
numeric_val = Expression("N", expr).evaluate(evaluation)
@@ -1364,7 +1363,6 @@ def apply(self, expr, evaluation):
13641363
if Expression("Simplify", expr).evaluate(evaluation) == Integer(0)
13651364
else SymbolFalse
13661365
)
1367-
13681366
return from_python(result)
13691367

13701368

mathics/builtin/graphics.py

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3145,7 +3145,7 @@ def get_range(min, max):
31453145

31463146
return elements, calc_dimensions
31473147

3148-
def boxes_to_tex(self, leaves=None, **options):
3148+
def boxes_to_tex(self, leaves=None, forxml=False, **options):
31493149
if not leaves:
31503150
leaves = self._leaves
31513151
elements, calc_dimensions = self._prepare_elements(
@@ -3181,14 +3181,12 @@ def boxes_to_tex(self, leaves=None, **options):
31813181
asy_background = ""
31823182

31833183
tex = r"""
3184-
\begin{asy}
31853184
usepackage("amsmath");
31863185
size(%scm, %scm);
31873186
%s
31883187
%s
31893188
clip(%s);
31903189
%s
3191-
\end{asy}
31923190
""" % (
31933191
asy_number(width / 60),
31943192
asy_number(height / 60),
@@ -3198,11 +3196,58 @@ def boxes_to_tex(self, leaves=None, **options):
31983196
asy_completely_visible,
31993197
)
32003198

3201-
return tex
3199+
if forxml:
3200+
return (tex, width, height)
3201+
else:
3202+
return "\n\\begin{asy}\n" + tex + "\n\\end{asy}\n"
32023203

32033204
def boxes_to_xml(self, leaves=None, **options):
32043205
if not leaves:
32053206
leaves = self._leaves
3207+
evaluation = options.get("evaluation", None)
3208+
check_asy = False
3209+
if evaluation:
3210+
check_asy = evaluation.definitions.get_ownvalue("Settings`UseAsyForGraphics2D")
3211+
if check_asy:
3212+
check_asy = check_asy.replace.is_true()
3213+
if check_asy:
3214+
import os
3215+
from subprocess import DEVNULL, STDOUT, check_call
3216+
import tempfile
3217+
try:
3218+
check_call(['asy', '--version'], stdout=DEVNULL, stderr=DEVNULL)
3219+
except:
3220+
check_asy = False
3221+
3222+
if check_asy:
3223+
asy, width, height = self.boxes_to_tex(leaves, forxml=True, **options)
3224+
fin = os.path.join(tempfile._get_default_tempdir(), next(tempfile._get_candidate_names()))
3225+
fout = fin + ".svg"
3226+
with open(fin, 'w+') as borrador:
3227+
borrador.write(asy)
3228+
3229+
try:
3230+
check_call(['asy', '-f', 'svg', '--svgemulation' ,'-o', fout, fin], stdout=DEVNULL, stderr=DEVNULL)
3231+
except:
3232+
print("Asy failed to build a svg")
3233+
check_asy = False
3234+
3235+
if check_asy:
3236+
with open(fout, 'rt') as ff:
3237+
svg = ff.read()
3238+
3239+
svg = svg[svg.find("<svg "):]
3240+
return (
3241+
'<img width="%dpx" height="%dpx" src="data:image/svg+xml;base64,%s"/>'
3242+
% (
3243+
int(width),
3244+
int(height),
3245+
base64.b64encode(svg.encode("utf8")).decode("utf8"),
3246+
)
3247+
)
3248+
else:
3249+
print("Asy not available. Continue with standard")
3250+
32063251
elements, calc_dimensions = self._prepare_elements(leaves, options, neg_y=True)
32073252

32083253
xmin, xmax, ymin, ymax, w, h, width, height = calc_dimensions()

mathics/builtin/system.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,16 @@ class UserName(Predefined):
406406

407407
name = "$UserName"
408408

409+
messages = {
410+
'nologin': "UserName not available in this system.",
411+
}
412+
409413
def evaluate(self, evaluation) -> String:
410-
return String(os.getlogin())
414+
try:
415+
return String(os.getlogin())
416+
except:
417+
evaluation.message('$UserName', 'nologin')
418+
return
411419

412420

413421
class Version(Predefined):

mathics/core/expression.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2117,7 +2117,7 @@ def __neg__(self) -> 'Rational':
21172117

21182118
@property
21192119
def is_zero(self) -> bool:
2120-
return self.numerator().is_zero and not self.denominator().is_zero()
2120+
return self.numerator().is_zero and not(self.denominator().is_zero)
21212121

21222122

21232123
class Real(Number):

0 commit comments

Comments
 (0)