#20173 closed defect (fixed)
Error executing code in Mathematica
Reported by:  yzh  Owned by:  

Priority:  major  Milestone:  sage7.1 
Component:  interfaces  Keywords:  mathematica 
Cc:  mkoeppe, dimpase, mmarco, vbraun  Merged in:  
Authors:  Matthias Koeppe  Reviewers:  Dima Pasechnik 
Report Upstream:  N/A  Work issues:  
Branch:  0fdafe3 (Commits, GitHub, GitLab)  Commit:  
Dependencies:  Stopgaps: 
Description
I got TypeError
when running the same code repeatedly on OSX 10.10.4.
Each time it stops at a different value of i and returns a different error message.
m = Mathematica(logfile='mathematicalog') for i in range(30000): print i condstr = '{x + 5*y  1<0, x  y<0, 0<2*x  5*y + 1<1/100}' varstr = '{x, y}' pt = m.FindInstance(condstr, varstr) pt_val = (pt[1][1][2], pt[1][2][2])
Usually the error appears before i=2000, and the error message is most often of the type
TypeError: Error executing code in Mathematica CODE: sage3=sage0[[2]]; Mathematica ERROR: sage3=sage0[[2]];
Is it caused by the same issue as in #20128? Any workaround?
I'm also curious about the following piece of code in Mathematica::set
(see Line 514 of mathematica.py)
if len(out) > 8: raise TypeError("Error executing code in Mathematica\nCODE:\n\t%s\nMathematica ERROR:\n\t%s"%(cmd, out))
What does the constant 8 mean here? Note that the error persists when I augment the number 8 to 4000.
Change History (22)
comment:1 Changed 7 years ago by
comment:2 followup: 3 Changed 7 years ago by
I don't have access to Mathematica on machines I have Sage on, so I can't test this easily. If this echo disabling does not affect the UI in any way, why don't we put this workaround in Sage?
(To me stty echo
is what one does to hide what's entered on the keyboard, but here it's not relevant?)
comment:3 followup: 19 Changed 7 years ago by
Replying to dimpase:
I don't have access to Mathematica on machines I have Sage on, so I can't test this easily. If this echo disabling does not affect the UI in any way, why don't we put this workaround in Sage?
(To me
stty echo
is what one does to hide what's entered on the keyboard, but here it's not relevant?)
This stty echo
affects the pseudoterminal that Sage uses to communicate with Mathematica. It does not affect the Sage UI.
I have not tested whether this workaround has any negative effect on running Sage via SSH on a remote host.
comment:4 Changed 7 years ago by
Branch:  → u/mkoeppe/error_executing_code_in_mathematica 

comment:5 Changed 7 years ago by
Authors:  → Matthias Koeppe 

Commit:  → 0fdafe3c176f6292a33ece07b9043d940a8b4e1b 
Status:  new → needs_review 
New commits:
0fdafe3  Disable echo on Mathematica pty to avoid sporadic errors on Mac OS X

comment:6 followup: 7 Changed 7 years ago by
Expect has a terminal_echo=False
constructor argument, this is presumably equivalent and the right way to do it. See e.g. sage.interfaces.singular.Singular.__init__
The terminal echo has bitten us before, really all expect interfaces should run with terminal_echo=False
as the echo is inherently and unfixably racy. But various interfaces have been built around the assumption of terminal echo and are hard to fix.
comment:7 Changed 7 years ago by
Replying to vbraun:
Expect has a
terminal_echo=False
constructor argument, this is presumably equivalent and the right way to do it. See e.g.sage.interfaces.singular.Singular.__init__
That's what I tried first, but it didn't fix the problem.
comment:8 Changed 7 years ago by
I just checked this with Mathematica 9.0 on Linux 64bit, the test works.
I noticed that most doctests fail: I run
sage btp optional=database_gap,gap_packages,python2,sage,mathematica src/sage/interfaces/mathematica.py
and get a lot of formatting errors  the output is TeXlike rather than textlike, e.g.
File "src/sage/interfaces/mathematica.py", line 322, in sage.interfaces.mathematica Failed example: mlist = mathematica(slist); mlist # optional  mathematica Expected: {{1, 2}, 3., 4 + I} Got: \{\{1,2\},3.,4+i\} ********************************************************************** File "src/sage/interfaces/mathematica.py", line 326, in sage.interfaces.mathematica Failed example: slist2[0] # optional  mathematica Expected: {1, 2} Got: \{1,2\} ********************************************************************** File "src/sage/interfaces/mathematica.py", line 335, in sage.interfaces.mathematica Failed example: mathematica('10.^80') # optional  mathematica Expected: 1.*^80 Got: 1.\times 10^{80} ********************************************************************** File "src/sage/interfaces/mathematica.py", line 576, in sage.interfaces.mathematica.Mathematica.chdir Failed example: mathematica('Directory[]') # optional  mathematica Expected: "/" Got: /
should these get fixed?
comment:9 Changed 7 years ago by
It is quite likely that this is specific to both the Mathematica version and the OS.
For example, the beginning of mathreadline
script says:
# Cleverly run Mathematica with the benefit of readline, which # is something the usual commercial mathematica doesn't provide!
But at least Mathematica 10.3.1 does provide commandline editing (though not the full range of readline's keybindings).
comment:10 Changed 7 years ago by
I think a lot of tests are outdated; e.g. the precision in N() operator should be what Mathematica does now, that is the total number of digits rather than the number of digits after '.'
I do get commandline editing on my terminal version, too.
comment:11 Changed 7 years ago by
I wonder if there is a magic switch to switch to the form of output used in tests; don't know what it is exactly  FortranForm?
comment:12 followup: 15 Changed 7 years ago by
I think the failing Mathematica doctests should go on another ticket. This ticket needs review...
comment:13 Changed 7 years ago by
Reviewers:  → Dima Pasechnik 

Status:  needs_review → positive_review 
looks good to me
comment:14 Changed 7 years ago by
Branch:  u/mkoeppe/error_executing_code_in_mathematica → 0fdafe3c176f6292a33ece07b9043d940a8b4e1b 

Resolution:  → fixed 
Status:  positive_review → closed 
comment:15 followup: 16 Changed 7 years ago by
Commit:  0fdafe3c176f6292a33ece07b9043d940a8b4e1b 

comment:16 followup: 17 Changed 7 years ago by
Replying to slabbe:
Replying to mkoeppe:
I think the failing Mathematica doctests should go on another ticket.
See #18888.
thanks. I completely forgot about it :)
One problem is that the version of Mathematica may change from one user to the other...
no, if you read info on #18888 you will see it has not much to do with versions, it is a change in behaviour.
comment:17 Changed 7 years ago by
Cc:  vbraun added 

Replying to dimpase:
Replying to slabbe:
Replying to mkoeppe:
I think the failing Mathematica doctests should go on another ticket.
See #18888.
thanks. I completely forgot about it :)
One problem is that the version of Mathematica may change from one user to the other...
no, if you read info on #18888 you will see it has not much to do with versions, it is a change in behaviour.
caused by Volker's commit 7747261d ($ git show 7747261d
will show it).
I suspect the reason was to have a better output in a notebook (sagenb or jupyther or whatever).
I have no clue how to fix this.
comment:19 followup: 20 Changed 6 years ago by
Replying to mkoeppe:
This
stty echo
affects the pseudoterminal that Sage uses to communicate with Mathematica. It does not affect the Sage UI.
I disagree with this statement, in fact it should be precisely the opposite: I don't think that the pexpect
interface needs readline, so why would it run mathreadline
instead of the plain Mathematica (i.e. math
) directly?
This is at least partially responsible for the breakage reported on #23112.
comment:20 Changed 6 years ago by
Replying to jdemeyer:
Replying to mkoeppe:
This
stty echo
affects the pseudoterminal that Sage uses to communicate with Mathematica. It does not affect the Sage UI.I disagree with this statement, in fact it should be precisely the opposite: I don't think that the
pexpect
interface needs readline, so why would it runmathreadline
instead of the plain Mathematica (i.e.math
) directly?This is at least partially responsible for the breakage reported on #23112.
The statement that you quoted is correct as stated.
Rather, what went wrong on this ticket is: It was overlooked that this script mathreadline
is intended to be a user script, rather than just one that is used internally by the pexpect interface. (The script is not mentioned in a manual.)
Also, note that current versions of mathematica (like "Mathematica 11.0.1 for Mac OS X x86") do provide a readlinelike interface, so mathreadline
is not needed; so certainly the description of mathreadline
is outdated.
We can fix the script on #23112 (for the benefit of users of ancient versions of mathematica??), but please not by breaking the mathematica pexpect interface.
comment:21 Changed 6 years ago by
Upon further investigation, this ticket did break echoing in mathematica_console()
. So something needs fixing (in #23112).
I was able to reproduce this error (same Mathematica version 10.3.1.0, also on OS X.)
This error is caused by sporadic double echoing, which confuses the pexpect code. This can be seen at the end of the logfile generated by the above testcase. I'm not sure where the double echoing comes from. Perhaps some strangeness of Mac OS ptys, perhaps a race condition, perhaps mathreadline is doing something wrong, perhaps a Mathematica bug.
A workaround is to disable echoing: