Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Further clean-up of interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
jdemeyer committed Feb 3, 2015
1 parent 8501bb1 commit d556229
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 87 deletions.
90 changes: 36 additions & 54 deletions src/sage/interfaces/expect.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ class gc_disabled(object):
sage: gc.isenabled()
True
sage: with gc_disabled():
... print gc.isenabled()
... with gc_disabled():
... print gc.isenabled()
... print gc.isenabled()
....: print gc.isenabled()
....: with gc_disabled():
....: print gc.isenabled()
....: print gc.isenabled()
False
False
False
Expand Down Expand Up @@ -530,8 +530,26 @@ def quit(self, verbose=False, timeout=0.25):
return

def _quit_string(self):
"""
Return the string which will be used to quit the application.
EXAMPLES::
sage: gp._quit_string()
'\\q'
sage: maxima._quit_string()
'quit();'
"""
return 'quit'

def _send_interrupt(self):
"""
Send an interrupt to the application. This is used internally
by :meth:`interrupt`.
"""
self._expect.sendline(chr(3))
self._expect.sendline(self._quit_string())

def _local_tmpfile(self):
"""
Return a filename that is used to buffer long command lines for this interface
Expand All @@ -552,7 +570,7 @@ def _local_tmpfile(self):
sage: gap._local_tmpfile() is gap._local_tmpfile()
True
The following two problems were fixed in #10004.
The following two problems were fixed in :trac:`10004`.
1. Different interfaces have different temp-files::
Expand All @@ -563,9 +581,8 @@ def _local_tmpfile(self):
function have different temp-files::
sage: @parallel
... def f(n):
... return gap._local_tmpfile()
...
....: def f(n):
....: return gap._local_tmpfile()
sage: L = [t[1] for t in f(range(5))]
sage: len(set(L))
5
Expand Down Expand Up @@ -620,25 +637,6 @@ def _remove_tmpfile_from_server(self):
if not (self.__remote_tmpfile is None):
raise NotImplementedError

def read(self, filename):
r"""
EXAMPLES::
sage: filename = tmp_filename()
sage: f = open(filename, 'w')
sage: f.write('x = 2\n')
sage: f.close()
sage: octave.read(filename) # optional - octave
sage: octave.get('x') #optional
' 2'
sage: import os
sage: os.unlink(filename)
"""
self.eval(self._read_in_file_command(filename))

def _read_in_file_command(self, filename):
raise NotImplementedError

def _eval_line_using_file(self, line, restart_if_needed=True):
"""
Evaluate a line of commands, using a temporary file.
Expand Down Expand Up @@ -917,15 +915,14 @@ def interrupt(self, tries=20, timeout=0.3, quit_on_fail=True):
success = False
try:
for i in range(tries):
E.sendline(chr(3))
E.sendline(self._quit_string())
self._send_interrupt()
try:
E.expect(self._prompt, timeout=timeout)
success= True
break
except (pexpect.TIMEOUT, pexpect.EOF) as msg:
#print msg
pass
else:
success = True
break
except Exception as msg:
pass
if success:
Expand All @@ -951,19 +948,6 @@ def _before(self):
"""
return self._expect.before

def _interrupt(self):
for i in range(15):
try:
self._sendstr('quit;\n'+chr(3))
self._expect_expr(timeout=2)
except pexpect.TIMEOUT:
pass
except pexpect.EOF:
self._crash_msg()
self.quit()
else:
return

def _expect_expr(self, expr=None, timeout=None):
r"""
Wait for a given expression expr (which could be a regular
Expand Down Expand Up @@ -1160,7 +1144,7 @@ def _synchronize(self, cmd='1+%s;\n'):
self._expect_expr(s,timeout=0.5)
self._expect_expr(timeout=0.5)
except pexpect.TIMEOUT:
self._interrupt()
self.interrupt()
except pexpect.EOF:
self._crash_msg()
self.quit()
Expand Down Expand Up @@ -1381,9 +1365,8 @@ def __init__(self, interface, silent=False, stdout=None):
EXAMPLE::
sage: from sage.interfaces.expect import StdOutContext
sage: with StdOutContext(gp):
... gp('1+1')
...
sage: with StdOutContext(Gp()) as g:
....: g('1+1')
sage=...
"""
self.interface = interface
Expand All @@ -1396,29 +1379,28 @@ def __enter__(self):
sage: from sage.interfaces.expect import StdOutContext
sage: with StdOutContext(singular):
... singular.eval('1+1')
...
....: singular.eval('1+1')
1+1;
...
"""
if self.silent:
return
return self.interface
if self.interface._expect is None:
self.interface._start()
self._logfile_backup = self.interface._expect.logfile
if self.interface._expect.logfile:
self.interface._expect.logfile = Multiplex(self.interface._expect.logfile, self.stdout)
else:
self.interface._expect.logfile = Multiplex(self.stdout)
return self.interface

def __exit__(self, typ, value, tb):
"""
EXAMPLE::
sage: from sage.interfaces.expect import StdOutContext
sage: with StdOutContext(gap):
... gap('1+1')
...
....: gap('1+1')
$sage...
"""
if self.silent:
Expand Down
2 changes: 1 addition & 1 deletion src/sage/interfaces/gap.py
Original file line number Diff line number Diff line change
Expand Up @@ -1167,7 +1167,7 @@ def _start(self):
except Exception:
if self.__use_workspace_cache and first_try:
first_try = False
self.quit(timeout=0)
self.quit()
gap_reset_workspace(verbose=False)
Expect._start(self, "Failed to start GAP.")
self._session_number = n
Expand Down
35 changes: 3 additions & 32 deletions src/sage/interfaces/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,6 @@ def _pre_interact(self):
def _post_interact(self):
pass

def __del__(self):
pass

def cputime(self):
"""
CPU time since this process started running.
Expand All @@ -115,7 +112,7 @@ def read(self, filename):
sage: f.write('x = 2\n')
sage: f.close()
sage: octave.read(filename) # optional - octave
sage: octave.get('x') #optional
sage: octave.get('x') # optional - octave
' 2'
sage: import os
sage: os.unlink(filename)
Expand All @@ -125,34 +122,8 @@ def read(self, filename):
def _read_in_file_command(self, filename):
raise NotImplementedError

def eval(self, code, locals=None, **kwds):
"""
INPUT:
- ``code`` - text to evaluate
- ``locals`` - None (ignored); this is used for compatibility with the
Sage notebook's generic system interface.
- ``**kwds`` - All other arguments are passed onto
the _eval_line method. An often useful example is
reformat=False.
"""

if not isinstance(code, basestring):
raise TypeError('input code must be a string.')

#Remove extra whitespace
code = code.strip()

try:
pass
# DO NOT CATCH KeyboardInterrupt, as it is being caught
# by _eval_line
# In particular, do NOT call self._keyboard_interrupt()
except TypeError as s:
raise TypeError('error evaluating "%s":\n%s'%(code,s))
def eval(self, code, **kwds):
raise NotImplementedError

_eval_line = eval

Expand Down

0 comments on commit d556229

Please sign in to comment.