summaryrefslogtreecommitdiff
path: root/devdocs/python~3.12/library%2Fcode.html
diff options
context:
space:
mode:
Diffstat (limited to 'devdocs/python~3.12/library%2Fcode.html')
-rw-r--r--devdocs/python~3.12/library%2Fcode.html45
1 files changed, 45 insertions, 0 deletions
diff --git a/devdocs/python~3.12/library%2Fcode.html b/devdocs/python~3.12/library%2Fcode.html
new file mode 100644
index 00000000..18948921
--- /dev/null
+++ b/devdocs/python~3.12/library%2Fcode.html
@@ -0,0 +1,45 @@
+ <span id="code-interpreter-base-classes"></span><h1>code — Interpreter base classes</h1> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/code.py">Lib/code.py</a></p> <p>The <code>code</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.</p> <dl class="py class"> <dt class="sig sig-object py" id="code.InteractiveInterpreter">
+<code>class code.InteractiveInterpreter(locals=None)</code> </dt> <dd>
+<p>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 <em>locals</em> argument specifies the dictionary in which code will be executed; it defaults to a newly created dictionary with key <code>'__name__'</code> set to <code>'__console__'</code> and key <code>'__doc__'</code> set to <code>None</code>.</p> </dd>
+</dl> <dl class="py class"> <dt class="sig sig-object py" id="code.InteractiveConsole">
+<code>class code.InteractiveConsole(locals=None, filename='&lt;console&gt;')</code> </dt> <dd>
+<p>Closely emulate the behavior of the interactive Python interpreter. This class builds on <a class="reference internal" href="#code.InteractiveInterpreter" title="code.InteractiveInterpreter"><code>InteractiveInterpreter</code></a> and adds prompting using the familiar <code>sys.ps1</code> and <code>sys.ps2</code>, and input buffering.</p> </dd>
+</dl> <dl class="py function"> <dt class="sig sig-object py" id="code.interact">
+<code>code.interact(banner=None, readfunc=None, local=None, exitmsg=None)</code> </dt> <dd>
+<p>Convenience function to run a read-eval-print loop. This creates a new instance of <a class="reference internal" href="#code.InteractiveConsole" title="code.InteractiveConsole"><code>InteractiveConsole</code></a> and sets <em>readfunc</em> to be used as the <a class="reference internal" href="#code.InteractiveConsole.raw_input" title="code.InteractiveConsole.raw_input"><code>InteractiveConsole.raw_input()</code></a> method, if provided. If <em>local</em> is provided, it is passed to the <a class="reference internal" href="#code.InteractiveConsole" title="code.InteractiveConsole"><code>InteractiveConsole</code></a> constructor for use as the default namespace for the interpreter loop. The <a class="reference internal" href="#code.interact" title="code.interact"><code>interact()</code></a> method of the instance is then run with <em>banner</em> and <em>exitmsg</em> passed as the banner and exit message to use, if provided. The console object is discarded after use.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Added <em>exitmsg</em> parameter.</p> </div> </dd>
+</dl> <dl class="py function"> <dt class="sig sig-object py" id="code.compile_command">
+<code>code.compile_command(source, filename='&lt;input&gt;', symbol='single')</code> </dt> <dd>
+<p>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 <em>almost</em> always makes the same decision as the real interpreter main loop.</p> <p><em>source</em> is the source string; <em>filename</em> is the optional filename from which source was read, defaulting to <code>'&lt;input&gt;'</code>; and <em>symbol</em> is the optional grammar start symbol, which should be <code>'single'</code> (the default), <code>'eval'</code> or <code>'exec'</code>.</p> <p>Returns a code object (the same as <code>compile(source, filename, symbol)</code>) if the command is complete and valid; <code>None</code> if the command is incomplete; raises <a class="reference internal" href="exceptions#SyntaxError" title="SyntaxError"><code>SyntaxError</code></a> if the command is complete and contains a syntax error, or raises <a class="reference internal" href="exceptions#OverflowError" title="OverflowError"><code>OverflowError</code></a> or <a class="reference internal" href="exceptions#ValueError" title="ValueError"><code>ValueError</code></a> if the command contains an invalid literal.</p> </dd>
+</dl> <section id="interactive-interpreter-objects"> <span id="interpreter-objects"></span><h2>Interactive Interpreter Objects</h2> <dl class="py method"> <dt class="sig sig-object py" id="code.InteractiveInterpreter.runsource">
+<code>InteractiveInterpreter.runsource(source, filename='&lt;input&gt;', symbol='single')</code> </dt> <dd>
+<p>Compile and run some source in the interpreter. Arguments are the same as for <a class="reference internal" href="#code.compile_command" title="code.compile_command"><code>compile_command()</code></a>; the default for <em>filename</em> is <code>'&lt;input&gt;'</code>, and for <em>symbol</em> is <code>'single'</code>. One of several things can happen:</p> <ul class="simple"> <li>The input is incorrect; <a class="reference internal" href="#code.compile_command" title="code.compile_command"><code>compile_command()</code></a> raised an exception (<a class="reference internal" href="exceptions#SyntaxError" title="SyntaxError"><code>SyntaxError</code></a> or <a class="reference internal" href="exceptions#OverflowError" title="OverflowError"><code>OverflowError</code></a>). A syntax traceback will be printed by calling the <a class="reference internal" href="#code.InteractiveInterpreter.showsyntaxerror" title="code.InteractiveInterpreter.showsyntaxerror"><code>showsyntaxerror()</code></a> method. <a class="reference internal" href="#code.InteractiveInterpreter.runsource" title="code.InteractiveInterpreter.runsource"><code>runsource()</code></a> returns <code>False</code>.</li> <li>The input is incomplete, and more input is required; <a class="reference internal" href="#code.compile_command" title="code.compile_command"><code>compile_command()</code></a> returned <code>None</code>. <a class="reference internal" href="#code.InteractiveInterpreter.runsource" title="code.InteractiveInterpreter.runsource"><code>runsource()</code></a> returns <code>True</code>.</li> <li>The input is complete; <a class="reference internal" href="#code.compile_command" title="code.compile_command"><code>compile_command()</code></a> returned a code object. The code is executed by calling the <a class="reference internal" href="#code.InteractiveInterpreter.runcode" title="code.InteractiveInterpreter.runcode"><code>runcode()</code></a> (which also handles run-time exceptions, except for <a class="reference internal" href="exceptions#SystemExit" title="SystemExit"><code>SystemExit</code></a>). <a class="reference internal" href="#code.InteractiveInterpreter.runsource" title="code.InteractiveInterpreter.runsource"><code>runsource()</code></a> returns <code>False</code>.</li> </ul> <p>The return value can be used to decide whether to use <code>sys.ps1</code> or <code>sys.ps2</code> to prompt the next line.</p> </dd>
+</dl> <dl class="py method"> <dt class="sig sig-object py" id="code.InteractiveInterpreter.runcode">
+<code>InteractiveInterpreter.runcode(code)</code> </dt> <dd>
+<p>Execute a code object. When an exception occurs, <a class="reference internal" href="#code.InteractiveInterpreter.showtraceback" title="code.InteractiveInterpreter.showtraceback"><code>showtraceback()</code></a> is called to display a traceback. All exceptions are caught except <a class="reference internal" href="exceptions#SystemExit" title="SystemExit"><code>SystemExit</code></a>, which is allowed to propagate.</p> <p>A note about <a class="reference internal" href="exceptions#KeyboardInterrupt" title="KeyboardInterrupt"><code>KeyboardInterrupt</code></a>: this exception may occur elsewhere in this code, and may not always be caught. The caller should be prepared to deal with it.</p> </dd>
+</dl> <dl class="py method"> <dt class="sig sig-object py" id="code.InteractiveInterpreter.showsyntaxerror">
+<code>InteractiveInterpreter.showsyntaxerror(filename=None)</code> </dt> <dd>
+<p>Display the syntax error that just occurred. This does not display a stack trace because there isn’t one for syntax errors. If <em>filename</em> is given, it is stuffed into the exception instead of the default filename provided by Python’s parser, because it always uses <code>'&lt;string&gt;'</code> when reading from a string. The output is written by the <a class="reference internal" href="#code.InteractiveInterpreter.write" title="code.InteractiveInterpreter.write"><code>write()</code></a> method.</p> </dd>
+</dl> <dl class="py method"> <dt class="sig sig-object py" id="code.InteractiveInterpreter.showtraceback">
+<code>InteractiveInterpreter.showtraceback()</code> </dt> <dd>
+<p>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 <a class="reference internal" href="#code.InteractiveInterpreter.write" title="code.InteractiveInterpreter.write"><code>write()</code></a> method.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.5: </span>The full chained traceback is displayed instead of just the primary traceback.</p> </div> </dd>
+</dl> <dl class="py method"> <dt class="sig sig-object py" id="code.InteractiveInterpreter.write">
+<code>InteractiveInterpreter.write(data)</code> </dt> <dd>
+<p>Write a string to the standard error stream (<code>sys.stderr</code>). Derived classes should override this to provide the appropriate output handling as needed.</p> </dd>
+</dl> </section> <section id="interactive-console-objects"> <span id="console-objects"></span><h2>Interactive Console Objects</h2> <p>The <a class="reference internal" href="#code.InteractiveConsole" title="code.InteractiveConsole"><code>InteractiveConsole</code></a> class is a subclass of <a class="reference internal" href="#code.InteractiveInterpreter" title="code.InteractiveInterpreter"><code>InteractiveInterpreter</code></a>, and so offers all the methods of the interpreter objects as well as the following additions.</p> <dl class="py method"> <dt class="sig sig-object py" id="code.InteractiveConsole.interact">
+<code>InteractiveConsole.interact(banner=None, exitmsg=None)</code> </dt> <dd>
+<p>Closely emulate the interactive Python console. The optional <em>banner</em> argument 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!).</p> <p>The optional <em>exitmsg</em> argument specifies an exit message printed when exiting. Pass the empty string to suppress the exit message. If <em>exitmsg</em> is not given or <code>None</code>, a default message is printed.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.4: </span>To suppress printing any banner, pass an empty string.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Print an exit message when exiting.</p> </div> </dd>
+</dl> <dl class="py method"> <dt class="sig sig-object py" id="code.InteractiveConsole.push">
+<code>InteractiveConsole.push(line)</code> </dt> <dd>
+<p>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 buffer and the interpreter’s <a class="reference internal" href="#code.InteractiveInterpreter.runsource" title="code.InteractiveInterpreter.runsource"><code>runsource()</code></a> method is called with the 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 <code>True</code> if more input is required, <code>False</code> if the line was dealt with in some way (this is the same as <code>runsource()</code>).</p> </dd>
+</dl> <dl class="py method"> <dt class="sig sig-object py" id="code.InteractiveConsole.resetbuffer">
+<code>InteractiveConsole.resetbuffer()</code> </dt> <dd>
+<p>Remove any unhandled source text from the input buffer.</p> </dd>
+</dl> <dl class="py method"> <dt class="sig sig-object py" id="code.InteractiveConsole.raw_input">
+<code>InteractiveConsole.raw_input(prompt='')</code> </dt> <dd>
+<p>Write a prompt and read a line. The returned line does not include the trailing newline. When the user enters the EOF key sequence, <a class="reference internal" href="exceptions#EOFError" title="EOFError"><code>EOFError</code></a> is raised. The base implementation reads from <code>sys.stdin</code>; a subclass may replace this with a different implementation.</p> </dd>
+</dl> </section> <div class="_attribution">
+ <p class="_attribution-p">
+ &copy; 2001&ndash;2023 Python Software Foundation<br>Licensed under the PSF License.<br>
+ <a href="https://docs.python.org/3.12/library/code.html" class="_attribution-link">https://docs.python.org/3.12/library/code.html</a>
+ </p>
+</div>