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.

AUTHORS:

  • 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 Expect.pid() 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='', 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)

Bases: sage.interfaces.interface.Interface

Expect interface object.

clear_prompts()

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)

INPUT:

  • 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.

expect()

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

is_local()

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

is_remote()

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

is_running()

Return True if self is currently running.

path()

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

pid()

Return the PID of the underlying sub-process.

REMARK:

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.

EXAMPLE:

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

EXAMPLES:

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.
read(filename)

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)
user_dir()

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.

sage.interfaces.expect.console(cmd)

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.

EXAMPLES:

sage: import gc
sage: from sage.interfaces.expect import gc_disabled
sage: gc.isenabled()
True
sage: with gc_disabled():
...       print gc.isenabled()
...       with gc_disabled():
...           print gc.isenabled()
...       print gc.isenabled()
False
False
False
sage: gc.isenabled()
True
sage.interfaces.expect.is_ExpectElement(x)

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

Previous topic

Interpreter Interfaces

Next topic

Interface to Axiom

This Page