gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

371 lines
21KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="codeop — Compile Python code" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/codeop.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <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..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <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..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>codeop — Compile Python code &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="Importing Modules" href="modules.html" />
  33. <link rel="prev" title="code — Interpreter base classes" href="code.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/codeop.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <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>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h4>Previous topic</h4>
  84. <p class="topless"><a href="code.html"
  85. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">code</span></code> — Interpreter base classes</a></p>
  86. </div>
  87. <div>
  88. <h4>Next topic</h4>
  89. <p class="topless"><a href="modules.html"
  90. title="next chapter">Importing Modules</a></p>
  91. </div>
  92. <div role="note" aria-label="source link">
  93. <h3>This Page</h3>
  94. <ul class="this-page-menu">
  95. <li><a href="../bugs.html">Report a Bug</a></li>
  96. <li>
  97. <a href="https://github.com/python/cpython/blob/main/Doc/library/codeop.rst"
  98. rel="nofollow">Show Source
  99. </a>
  100. </li>
  101. </ul>
  102. </div>
  103. </nav>
  104. </div>
  105. </div>
  106. <div class="related" role="navigation" aria-label="related navigation">
  107. <h3>Navigation</h3>
  108. <ul>
  109. <li class="right" style="margin-right: 10px">
  110. <a href="../genindex.html" title="General Index"
  111. accesskey="I">index</a></li>
  112. <li class="right" >
  113. <a href="../py-modindex.html" title="Python Module Index"
  114. >modules</a> |</li>
  115. <li class="right" >
  116. <a href="modules.html" title="Importing Modules"
  117. accesskey="N">next</a> |</li>
  118. <li class="right" >
  119. <a href="code.html" title="code — Interpreter base classes"
  120. accesskey="P">previous</a> |</li>
  121. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  122. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  123. <li class="switchers">
  124. <div class="language_switcher_placeholder"></div>
  125. <div class="version_switcher_placeholder"></div>
  126. </li>
  127. <li>
  128. </li>
  129. <li id="cpython-language-and-version">
  130. <a href="../index.html">3.12.3 Documentation</a> &#187;
  131. </li>
  132. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  133. <li class="nav-item nav-item-2"><a href="custominterp.html" accesskey="U">Custom Python Interpreters</a> &#187;</li>
  134. <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>
  135. <li class="right">
  136. <div class="inline-search" role="search">
  137. <form class="inline-search" action="../search.html" method="get">
  138. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  139. <input type="submit" value="Go" />
  140. </form>
  141. </div>
  142. |
  143. </li>
  144. <li class="right">
  145. <label class="theme-selector-label">
  146. Theme
  147. <select class="theme-selector" oninput="activateTheme(this.value)">
  148. <option value="auto" selected>Auto</option>
  149. <option value="light">Light</option>
  150. <option value="dark">Dark</option>
  151. </select>
  152. </label> |</li>
  153. </ul>
  154. </div>
  155. <div class="document">
  156. <div class="documentwrapper">
  157. <div class="bodywrapper">
  158. <div class="body" role="main">
  159. <section id="module-codeop">
  160. <span id="codeop-compile-python-code"></span><h1><a class="reference internal" href="#module-codeop" title="codeop: Compile (possibly incomplete) Python code."><code class="xref py py-mod docutils literal notranslate"><span class="pre">codeop</span></code></a> — Compile Python code<a class="headerlink" href="#module-codeop" title="Link to this heading">¶</a></h1>
  161. <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>
  162. <hr class="docutils" />
  163. <p>The <a class="reference internal" href="#module-codeop" title="codeop: Compile (possibly incomplete) Python code."><code class="xref py py-mod docutils literal notranslate"><span class="pre">codeop</span></code></a> module provides utilities upon which the Python
  164. read-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. As
  165. a result, you probably don’t want to use the module directly; if you want to
  166. include such a loop in your program you probably want to use 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>
  167. module instead.</p>
  168. <p>There are two parts to this job:</p>
  169. <ol class="arabic simple">
  170. <li><p>Being able to tell if a line of input completes a Python statement: in
  171. short, telling whether to print ‘<code class="docutils literal notranslate"><span class="pre">&gt;&gt;&gt;</span></code>’ or ‘<code class="docutils literal notranslate"><span class="pre">...</span></code>’ next.</p></li>
  172. <li><p>Remembering which future statements the user has entered, so subsequent
  173. input can be compiled with these in effect.</p></li>
  174. </ol>
  175. <p>The <a class="reference internal" href="#module-codeop" title="codeop: Compile (possibly incomplete) Python code."><code class="xref py py-mod docutils literal notranslate"><span class="pre">codeop</span></code></a> module provides a way of doing each of these things, and a way
  176. of doing them both.</p>
  177. <p>To do just the former:</p>
  178. <dl class="py function">
  179. <dt class="sig sig-object py" id="codeop.compile_command">
  180. <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">'&lt;input&gt;'</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>
  181. <dd><p>Tries to compile <em>source</em>, which should be a string of Python code and return a
  182. code object if <em>source</em> is valid Python code. In that case, the filename
  183. attribute of the code object will be <em>filename</em>, which defaults to
  184. <code class="docutils literal notranslate"><span class="pre">'&lt;input&gt;'</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 a
  185. prefix of valid Python code.</p>
  186. <p>If there is a problem with <em>source</em>, an exception will be raised.
  187. <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, and
  188. <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>
  189. <p>The <em>symbol</em> argument determines whether <em>source</em> is compiled as a statement
  190. (<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>) or
  191. as 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 will
  192. cause <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>
  193. <div class="admonition note">
  194. <p class="admonition-title">Note</p>
  195. <p>It is possible (but not likely) that the parser stops parsing with a
  196. successful outcome before reaching the end of the source; in this case,
  197. trailing symbols may be ignored instead of causing an error. For example,
  198. a backslash followed by two newlines may be followed by arbitrary garbage.
  199. This will be fixed once the API for the parser is better.</p>
  200. </div>
  201. </dd></dl>
  202. <dl class="py class">
  203. <dt class="sig sig-object py" id="codeop.Compile">
  204. <em class="property"><span class="pre">class</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>
  205. <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 to
  206. the 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 the
  207. instance 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, the
  208. instance ‘remembers’ and compiles all subsequent program texts with the
  209. statement in force.</p>
  210. </dd></dl>
  211. <dl class="py class">
  212. <dt class="sig sig-object py" id="codeop.CommandCompiler">
  213. <em class="property"><span class="pre">class</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>
  214. <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 to
  215. <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 program
  216. 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, the instance ‘remembers’ and
  217. compiles all subsequent program texts with the statement in force.</p>
  218. </dd></dl>
  219. </section>
  220. <div class="clearer"></div>
  221. </div>
  222. </div>
  223. </div>
  224. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  225. <div class="sphinxsidebarwrapper">
  226. <div>
  227. <h4>Previous topic</h4>
  228. <p class="topless"><a href="code.html"
  229. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">code</span></code> — Interpreter base classes</a></p>
  230. </div>
  231. <div>
  232. <h4>Next topic</h4>
  233. <p class="topless"><a href="modules.html"
  234. title="next chapter">Importing Modules</a></p>
  235. </div>
  236. <div role="note" aria-label="source link">
  237. <h3>This Page</h3>
  238. <ul class="this-page-menu">
  239. <li><a href="../bugs.html">Report a Bug</a></li>
  240. <li>
  241. <a href="https://github.com/python/cpython/blob/main/Doc/library/codeop.rst"
  242. rel="nofollow">Show Source
  243. </a>
  244. </li>
  245. </ul>
  246. </div>
  247. </div>
  248. <div id="sidebarbutton" title="Collapse sidebar">
  249. <span>«</span>
  250. </div>
  251. </div>
  252. <div class="clearer"></div>
  253. </div>
  254. <div class="related" role="navigation" aria-label="related navigation">
  255. <h3>Navigation</h3>
  256. <ul>
  257. <li class="right" style="margin-right: 10px">
  258. <a href="../genindex.html" title="General Index"
  259. >index</a></li>
  260. <li class="right" >
  261. <a href="../py-modindex.html" title="Python Module Index"
  262. >modules</a> |</li>
  263. <li class="right" >
  264. <a href="modules.html" title="Importing Modules"
  265. >next</a> |</li>
  266. <li class="right" >
  267. <a href="code.html" title="code — Interpreter base classes"
  268. >previous</a> |</li>
  269. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  270. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  271. <li class="switchers">
  272. <div class="language_switcher_placeholder"></div>
  273. <div class="version_switcher_placeholder"></div>
  274. </li>
  275. <li>
  276. </li>
  277. <li id="cpython-language-and-version">
  278. <a href="../index.html">3.12.3 Documentation</a> &#187;
  279. </li>
  280. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  281. <li class="nav-item nav-item-2"><a href="custominterp.html" >Custom Python Interpreters</a> &#187;</li>
  282. <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>
  283. <li class="right">
  284. <div class="inline-search" role="search">
  285. <form class="inline-search" action="../search.html" method="get">
  286. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  287. <input type="submit" value="Go" />
  288. </form>
  289. </div>
  290. |
  291. </li>
  292. <li class="right">
  293. <label class="theme-selector-label">
  294. Theme
  295. <select class="theme-selector" oninput="activateTheme(this.value)">
  296. <option value="auto" selected>Auto</option>
  297. <option value="light">Light</option>
  298. <option value="dark">Dark</option>
  299. </select>
  300. </label> |</li>
  301. </ul>
  302. </div>
  303. <div class="footer">
  304. &copy;
  305. <a href="../copyright.html">
  306. Copyright
  307. </a>
  308. 2001-2024, Python Software Foundation.
  309. <br />
  310. This page is licensed under the Python Software Foundation License Version 2.
  311. <br />
  312. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  313. <br />
  314. See <a href="/license.html">History and License</a> for more information.<br />
  315. <br />
  316. The Python Software Foundation is a non-profit corporation.
  317. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  318. <br />
  319. <br />
  320. Last updated on Apr 09, 2024 (13:47 UTC).
  321. <a href="/bugs.html">Found a bug</a>?
  322. <br />
  323. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  324. </div>
  325. </body>
  326. </html>
上海开阖软件有限公司 沪ICP备12045867号-1