17db96d56Sopenharmony_ci:mod:`code` --- Interpreter base classes 27db96d56Sopenharmony_ci======================================== 37db96d56Sopenharmony_ci 47db96d56Sopenharmony_ci.. module:: code 57db96d56Sopenharmony_ci :synopsis: Facilities to implement read-eval-print loops. 67db96d56Sopenharmony_ci 77db96d56Sopenharmony_ci**Source code:** :source:`Lib/code.py` 87db96d56Sopenharmony_ci 97db96d56Sopenharmony_ci-------------- 107db96d56Sopenharmony_ci 117db96d56Sopenharmony_ciThe ``code`` module provides facilities to implement read-eval-print loops in 127db96d56Sopenharmony_ciPython. Two classes and convenience functions are included which can be used to 137db96d56Sopenharmony_cibuild applications which provide an interactive interpreter prompt. 147db96d56Sopenharmony_ci 157db96d56Sopenharmony_ci 167db96d56Sopenharmony_ci.. class:: InteractiveInterpreter(locals=None) 177db96d56Sopenharmony_ci 187db96d56Sopenharmony_ci This class deals with parsing and interpreter state (the user's namespace); it 197db96d56Sopenharmony_ci does not deal with input buffering or prompting or input file naming (the 207db96d56Sopenharmony_ci filename is always passed in explicitly). The optional *locals* argument 217db96d56Sopenharmony_ci specifies the dictionary in which code will be executed; it defaults to a newly 227db96d56Sopenharmony_ci created dictionary with key ``'__name__'`` set to ``'__console__'`` and key 237db96d56Sopenharmony_ci ``'__doc__'`` set to ``None``. 247db96d56Sopenharmony_ci 257db96d56Sopenharmony_ci 267db96d56Sopenharmony_ci.. class:: InteractiveConsole(locals=None, filename="<console>") 277db96d56Sopenharmony_ci 287db96d56Sopenharmony_ci Closely emulate the behavior of the interactive Python interpreter. This class 297db96d56Sopenharmony_ci builds on :class:`InteractiveInterpreter` and adds prompting using the familiar 307db96d56Sopenharmony_ci ``sys.ps1`` and ``sys.ps2``, and input buffering. 317db96d56Sopenharmony_ci 327db96d56Sopenharmony_ci 337db96d56Sopenharmony_ci.. function:: interact(banner=None, readfunc=None, local=None, exitmsg=None) 347db96d56Sopenharmony_ci 357db96d56Sopenharmony_ci Convenience function to run a read-eval-print loop. This creates a new 367db96d56Sopenharmony_ci instance of :class:`InteractiveConsole` and sets *readfunc* to be used as 377db96d56Sopenharmony_ci the :meth:`InteractiveConsole.raw_input` method, if provided. If *local* is 387db96d56Sopenharmony_ci provided, it is passed to the :class:`InteractiveConsole` constructor for 397db96d56Sopenharmony_ci use as the default namespace for the interpreter loop. The :meth:`interact` 407db96d56Sopenharmony_ci method of the instance is then run with *banner* and *exitmsg* passed as the 417db96d56Sopenharmony_ci banner and exit message to use, if provided. The console object is discarded 427db96d56Sopenharmony_ci after use. 437db96d56Sopenharmony_ci 447db96d56Sopenharmony_ci .. versionchanged:: 3.6 457db96d56Sopenharmony_ci Added *exitmsg* parameter. 467db96d56Sopenharmony_ci 477db96d56Sopenharmony_ci 487db96d56Sopenharmony_ci.. function:: compile_command(source, filename="<input>", symbol="single") 497db96d56Sopenharmony_ci 507db96d56Sopenharmony_ci This function is useful for programs that want to emulate Python's interpreter 517db96d56Sopenharmony_ci main loop (a.k.a. the read-eval-print loop). The tricky part is to determine 527db96d56Sopenharmony_ci when the user has entered an incomplete command that can be completed by 537db96d56Sopenharmony_ci entering more text (as opposed to a complete command or a syntax error). This 547db96d56Sopenharmony_ci function *almost* always makes the same decision as the real interpreter main 557db96d56Sopenharmony_ci loop. 567db96d56Sopenharmony_ci 577db96d56Sopenharmony_ci *source* is the source string; *filename* is the optional filename from which 587db96d56Sopenharmony_ci source was read, defaulting to ``'<input>'``; and *symbol* is the optional 597db96d56Sopenharmony_ci grammar start symbol, which should be ``'single'`` (the default), ``'eval'`` 607db96d56Sopenharmony_ci or ``'exec'``. 617db96d56Sopenharmony_ci 627db96d56Sopenharmony_ci Returns a code object (the same as ``compile(source, filename, symbol)``) if the 637db96d56Sopenharmony_ci command is complete and valid; ``None`` if the command is incomplete; raises 647db96d56Sopenharmony_ci :exc:`SyntaxError` if the command is complete and contains a syntax error, or 657db96d56Sopenharmony_ci raises :exc:`OverflowError` or :exc:`ValueError` if the command contains an 667db96d56Sopenharmony_ci invalid literal. 677db96d56Sopenharmony_ci 687db96d56Sopenharmony_ci 697db96d56Sopenharmony_ci.. _interpreter-objects: 707db96d56Sopenharmony_ci 717db96d56Sopenharmony_ciInteractive Interpreter Objects 727db96d56Sopenharmony_ci------------------------------- 737db96d56Sopenharmony_ci 747db96d56Sopenharmony_ci 757db96d56Sopenharmony_ci.. method:: InteractiveInterpreter.runsource(source, filename="<input>", symbol="single") 767db96d56Sopenharmony_ci 777db96d56Sopenharmony_ci Compile and run some source in the interpreter. Arguments are the same as for 787db96d56Sopenharmony_ci :func:`compile_command`; the default for *filename* is ``'<input>'``, and for 797db96d56Sopenharmony_ci *symbol* is ``'single'``. One of several things can happen: 807db96d56Sopenharmony_ci 817db96d56Sopenharmony_ci * The input is incorrect; :func:`compile_command` raised an exception 827db96d56Sopenharmony_ci (:exc:`SyntaxError` or :exc:`OverflowError`). A syntax traceback will be 837db96d56Sopenharmony_ci printed by calling the :meth:`showsyntaxerror` method. :meth:`runsource` 847db96d56Sopenharmony_ci returns ``False``. 857db96d56Sopenharmony_ci 867db96d56Sopenharmony_ci * The input is incomplete, and more input is required; :func:`compile_command` 877db96d56Sopenharmony_ci returned ``None``. :meth:`runsource` returns ``True``. 887db96d56Sopenharmony_ci 897db96d56Sopenharmony_ci * The input is complete; :func:`compile_command` returned a code object. The 907db96d56Sopenharmony_ci code is executed by calling the :meth:`runcode` (which also handles run-time 917db96d56Sopenharmony_ci exceptions, except for :exc:`SystemExit`). :meth:`runsource` returns ``False``. 927db96d56Sopenharmony_ci 937db96d56Sopenharmony_ci The return value can be used to decide whether to use ``sys.ps1`` or ``sys.ps2`` 947db96d56Sopenharmony_ci to prompt the next line. 957db96d56Sopenharmony_ci 967db96d56Sopenharmony_ci 977db96d56Sopenharmony_ci.. method:: InteractiveInterpreter.runcode(code) 987db96d56Sopenharmony_ci 997db96d56Sopenharmony_ci Execute a code object. When an exception occurs, :meth:`showtraceback` is called 1007db96d56Sopenharmony_ci to display a traceback. All exceptions are caught except :exc:`SystemExit`, 1017db96d56Sopenharmony_ci which is allowed to propagate. 1027db96d56Sopenharmony_ci 1037db96d56Sopenharmony_ci A note about :exc:`KeyboardInterrupt`: this exception may occur elsewhere in 1047db96d56Sopenharmony_ci this code, and may not always be caught. The caller should be prepared to deal 1057db96d56Sopenharmony_ci with it. 1067db96d56Sopenharmony_ci 1077db96d56Sopenharmony_ci 1087db96d56Sopenharmony_ci.. method:: InteractiveInterpreter.showsyntaxerror(filename=None) 1097db96d56Sopenharmony_ci 1107db96d56Sopenharmony_ci Display the syntax error that just occurred. This does not display a stack 1117db96d56Sopenharmony_ci trace because there isn't one for syntax errors. If *filename* is given, it is 1127db96d56Sopenharmony_ci stuffed into the exception instead of the default filename provided by Python's 1137db96d56Sopenharmony_ci parser, because it always uses ``'<string>'`` when reading from a string. The 1147db96d56Sopenharmony_ci output is written by the :meth:`write` method. 1157db96d56Sopenharmony_ci 1167db96d56Sopenharmony_ci 1177db96d56Sopenharmony_ci.. method:: InteractiveInterpreter.showtraceback() 1187db96d56Sopenharmony_ci 1197db96d56Sopenharmony_ci Display the exception that just occurred. We remove the first stack item 1207db96d56Sopenharmony_ci because it is within the interpreter object implementation. The output is 1217db96d56Sopenharmony_ci written by the :meth:`write` method. 1227db96d56Sopenharmony_ci 1237db96d56Sopenharmony_ci .. versionchanged:: 3.5 The full chained traceback is displayed instead 1247db96d56Sopenharmony_ci of just the primary traceback. 1257db96d56Sopenharmony_ci 1267db96d56Sopenharmony_ci 1277db96d56Sopenharmony_ci.. method:: InteractiveInterpreter.write(data) 1287db96d56Sopenharmony_ci 1297db96d56Sopenharmony_ci Write a string to the standard error stream (``sys.stderr``). Derived classes 1307db96d56Sopenharmony_ci should override this to provide the appropriate output handling as needed. 1317db96d56Sopenharmony_ci 1327db96d56Sopenharmony_ci 1337db96d56Sopenharmony_ci.. _console-objects: 1347db96d56Sopenharmony_ci 1357db96d56Sopenharmony_ciInteractive Console Objects 1367db96d56Sopenharmony_ci--------------------------- 1377db96d56Sopenharmony_ci 1387db96d56Sopenharmony_ciThe :class:`InteractiveConsole` class is a subclass of 1397db96d56Sopenharmony_ci:class:`InteractiveInterpreter`, and so offers all the methods of the 1407db96d56Sopenharmony_ciinterpreter objects as well as the following additions. 1417db96d56Sopenharmony_ci 1427db96d56Sopenharmony_ci 1437db96d56Sopenharmony_ci.. method:: InteractiveConsole.interact(banner=None, exitmsg=None) 1447db96d56Sopenharmony_ci 1457db96d56Sopenharmony_ci Closely emulate the interactive Python console. The optional *banner* argument 1467db96d56Sopenharmony_ci specify the banner to print before the first interaction; by default it prints a 1477db96d56Sopenharmony_ci banner similar to the one printed by the standard Python interpreter, followed 1487db96d56Sopenharmony_ci by the class name of the console object in parentheses (so as not to confuse 1497db96d56Sopenharmony_ci this with the real interpreter -- since it's so close!). 1507db96d56Sopenharmony_ci 1517db96d56Sopenharmony_ci The optional *exitmsg* argument specifies an exit message printed when exiting. 1527db96d56Sopenharmony_ci Pass the empty string to suppress the exit message. If *exitmsg* is not given or 1537db96d56Sopenharmony_ci ``None``, a default message is printed. 1547db96d56Sopenharmony_ci 1557db96d56Sopenharmony_ci .. versionchanged:: 3.4 1567db96d56Sopenharmony_ci To suppress printing any banner, pass an empty string. 1577db96d56Sopenharmony_ci 1587db96d56Sopenharmony_ci .. versionchanged:: 3.6 1597db96d56Sopenharmony_ci Print an exit message when exiting. 1607db96d56Sopenharmony_ci 1617db96d56Sopenharmony_ci 1627db96d56Sopenharmony_ci.. method:: InteractiveConsole.push(line) 1637db96d56Sopenharmony_ci 1647db96d56Sopenharmony_ci Push a line of source text to the interpreter. The line should not have a 1657db96d56Sopenharmony_ci trailing newline; it may have internal newlines. The line is appended to a 1667db96d56Sopenharmony_ci buffer and the interpreter's :meth:`runsource` method is called with the 1677db96d56Sopenharmony_ci concatenated contents of the buffer as source. If this indicates that the 1687db96d56Sopenharmony_ci command was executed or invalid, the buffer is reset; otherwise, the command is 1697db96d56Sopenharmony_ci incomplete, and the buffer is left as it was after the line was appended. The 1707db96d56Sopenharmony_ci return value is ``True`` if more input is required, ``False`` if the line was 1717db96d56Sopenharmony_ci dealt with in some way (this is the same as :meth:`runsource`). 1727db96d56Sopenharmony_ci 1737db96d56Sopenharmony_ci 1747db96d56Sopenharmony_ci.. method:: InteractiveConsole.resetbuffer() 1757db96d56Sopenharmony_ci 1767db96d56Sopenharmony_ci Remove any unhandled source text from the input buffer. 1777db96d56Sopenharmony_ci 1787db96d56Sopenharmony_ci 1797db96d56Sopenharmony_ci.. method:: InteractiveConsole.raw_input(prompt="") 1807db96d56Sopenharmony_ci 1817db96d56Sopenharmony_ci Write a prompt and read a line. The returned line does not include the trailing 1827db96d56Sopenharmony_ci newline. When the user enters the EOF key sequence, :exc:`EOFError` is raised. 1837db96d56Sopenharmony_ci The base implementation reads from ``sys.stdin``; a subclass may replace this 1847db96d56Sopenharmony_ci with a different implementation. 185