| Download
fix a few problems with the Sage octave interface
Project: Many Interacting Worlds
Path: octave.py.patch
Views: 95--- octave.py.old 2016-01-04 16:26:50.188082453 +00001+++ octave.py 2016-01-05 16:54:42.400050103 +00002@@ -153,6 +153,7 @@34import os5from expect import Expect, ExpectElement6+from sage.misc.misc import verbose789class Octave(Expect):10@@ -181,8 +182,10 @@11"""12Expect.__init__(self,13name = 'octave',14- prompt = '>',15- command = "sage-native-execute octave --no-line-editing --silent",16+ # We want the prompt sequence to be unique to avoid confusion with syntax error messages containing >>>17+ prompt = 'octave\:\d+> ',18+ # We don't want any pagination of output19+ command = "sage-native-execute octave --no-line-editing --silent --eval 'PS2(PS1());more off' --persist",20maxread = maxread,21server = server,22server_tmpdir = server_tmpdir,23@@ -270,6 +273,57 @@24octave starts up.25* Darwin ports and fink have Octave as well.26"""27+ def _eval_line(self, line, reformat=True, allow_use_file=False,28+ wait_for_prompt=True, restart_if_needed=False):29+ """30+ EXAMPLES::31+32+ sage: print octave._eval_line('2+2') #optional - octave33+ ans = 434+ """35+ if not wait_for_prompt:36+ return Expect._eval_line(self, line)37+ if line == '':38+ return ''39+ if self._expect is None:40+ self._start()41+ if allow_use_file and len(line)>3000:42+ return self._eval_line_using_file(line)43+ try:44+ E = self._expect45+ # debug46+ # self._synchronize(cmd='1+%s\n')47+ verbose("in = '%s'"%line,level=3)48+ E.sendline(line)49+ E.expect(self._prompt)50+ out = E.before51+ # debug52+ verbose("out = '%s'"%out,level=3)53+ except EOF:54+ if self._quit_string() in line:55+ return ''56+ except KeyboardInterrupt:57+ self._keyboard_interrupt()58+ if reformat:59+ if 'syntax error' in out:60+ raise SyntaxError(out)61+ out = "\n".join(out.splitlines()[1:])62+ return out63+64+ def _keyboard_interrupt(self):65+ print "CntrlC: Interrupting %s..."%self66+ if self._restart_on_ctrlc:67+ try:68+ self._expect.close(force=1)69+ except pexpect.ExceptionPexpect as msg:70+ raise pexpect.ExceptionPexpect( "THIS IS A BUG -- PLEASE REPORT. This should never happen.\n" + msg)71+ self._start()72+ raise KeyboardInterrupt("Restarting %s (WARNING: all variables defined in previous session are now invalid)"%self)73+ else:74+ self._expect.send('\003') # control-c75+ #self._expect.expect(self._prompt)76+ #self._expect.expect(self._prompt)77+ raise KeyboardInterrupt("Ctrl-c pressed while running %s"%self)7879def quit(self, verbose=False):80"""81@@ -348,7 +402,7 @@82"""83cmd = '%s=%s;'%(var,value)84out = self.eval(cmd)85- if out.find("error") != -1:86+ if out.find("error") != -1 or out.find("Error") != -1:87raise TypeError("Error executing code in Octave\nCODE:\n\t%s\nOctave ERROR:\n\t%s"%(cmd, out))8889def get(self, var):90@@ -622,6 +676,10 @@91sage: matrix(ZZ, A) # optional - octave92[1 2]93[3 4]94+ sage: A = octave('[1,2;3,4.5]') # optional - octave95+ sage: matrix(RR, A) # optional - octave96+ [1.00000000000000 2.00000000000000]97+ [3.00000000000000 4.50000000000000]98"""99oc = self.parent()100if not self.ismatrix():101@@ -638,7 +696,15 @@102w = [[to_complex(x,R) for x in row] for row in w]103104from sage.matrix.all import MatrixSpace105- return MatrixSpace(R, nrows, ncols)(w)106+ s = str(self).strip()107+ v = s.split('\n ')108+ nrows = len(v)109+ if nrows == 0:110+ return MatrixSpace(R,0,0)(0)111+ ncols = len(v[0].split())112+ M = MatrixSpace(R, nrows, ncols)113+ v = sum([[x for x in w.split()] for w in v], [])114+ return M(v)115116def _vector_(self, R=None):117r"""118119120