Opened 7 years ago

Closed 7 years ago

Last modified 6 years ago

#20173 closed defect (fixed)

Error executing code in Mathematica

Reported by: yzh Owned by:
Priority: major Milestone: sage-7.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:

Status badges

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='mathematica-log')
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 mkoeppe

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 math-readline is doing something wrong, perhaps a Mathematica bug.

A workaround is to disable echoing:

diff --git a/src/bin/math-readline b/src/bin/math-readline
index 5a13906..72e3795 100755
--- a/src/bin/math-readline
+++ b/src/bin/math-readline
@@ -9,6 +9,9 @@ import sys, signal, subprocess
 import readline
 from six.moves import input
 
+import os
+os.system("stty -echo")
+
 def child_exited(*args):
     global child
     status = child.poll()

comment:2 Changed 7 years ago by 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?)

comment:3 in reply to:  2 ; Changed 7 years ago by mkoeppe

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 pseudo-terminal 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 mkoeppe

Branch: u/mkoeppe/error_executing_code_in_mathematica

comment:5 Changed 7 years ago by mkoeppe

Authors: Matthias Koeppe
Commit: 0fdafe3c176f6292a33ece07b9043d940a8b4e1b
Status: newneeds_review

New commits:

0fdafe3Disable echo on Mathematica pty to avoid sporadic errors on Mac OS X

comment:6 Changed 7 years ago by 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__

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 in reply to:  6 Changed 7 years ago by mkoeppe

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.

Last edited 7 years ago by mkoeppe (previous) (diff)

comment:8 Changed 7 years ago by dimpase

I just checked this with Mathematica 9.0 on Linux 64-bit, 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 TeX-like rather than text-like, 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 mkoeppe

It is quite likely that this is specific to both the Mathematica version and the OS.

For example, the beginning of math-readline 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 command-line editing (though not the full range of readline's keybindings).

comment:10 Changed 7 years ago by dimpase

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 command-line editing on my terminal version, too.

comment:11 Changed 7 years ago by dimpase

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 Changed 7 years ago by mkoeppe

I think the failing Mathematica doctests should go on another ticket. This ticket needs review...

comment:13 Changed 7 years ago by dimpase

Reviewers: Dima Pasechnik
Status: needs_reviewpositive_review

looks good to me

comment:14 Changed 7 years ago by vbraun

Branch: u/mkoeppe/error_executing_code_in_mathematica0fdafe3c176f6292a33ece07b9043d940a8b4e1b
Resolution: fixed
Status: positive_reviewclosed

comment:15 in reply to:  12 ; Changed 7 years ago by slabbe

Commit: 0fdafe3c176f6292a33ece07b9043d940a8b4e1b

Replying to mkoeppe:

I think the failing Mathematica doctests should go on another ticket.

See #18888.

One problem is that the version of Mathematica may change from one user to the other...

comment:16 in reply to:  15 ; Changed 7 years ago by 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.

comment:17 in reply to:  16 Changed 7 years ago by dimpase

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:18 Changed 7 years ago by dimpase

#18888 is now updated and ready for review.

comment:19 in reply to:  3 ; Changed 6 years ago by jdemeyer

Replying to mkoeppe:

This stty -echo affects the pseudo-terminal 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 math-readline instead of the plain Mathematica (i.e. math) directly?

This is at least partially responsible for the breakage reported on #23112.

comment:20 in reply to:  19 Changed 6 years ago by mkoeppe

Replying to jdemeyer:

Replying to mkoeppe:

This stty -echo affects the pseudo-terminal 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 math-readline 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 math-readline 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 readline-like interface, so math-readline is not needed; so certainly the description of math-readline 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 mkoeppe

Upon further investigation, this ticket did break echoing in mathematica_console(). So something needs fixing (in #23112).

comment:22 Changed 6 years ago by mkoeppe

A working fix is now available on #23112.

Note: See TracTickets for help on using tickets.