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.

509 satır
38KB

  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="code — Interpreter base classes" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/code.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/code.py The 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..." />
  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/code.py The 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..." />
  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>code — Interpreter base classes &#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="codeop — Compile Python code" href="codeop.html" />
  33. <link rel="prev" title="Custom Python Interpreters" href="custominterp.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/code.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. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">code</span></code> — Interpreter base classes</a><ul>
  86. <li><a class="reference internal" href="#interactive-interpreter-objects">Interactive Interpreter Objects</a></li>
  87. <li><a class="reference internal" href="#interactive-console-objects">Interactive Console Objects</a></li>
  88. </ul>
  89. </li>
  90. </ul>
  91. </div>
  92. <div>
  93. <h4>Previous topic</h4>
  94. <p class="topless"><a href="custominterp.html"
  95. title="previous chapter">Custom Python Interpreters</a></p>
  96. </div>
  97. <div>
  98. <h4>Next topic</h4>
  99. <p class="topless"><a href="codeop.html"
  100. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">codeop</span></code> — Compile Python code</a></p>
  101. </div>
  102. <div role="note" aria-label="source link">
  103. <h3>This Page</h3>
  104. <ul class="this-page-menu">
  105. <li><a href="../bugs.html">Report a Bug</a></li>
  106. <li>
  107. <a href="https://github.com/python/cpython/blob/main/Doc/library/code.rst"
  108. rel="nofollow">Show Source
  109. </a>
  110. </li>
  111. </ul>
  112. </div>
  113. </nav>
  114. </div>
  115. </div>
  116. <div class="related" role="navigation" aria-label="related navigation">
  117. <h3>Navigation</h3>
  118. <ul>
  119. <li class="right" style="margin-right: 10px">
  120. <a href="../genindex.html" title="General Index"
  121. accesskey="I">index</a></li>
  122. <li class="right" >
  123. <a href="../py-modindex.html" title="Python Module Index"
  124. >modules</a> |</li>
  125. <li class="right" >
  126. <a href="codeop.html" title="codeop — Compile Python code"
  127. accesskey="N">next</a> |</li>
  128. <li class="right" >
  129. <a href="custominterp.html" title="Custom Python Interpreters"
  130. accesskey="P">previous</a> |</li>
  131. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  132. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  133. <li class="switchers">
  134. <div class="language_switcher_placeholder"></div>
  135. <div class="version_switcher_placeholder"></div>
  136. </li>
  137. <li>
  138. </li>
  139. <li id="cpython-language-and-version">
  140. <a href="../index.html">3.12.3 Documentation</a> &#187;
  141. </li>
  142. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  143. <li class="nav-item nav-item-2"><a href="custominterp.html" accesskey="U">Custom Python Interpreters</a> &#187;</li>
  144. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">code</span></code> — Interpreter base classes</a></li>
  145. <li class="right">
  146. <div class="inline-search" role="search">
  147. <form class="inline-search" action="../search.html" method="get">
  148. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  149. <input type="submit" value="Go" />
  150. </form>
  151. </div>
  152. |
  153. </li>
  154. <li class="right">
  155. <label class="theme-selector-label">
  156. Theme
  157. <select class="theme-selector" oninput="activateTheme(this.value)">
  158. <option value="auto" selected>Auto</option>
  159. <option value="light">Light</option>
  160. <option value="dark">Dark</option>
  161. </select>
  162. </label> |</li>
  163. </ul>
  164. </div>
  165. <div class="document">
  166. <div class="documentwrapper">
  167. <div class="bodywrapper">
  168. <div class="body" role="main">
  169. <section id="module-code">
  170. <span id="code-interpreter-base-classes"></span><h1><a class="reference internal" href="#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> — Interpreter base classes<a class="headerlink" href="#module-code" title="Link to this heading">¶</a></h1>
  171. <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>
  172. <hr class="docutils" />
  173. <p>The <code class="docutils literal notranslate"><span class="pre">code</span></code> module provides facilities to implement read-eval-print loops in
  174. Python. Two classes and convenience functions are included which can be used to
  175. build applications which provide an interactive interpreter prompt.</p>
  176. <dl class="py class">
  177. <dt class="sig sig-object py" id="code.InteractiveInterpreter">
  178. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">code.</span></span><span class="sig-name descname"><span class="pre">InteractiveInterpreter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">locals</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#code.InteractiveInterpreter" title="Link to this definition">¶</a></dt>
  179. <dd><p>This class deals with parsing and interpreter state (the user’s namespace); it
  180. does not deal with input buffering or prompting or input file naming (the
  181. filename is always passed in explicitly). The optional <em>locals</em> argument
  182. specifies the dictionary in which code will be executed; it defaults to a newly
  183. created dictionary with key <code class="docutils literal notranslate"><span class="pre">'__name__'</span></code> set to <code class="docutils literal notranslate"><span class="pre">'__console__'</span></code> and key
  184. <code class="docutils literal notranslate"><span class="pre">'__doc__'</span></code> set to <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  185. </dd></dl>
  186. <dl class="py class">
  187. <dt class="sig sig-object py" id="code.InteractiveConsole">
  188. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">code.</span></span><span class="sig-name descname"><span class="pre">InteractiveConsole</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">locals</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</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;console&gt;'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#code.InteractiveConsole" title="Link to this definition">¶</a></dt>
  189. <dd><p>Closely emulate the behavior of the interactive Python interpreter. This class
  190. builds on <a class="reference internal" href="#code.InteractiveInterpreter" title="code.InteractiveInterpreter"><code class="xref py py-class docutils literal notranslate"><span class="pre">InteractiveInterpreter</span></code></a> and adds prompting using the familiar
  191. <code class="docutils literal notranslate"><span class="pre">sys.ps1</span></code> and <code class="docutils literal notranslate"><span class="pre">sys.ps2</span></code>, and input buffering.</p>
  192. </dd></dl>
  193. <dl class="py function">
  194. <dt class="sig sig-object py" id="code.interact">
  195. <span class="sig-prename descclassname"><span class="pre">code.</span></span><span class="sig-name descname"><span class="pre">interact</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">banner</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">readfunc</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">local</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">exitmsg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#code.interact" title="Link to this definition">¶</a></dt>
  196. <dd><p>Convenience function to run a read-eval-print loop. This creates a new
  197. instance of <a class="reference internal" href="#code.InteractiveConsole" title="code.InteractiveConsole"><code class="xref py py-class docutils literal notranslate"><span class="pre">InteractiveConsole</span></code></a> and sets <em>readfunc</em> to be used as
  198. the <a class="reference internal" href="#code.InteractiveConsole.raw_input" title="code.InteractiveConsole.raw_input"><code class="xref py py-meth docutils literal notranslate"><span class="pre">InteractiveConsole.raw_input()</span></code></a> method, if provided. If <em>local</em> is
  199. provided, it is passed to the <a class="reference internal" href="#code.InteractiveConsole" title="code.InteractiveConsole"><code class="xref py py-class docutils literal notranslate"><span class="pre">InteractiveConsole</span></code></a> constructor for
  200. use as the default namespace for the interpreter loop. The <a class="reference internal" href="#code.InteractiveConsole.interact" title="code.InteractiveConsole.interact"><code class="xref py py-meth docutils literal notranslate"><span class="pre">interact()</span></code></a>
  201. method of the instance is then run with <em>banner</em> and <em>exitmsg</em> passed as the
  202. banner and exit message to use, if provided. The console object is discarded
  203. after use.</p>
  204. <div class="versionchanged">
  205. <p><span class="versionmodified changed">Changed in version 3.6: </span>Added <em>exitmsg</em> parameter.</p>
  206. </div>
  207. </dd></dl>
  208. <dl class="py function">
  209. <dt class="sig sig-object py" id="code.compile_command">
  210. <span class="sig-prename descclassname"><span class="pre">code.</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="#code.compile_command" title="Link to this definition">¶</a></dt>
  211. <dd><p>This function is useful for programs that want to emulate Python’s interpreter
  212. main loop (a.k.a. the read-eval-print loop). The tricky part is to determine
  213. when the user has entered an incomplete command that can be completed by
  214. entering more text (as opposed to a complete command or a syntax error). This
  215. function <em>almost</em> always makes the same decision as the real interpreter main
  216. loop.</p>
  217. <p><em>source</em> is the source string; <em>filename</em> is the optional filename from which
  218. source was read, defaulting to <code class="docutils literal notranslate"><span class="pre">'&lt;input&gt;'</span></code>; and <em>symbol</em> is the optional
  219. grammar start symbol, which should be <code class="docutils literal notranslate"><span class="pre">'single'</span></code> (the default), <code class="docutils literal notranslate"><span class="pre">'eval'</span></code>
  220. or <code class="docutils literal notranslate"><span class="pre">'exec'</span></code>.</p>
  221. <p>Returns a code object (the same as <code class="docutils literal notranslate"><span class="pre">compile(source,</span> <span class="pre">filename,</span> <span class="pre">symbol)</span></code>) if the
  222. command is complete and valid; <code class="docutils literal notranslate"><span class="pre">None</span></code> if the command is incomplete; raises
  223. <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> if the command is complete and contains a syntax error, or
  224. raises <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 the command contains an
  225. invalid literal.</p>
  226. </dd></dl>
  227. <section id="interactive-interpreter-objects">
  228. <span id="interpreter-objects"></span><h2>Interactive Interpreter Objects<a class="headerlink" href="#interactive-interpreter-objects" title="Link to this heading">¶</a></h2>
  229. <dl class="py method">
  230. <dt class="sig sig-object py" id="code.InteractiveInterpreter.runsource">
  231. <span class="sig-prename descclassname"><span class="pre">InteractiveInterpreter.</span></span><span class="sig-name descname"><span class="pre">runsource</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="#code.InteractiveInterpreter.runsource" title="Link to this definition">¶</a></dt>
  232. <dd><p>Compile and run some source in the interpreter. Arguments are the same as for
  233. <a class="reference internal" href="#code.compile_command" title="code.compile_command"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile_command()</span></code></a>; the default for <em>filename</em> is <code class="docutils literal notranslate"><span class="pre">'&lt;input&gt;'</span></code>, and for
  234. <em>symbol</em> is <code class="docutils literal notranslate"><span class="pre">'single'</span></code>. One of several things can happen:</p>
  235. <ul class="simple">
  236. <li><p>The input is incorrect; <a class="reference internal" href="#code.compile_command" title="code.compile_command"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile_command()</span></code></a> raised an exception
  237. (<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> or <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>). A syntax traceback will be
  238. printed by calling the <a class="reference internal" href="#code.InteractiveInterpreter.showsyntaxerror" title="code.InteractiveInterpreter.showsyntaxerror"><code class="xref py py-meth docutils literal notranslate"><span class="pre">showsyntaxerror()</span></code></a> method. <a class="reference internal" href="#code.InteractiveInterpreter.runsource" title="code.InteractiveInterpreter.runsource"><code class="xref py py-meth docutils literal notranslate"><span class="pre">runsource()</span></code></a>
  239. returns <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p></li>
  240. <li><p>The input is incomplete, and more input is required; <a class="reference internal" href="#code.compile_command" title="code.compile_command"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile_command()</span></code></a>
  241. returned <code class="docutils literal notranslate"><span class="pre">None</span></code>. <a class="reference internal" href="#code.InteractiveInterpreter.runsource" title="code.InteractiveInterpreter.runsource"><code class="xref py py-meth docutils literal notranslate"><span class="pre">runsource()</span></code></a> returns <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p></li>
  242. <li><p>The input is complete; <a class="reference internal" href="#code.compile_command" title="code.compile_command"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile_command()</span></code></a> returned a code object. The
  243. code is executed by calling the <a class="reference internal" href="#code.InteractiveInterpreter.runcode" title="code.InteractiveInterpreter.runcode"><code class="xref py py-meth docutils literal notranslate"><span class="pre">runcode()</span></code></a> (which also handles run-time
  244. exceptions, except for <a class="reference internal" href="exceptions.html#SystemExit" title="SystemExit"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SystemExit</span></code></a>). <a class="reference internal" href="#code.InteractiveInterpreter.runsource" title="code.InteractiveInterpreter.runsource"><code class="xref py py-meth docutils literal notranslate"><span class="pre">runsource()</span></code></a> returns <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p></li>
  245. </ul>
  246. <p>The return value can be used to decide whether to use <code class="docutils literal notranslate"><span class="pre">sys.ps1</span></code> or <code class="docutils literal notranslate"><span class="pre">sys.ps2</span></code>
  247. to prompt the next line.</p>
  248. </dd></dl>
  249. <dl class="py method">
  250. <dt class="sig sig-object py" id="code.InteractiveInterpreter.runcode">
  251. <span class="sig-prename descclassname"><span class="pre">InteractiveInterpreter.</span></span><span class="sig-name descname"><span class="pre">runcode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">code</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#code.InteractiveInterpreter.runcode" title="Link to this definition">¶</a></dt>
  252. <dd><p>Execute a code object. When an exception occurs, <a class="reference internal" href="#code.InteractiveInterpreter.showtraceback" title="code.InteractiveInterpreter.showtraceback"><code class="xref py py-meth docutils literal notranslate"><span class="pre">showtraceback()</span></code></a> is called
  253. to display a traceback. All exceptions are caught except <a class="reference internal" href="exceptions.html#SystemExit" title="SystemExit"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SystemExit</span></code></a>,
  254. which is allowed to propagate.</p>
  255. <p>A note about <a class="reference internal" href="exceptions.html#KeyboardInterrupt" title="KeyboardInterrupt"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyboardInterrupt</span></code></a>: this exception may occur elsewhere in
  256. this code, and may not always be caught. The caller should be prepared to deal
  257. with it.</p>
  258. </dd></dl>
  259. <dl class="py method">
  260. <dt class="sig sig-object py" id="code.InteractiveInterpreter.showsyntaxerror">
  261. <span class="sig-prename descclassname"><span class="pre">InteractiveInterpreter.</span></span><span class="sig-name descname"><span class="pre">showsyntaxerror</span></span><span class="sig-paren">(</span><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">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#code.InteractiveInterpreter.showsyntaxerror" title="Link to this definition">¶</a></dt>
  262. <dd><p>Display the syntax error that just occurred. This does not display a stack
  263. trace because there isn’t one for syntax errors. If <em>filename</em> is given, it is
  264. stuffed into the exception instead of the default filename provided by Python’s
  265. parser, because it always uses <code class="docutils literal notranslate"><span class="pre">'&lt;string&gt;'</span></code> when reading from a string. The
  266. output is written by the <a class="reference internal" href="#code.InteractiveInterpreter.write" title="code.InteractiveInterpreter.write"><code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code></a> method.</p>
  267. </dd></dl>
  268. <dl class="py method">
  269. <dt class="sig sig-object py" id="code.InteractiveInterpreter.showtraceback">
  270. <span class="sig-prename descclassname"><span class="pre">InteractiveInterpreter.</span></span><span class="sig-name descname"><span class="pre">showtraceback</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#code.InteractiveInterpreter.showtraceback" title="Link to this definition">¶</a></dt>
  271. <dd><p>Display the exception that just occurred. We remove the first stack item
  272. because it is within the interpreter object implementation. The output is
  273. written by the <a class="reference internal" href="#code.InteractiveInterpreter.write" title="code.InteractiveInterpreter.write"><code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code></a> method.</p>
  274. <div class="versionchanged">
  275. <p><span class="versionmodified changed">Changed in version 3.5: </span>The full chained traceback is displayed instead
  276. of just the primary traceback.</p>
  277. </div>
  278. </dd></dl>
  279. <dl class="py method">
  280. <dt class="sig sig-object py" id="code.InteractiveInterpreter.write">
  281. <span class="sig-prename descclassname"><span class="pre">InteractiveInterpreter.</span></span><span class="sig-name descname"><span class="pre">write</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#code.InteractiveInterpreter.write" title="Link to this definition">¶</a></dt>
  282. <dd><p>Write a string to the standard error stream (<code class="docutils literal notranslate"><span class="pre">sys.stderr</span></code>). Derived classes
  283. should override this to provide the appropriate output handling as needed.</p>
  284. </dd></dl>
  285. </section>
  286. <section id="interactive-console-objects">
  287. <span id="console-objects"></span><h2>Interactive Console Objects<a class="headerlink" href="#interactive-console-objects" title="Link to this heading">¶</a></h2>
  288. <p>The <a class="reference internal" href="#code.InteractiveConsole" title="code.InteractiveConsole"><code class="xref py py-class docutils literal notranslate"><span class="pre">InteractiveConsole</span></code></a> class is a subclass of
  289. <a class="reference internal" href="#code.InteractiveInterpreter" title="code.InteractiveInterpreter"><code class="xref py py-class docutils literal notranslate"><span class="pre">InteractiveInterpreter</span></code></a>, and so offers all the methods of the
  290. interpreter objects as well as the following additions.</p>
  291. <dl class="py method">
  292. <dt class="sig sig-object py" id="code.InteractiveConsole.interact">
  293. <span class="sig-prename descclassname"><span class="pre">InteractiveConsole.</span></span><span class="sig-name descname"><span class="pre">interact</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">banner</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">exitmsg</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#code.InteractiveConsole.interact" title="Link to this definition">¶</a></dt>
  294. <dd><p>Closely emulate the interactive Python console. The optional <em>banner</em> argument
  295. specify the banner to print before the first interaction; by default it prints a
  296. banner similar to the one printed by the standard Python interpreter, followed
  297. by the class name of the console object in parentheses (so as not to confuse
  298. this with the real interpreter – since it’s so close!).</p>
  299. <p>The optional <em>exitmsg</em> argument specifies an exit message printed when exiting.
  300. Pass the empty string to suppress the exit message. If <em>exitmsg</em> is not given or
  301. <code class="docutils literal notranslate"><span class="pre">None</span></code>, a default message is printed.</p>
  302. <div class="versionchanged">
  303. <p><span class="versionmodified changed">Changed in version 3.4: </span>To suppress printing any banner, pass an empty string.</p>
  304. </div>
  305. <div class="versionchanged">
  306. <p><span class="versionmodified changed">Changed in version 3.6: </span>Print an exit message when exiting.</p>
  307. </div>
  308. </dd></dl>
  309. <dl class="py method">
  310. <dt class="sig sig-object py" id="code.InteractiveConsole.push">
  311. <span class="sig-prename descclassname"><span class="pre">InteractiveConsole.</span></span><span class="sig-name descname"><span class="pre">push</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">line</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#code.InteractiveConsole.push" title="Link to this definition">¶</a></dt>
  312. <dd><p>Push a line of source text to the interpreter. The line should not have a
  313. trailing newline; it may have internal newlines. The line is appended to a
  314. buffer and the interpreter’s <a class="reference internal" href="#code.InteractiveInterpreter.runsource" title="code.InteractiveInterpreter.runsource"><code class="xref py py-meth docutils literal notranslate"><span class="pre">runsource()</span></code></a> method is called with the
  315. concatenated contents of the buffer as source. If this indicates that the
  316. command was executed or invalid, the buffer is reset; otherwise, the command is
  317. incomplete, and the buffer is left as it was after the line was appended. The
  318. return value is <code class="docutils literal notranslate"><span class="pre">True</span></code> if more input is required, <code class="docutils literal notranslate"><span class="pre">False</span></code> if the line was
  319. dealt with in some way (this is the same as <code class="xref py py-meth docutils literal notranslate"><span class="pre">runsource()</span></code>).</p>
  320. </dd></dl>
  321. <dl class="py method">
  322. <dt class="sig sig-object py" id="code.InteractiveConsole.resetbuffer">
  323. <span class="sig-prename descclassname"><span class="pre">InteractiveConsole.</span></span><span class="sig-name descname"><span class="pre">resetbuffer</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#code.InteractiveConsole.resetbuffer" title="Link to this definition">¶</a></dt>
  324. <dd><p>Remove any unhandled source text from the input buffer.</p>
  325. </dd></dl>
  326. <dl class="py method">
  327. <dt class="sig sig-object py" id="code.InteractiveConsole.raw_input">
  328. <span class="sig-prename descclassname"><span class="pre">InteractiveConsole.</span></span><span class="sig-name descname"><span class="pre">raw_input</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">prompt</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">''</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#code.InteractiveConsole.raw_input" title="Link to this definition">¶</a></dt>
  329. <dd><p>Write a prompt and read a line. The returned line does not include the trailing
  330. newline. When the user enters the EOF key sequence, <a class="reference internal" href="exceptions.html#EOFError" title="EOFError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">EOFError</span></code></a> is raised.
  331. The base implementation reads from <code class="docutils literal notranslate"><span class="pre">sys.stdin</span></code>; a subclass may replace this
  332. with a different implementation.</p>
  333. </dd></dl>
  334. </section>
  335. </section>
  336. <div class="clearer"></div>
  337. </div>
  338. </div>
  339. </div>
  340. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  341. <div class="sphinxsidebarwrapper">
  342. <div>
  343. <h3><a href="../contents.html">Table of Contents</a></h3>
  344. <ul>
  345. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">code</span></code> — Interpreter base classes</a><ul>
  346. <li><a class="reference internal" href="#interactive-interpreter-objects">Interactive Interpreter Objects</a></li>
  347. <li><a class="reference internal" href="#interactive-console-objects">Interactive Console Objects</a></li>
  348. </ul>
  349. </li>
  350. </ul>
  351. </div>
  352. <div>
  353. <h4>Previous topic</h4>
  354. <p class="topless"><a href="custominterp.html"
  355. title="previous chapter">Custom Python Interpreters</a></p>
  356. </div>
  357. <div>
  358. <h4>Next topic</h4>
  359. <p class="topless"><a href="codeop.html"
  360. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">codeop</span></code> — Compile Python code</a></p>
  361. </div>
  362. <div role="note" aria-label="source link">
  363. <h3>This Page</h3>
  364. <ul class="this-page-menu">
  365. <li><a href="../bugs.html">Report a Bug</a></li>
  366. <li>
  367. <a href="https://github.com/python/cpython/blob/main/Doc/library/code.rst"
  368. rel="nofollow">Show Source
  369. </a>
  370. </li>
  371. </ul>
  372. </div>
  373. </div>
  374. <div id="sidebarbutton" title="Collapse sidebar">
  375. <span>«</span>
  376. </div>
  377. </div>
  378. <div class="clearer"></div>
  379. </div>
  380. <div class="related" role="navigation" aria-label="related navigation">
  381. <h3>Navigation</h3>
  382. <ul>
  383. <li class="right" style="margin-right: 10px">
  384. <a href="../genindex.html" title="General Index"
  385. >index</a></li>
  386. <li class="right" >
  387. <a href="../py-modindex.html" title="Python Module Index"
  388. >modules</a> |</li>
  389. <li class="right" >
  390. <a href="codeop.html" title="codeop — Compile Python code"
  391. >next</a> |</li>
  392. <li class="right" >
  393. <a href="custominterp.html" title="Custom Python Interpreters"
  394. >previous</a> |</li>
  395. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  396. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  397. <li class="switchers">
  398. <div class="language_switcher_placeholder"></div>
  399. <div class="version_switcher_placeholder"></div>
  400. </li>
  401. <li>
  402. </li>
  403. <li id="cpython-language-and-version">
  404. <a href="../index.html">3.12.3 Documentation</a> &#187;
  405. </li>
  406. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  407. <li class="nav-item nav-item-2"><a href="custominterp.html" >Custom Python Interpreters</a> &#187;</li>
  408. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">code</span></code> — Interpreter base classes</a></li>
  409. <li class="right">
  410. <div class="inline-search" role="search">
  411. <form class="inline-search" action="../search.html" method="get">
  412. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  413. <input type="submit" value="Go" />
  414. </form>
  415. </div>
  416. |
  417. </li>
  418. <li class="right">
  419. <label class="theme-selector-label">
  420. Theme
  421. <select class="theme-selector" oninput="activateTheme(this.value)">
  422. <option value="auto" selected>Auto</option>
  423. <option value="light">Light</option>
  424. <option value="dark">Dark</option>
  425. </select>
  426. </label> |</li>
  427. </ul>
  428. </div>
  429. <div class="footer">
  430. &copy;
  431. <a href="../copyright.html">
  432. Copyright
  433. </a>
  434. 2001-2024, Python Software Foundation.
  435. <br />
  436. This page is licensed under the Python Software Foundation License Version 2.
  437. <br />
  438. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  439. <br />
  440. See <a href="/license.html">History and License</a> for more information.<br />
  441. <br />
  442. The Python Software Foundation is a non-profit corporation.
  443. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  444. <br />
  445. <br />
  446. Last updated on Apr 09, 2024 (13:47 UTC).
  447. <a href="/bugs.html">Found a bug</a>?
  448. <br />
  449. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  450. </div>
  451. </body>
  452. </html>
上海开阖软件有限公司 沪ICP备12045867号-1