Common Interface Functionality through Pexpect

See the examples in the other sections for how to use specific interfaces. The interface classes all derive from the generic interface that is described in this section.


  • William Stein (2005): initial version
  • William Stein (2006-03-01): got rid of infinite loop on startup if client system missing
  • Felix Lawrence (2009-08-21): edited ._sage_() to support lists and float exponents in foreign notation.
  • Simon King (2010-09-25): Expect._local_tmpfile() depends on and is cached; Expect.quit() clears that cache, which is important for forking.
  • Jean-Pierre Flori (2010,2011): Split non Pexpect stuff into a parent class.
  • Simon King (2010-11-23): Ensure that the interface is started again after a crash, when a command is executed in _eval_line. Allow synchronisation of the GAP interface.
class sage.interfaces.expect.Expect(name, prompt, command=None, server=None, server_tmpdir=None, ulimit=None, maxread=100000, script_subdirectory=None, restart_on_ctrlc=False, verbose_start=False, init_code=, []max_startup_time=None, logfile=None, eval_using_file_cutoff=0, do_cleaner=True, remote_cleaner=False, path=None, terminal_echo=True)

Bases: sage.interfaces.interface.Interface

Expect interface object.


x.__init__(...) initializes x; see help(type(x)) for signature

eval(code, strip=True, synchronize=False, locals=None, allow_use_file=True, split_lines='nofile', **kwds)


  • code – text to evaluate

  • strip – bool; whether to strip output prompts,

    etc. (ignored in the base class).

  • locals – None (ignored); this is used for compatibility

    with the Sage notebook’s generic system interface.

  • allow_use_file – bool (default: True); if True and code exceeds an

    interface-specific threshold then code will be communicated via a temporary file rather that the character-based interface. If False then the code will be communicated via the character interface.

  • split_lines – Tri-state (default: “nofile”); if “nofile” then code is sent line by line

    unless it gets communicated via a temporary file. If True then code is sent line by line, but some lines individually might be sent via temporary file. Depending on the interface, this may transform grammatical code into ungrammatical input. If False, then the whole block of code is evaluated all at once.

  • **kwds – All other arguments are passed onto the _eval_line

    method. An often useful example is reformat=False.


x.__init__(...) initializes x; see help(type(x)) for signature

interrupt(tries=20, timeout=0.3, quit_on_fail=True)

x.__init__(...) initializes x; see help(type(x)) for signature


x.__init__(...) initializes x; see help(type(x)) for signature


x.__init__(...) initializes x; see help(type(x)) for signature


Return True if self is currently running.


x.__init__(...) initializes x; see help(type(x)) for signature


Return the PID of the underlying sub-process.


If the interface terminates unexpectedly, the original PID will still be used. But if it was terminated using quit(), a new sub-process with a new PID is automatically started.


sage: pid =
sage: gap.eval('quit;')
sage: pid ==
sage: gap.quit()
sage: pid ==
quit(verbose=False, timeout=0.25)


sage: a = maxima('y')
sage: maxima.quit()
sage: a._check_valid()
Traceback (most recent call last):
ValueError: The maxima session in which this object was defined is no longer running.


sage: filename = tmp_filename()
sage: f = open(filename, 'w')
sage: f.write('x = 2\n')
sage: f.close()
sage:  # optional - octave
sage: octave.get('x')        #optional
' 2'
sage: import os
sage: os.unlink(filename)

x.__init__(...) initializes x; see help(type(x)) for signature

class sage.interfaces.expect.ExpectElement(parent, value, is_name=False, name=None)

Bases: sage.interfaces.interface.InterfaceElement

Expect element.

class sage.interfaces.expect.ExpectFunction(parent, name)

Bases: sage.interfaces.interface.InterfaceFunction

Expect function.

class sage.interfaces.expect.FunctionElement(obj, name)

Bases: sage.interfaces.interface.InterfaceFunctionElement

Expect function element.

class sage.interfaces.expect.StdOutContext(interface, silent=False, stdout=None)

A context in which all communation between Sage and a subprocess interfaced via pexpect is printed to stdout.


x.__init__(...) initializes x; see help(type(x)) for signature

class sage.interfaces.expect.gc_disabled

Bases: object

This is a “with” statement context manager. Garbage collection is disabled within its scope. Nested usage is properly handled.


sage: import gc
sage: from sage.interfaces.expect import gc_disabled
sage: gc.isenabled()
sage: with gc_disabled():
...       print gc.isenabled()
...       with gc_disabled():
...           print gc.isenabled()
...       print gc.isenabled()
sage: gc.isenabled()

x.__init__(...) initializes x; see help(type(x)) for signature

Previous topic

Interpreter Interfaces

Next topic

Interface to Axiom

This Page