parthtamu/rag-code-assistant
0
1<!DOCTYPE html>2 3<html lang="en" data-content_root="../">4 <head>5 <meta charset="utf-8" />6 <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />7<meta property="og:title" content="codeop — Compile Python code" />8<meta property="og:type" content="website" />9<meta property="og:url" content="https://docs.python.org/3/library/codeop.html" />10<meta property="og:site_name" content="Python documentation" />11<meta property="og:description" content="Source code: Lib/codeop.py The codeop module provides utilities upon which the Python read-eval-print loop can be emulated, as is done in the code module. As a result, you probably don’t want to us..." />12<meta property="og:image:width" content="1146" />13<meta property="og:image:height" content="600" />14<meta property="og:image" content="https://docs.python.org/3.15/_images/social_previews/summary_library_codeop_2f948fc5.png" />15<meta property="og:image:alt" content="Source code: Lib/codeop.py The codeop module provides utilities upon which the Python read-eval-print loop can be emulated, as is done in the code module. As a result, you probably don’t want to us..." />16<meta name="description" content="Source code: Lib/codeop.py The codeop module provides utilities upon which the Python read-eval-print loop can be emulated, as is done in the code module. As a result, you probably don’t want to us..." />17<meta name="twitter:card" content="summary_large_image" />18<meta name="theme-color" content="#3776ab">19 20 <title>codeop — Compile Python code — Python 3.15.0a6 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">21 22 <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />23 <link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />24 <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=89a2f22a" />25 <link rel="stylesheet" type="text/css" href="../_static/profiling-sampling-visualization.css?v=0c2600ae" />26 <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />27 28 <script src="../_static/documentation_options.js?v=6b7c9ff5"></script>29 <script src="../_static/doctools.js?v=9bcbadda"></script>30 <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>31 <script src="../_static/profiling-sampling-visualization.js?v=9811ed04"></script>32 33 <script src="../_static/sidebar.js"></script>34 35 <link rel="search" type="application/opensearchdescription+xml"36 title="Search within Python 3.15.0a6 documentation"37 href="../_static/opensearch.xml"/>38 <link rel="author" title="About these documents" href="../about.html" />39 <link rel="index" title="Index" href="../genindex.html" />40 <link rel="search" title="Search" href="../search.html" />41 <link rel="copyright" title="Copyright" href="../copyright.html" />42 <link rel="next" title="Importing Modules" href="modules.html" />43 <link rel="prev" title="code — Interpreter base classes" href="code.html" />44 45 46 <script defer file-types="bz2,epub,zip" data-domain="docs.python.org" src="https://analytics.python.org/js/script.file-downloads.outbound-links.js"></script>47 48 <link rel="canonical" href="https://docs.python.org/3/library/codeop.html">49 50 51 52 53 <style>54 @media only screen {55 table.full-width-table {56 width: 100%;57 }58 }59 </style>60<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">61 <link rel="shortcut icon" type="image/png" href="../_static/py.svg">62 <script type="text/javascript" src="../_static/copybutton.js"></script>63 <script type="text/javascript" src="../_static/menu.js"></script>64 <script type="text/javascript" src="../_static/search-focus.js"></script>65 <script type="text/javascript" src="../_static/themetoggle.js"></script> 66 <script type="text/javascript" src="../_static/rtd_switcher.js"></script>67 <meta name="readthedocs-addons-api-version" content="1">68 69 </head>70<body>71<div class="mobile-nav">72 <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"73 aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu">74 <nav class="nav-content" role="navigation">75 <label for="menuToggler" class="toggler__label">76 <span></span>77 </label>78 <span class="nav-items-wrapper">79 <a href="https://www.python.org/" class="nav-logo">80 <img src="../_static/py.svg" alt="Python logo">81 </a>82 <span class="version_switcher_placeholder"></span>83 <form role="search" class="search" action="../search.html" method="get">84 <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">85 <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>86 </svg>87 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q">88 <input type="submit" value="Go">89 </form>90 </span>91 </nav>92 <div class="menu-wrapper">93 <nav class="menu" role="navigation" aria-label="main navigation">94 <div class="language_switcher_placeholder"></div>95 96<label class="theme-selector-label">97 Theme98 <select class="theme-selector" oninput="activateTheme(this.value)">99 <option value="auto" selected>Auto</option>100 <option value="light">Light</option>101 <option value="dark">Dark</option>102 </select>103</label>104 <div>105 <h4>Previous topic</h4>106 <p class="topless"><a href="code.html"107 title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">code</span></code> — Interpreter base classes</a></p>108 </div>109 <div>110 <h4>Next topic</h4>111 <p class="topless"><a href="modules.html"112 title="next chapter">Importing Modules</a></p>113 </div>114 <script>115 document.addEventListener('DOMContentLoaded', () => {116 const title = document.querySelector('meta[property="og:title"]').content;117 const elements = document.querySelectorAll('.improvepage');118 const pageurl = window.location.href.split('?')[0];119 elements.forEach(element => {120 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));121 url.searchParams.set('pagetitle', title);122 url.searchParams.set('pageurl', pageurl);123 url.searchParams.set('pagesource', "library/codeop.rst");124 element.href = url.toString();125 });126 });127 </script>128 <div role="note" aria-label="source link">129 <h3>This page</h3>130 <ul class="this-page-menu">131 <li><a href="../bugs.html">Report a bug</a></li>132 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>133 <li>134 <a href="https://github.com/python/cpython/blob/main/Doc/library/codeop.rst?plain=1"135 rel="nofollow">Show source136 </a>137 </li>138 139 </ul>140 </div>141 </nav>142 </div>143</div>144 145 146 <div class="related" role="navigation" aria-label="Related">147 <h3>Navigation</h3>148 <ul>149 <li class="right" style="margin-right: 10px">150 <a href="../genindex.html" title="General Index"151 accesskey="I">index</a></li>152 <li class="right" >153 <a href="../py-modindex.html" title="Python Module Index"154 >modules</a> |</li>155 <li class="right" >156 <a href="modules.html" title="Importing Modules"157 accesskey="N">next</a> |</li>158 <li class="right" >159 <a href="code.html" title="code — Interpreter base classes"160 accesskey="P">previous</a> |</li>161 162 <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>163 <li><a href="https://www.python.org/">Python</a> »</li>164 <li class="switchers">165 <div class="language_switcher_placeholder"></div>166 <div class="version_switcher_placeholder"></div>167 </li>168 <li>169 170 </li>171 <li id="cpython-language-and-version">172 <a href="../index.html">3.15.0a6 Documentation</a> »173 </li>174 175 <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>176 <li class="nav-item nav-item-2"><a href="custominterp.html" accesskey="U">Custom Python Interpreters</a> »</li>177 <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">codeop</span></code> — Compile Python code</a></li>178 <li class="right">179 180 181 <div class="inline-search" role="search">182 <form class="inline-search" action="../search.html" method="get">183 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">184 <input type="submit" value="Go">185 </form>186 </div>187 |188 </li>189 <li class="right">190<label class="theme-selector-label">191 Theme192 <select class="theme-selector" oninput="activateTheme(this.value)">193 <option value="auto" selected>Auto</option>194 <option value="light">Light</option>195 <option value="dark">Dark</option>196 </select>197</label> |</li>198 199 </ul>200 </div> 201 202 <div class="document">203 <div class="documentwrapper">204 <div class="bodywrapper">205 <div class="body" role="main">206 207 <section id="module-codeop">208<span id="codeop-compile-python-code"></span><h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">codeop</span></code> — Compile Python code<a class="headerlink" href="#module-codeop" title="Link to this heading">¶</a></h1>209<p><strong>Source code:</strong> <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/main/Lib/codeop.py">Lib/codeop.py</a></p>210<hr class="docutils" />211<p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">codeop</span></code> module provides utilities upon which the Python212read-eval-print loop can be emulated, as is done in the <a class="reference internal" href="code.html#module-code" title="code: Facilities to implement read-eval-print loops."><code class="xref py py-mod docutils literal notranslate"><span class="pre">code</span></code></a> module. As213a result, you probably don’t want to use the module directly; if you want to214include such a loop in your program you probably want to use the <code class="xref py py-mod docutils literal notranslate"><span class="pre">code</span></code>215module instead.</p>216<p>There are two parts to this job:</p>217<ol class="arabic simple">218<li><p>Being able to tell if a line of input completes a Python statement: in219short, telling whether to print ‘<code class="docutils literal notranslate"><span class="pre">>>></span></code>’ or ‘<code class="docutils literal notranslate"><span class="pre">...</span></code>’ next.</p></li>220<li><p>Remembering which future statements the user has entered, so subsequent221input can be compiled with these in effect.</p></li>222</ol>223<p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">codeop</span></code> module provides a way of doing each of these things, and a way224of doing them both.</p>225<p>To do just the former:</p>226<dl class="py function">227<dt class="sig sig-object py" id="codeop.compile_command">228<span class="sig-prename descclassname"><span class="pre">codeop.</span></span><span class="sig-name descname"><span class="pre">compile_command</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">source</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">filename</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'<input>'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">symbol</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'single'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codeop.compile_command" title="Link to this definition">¶</a></dt>229<dd><p>Tries to compile <em>source</em>, which should be a string of Python code and return a230code object if <em>source</em> is valid Python code. In that case, the filename231attribute of the code object will be <em>filename</em>, which defaults to232<code class="docutils literal notranslate"><span class="pre">'<input>'</span></code>. Returns <code class="docutils literal notranslate"><span class="pre">None</span></code> if <em>source</em> is <em>not</em> valid Python code, but is a233prefix of valid Python code.</p>234<p>If there is a problem with <em>source</em>, an exception will be raised.235<a class="reference internal" href="exceptions.html#SyntaxError" title="SyntaxError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxError</span></code></a> is raised if there is invalid Python syntax, and236<a class="reference internal" href="exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a> or <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> if there is an invalid literal.</p>237<p>The <em>symbol</em> argument determines whether <em>source</em> is compiled as a statement238(<code class="docutils literal notranslate"><span class="pre">'single'</span></code>, the default), as a sequence of <a class="reference internal" href="../glossary.html#term-statement"><span class="xref std std-term">statement</span></a> (<code class="docutils literal notranslate"><span class="pre">'exec'</span></code>) or239as an <a class="reference internal" href="../glossary.html#term-expression"><span class="xref std std-term">expression</span></a> (<code class="docutils literal notranslate"><span class="pre">'eval'</span></code>). Any other value will240cause <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> to be raised.</p>241<div class="admonition note">242<p class="admonition-title">Note</p>243<p>It is possible (but not likely) that the parser stops parsing with a244successful outcome before reaching the end of the source; in this case,245trailing symbols may be ignored instead of causing an error. For example,246a backslash followed by two newlines may be followed by arbitrary garbage.247This will be fixed once the API for the parser is better.</p>248</div>249</dd></dl>250 251<dl class="py class">252<dt class="sig sig-object py" id="codeop.Compile">253<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">codeop.</span></span><span class="sig-name descname"><span class="pre">Compile</span></span><a class="headerlink" href="#codeop.Compile" title="Link to this definition">¶</a></dt>254<dd><p>Instances of this class have <a class="reference internal" href="../reference/datamodel.html#object.__call__" title="object.__call__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__call__()</span></code></a> methods identical in signature to255the built-in function <a class="reference internal" href="functions.html#compile" title="compile"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile()</span></code></a>, but with the difference that if the256instance compiles program text containing a <a class="reference internal" href="__future__.html#module-__future__" title="__future__: Future statement definitions"><code class="xref py py-mod docutils literal notranslate"><span class="pre">__future__</span></code></a> statement, the257instance ‘remembers’ and compiles all subsequent program texts with the258statement in force.</p>259</dd></dl>260 261<dl class="py class">262<dt class="sig sig-object py" id="codeop.CommandCompiler">263<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">codeop.</span></span><span class="sig-name descname"><span class="pre">CommandCompiler</span></span><a class="headerlink" href="#codeop.CommandCompiler" title="Link to this definition">¶</a></dt>264<dd><p>Instances of this class have <a class="reference internal" href="../reference/datamodel.html#object.__call__" title="object.__call__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__call__()</span></code></a> methods identical in signature to265<a class="reference internal" href="#codeop.compile_command" title="codeop.compile_command"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile_command()</span></code></a>; the difference is that if the instance compiles program266text containing a <a class="reference internal" href="__future__.html#module-__future__" title="__future__: Future statement definitions"><code class="xref py py-mod docutils literal notranslate"><span class="pre">__future__</span></code></a> statement, the instance ‘remembers’ and267compiles all subsequent program texts with the statement in force.</p>268</dd></dl>269 270</section>271 272 273 <div class="clearer"></div>274 </div>275 </div>276 </div>277 <div class="sphinxsidebar" role="navigation" aria-label="Main">278 <div class="sphinxsidebarwrapper">279 <div>280 <h4>Previous topic</h4>281 <p class="topless"><a href="code.html"282 title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">code</span></code> — Interpreter base classes</a></p>283 </div>284 <div>285 <h4>Next topic</h4>286 <p class="topless"><a href="modules.html"287 title="next chapter">Importing Modules</a></p>288 </div>289 <script>290 document.addEventListener('DOMContentLoaded', () => {291 const title = document.querySelector('meta[property="og:title"]').content;292 const elements = document.querySelectorAll('.improvepage');293 const pageurl = window.location.href.split('?')[0];294 elements.forEach(element => {295 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));296 url.searchParams.set('pagetitle', title);297 url.searchParams.set('pageurl', pageurl);298 url.searchParams.set('pagesource', "library/codeop.rst");299 element.href = url.toString();300 });301 });302 </script>303 <div role="note" aria-label="source link">304 <h3>This page</h3>305 <ul class="this-page-menu">306 <li><a href="../bugs.html">Report a bug</a></li>307 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>308 <li>309 <a href="https://github.com/python/cpython/blob/main/Doc/library/codeop.rst?plain=1"310 rel="nofollow">Show source311 </a>312 </li>313 314 </ul>315 </div>316 </div>317<div id="sidebarbutton" title="Collapse sidebar">318<span>«</span>319</div>320 321 </div>322 <div class="clearer"></div>323 </div> 324 <div class="related" role="navigation" aria-label="Related">325 <h3>Navigation</h3>326 <ul>327 <li class="right" style="margin-right: 10px">328 <a href="../genindex.html" title="General Index"329 >index</a></li>330 <li class="right" >331 <a href="../py-modindex.html" title="Python Module Index"332 >modules</a> |</li>333 <li class="right" >334 <a href="modules.html" title="Importing Modules"335 >next</a> |</li>336 <li class="right" >337 <a href="code.html" title="code — Interpreter base classes"338 >previous</a> |</li>339 340 <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>341 <li><a href="https://www.python.org/">Python</a> »</li>342 <li class="switchers">343 <div class="language_switcher_placeholder"></div>344 <div class="version_switcher_placeholder"></div>345 </li>346 <li>347 348 </li>349 <li id="cpython-language-and-version">350 <a href="../index.html">3.15.0a6 Documentation</a> »351 </li>352 353 <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>354 <li class="nav-item nav-item-2"><a href="custominterp.html" >Custom Python Interpreters</a> »</li>355 <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">codeop</span></code> — Compile Python code</a></li>356 <li class="right">357 358 359 <div class="inline-search" role="search">360 <form class="inline-search" action="../search.html" method="get">361 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">362 <input type="submit" value="Go">363 </form>364 </div>365 |366 </li>367 <li class="right">368<label class="theme-selector-label">369 Theme370 <select class="theme-selector" oninput="activateTheme(this.value)">371 <option value="auto" selected>Auto</option>372 <option value="light">Light</option>373 <option value="dark">Dark</option>374 </select>375</label> |</li>376 377 </ul>378 </div> 379 <div class="footer">380 © <a href="../copyright.html">Copyright</a> 2001 Python Software Foundation.381 <br>382 This page is licensed under the Python Software Foundation License Version 2.383 <br>384 Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.385 <br>386 387 See <a href="/license.html">History and License</a> for more information.<br>388 389 390 <br>391 392 The Python Software Foundation is a non-profit corporation.393<a href="https://www.python.org/psf/donations/">Please donate.</a>394<br>395 <br>396 Last updated on Mar 10, 2026 (08:58 UTC).397 398 <a href="/bugs.html">Found a bug</a>?399 400 <br>401 402 Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.2.3.403 </div>404 405 </body>406</html>