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.
  • François Bissey, Bill Page, Jeroen Demeyer (2015-12-09): Upgrade to pexpect 4.0.1 + patches, see trac ticket #10295.
class sage.interfaces.expect.Expect(name, prompt, command=None, env={}, server=None, server_tmpdir=None, ulimit=None, maxread=None, 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.

clear_prompts()
command()

Returns the command used in this interface.

EXAMPLES:

sage: magma.set_server_and_command(command = 'magma-2.19')
sage: magma.command() # indirect doctest
'magma-2.19'
detach()

Forget the running subprocess: keep it running but pretend that it’s no longer running.

EXAMPLES:

sage: a = maxima('y')
sage: saved_expect = maxima._expect  # Save this to close later
sage: maxima.detach()
sage: a._check_valid()
Traceback (most recent call last):
...
ValueError: The maxima session in which this object was defined is no longer running.
sage: saved_expect.close()  # Close child process

Calling detach() a second time does nothing:

sage: maxima.detach()
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()
interrupt(tries=5, timeout=2.0, quit_on_fail=True)
is_local()
is_remote()
is_running()

Return True if self is currently running.

path()
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.

EXAMPLES:

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

Quit the running subprocess.

INPUT:

  • verbose – (boolean, default False) print a message when quitting this process?

EXAMPLES:

sage: a = maxima('y')
sage: maxima.quit(verbose=True)
Exiting Maxima with PID ... running .../bin/maxima ...
sage: a._check_valid()
Traceback (most recent call last):
...
ValueError: The maxima session in which this object was defined is no longer running.

Calling quit() a second time does nothing:

sage: maxima.quit(verbose=True)
server()

Returns the server used in this interface.

EXAMPLES:

sage: magma.set_server_and_command(server = 'remote')
No remote temporary directory (option server_tmpdir) specified, using /tmp/ on remote
sage: magma.server() # indirect doctest
'remote'
set_server_and_command(server=None, command=None, server_tmpdir=None, ulimit=None)

Changes the server and the command to use for this interface. This raises a Runtime error if the interface is already started.

EXAMPLES:

sage: magma.set_server_and_command(server = 'remote', command = 'mymagma') # indirect doctest
No remote temporary directory (option server_tmpdir) specified, using /tmp/ on remote
sage: magma.server()
'remote'
sage: magma.command()
"sage-native-execute ssh -t remote 'mymagma'"
user_dir()
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 communication between Sage and a subprocess interfaced via pexpect is printed to stdout.

sage.interfaces.expect.console(cmd)
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)