382 lines
31 KiB
HTML
382 lines
31 KiB
HTML
|
||
<!DOCTYPE html>
|
||
|
||
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
|
||
<head>
|
||
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
|
||
|
||
<title>code --- 解释器基类 — Python 3.8.20 文档</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
|
||
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
|
||
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
|
||
|
||
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
|
||
<script src="../_static/jquery.js"></script>
|
||
<script src="../_static/underscore.js"></script>
|
||
<script src="../_static/doctools.js"></script>
|
||
<script src="../_static/language_data.js"></script>
|
||
<script src="../_static/translations.js"></script>
|
||
|
||
<script src="../_static/sidebar.js"></script>
|
||
|
||
<link rel="search" type="application/opensearchdescription+xml"
|
||
title="在 Python 3.8.20 文档 中搜索"
|
||
href="../_static/opensearch.xml"/>
|
||
<link rel="author" title="关于这些文档" href="../about.html" />
|
||
<link rel="index" title="索引" href="../genindex.html" />
|
||
<link rel="search" title="搜索" href="../search.html" />
|
||
<link rel="copyright" title="版权所有" href="../copyright.html" />
|
||
<link rel="next" title="codeop --- 编译Python代码" href="codeop.html" />
|
||
<link rel="prev" title="自定义 Python 解释器" href="custominterp.html" />
|
||
<link rel="canonical" href="https://docs.python.org/3/library/code.html" />
|
||
|
||
|
||
|
||
|
||
|
||
<style>
|
||
@media only screen {
|
||
table.full-width-table {
|
||
width: 100%;
|
||
}
|
||
}
|
||
</style>
|
||
<link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
|
||
<script type="text/javascript" src="../_static/copybutton.js"></script>
|
||
<script type="text/javascript" src="../_static/menu.js"></script>
|
||
|
||
</head>
|
||
<body>
|
||
<div class="mobile-nav">
|
||
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
|
||
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
|
||
<label for="menuToggler" class="toggler__label">
|
||
<span></span>
|
||
</label>
|
||
<nav class="nav-content" role="navigation">
|
||
<a href="https://www.python.org/" class="nav-logo">
|
||
<img src="../_static/py.svg" alt="Logo"/>
|
||
</a>
|
||
<div class="version_switcher_placeholder"></div>
|
||
<form role="search" class="search" action="../search.html" method="get">
|
||
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
|
||
<path fill-rule="nonzero"
|
||
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" fill="#444"></path>
|
||
</svg>
|
||
<input type="text" name="q" aria-label="快速搜索"/>
|
||
<input type="submit" value="转向"/>
|
||
</form>
|
||
</nav>
|
||
<div class="menu-wrapper">
|
||
<nav class="menu" role="navigation" aria-label="main navigation">
|
||
<div class="language_switcher_placeholder"></div>
|
||
<h3><a href="../contents.html">目录</a></h3>
|
||
<ul>
|
||
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">code</span></code> --- 解释器基类</a><ul>
|
||
<li><a class="reference internal" href="#interactive-interpreter-objects">交互解释器对象</a></li>
|
||
<li><a class="reference internal" href="#interactive-console-objects">交互式控制台对象</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
<h4>上一个主题</h4>
|
||
<p class="topless"><a href="custominterp.html"
|
||
title="上一章">自定义 Python 解释器</a></p>
|
||
<h4>下一个主题</h4>
|
||
<p class="topless"><a href="codeop.html"
|
||
title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">codeop</span></code> --- 编译Python代码</a></p>
|
||
<div role="note" aria-label="source link">
|
||
<h3>本页</h3>
|
||
<ul class="this-page-menu">
|
||
<li><a href="../bugs.html">报告 Bug</a></li>
|
||
<li>
|
||
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/code.rst"
|
||
rel="nofollow">显示源代码
|
||
</a>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</nav>
|
||
</div>
|
||
</div>
|
||
|
||
<div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;">
|
||
这个文档所针对的是一个已不再受支持的 Python 旧版本。
|
||
你应当升级版本,并阅读
|
||
<a href="/3/library/code.html"> Python 当前稳定版本的文档</a>.
|
||
</div>
|
||
|
||
<div class="related" role="navigation" aria-label="related navigation">
|
||
<h3>导航</h3>
|
||
<ul>
|
||
<li class="right" style="margin-right: 10px">
|
||
<a href="../genindex.html" title="总目录"
|
||
accesskey="I">索引</a></li>
|
||
<li class="right" >
|
||
<a href="../py-modindex.html" title="Python 模块索引"
|
||
>模块</a> |</li>
|
||
<li class="right" >
|
||
<a href="codeop.html" title="codeop --- 编译Python代码"
|
||
accesskey="N">下一页</a> |</li>
|
||
<li class="right" >
|
||
<a href="custominterp.html" title="自定义 Python 解释器"
|
||
accesskey="P">上一页</a> |</li>
|
||
|
||
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
|
||
<li><a href="https://www.python.org/">Python</a> »</li>
|
||
<li class="switchers">
|
||
<div class="language_switcher_placeholder"></div>
|
||
<div class="version_switcher_placeholder"></div>
|
||
</li>
|
||
<li>
|
||
|
||
</li>
|
||
<li id="cpython-language-and-version">
|
||
<a href="../index.html">3.8.20 Documentation</a> »
|
||
</li>
|
||
|
||
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> »</li>
|
||
<li class="nav-item nav-item-2"><a href="custominterp.html" accesskey="U">自定义 Python 解释器</a> »</li>
|
||
<li class="right">
|
||
|
||
|
||
<div class="inline-search" role="search">
|
||
<form class="inline-search" action="../search.html" method="get">
|
||
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
|
||
<input type="submit" value="转向" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
|
|
||
</li>
|
||
|
||
</ul>
|
||
</div>
|
||
|
||
<div class="document">
|
||
<div class="documentwrapper">
|
||
<div class="bodywrapper">
|
||
<div class="body" role="main">
|
||
|
||
<section id="module-code">
|
||
<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> --- 解释器基类<a class="headerlink" href="#module-code" title="永久链接至标题">¶</a></h1>
|
||
<p><strong>源代码:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.8/Lib/code.py">Lib/code.py</a></p>
|
||
<hr class="docutils" />
|
||
<p><code class="docutils literal notranslate"><span class="pre">code</span></code> 模块提供了在 Python 中实现 read-eval-print 循环的功能。它包含两个类和一些快捷功能,可用于构建提供交互式解释器的应用程序。</p>
|
||
<dl class="class">
|
||
<dt id="code.InteractiveInterpreter">
|
||
<em class="property">class </em><code class="sig-prename descclassname">code.</code><code class="sig-name descname">InteractiveInterpreter</code><span class="sig-paren">(</span><em class="sig-param">locals=None</em><span class="sig-paren">)</span><a class="headerlink" href="#code.InteractiveInterpreter" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>这个类处理解析器和解释器状态(用户命名空间的);它不处理缓冲器、终端提示区或着输入文件名(文件名总是显示地传递)。可选的 <em>locals</em> 参数指定一个字典,字典里面包含将在此类执行的代码;它默认创建新的字典,其键 <code class="docutils literal notranslate"><span class="pre">'__name__'</span></code> 设置为 <code class="docutils literal notranslate"><span class="pre">'__console__'</span></code> ,键 <code class="docutils literal notranslate"><span class="pre">'__doc__'</span></code> 设置为 <code class="docutils literal notranslate"><span class="pre">None</span></code> 。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="class">
|
||
<dt id="code.InteractiveConsole">
|
||
<em class="property">class </em><code class="sig-prename descclassname">code.</code><code class="sig-name descname">InteractiveConsole</code><span class="sig-paren">(</span><em class="sig-param">locals=None</em>, <em class="sig-param">filename="<console>"</em><span class="sig-paren">)</span><a class="headerlink" href="#code.InteractiveConsole" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>尽可能模拟交互式 Python 解释器的行为。此类建立在 <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> 的基础上,使用熟悉的 <code class="docutils literal notranslate"><span class="pre">sys.ps1</span></code> 和 <code class="docutils literal notranslate"><span class="pre">sys.ps2</span></code> 作为输入提示符,并有输入缓冲。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="code.interact">
|
||
<code class="sig-prename descclassname">code.</code><code class="sig-name descname">interact</code><span class="sig-paren">(</span><em class="sig-param">banner=None</em>, <em class="sig-param">readfunc=None</em>, <em class="sig-param">local=None</em>, <em class="sig-param">exitmsg=None</em><span class="sig-paren">)</span><a class="headerlink" href="#code.interact" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>运行一个 read-eval-print 循环的便捷函数。这会创建一个新的 <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> 实例。如果提供了 <em>readfunc</em> ,会设置为 <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> 方法。如果提供了 <em>local</em> ,则将其传递给 <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> 的构造函数,以用作解释器循环的默认命名空间。然后,如果提供了 <em>banner</em> 和 <em>exitmsg</em> ,实例的 <a class="reference internal" href="#code.interact" title="code.interact"><code class="xref py py-meth docutils literal notranslate"><span class="pre">interact()</span></code></a> 方法会以此为标题和退出消息。控制台对象在使用后将被丢弃。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>加入 <em>exitmsg</em> 参数。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="code.compile_command">
|
||
<code class="sig-prename descclassname">code.</code><code class="sig-name descname">compile_command</code><span class="sig-paren">(</span><em class="sig-param">source</em>, <em class="sig-param">filename="<input>"</em>, <em class="sig-param">symbol="single"</em><span class="sig-paren">)</span><a class="headerlink" href="#code.compile_command" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>这个函数主要用来模拟 Python 解释器的主循环(即 read-eval-print 循环)。难点的部分是当用户输入不完整命令时,判断能否通过之后的输入来完成(要么成为完整的命令,要么语法错误)。该函数 <em>几乎</em> 和实际的解释器主循环的判断是相同的。</p>
|
||
<p><em>source</em> 是源字符串;<em>filename</em> 是可选的用作读取源的文件名,默认为 <code class="docutils literal notranslate"><span class="pre">'<input>'</span></code>;<em>symbol</em> 是可选的语法开启符号,应为 <code class="docutils literal notranslate"><span class="pre">'single'</span></code> (默认), <code class="docutils literal notranslate"><span class="pre">'eval'</span></code> 或 <code class="docutils literal notranslate"><span class="pre">'exec'</span></code>。</p>
|
||
<p>如果命令完整且有效则返回一个代码对象 (等价于 <code class="docutils literal notranslate"><span class="pre">compile(source,</span> <span class="pre">filename,</span> <span class="pre">symbol)</span></code>);如果命令不完整则返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>;如果命令完整但包含语法错误则会引发 <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> 或 <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 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>。</p>
|
||
</dd></dl>
|
||
|
||
<section id="interactive-interpreter-objects">
|
||
<span id="interpreter-objects"></span><h2>交互解释器对象<a class="headerlink" href="#interactive-interpreter-objects" title="永久链接至标题">¶</a></h2>
|
||
<dl class="method">
|
||
<dt id="code.InteractiveInterpreter.runsource">
|
||
<code class="sig-prename descclassname">InteractiveInterpreter.</code><code class="sig-name descname">runsource</code><span class="sig-paren">(</span><em class="sig-param">source</em>, <em class="sig-param">filename="<input>"</em>, <em class="sig-param">symbol="single"</em><span class="sig-paren">)</span><a class="headerlink" href="#code.InteractiveInterpreter.runsource" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>在解释器中编译并运行一段源码。 所用参数与 <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> 一样;<em>filename</em> 的默认值为 <code class="docutils literal notranslate"><span class="pre">'<input>'</span></code>,<em>symbol</em> 则为 <code class="docutils literal notranslate"><span class="pre">'single'</span></code>。 可能发生以下情况之一:</p>
|
||
<ul class="simple">
|
||
<li><p>输入不正确;<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> 引发了一个异常 (<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> 或 <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 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> 方法打印语法回溯信息。 <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> 返回 <code class="docutils literal notranslate"><span class="pre">False</span></code>。</p></li>
|
||
<li><p>输入不完整,需要更多输入;函数 <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> 返回 <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> 返回 <code class="docutils literal notranslate"><span class="pre">True</span></code> 。</p></li>
|
||
<li><p>输入完整;<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> 返回了一个代码对象。 将通过调用 <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> 执行代码(该方法也会处理运行时异常,<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> 返回 <code class="docutils literal notranslate"><span class="pre">False</span></code>。</p></li>
|
||
</ul>
|
||
<p>该返回值用于决定使用 <code class="docutils literal notranslate"><span class="pre">sys.ps1</span></code> 还是 <code class="docutils literal notranslate"><span class="pre">sys.ps2</span></code> 来作为下一行的输入提示符。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="code.InteractiveInterpreter.runcode">
|
||
<code class="sig-prename descclassname">InteractiveInterpreter.</code><code class="sig-name descname">runcode</code><span class="sig-paren">(</span><em class="sig-param">code</em><span class="sig-paren">)</span><a class="headerlink" href="#code.InteractiveInterpreter.runcode" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>执行一个代码对象。当发生异常时,调用 <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> 来显示回溯。除 <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> (允许传播)以外的所有异常都会被捕获。</p>
|
||
<p>有关 <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> 的说明,该异常可能发生于此代码的其他位置,并且并不总能被捕获。 调用者应当准备好处理它。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="code.InteractiveInterpreter.showsyntaxerror">
|
||
<code class="sig-prename descclassname">InteractiveInterpreter.</code><code class="sig-name descname">showsyntaxerror</code><span class="sig-paren">(</span><em class="sig-param">filename=None</em><span class="sig-paren">)</span><a class="headerlink" href="#code.InteractiveInterpreter.showsyntaxerror" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>显示刚发生的语法错误。 这不会显示堆栈回溯因为语法错误并无此种信息。 如果给出了 <em>filename</em>,它会被放入异常来替代 Python 解析器所提供的默认文件名,因为它在从一个字符串读取时总是会使用 <code class="docutils literal notranslate"><span class="pre">'<string>'</span></code>。 输出将由 <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> 方法来写入。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="code.InteractiveInterpreter.showtraceback">
|
||
<code class="sig-prename descclassname">InteractiveInterpreter.</code><code class="sig-name descname">showtraceback</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#code.InteractiveInterpreter.showtraceback" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>显示刚发生的异常。 我们移除了第一个堆栈条目因为它从属于解释器对象的实现。 输出将由 <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> 方法来写入。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.5 版更改: </span>将显示完整的链式回溯,而不只是主回溯。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="code.InteractiveInterpreter.write">
|
||
<code class="sig-prename descclassname">InteractiveInterpreter.</code><code class="sig-name descname">write</code><span class="sig-paren">(</span><em class="sig-param">data</em><span class="sig-paren">)</span><a class="headerlink" href="#code.InteractiveInterpreter.write" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>将一个字符串写入到标准错误流 (<code class="docutils literal notranslate"><span class="pre">sys.stderr</span></code>)。 所有派生类都应重写此方法以提供必要的正确输出处理。</p>
|
||
</dd></dl>
|
||
|
||
</section>
|
||
<section id="interactive-console-objects">
|
||
<span id="console-objects"></span><h2>交互式控制台对象<a class="headerlink" href="#interactive-console-objects" title="永久链接至标题">¶</a></h2>
|
||
<p><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> 类是 <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> 的子类,因此它提供了解释器对象的所有方法,还有以下的额外方法。</p>
|
||
<dl class="method">
|
||
<dt id="code.InteractiveConsole.interact">
|
||
<code class="sig-prename descclassname">InteractiveConsole.</code><code class="sig-name descname">interact</code><span class="sig-paren">(</span><em class="sig-param">banner=None</em>, <em class="sig-param">exitmsg=None</em><span class="sig-paren">)</span><a class="headerlink" href="#code.InteractiveConsole.interact" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>近似地模拟交互式 Python 终端。 可选的 <em>banner</em> 参数指定要在第一次交互前打印的条幅;默认情况下会类似于标准 Python 解释器所打印的内容,并附上外加圆括号的终端对象类名(这样就不会与真正的解释器混淆 —— 因为确实太像了!)</p>
|
||
<p>可选的 <em>exitmsg</em> 参数指定要在退出时打印的退出消息。 传入空字符串可以屏蔽退出消息。 如果 <em>exitmsg</em> 未给出或为 <code class="docutils literal notranslate"><span class="pre">None</span></code>,则将打印默认消息。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.4 版更改: </span>要禁止打印任何条幅消息,请传递一个空字符串。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span>退出时打印退出消息。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="code.InteractiveConsole.push">
|
||
<code class="sig-prename descclassname">InteractiveConsole.</code><code class="sig-name descname">push</code><span class="sig-paren">(</span><em class="sig-param">line</em><span class="sig-paren">)</span><a class="headerlink" href="#code.InteractiveConsole.push" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>将一行源文本推入解释器。 行内容不应带有末尾换行符;它可以有内部换行符。 行内容会被添加到一个缓冲区并且会调用解释器的 <code class="xref py py-meth docutils literal notranslate"><span class="pre">runsource()</span></code> 方法,附带缓冲区内容的拼接结果作为源文本。 如果显示命令已执行或不合法,缓冲区将被重置;否则,则命令尚未结束,缓冲区将在添加行后保持原样。 如果要求更多输入则返回值为 <code class="docutils literal notranslate"><span class="pre">True</span></code>,如果行已按某种方式被处理则返回值为 <code class="docutils literal notranslate"><span class="pre">False</span></code> (这与 <code class="xref py py-meth docutils literal notranslate"><span class="pre">runsource()</span></code> 相同)。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="code.InteractiveConsole.resetbuffer">
|
||
<code class="sig-prename descclassname">InteractiveConsole.</code><code class="sig-name descname">resetbuffer</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#code.InteractiveConsole.resetbuffer" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>从输入缓冲区中删除所有未处理的内容。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="code.InteractiveConsole.raw_input">
|
||
<code class="sig-prename descclassname">InteractiveConsole.</code><code class="sig-name descname">raw_input</code><span class="sig-paren">(</span><em class="sig-param">prompt=""</em><span class="sig-paren">)</span><a class="headerlink" href="#code.InteractiveConsole.raw_input" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>输出提示并读取一行。返回的行不包含末尾的换行符。当用户输入 EOF 键序列时,会引发 <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> 异常。默认实现是从 <code class="docutils literal notranslate"><span class="pre">sys.stdin</span></code> 读取;子类可以用其他实现代替。</p>
|
||
</dd></dl>
|
||
|
||
</section>
|
||
</section>
|
||
|
||
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
|
||
<div class="sphinxsidebarwrapper">
|
||
<h3><a href="../contents.html">目录</a></h3>
|
||
<ul>
|
||
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">code</span></code> --- 解释器基类</a><ul>
|
||
<li><a class="reference internal" href="#interactive-interpreter-objects">交互解释器对象</a></li>
|
||
<li><a class="reference internal" href="#interactive-console-objects">交互式控制台对象</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
<h4>上一个主题</h4>
|
||
<p class="topless"><a href="custominterp.html"
|
||
title="上一章">自定义 Python 解释器</a></p>
|
||
<h4>下一个主题</h4>
|
||
<p class="topless"><a href="codeop.html"
|
||
title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">codeop</span></code> --- 编译Python代码</a></p>
|
||
<div role="note" aria-label="source link">
|
||
<h3>本页</h3>
|
||
<ul class="this-page-menu">
|
||
<li><a href="../bugs.html">报告 Bug</a></li>
|
||
<li>
|
||
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/code.rst"
|
||
rel="nofollow">显示源代码
|
||
</a>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="clearer"></div>
|
||
</div>
|
||
<div class="related" role="navigation" aria-label="related navigation">
|
||
<h3>导航</h3>
|
||
<ul>
|
||
<li class="right" style="margin-right: 10px">
|
||
<a href="../genindex.html" title="总目录"
|
||
>索引</a></li>
|
||
<li class="right" >
|
||
<a href="../py-modindex.html" title="Python 模块索引"
|
||
>模块</a> |</li>
|
||
<li class="right" >
|
||
<a href="codeop.html" title="codeop --- 编译Python代码"
|
||
>下一页</a> |</li>
|
||
<li class="right" >
|
||
<a href="custominterp.html" title="自定义 Python 解释器"
|
||
>上一页</a> |</li>
|
||
|
||
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
|
||
<li><a href="https://www.python.org/">Python</a> »</li>
|
||
<li class="switchers">
|
||
<div class="language_switcher_placeholder"></div>
|
||
<div class="version_switcher_placeholder"></div>
|
||
</li>
|
||
<li>
|
||
|
||
</li>
|
||
<li id="cpython-language-and-version">
|
||
<a href="../index.html">3.8.20 Documentation</a> »
|
||
</li>
|
||
|
||
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> »</li>
|
||
<li class="nav-item nav-item-2"><a href="custominterp.html" >自定义 Python 解释器</a> »</li>
|
||
<li class="right">
|
||
|
||
|
||
<div class="inline-search" role="search">
|
||
<form class="inline-search" action="../search.html" method="get">
|
||
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
|
||
<input type="submit" value="转向" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
|
|
||
</li>
|
||
|
||
</ul>
|
||
</div>
|
||
<div class="footer">
|
||
© <a href="../copyright.html">版权所有</a> 2001-2024, Python Software Foundation.
|
||
<br />
|
||
This page is licensed under the Python Software Foundation License Version 2.
|
||
<br />
|
||
Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
|
||
<br />
|
||
|
||
<br />
|
||
|
||
The Python Software Foundation is a non-profit corporation.
|
||
<a href="https://www.python.org/psf/donations/">Please donate.</a>
|
||
<br />
|
||
<br />
|
||
|
||
最后更新于 12月 09, 2024.
|
||
<a href="https://docs.python.org/3/bugs.html">Found a bug</a>?
|
||
<br />
|
||
|
||
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 2.4.4.
|
||
</div>
|
||
|
||
</body>
|
||
</html> |