| 
									
										
										
										
											2024-05-08 15:34:40 -04:00
										 |  |  | :mod:`!code` --- Interpreter base classes
 | 
					
						
							|  |  |  | =========================================
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. module:: code
 | 
					
						
							|  |  |  |    :synopsis: Facilities to implement read-eval-print loops.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-19 16:23:01 -04:00
										 |  |  | **Source code:** :source:`Lib/code.py`
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-11 15:02:54 -04:00
										 |  |  | --------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | The ``code`` module provides facilities to implement read-eval-print loops in
 | 
					
						
							|  |  |  | Python.  Two classes and convenience functions are included which can be used to
 | 
					
						
							|  |  |  | build applications which provide an interactive interpreter prompt.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-05 22:20:44 +00:00
										 |  |  | .. class:: InteractiveInterpreter(locals=None)
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    This class deals with parsing and interpreter state (the user's namespace); it
 | 
					
						
							|  |  |  |    does not deal with input buffering or prompting or input file naming (the
 | 
					
						
							|  |  |  |    filename is always passed in explicitly). The optional *locals* argument
 | 
					
						
							| 
									
										
										
										
											2024-05-21 13:32:15 +10:00
										 |  |  |    specifies a mapping to use as the namespace in which code will be executed;
 | 
					
						
							|  |  |  |    it defaults to a newly created dictionary with key ``'__name__'`` set to
 | 
					
						
							|  |  |  |    ``'__console__'`` and key ``'__doc__'`` set to ``None``.
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-18 11:36:43 -07:00
										 |  |  | .. class:: InteractiveConsole(locals=None, filename="<console>", local_exit=False)
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    Closely emulate the behavior of the interactive Python interpreter. This class
 | 
					
						
							|  |  |  |    builds on :class:`InteractiveInterpreter` and adds prompting using the familiar
 | 
					
						
							| 
									
										
										
										
											2024-05-08 22:35:16 +03:00
										 |  |  |    ``sys.ps1`` and ``sys.ps2``, and input buffering. If *local_exit* is true,
 | 
					
						
							| 
									
										
										
										
											2023-10-18 11:36:43 -07:00
										 |  |  |    ``exit()`` and ``quit()`` in the console will not raise :exc:`SystemExit`, but
 | 
					
						
							|  |  |  |    instead return to the calling code.
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-18 11:36:43 -07:00
										 |  |  |    .. versionchanged:: 3.13
 | 
					
						
							|  |  |  |       Added *local_exit* parameter.
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-18 11:36:43 -07:00
										 |  |  | .. function:: interact(banner=None, readfunc=None, local=None, exitmsg=None, local_exit=False)
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-13 23:09:14 +03:00
										 |  |  |    Convenience function to run a read-eval-print loop.  This creates a new
 | 
					
						
							|  |  |  |    instance of :class:`InteractiveConsole` and sets *readfunc* to be used as
 | 
					
						
							|  |  |  |    the :meth:`InteractiveConsole.raw_input` method, if provided.  If *local* is
 | 
					
						
							|  |  |  |    provided, it is passed to the :class:`InteractiveConsole` constructor for
 | 
					
						
							| 
									
										
										
										
											2023-10-18 11:36:43 -07:00
										 |  |  |    use as the default namespace for the interpreter loop.  If *local_exit* is provided,
 | 
					
						
							| 
									
										
										
										
											2024-04-07 23:58:57 +01:00
										 |  |  |    it is passed to the :class:`InteractiveConsole` constructor.  The :meth:`~InteractiveConsole.interact`
 | 
					
						
							| 
									
										
										
										
											2016-08-24 01:42:15 +10:00
										 |  |  |    method of the instance is then run with *banner* and *exitmsg* passed as the
 | 
					
						
							|  |  |  |    banner and exit message to use, if provided.  The console object is discarded
 | 
					
						
							|  |  |  |    after use.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. versionchanged:: 3.6
 | 
					
						
							|  |  |  |       Added *exitmsg* parameter.
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-18 11:36:43 -07:00
										 |  |  |    .. versionchanged:: 3.13
 | 
					
						
							|  |  |  |       Added *local_exit* parameter.
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-05 22:20:44 +00:00
										 |  |  | .. function:: compile_command(source, filename="<input>", symbol="single")
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    This function is useful for programs that want to emulate Python's interpreter
 | 
					
						
							|  |  |  |    main loop (a.k.a. the read-eval-print loop).  The tricky part is to determine
 | 
					
						
							|  |  |  |    when the user has entered an incomplete command that can be completed by
 | 
					
						
							|  |  |  |    entering more text (as opposed to a complete command or a syntax error).  This
 | 
					
						
							|  |  |  |    function *almost* always makes the same decision as the real interpreter main
 | 
					
						
							|  |  |  |    loop.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    *source* is the source string; *filename* is the optional filename from which
 | 
					
						
							|  |  |  |    source was read, defaulting to ``'<input>'``; and *symbol* is the optional
 | 
					
						
							| 
									
										
										
										
											2020-05-14 21:59:46 -03:00
										 |  |  |    grammar start symbol, which should be ``'single'`` (the default), ``'eval'``
 | 
					
						
							|  |  |  |    or ``'exec'``.
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    Returns a code object (the same as ``compile(source, filename, symbol)``) if the
 | 
					
						
							|  |  |  |    command is complete and valid; ``None`` if the command is incomplete; raises
 | 
					
						
							|  |  |  |    :exc:`SyntaxError` if the command is complete and contains a syntax error, or
 | 
					
						
							|  |  |  |    raises :exc:`OverflowError` or :exc:`ValueError` if the command contains an
 | 
					
						
							|  |  |  |    invalid literal.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. _interpreter-objects:
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Interactive Interpreter Objects
 | 
					
						
							|  |  |  | -------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-05 22:20:44 +00:00
										 |  |  | .. method:: InteractiveInterpreter.runsource(source, filename="<input>", symbol="single")
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    Compile and run some source in the interpreter. Arguments are the same as for
 | 
					
						
							|  |  |  |    :func:`compile_command`; the default for *filename* is ``'<input>'``, and for
 | 
					
						
							| 
									
										
										
										
											2020-01-14 13:47:26 -06:00
										 |  |  |    *symbol* is ``'single'``.  One of several things can happen:
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    * The input is incorrect; :func:`compile_command` raised an exception
 | 
					
						
							|  |  |  |      (:exc:`SyntaxError` or :exc:`OverflowError`).  A syntax traceback will be
 | 
					
						
							|  |  |  |      printed by calling the :meth:`showsyntaxerror` method.  :meth:`runsource`
 | 
					
						
							|  |  |  |      returns ``False``.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    * The input is incomplete, and more input is required; :func:`compile_command`
 | 
					
						
							|  |  |  |      returned ``None``. :meth:`runsource` returns ``True``.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    * The input is complete; :func:`compile_command` returned a code object.  The
 | 
					
						
							|  |  |  |      code is executed by calling the :meth:`runcode` (which also handles run-time
 | 
					
						
							|  |  |  |      exceptions, except for :exc:`SystemExit`). :meth:`runsource` returns ``False``.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    The return value can be used to decide whether to use ``sys.ps1`` or ``sys.ps2``
 | 
					
						
							|  |  |  |    to prompt the next line.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. method:: InteractiveInterpreter.runcode(code)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Execute a code object. When an exception occurs, :meth:`showtraceback` is called
 | 
					
						
							|  |  |  |    to display a traceback.  All exceptions are caught except :exc:`SystemExit`,
 | 
					
						
							|  |  |  |    which is allowed to propagate.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    A note about :exc:`KeyboardInterrupt`: this exception may occur elsewhere in
 | 
					
						
							|  |  |  |    this code, and may not always be caught.  The caller should be prepared to deal
 | 
					
						
							|  |  |  |    with it.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-05 22:20:44 +00:00
										 |  |  | .. method:: InteractiveInterpreter.showsyntaxerror(filename=None)
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    Display the syntax error that just occurred.  This does not display a stack
 | 
					
						
							|  |  |  |    trace because there isn't one for syntax errors. If *filename* is given, it is
 | 
					
						
							|  |  |  |    stuffed into the exception instead of the default filename provided by Python's
 | 
					
						
							|  |  |  |    parser, because it always uses ``'<string>'`` when reading from a string. The
 | 
					
						
							|  |  |  |    output is written by the :meth:`write` method.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. method:: InteractiveInterpreter.showtraceback()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Display the exception that just occurred.  We remove the first stack item
 | 
					
						
							|  |  |  |    because it is within the interpreter object implementation. The output is
 | 
					
						
							|  |  |  |    written by the :meth:`write` method.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-29 11:25:00 -04:00
										 |  |  |    .. versionchanged:: 3.5 The full chained traceback is displayed instead
 | 
					
						
							|  |  |  |       of just the primary traceback.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. method:: InteractiveInterpreter.write(data)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Write a string to the standard error stream (``sys.stderr``). Derived classes
 | 
					
						
							|  |  |  |    should override this to provide the appropriate output handling as needed.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. _console-objects:
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Interactive Console Objects
 | 
					
						
							|  |  |  | ---------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The :class:`InteractiveConsole` class is a subclass of
 | 
					
						
							|  |  |  | :class:`InteractiveInterpreter`, and so offers all the methods of the
 | 
					
						
							|  |  |  | interpreter objects as well as the following additions.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-24 01:42:15 +10:00
										 |  |  | .. method:: InteractiveConsole.interact(banner=None, exitmsg=None)
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-13 21:49:06 +02:00
										 |  |  |    Closely emulate the interactive Python console. The optional *banner* argument
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  |    specify the banner to print before the first interaction; by default it prints a
 | 
					
						
							|  |  |  |    banner similar to the one printed by the standard Python interpreter, followed
 | 
					
						
							|  |  |  |    by the class name of the console object in parentheses (so as not to confuse
 | 
					
						
							|  |  |  |    this with the real interpreter -- since it's so close!).
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-24 01:42:15 +10:00
										 |  |  |    The optional *exitmsg* argument specifies an exit message printed when exiting.
 | 
					
						
							|  |  |  |    Pass the empty string to suppress the exit message. If *exitmsg* is not given or
 | 
					
						
							| 
									
										
										
										
											2016-10-19 16:37:13 +03:00
										 |  |  |    ``None``, a default message is printed.
 | 
					
						
							| 
									
										
										
										
											2016-08-24 01:42:15 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-13 21:49:06 +02:00
										 |  |  |    .. versionchanged:: 3.4
 | 
					
						
							|  |  |  |       To suppress printing any banner, pass an empty string.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-15 04:14:33 +10:00
										 |  |  |    .. versionchanged:: 3.6
 | 
					
						
							| 
									
										
										
										
											2016-08-24 01:42:15 +10:00
										 |  |  |       Print an exit message when exiting.
 | 
					
						
							| 
									
										
										
										
											2016-08-15 04:14:33 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. method:: InteractiveConsole.push(line)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Push a line of source text to the interpreter. The line should not have a
 | 
					
						
							|  |  |  |    trailing newline; it may have internal newlines.  The line is appended to a
 | 
					
						
							| 
									
										
										
										
											2023-07-29 08:48:10 +03:00
										 |  |  |    buffer and the interpreter's :meth:`~InteractiveInterpreter.runsource` method is called with the
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  |    concatenated contents of the buffer as source.  If this indicates that the
 | 
					
						
							|  |  |  |    command was executed or invalid, the buffer is reset; otherwise, the command is
 | 
					
						
							|  |  |  |    incomplete, and the buffer is left as it was after the line was appended.  The
 | 
					
						
							|  |  |  |    return value is ``True`` if more input is required, ``False`` if the line was
 | 
					
						
							| 
									
										
										
										
											2023-07-29 08:48:10 +03:00
										 |  |  |    dealt with in some way (this is the same as :meth:`!runsource`).
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. method:: InteractiveConsole.resetbuffer()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Remove any unhandled source text from the input buffer.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-05 22:20:44 +00:00
										 |  |  | .. method:: InteractiveConsole.raw_input(prompt="")
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    Write a prompt and read a line.  The returned line does not include the trailing
 | 
					
						
							|  |  |  |    newline.  When the user enters the EOF key sequence, :exc:`EOFError` is raised.
 | 
					
						
							|  |  |  |    The base implementation reads from ``sys.stdin``; a subclass may replace this
 | 
					
						
							|  |  |  |    with a different implementation.
 |