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 Issue #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)[source]¶
- Bases: - Interface- Expect interface object. - command()[source]¶
- Return the command used in this interface as a string. - EXAMPLES: - sage: magma.set_server_and_command(command='magma-2.19') sage: magma.command() # indirect doctest 'magma-2.19' - >>> from sage.all import * >>> magma.set_server_and_command(command='magma-2.19') >>> magma.command() # indirect doctest 'magma-2.19' 
 - detach()[source]¶
- 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 - >>> from sage.all import * >>> a = maxima('y') >>> saved_expect = maxima._expect # Save this to close later >>> maxima.detach() >>> a._check_valid() Traceback (most recent call last): ... ValueError: The maxima session in which this object was defined is no longer running. >>> saved_expect.close() # Close child process - Calling - detach()a second time does nothing:- sage: maxima.detach() - >>> from sage.all import * >>> maxima.detach() 
 - eval(code, strip=True, synchronize=False, locals=None, allow_use_file=True, split_lines='nofile', **kwds)[source]¶
- INPUT: - code– text to evaluate
- strip– boolean; 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– boolean (default:- True); if- Trueand- codeexceeds an interface-specific threshold then- codewill be communicated via a temporary file rather that the character-based interface. If- Falsethen the code will be communicated via the character interface.
- split_lines– Tri-state (default:- 'nofile'); if “nofile” then- codeis sent line by line unless it gets communicated via a temporary file. If- Truethen- codeis sent line by line, but some lines individually might be sent via temporary file. Depending on the interface, this may transform grammatical- codeinto ungrammatical input. If- False, then the whole block of code is evaluated all at once.
- **kwds– all other arguments are passed onto the- _eval_linemethod. An often useful example is- reformat=False.
 
 - pid()[source]¶
- 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 - >>> from sage.all import * >>> pid = gap.pid() >>> gap.eval('quit;') '' >>> pid == gap.pid() True >>> gap.quit() >>> pid == gap.pid() False 
 - quit(verbose=False)[source]¶
- Quit the running subprocess. - INPUT: - verbose– boolean (default:- False); whether to print a message when quitting the process
 - EXAMPLES: - sage: a = maxima('y') sage: maxima.quit(verbose=True) Exiting Maxima with PID ... running ...maxima... sage: a._check_valid() Traceback (most recent call last): ... ValueError: The maxima session in which this object was defined is no longer running. - >>> from sage.all import * >>> a = maxima('y') >>> maxima.quit(verbose=True) Exiting Maxima with PID ... running ...maxima... >>> 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) - >>> from sage.all import * >>> maxima.quit(verbose=True) 
 - server()[source]¶
- Return 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' - >>> from sage.all import * >>> magma.set_server_and_command(server='remote') No remote temporary directory (option server_tmpdir) specified, using /tmp/ on remote >>> magma.server() # indirect doctest 'remote' 
 - set_server_and_command(server=None, command=None, server_tmpdir=None, ulimit=None)[source]¶
- Changes the server and the command to use for this interface. - This raises a - RuntimeErrorif the interface is already started.- INPUT: - server– string or- None(default); name of a remote host to connect to using- ssh
- command– one of:- a string; command line passed to the shell 
- a sequence of an - Executableand strings, arguments to pass to the executable.
 
 - 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() 'ssh -t remote mymagma' - >>> from sage.all import * >>> magma.set_server_and_command(server='remote', command='mymagma') # indirect doctest No remote temporary directory (option server_tmpdir) specified, using /tmp/ on remote >>> magma.server() 'remote' >>> magma.command() 'ssh -t remote mymagma' 
 
- class sage.interfaces.expect.ExpectElement(parent, value, is_name=False, name=None)[source]¶
- Bases: - InterfaceElement,- ExpectElement- Expect element. 
- class sage.interfaces.expect.ExpectFunction(parent, name)[source]¶
- Bases: - InterfaceFunction- Expect function. 
- class sage.interfaces.expect.FunctionElement(obj, name)[source]¶
- Bases: - InterfaceFunctionElement- Expect function element. 
- class sage.interfaces.expect.StdOutContext(interface, silent=False, stdout=None)[source]¶
- Bases: - object- A context in which all communication between Sage and a subprocess interfaced via pexpect is printed to stdout. 
- class sage.interfaces.expect.gc_disabled[source]¶
- 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 - >>> from sage.all import * >>> import gc >>> from sage.interfaces.expect import gc_disabled >>> gc.isenabled() True >>> with gc_disabled(): ... print(gc.isenabled()) ... with gc_disabled(): ... print(gc.isenabled()) ... print(gc.isenabled()) False False False >>> gc.isenabled() True 
- sage.interfaces.expect.is_ExpectElement(x)[source]¶
- Return - Trueif- xis of type- ExpectElement.- This function is deprecated; use - isinstance()(of- sage.interfaces.abc.ExpectElement) instead.- EXAMPLES: - sage: from sage.interfaces.expect import is_ExpectElement sage: is_ExpectElement(2) doctest:...: DeprecationWarning: the function is_ExpectElement is deprecated; use isinstance(x, sage.interfaces.abc.ExpectElement) instead See https://github.com/sagemath/sage/issues/34804 for details. False - >>> from sage.all import * >>> from sage.interfaces.expect import is_ExpectElement >>> is_ExpectElement(Integer(2)) doctest:...: DeprecationWarning: the function is_ExpectElement is deprecated; use isinstance(x, sage.interfaces.abc.ExpectElement) instead See https://github.com/sagemath/sage/issues/34804 for details. False