summaryrefslogtreecommitdiff
path: root/devdocs/python~3.12/library%2Fcodeop.html
blob: 557ace14f0f385f088be692cd0bc9b2d4f26551b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 <span id="codeop-compile-python-code"></span><h1>codeop — Compile Python code</h1> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/codeop.py">Lib/codeop.py</a></p>  <p>The <a class="reference internal" href="#module-codeop" title="codeop: Compile (possibly incomplete) Python code."><code>codeop</code></a> module provides utilities upon which the Python read-eval-print loop can be emulated, as is done in the <a class="reference internal" href="code#module-code" title="code: Facilities to implement read-eval-print loops."><code>code</code></a> module. As a result, you probably don’t want to use the module directly; if you want to include such a loop in your program you probably want to use the <a class="reference internal" href="code#module-code" title="code: Facilities to implement read-eval-print loops."><code>code</code></a> module instead.</p> <p>There are two parts to this job:</p> <ol class="arabic simple"> <li>Being able to tell if a line of input completes a Python statement: in short, telling whether to print ‘<code>&gt;&gt;&gt;</code>’ or ‘<code>...</code>’ next.</li> <li>Remembering which future statements the user has entered, so subsequent input can be compiled with these in effect.</li> </ol> <p>The <a class="reference internal" href="#module-codeop" title="codeop: Compile (possibly incomplete) Python code."><code>codeop</code></a> module provides a way of doing each of these things, and a way of doing them both.</p> <p>To do just the former:</p> <dl class="py function"> <dt class="sig sig-object py" id="codeop.compile_command">
<code>codeop.compile_command(source, filename='&lt;input&gt;', symbol='single')</code> </dt> <dd>
<p>Tries to compile <em>source</em>, which should be a string of Python code and return a code object if <em>source</em> is valid Python code. In that case, the filename attribute of the code object will be <em>filename</em>, which defaults to <code>'&lt;input&gt;'</code>. Returns <code>None</code> if <em>source</em> is <em>not</em> valid Python code, but is a prefix of valid Python code.</p> <p>If there is a problem with <em>source</em>, an exception will be raised. <a class="reference internal" href="exceptions#SyntaxError" title="SyntaxError"><code>SyntaxError</code></a> is raised if there is invalid Python syntax, and <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 there is an invalid literal.</p> <p>The <em>symbol</em> argument determines whether <em>source</em> is compiled as a statement (<code>'single'</code>, the default), as a sequence of <a class="reference internal" href="../glossary#term-statement"><span class="xref std std-term">statement</span></a> (<code>'exec'</code>) or as an <a class="reference internal" href="../glossary#term-expression"><span class="xref std std-term">expression</span></a> (<code>'eval'</code>). Any other value will cause <a class="reference internal" href="exceptions#ValueError" title="ValueError"><code>ValueError</code></a> to be raised.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>It is possible (but not likely) that the parser stops parsing with a successful outcome before reaching the end of the source; in this case, trailing symbols may be ignored instead of causing an error. For example, a backslash followed by two newlines may be followed by arbitrary garbage. This will be fixed once the API for the parser is better.</p> </div> </dd>
</dl> <dl class="py class"> <dt class="sig sig-object py" id="codeop.Compile">
<code>class codeop.Compile</code> </dt> <dd>
<p>Instances of this class have <a class="reference internal" href="../reference/datamodel#object.__call__" title="object.__call__"><code>__call__()</code></a> methods identical in signature to the built-in function <a class="reference internal" href="functions#compile" title="compile"><code>compile()</code></a>, but with the difference that if the instance compiles program text containing a <a class="reference internal" href="__future__#module-__future__" title="__future__: Future statement definitions"><code>__future__</code></a> statement, the instance ‘remembers’ and compiles all subsequent program texts with the statement in force.</p> </dd>
</dl> <dl class="py class"> <dt class="sig sig-object py" id="codeop.CommandCompiler">
<code>class codeop.CommandCompiler</code> </dt> <dd>
<p>Instances of this class have <a class="reference internal" href="../reference/datamodel#object.__call__" title="object.__call__"><code>__call__()</code></a> methods identical in signature to <a class="reference internal" href="#codeop.compile_command" title="codeop.compile_command"><code>compile_command()</code></a>; the difference is that if the instance compiles program text containing a <a class="reference internal" href="__future__#module-__future__" title="__future__: Future statement definitions"><code>__future__</code></a> statement, the instance ‘remembers’ and compiles all subsequent program texts with the statement in force.</p> </dd>
</dl> <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/codeop.html" class="_attribution-link">https://docs.python.org/3.12/library/codeop.html</a>
  </p>
</div>