687 lines
58 KiB
HTML
687 lines
58 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>pdb --- Python 的调试器 — 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="Python Profilers 分析器" href="profile.html" />
|
||
<link rel="prev" title="faulthandler —— 转储 Python 的跟踪信息" href="faulthandler.html" />
|
||
<link rel="canonical" href="https://docs.python.org/3/library/pdb.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">pdb</span></code> --- Python 的调试器</a><ul>
|
||
<li><a class="reference internal" href="#debugger-commands">调试器命令</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
<h4>上一个主题</h4>
|
||
<p class="topless"><a href="faulthandler.html"
|
||
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">faulthandler</span></code> —— 转储 Python 的跟踪信息</a></p>
|
||
<h4>下一个主题</h4>
|
||
<p class="topless"><a href="profile.html"
|
||
title="下一章">Python Profilers 分析器</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/pdb.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/pdb.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="profile.html" title="Python Profilers 分析器"
|
||
accesskey="N">下一页</a> |</li>
|
||
<li class="right" >
|
||
<a href="faulthandler.html" title="faulthandler —— 转储 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="debug.html" accesskey="U">调试和分析</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-pdb">
|
||
<span id="pdb-the-python-debugger"></span><span id="debugger"></span><h1><a class="reference internal" href="#module-pdb" title="pdb: The Python debugger for interactive interpreters."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pdb</span></code></a> --- Python 的调试器<a class="headerlink" href="#module-pdb" title="永久链接至标题">¶</a></h1>
|
||
<p><strong>源代码:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.8/Lib/pdb.py">Lib/pdb.py</a></p>
|
||
<hr class="docutils" id="index-0" />
|
||
<p><a class="reference internal" href="#module-pdb" title="pdb: The Python debugger for interactive interpreters."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pdb</span></code></a> 模块定义了一个交互式源代码调试器,用于 Python 程序。它支持在源码行间设置(有条件的)断点和单步执行,检视堆栈帧,列出源码列表,以及在任何堆栈帧的上下文中运行任意 Python 代码。它还支持事后调试,可以在程序控制下调用。</p>
|
||
<p id="index-1">调试器是可扩展的——调试器实际被定义为 <a class="reference internal" href="#pdb.Pdb" title="pdb.Pdb"><code class="xref py py-class docutils literal notranslate"><span class="pre">Pdb</span></code></a> 类。该类目前没有文档,但通过阅读源码很容易理解它。扩展接口使用了 <a class="reference internal" href="bdb.html#module-bdb" title="bdb: Debugger framework."><code class="xref py py-mod docutils literal notranslate"><span class="pre">bdb</span></code></a> 和 <a class="reference internal" href="cmd.html#module-cmd" title="cmd: Build line-oriented command interpreters."><code class="xref py py-mod docutils literal notranslate"><span class="pre">cmd</span></code></a> 模块。</p>
|
||
<p>调试器的提示符是 <code class="docutils literal notranslate"><span class="pre">(Pdb)</span></code>。在调试器的控制下运行程序的典型用法是:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">pdb</span>
|
||
<span class="gp">>>> </span><span class="kn">import</span> <span class="nn">mymodule</span>
|
||
<span class="gp">>>> </span><span class="n">pdb</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="s1">'mymodule.test()'</span><span class="p">)</span>
|
||
<span class="go">> <string>(0)?()</span>
|
||
<span class="go">(Pdb) continue</span>
|
||
<span class="go">> <string>(1)?()</span>
|
||
<span class="go">(Pdb) continue</span>
|
||
<span class="go">NameError: 'spam'</span>
|
||
<span class="go">> <string>(1)?()</span>
|
||
<span class="go">(Pdb)</span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.3 版更改: </span>由 <a class="reference internal" href="readline.html#module-readline" title="readline: GNU readline support for Python. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">readline</span></code></a> 模块实现的 Tab 补全可用于补全本模块的命令和命令的参数,例如,Tab 补全会提供当前的全局变量和局部变量,用作 <code class="docutils literal notranslate"><span class="pre">p</span></code> 命令的参数。</p>
|
||
</div>
|
||
<p>也可以将 <code class="file docutils literal notranslate"><span class="pre">pdb.py</span></code> 作为脚本调用,来调试其他脚本。例如:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python3</span> <span class="o">-</span><span class="n">m</span> <span class="n">pdb</span> <span class="n">myscript</span><span class="o">.</span><span class="n">py</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>当作为脚本调用时,如果要调试的程序异常退出,pdb 调试将自动进入事后调试。事后调试之后(或程序正常退出之后),pdb 将重新启动程序。自动重启会保留 pdb 的状态(如断点),在大多数情况下,这比在退出程序的同时退出调试器更加实用。</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.2 新版功能: </span><code class="file docutils literal notranslate"><span class="pre">pdb.py</span></code> 现在接受 <code class="docutils literal notranslate"><span class="pre">-c</span></code> 选项,可以执行命令,这与将该命令写入 <code class="file docutils literal notranslate"><span class="pre">.pdbrc</span></code> 文件相同,请参阅 <a class="reference internal" href="#debugger-commands"><span class="std std-ref">调试器命令</span></a>。</p>
|
||
</div>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.7 新版功能: </span><code class="file docutils literal notranslate"><span class="pre">pdb.py</span></code> 现在接受 <code class="docutils literal notranslate"><span class="pre">-m</span></code> 选项,该选项用于执行一个模块,类似于 <code class="docutils literal notranslate"><span class="pre">python3</span> <span class="pre">-m</span></code>。与脚本相同,调试器将暂停在待执行模块第一行前。</p>
|
||
</div>
|
||
<p>从正在运行的程序进入调试器的典型用法是插入</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">pdb</span><span class="p">;</span> <span class="n">pdb</span><span class="o">.</span><span class="n">set_trace</span><span class="p">()</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>到你想进入调试器的位置。然后就可以单步执行上述语句之后的代码,要关闭调试器继续运行,请使用 <a class="reference internal" href="#pdbcommand-continue"><code class="xref std std-pdbcmd docutils literal notranslate"><span class="pre">continue</span></code></a> 命令。</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.7 新版功能: </span>内置函数 <a class="reference internal" href="functions.html#breakpoint" title="breakpoint"><code class="xref py py-func docutils literal notranslate"><span class="pre">breakpoint()</span></code></a>,当以默认参数调用它时,可以用来代替 <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">pdb;</span> <span class="pre">pdb.set_trace()</span></code>。</p>
|
||
</div>
|
||
<p>检查已崩溃程序的典型用法是:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">pdb</span>
|
||
<span class="gp">>>> </span><span class="kn">import</span> <span class="nn">mymodule</span>
|
||
<span class="gp">>>> </span><span class="n">mymodule</span><span class="o">.</span><span class="n">test</span><span class="p">()</span>
|
||
<span class="gt">Traceback (most recent call last):</span>
|
||
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n"><module></span>
|
||
File <span class="nb">"./mymodule.py"</span>, line <span class="m">4</span>, in <span class="n">test</span>
|
||
<span class="w"> </span><span class="n">test2</span><span class="p">()</span>
|
||
File <span class="nb">"./mymodule.py"</span>, line <span class="m">3</span>, in <span class="n">test2</span>
|
||
<span class="w"> </span><span class="nb">print</span><span class="p">(</span><span class="n">spam</span><span class="p">)</span>
|
||
<span class="gr">NameError</span>: <span class="n">spam</span>
|
||
<span class="gp">>>> </span><span class="n">pdb</span><span class="o">.</span><span class="n">pm</span><span class="p">()</span>
|
||
<span class="go">> ./mymodule.py(3)test2()</span>
|
||
<span class="go">-> print(spam)</span>
|
||
<span class="go">(Pdb)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>本模块定义了下列函数,每个函数进入调试器的方式略有不同:</p>
|
||
<dl class="function">
|
||
<dt id="pdb.run">
|
||
<code class="sig-prename descclassname">pdb.</code><code class="sig-name descname">run</code><span class="sig-paren">(</span><em class="sig-param">statement</em>, <em class="sig-param">globals=None</em>, <em class="sig-param">locals=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pdb.run" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>在调试器控制范围内执行 <em>statement</em> (以字符串或代码对象的形式提供)。调试器提示符会在执行代码前出现,你可以设置断点并键入 <a class="reference internal" href="#pdbcommand-continue"><code class="xref std std-pdbcmd docutils literal notranslate"><span class="pre">continue</span></code></a>,也可以使用 <a class="reference internal" href="#pdbcommand-step"><code class="xref std std-pdbcmd docutils literal notranslate"><span class="pre">step</span></code></a> 或 <a class="reference internal" href="#pdbcommand-next"><code class="xref std std-pdbcmd docutils literal notranslate"><span class="pre">next</span></code></a> 逐步执行语句(上述所有命令在后文有说明)。可选参数 <em>globals</em> 和 <em>locals</em> 指定代码执行环境,默认时使用 <a class="reference internal" href="__main__.html#module-__main__" title="__main__: The environment where the top-level script is run."><code class="xref py py-mod docutils literal notranslate"><span class="pre">__main__</span></code></a> 模块的字典。(请参阅内置函数 <a class="reference internal" href="functions.html#exec" title="exec"><code class="xref py py-func docutils literal notranslate"><span class="pre">exec()</span></code></a> 或 <a class="reference internal" href="functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a> 的说明。)</p>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="pdb.runeval">
|
||
<code class="sig-prename descclassname">pdb.</code><code class="sig-name descname">runeval</code><span class="sig-paren">(</span><em class="sig-param">expression</em>, <em class="sig-param">globals=None</em>, <em class="sig-param">locals=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pdb.runeval" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>在调试器控制范围内执行 <em>expression</em> (以字符串或代码对象的形式提供)。<a class="reference internal" href="#pdb.runeval" title="pdb.runeval"><code class="xref py py-func docutils literal notranslate"><span class="pre">runeval()</span></code></a> 返回时将返回表达式的值。本函数在其他方面与 <a class="reference internal" href="#pdb.run" title="pdb.run"><code class="xref py py-func docutils literal notranslate"><span class="pre">run()</span></code></a> 类似。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="pdb.runcall">
|
||
<code class="sig-prename descclassname">pdb.</code><code class="sig-name descname">runcall</code><span class="sig-paren">(</span><em class="sig-param">function</em>, <em class="sig-param">*args</em>, <em class="sig-param">**kwds</em><span class="sig-paren">)</span><a class="headerlink" href="#pdb.runcall" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>使用给定的参数调用 <em>function</em> (以函数或方法对象的形式提供,不能是字符串)。<a class="reference internal" href="#pdb.runcall" title="pdb.runcall"><code class="xref py py-func docutils literal notranslate"><span class="pre">runcall()</span></code></a> 返回的是所调用函数的返回值。调试器提示符将在进入函数后立即出现。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="pdb.set_trace">
|
||
<code class="sig-prename descclassname">pdb.</code><code class="sig-name descname">set_trace</code><span class="sig-paren">(</span><em class="sig-param">*</em>, <em class="sig-param">header=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pdb.set_trace" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>在调用本函数的堆栈帧处进入调试器。用于硬编码一个断点到程序中的固定点处,即使该代码不在调试状态(如断言失败时)。如果传入 <em>header</em>,它将在调试开始前被打印到控制台。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.7 版更改: </span>仅关键字参数 <em>header</em>。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="pdb.post_mortem">
|
||
<code class="sig-prename descclassname">pdb.</code><code class="sig-name descname">post_mortem</code><span class="sig-paren">(</span><em class="sig-param">traceback=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pdb.post_mortem" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>进入 <em>traceback</em> 对象的事后调试。如果没有给定 <em>traceback</em>,默认使用当前正在处理的异常之一(默认时,必须存在正在处理的异常)。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="pdb.pm">
|
||
<code class="sig-prename descclassname">pdb.</code><code class="sig-name descname">pm</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pdb.pm" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>在 <a class="reference internal" href="sys.html#sys.last_traceback" title="sys.last_traceback"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.last_traceback</span></code></a> 中查找 traceback,并进入其事后调试。</p>
|
||
</dd></dl>
|
||
|
||
<p><code class="docutils literal notranslate"><span class="pre">run*</span></code> 函数和 <a class="reference internal" href="#pdb.set_trace" title="pdb.set_trace"><code class="xref py py-func docutils literal notranslate"><span class="pre">set_trace()</span></code></a> 都是别名,用于实例化 <a class="reference internal" href="#pdb.Pdb" title="pdb.Pdb"><code class="xref py py-class docutils literal notranslate"><span class="pre">Pdb</span></code></a> 类和调用同名方法。如果要使用其他功能,则必须自己执行以下操作:</p>
|
||
<dl class="class">
|
||
<dt id="pdb.Pdb">
|
||
<em class="property">class </em><code class="sig-prename descclassname">pdb.</code><code class="sig-name descname">Pdb</code><span class="sig-paren">(</span><em class="sig-param">completekey='tab'</em>, <em class="sig-param">stdin=None</em>, <em class="sig-param">stdout=None</em>, <em class="sig-param">skip=None</em>, <em class="sig-param">nosigint=False</em>, <em class="sig-param">readrc=True</em><span class="sig-paren">)</span><a class="headerlink" href="#pdb.Pdb" title="永久链接至目标">¶</a></dt>
|
||
<dd><p><a class="reference internal" href="#pdb.Pdb" title="pdb.Pdb"><code class="xref py py-class docutils literal notranslate"><span class="pre">Pdb</span></code></a> 是调试器类。</p>
|
||
<p><em>completekey</em>、<em>stdin</em> 和 <em>stdout</em> 参数都会传递给底层的 <a class="reference internal" href="cmd.html#cmd.Cmd" title="cmd.Cmd"><code class="xref py py-class docutils literal notranslate"><span class="pre">cmd.Cmd</span></code></a> 类,请参考相应的描述。</p>
|
||
<p>如果给出 <em>skip</em> 参数,则它必须是一个迭代器,可以迭代出 glob-style 样式的模块名称。如果遇到匹配上述样式的模块,调试器将不会进入来自该模块的堆栈帧。 <a class="footnote-reference brackets" href="#id3" id="id1">1</a></p>
|
||
<p>默认情况下,当发出 <code class="docutils literal notranslate"><span class="pre">continue</span></code> 命令时,Pdb 将为 SIGINT 信号设置一个处理程序(SIGINT 信号是用户在控制台按 <kbd class="kbd docutils literal notranslate">Ctrl-C</kbd> 时发出的)。这使用户可以按 <kbd class="kbd docutils literal notranslate">Ctrl-C</kbd> 再次进入调试器。如果希望 Pdb 不要修改 SIGINT 处理程序,请将 <em>nosigint</em> 设置为 true。</p>
|
||
<p><em>readrc</em> 参数默认为 true,它控制 Pdb 是否从文件系统加载 .pdbrc 文件。</p>
|
||
<p>启用跟踪且带有 <em>skip</em> 参数的调用示范:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">pdb</span><span class="p">;</span> <span class="n">pdb</span><span class="o">.</span><span class="n">Pdb</span><span class="p">(</span><span class="n">skip</span><span class="o">=</span><span class="p">[</span><span class="s1">'django.*'</span><span class="p">])</span><span class="o">.</span><span class="n">set_trace</span><span class="p">()</span>
|
||
</pre></div>
|
||
</div>
|
||
<p class="audit-hook">引发一个不带参数的 <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">审计事件</span></a> <code class="docutils literal notranslate"><span class="pre">pdb.Pdb</span></code>。</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.1 新版功能: </span><em>skip</em> 参数。</p>
|
||
</div>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.2 新版功能: </span><em>nosigint</em> 参数。在这之前,Pdb 不为 SIGINT 设置处理程序。</p>
|
||
</div>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.6 版更改: </span><em>readrc</em> 参数。</p>
|
||
</div>
|
||
<dl class="method">
|
||
<dt id="pdb.Pdb.run">
|
||
<code class="sig-name descname">run</code><span class="sig-paren">(</span><em class="sig-param">statement</em>, <em class="sig-param">globals=None</em>, <em class="sig-param">locals=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pdb.Pdb.run" title="永久链接至目标">¶</a></dt>
|
||
<dt id="pdb.Pdb.runeval">
|
||
<code class="sig-name descname">runeval</code><span class="sig-paren">(</span><em class="sig-param">expression</em>, <em class="sig-param">globals=None</em>, <em class="sig-param">locals=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pdb.Pdb.runeval" title="永久链接至目标">¶</a></dt>
|
||
<dt id="pdb.Pdb.runcall">
|
||
<code class="sig-name descname">runcall</code><span class="sig-paren">(</span><em class="sig-param">function</em>, <em class="sig-param">*args</em>, <em class="sig-param">**kwds</em><span class="sig-paren">)</span><a class="headerlink" href="#pdb.Pdb.runcall" title="永久链接至目标">¶</a></dt>
|
||
<dt id="pdb.Pdb.set_trace">
|
||
<code class="sig-name descname">set_trace</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#pdb.Pdb.set_trace" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>请参阅上文解释同名函数的文档。</p>
|
||
</dd></dl>
|
||
|
||
</dd></dl>
|
||
|
||
<section id="debugger-commands">
|
||
<span id="id2"></span><h2>调试器命令<a class="headerlink" href="#debugger-commands" title="永久链接至标题">¶</a></h2>
|
||
<p>下方列出的是调试器可接受的命令。如下所示,大多数命令可以缩写为一个或两个字母。如 <code class="docutils literal notranslate"><span class="pre">h(elp)</span></code> 表示可以输入 <code class="docutils literal notranslate"><span class="pre">h</span></code> 或 <code class="docutils literal notranslate"><span class="pre">help</span></code> 来输入帮助命令(但不能输入 <code class="docutils literal notranslate"><span class="pre">he</span></code> 或 <code class="docutils literal notranslate"><span class="pre">hel</span></code>,也不能是 <code class="docutils literal notranslate"><span class="pre">H</span></code> 或 <code class="docutils literal notranslate"><span class="pre">Help</span></code> 或 <code class="docutils literal notranslate"><span class="pre">HELP</span></code>)。命令的参数必须用空格(空格符或制表符)分隔。在命令语法中,可选参数括在方括号 (<code class="docutils literal notranslate"><span class="pre">[]</span></code>) 中,使用时请勿输入方括号。命令语法中的选择项由竖线 (<code class="docutils literal notranslate"><span class="pre">|</span></code>) 分隔。</p>
|
||
<p>输入一个空白行将重复最后输入的命令。例外:如果最后一个命令是 <a class="reference internal" href="#pdbcommand-list"><code class="xref std std-pdbcmd docutils literal notranslate"><span class="pre">list</span></code></a> 命令,则会列出接下来的 11 行。</p>
|
||
<p>调试器无法识别的命令将被认为是 Python 语句,并在正在调试的程序的上下文中执行。Python 语句也可以用感叹号 (<code class="docutils literal notranslate"><span class="pre">!</span></code>) 作为前缀。这是检查正在调试的程序的强大方法,甚至可以修改变量或调用函数。当此类语句发生异常,将打印异常名称,但调试器的状态不会改变。</p>
|
||
<p>调试器支持 <a class="reference internal" href="#debugger-aliases"><span class="std std-ref">别名</span></a>。别名可以有参数,使得调试器对被检查的上下文有一定程度的适应性。</p>
|
||
<p>在一行中可以输入多个命令,以 <code class="docutils literal notranslate"><span class="pre">;;</span></code> 分隔。(不能使用单个 <code class="docutils literal notranslate"><span class="pre">;</span></code>,因为它用于分隔传递给 Python 解释器的一行中的多条语句。)切分命令很无脑,总是在第一个 <code class="docutils literal notranslate"><span class="pre">;;</span></code> 对处将输入切分开,即使它位于带引号的字符串之中。</p>
|
||
<p id="index-2">如果文件 <code class="file docutils literal notranslate"><span class="pre">.pdbrc</span></code> 存在于用户主目录或当前目录中,则将其读入并执行,等同于在调试器提示符下键入该文件。这对于别名很有用。若两个文件都存在则首先读取主目录中的文件,且本地文件可以覆盖其中定义的别名。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.2 版更改: </span><code class="file docutils literal notranslate"><span class="pre">.pdbrc</span></code> 现在可以包含继续调试的命令,如 <a class="reference internal" href="#pdbcommand-continue"><code class="xref std std-pdbcmd docutils literal notranslate"><span class="pre">continue</span></code></a> 或 <a class="reference internal" href="#pdbcommand-next"><code class="xref std std-pdbcmd docutils literal notranslate"><span class="pre">next</span></code></a>。文件中的这些命令以前是无效的。</p>
|
||
</div>
|
||
<dl class="pdbcommand">
|
||
<dt id="pdbcommand-help">
|
||
<code class="sig-name descname">h(elp)</code><code class="sig-prename descclassname"> [command]</code><a class="headerlink" href="#pdbcommand-help" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>不带参数时,显示可用的命令列表。参数为 <em>command</em> 时,打印有关该命令的帮助。<code class="docutils literal notranslate"><span class="pre">help</span> <span class="pre">pdb</span></code> 显示完整文档(即 <a class="reference internal" href="#module-pdb" title="pdb: The Python debugger for interactive interpreters."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pdb</span></code></a> 模块的文档字符串)。由于 <em>command</em> 参数必须是标识符,因此要获取 <code class="docutils literal notranslate"><span class="pre">!</span></code> 的帮助必须输入 <code class="docutils literal notranslate"><span class="pre">help</span> <span class="pre">exec</span></code>。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="pdbcommand">
|
||
<dt id="pdbcommand-where">
|
||
<code class="sig-name descname">w(here)</code><a class="headerlink" href="#pdbcommand-where" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>打印堆栈回溯,最新一帧在底部。有一个箭头指向当前帧,该帧决定了大多数命令的上下文。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="pdbcommand">
|
||
<dt id="pdbcommand-down">
|
||
<code class="sig-name descname">d(own)</code><code class="sig-prename descclassname"> [count]</code><a class="headerlink" href="#pdbcommand-down" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>在堆栈回溯中,将当前帧向下移动 <em>count</em> 级(默认为 1 级,移向更新的帧)。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="pdbcommand">
|
||
<dt id="pdbcommand-up">
|
||
<code class="sig-name descname">u(p)</code><code class="sig-prename descclassname"> [count]</code><a class="headerlink" href="#pdbcommand-up" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>在堆栈回溯中,将当前帧向上移动 <em>count</em> 级(默认为 1 级,移向更老的帧)。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="pdbcommand">
|
||
<dt id="pdbcommand-break">
|
||
<code class="sig-name descname">b(reak)</code><code class="sig-prename descclassname"> [([filename:]lineno | function) [, condition]]</code><a class="headerlink" href="#pdbcommand-break" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>如果带有 <em>lineno</em> 参数,则在当前文件相应行处设置一个断点。如果带有 <em>function</em> 参数,则在该函数的第一条可执行语句处设置一个断点。行号可以加上文件名和冒号作为前缀,以在另一个文件(可能是尚未加载的文件)中设置一个断点。另一个文件将在 <a class="reference internal" href="sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> 范围内搜索。请注意,每个断点都分配有一个编号,其他所有断点命令都引用该编号。</p>
|
||
<p>如果第二个参数存在,它应该是一个表达式,且它的计算值为 true 时断点才起作用。</p>
|
||
<p>如果不带参数执行,将列出所有中断,包括每个断点、命中该断点的次数、当前的忽略次数以及关联的条件(如果有)。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="pdbcommand">
|
||
<dt id="pdbcommand-tbreak">
|
||
<code class="sig-name descname">tbreak</code><code class="sig-prename descclassname"> [([filename:]lineno | function) [, condition]]</code><a class="headerlink" href="#pdbcommand-tbreak" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>临时断点,在第一次命中时会自动删除。它的参数与 <a class="reference internal" href="#pdbcommand-break"><code class="xref std std-pdbcmd docutils literal notranslate"><span class="pre">break</span></code></a> 相同。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="pdbcommand">
|
||
<dt id="pdbcommand-clear">
|
||
<code class="sig-name descname">cl(ear)</code><code class="sig-prename descclassname"> [filename:lineno | bpnumber [bpnumber ...]]</code><a class="headerlink" href="#pdbcommand-clear" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>如果参数是 <em>filename:lineno</em>,则清除此行上的所有断点。如果参数是空格分隔的断点编号列表,则清除这些断点。如果不带参数,则清除所有断点(但会先提示确认)。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="pdbcommand">
|
||
<dt id="pdbcommand-disable">
|
||
<code class="sig-name descname">disable</code><code class="sig-prename descclassname"> [bpnumber [bpnumber ...]]</code><a class="headerlink" href="#pdbcommand-disable" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>禁用断点,断点以空格分隔的断点编号列表给出。禁用断点表示它不会导致程序停止执行,但是与清除断点不同,禁用的断点将保留在断点列表中并且可以(重新)启用。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="pdbcommand">
|
||
<dt id="pdbcommand-enable">
|
||
<code class="sig-name descname">enable</code><code class="sig-prename descclassname"> [bpnumber [bpnumber ...]]</code><a class="headerlink" href="#pdbcommand-enable" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>启用指定的断点。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="pdbcommand">
|
||
<dt id="pdbcommand-ignore">
|
||
<code class="sig-name descname">ignore</code><code class="sig-prename descclassname"> bpnumber [count]</code><a class="headerlink" href="#pdbcommand-ignore" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>为指定的断点编号设置忽略次数。如果省略 count,则忽略次数将设置为 0。忽略次数为 0 时断点将变为活动状态。如果为非零值,在每次达到断点,且断点未禁用,且关联条件计算值为 true 的情况下,该忽略次数会递减。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="pdbcommand">
|
||
<dt id="pdbcommand-condition">
|
||
<code class="sig-name descname">condition</code><code class="sig-prename descclassname"> bpnumber [condition]</code><a class="headerlink" href="#pdbcommand-condition" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>为断点设置一个新 <em>condition</em>,它是一个表达式,且它的计算值为 true 时断点才起作用。如果没有给出 <em>condition</em>,则删除现有条件,也就是将断点设为无条件。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="pdbcommand">
|
||
<dt id="pdbcommand-commands">
|
||
<code class="sig-name descname">commands</code><code class="sig-prename descclassname"> [bpnumber]</code><a class="headerlink" href="#pdbcommand-commands" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>为编号是 <em>bpnumber</em> 的断点指定一系列命令。命令内容将显示在后续的几行中。输入仅包含 <code class="docutils literal notranslate"><span class="pre">end</span></code> 的行来结束命令列表。举个例子:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="n">Pdb</span><span class="p">)</span> <span class="n">commands</span> <span class="mi">1</span>
|
||
<span class="p">(</span><span class="n">com</span><span class="p">)</span> <span class="n">p</span> <span class="n">some_variable</span>
|
||
<span class="p">(</span><span class="n">com</span><span class="p">)</span> <span class="n">end</span>
|
||
<span class="p">(</span><span class="n">Pdb</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>要删除断点上的所有命令,请输入 <code class="docutils literal notranslate"><span class="pre">commands</span></code> 并立即以 <code class="docutils literal notranslate"><span class="pre">end</span></code> 结尾,也就是不指定任何命令。</p>
|
||
<p>如果不带 <em>bpnumber</em> 参数,<code class="docutils literal notranslate"><span class="pre">commands</span></code> 作用于最后一个被设置的断点。</p>
|
||
<p>可以为断点指定命令来重新启动程序。只需使用 <a class="reference internal" href="#pdbcommand-continue"><code class="xref std std-pdbcmd docutils literal notranslate"><span class="pre">continue</span></code></a> 或 <a class="reference internal" href="#pdbcommand-step"><code class="xref std std-pdbcmd docutils literal notranslate"><span class="pre">step</span></code></a> 命令或其他可以继续运行程序的命令。</p>
|
||
<p>如果指定了某个继续运行程序的命令(目前包括 <a class="reference internal" href="#pdbcommand-continue"><code class="xref std std-pdbcmd docutils literal notranslate"><span class="pre">continue</span></code></a>, <a class="reference internal" href="#pdbcommand-step"><code class="xref std std-pdbcmd docutils literal notranslate"><span class="pre">step</span></code></a>, <a class="reference internal" href="#pdbcommand-next"><code class="xref std std-pdbcmd docutils literal notranslate"><span class="pre">next</span></code></a>, <a class="reference internal" href="#pdbcommand-return"><code class="xref std std-pdbcmd docutils literal notranslate"><span class="pre">return</span></code></a>, <a class="reference internal" href="#pdbcommand-jump"><code class="xref std std-pdbcmd docutils literal notranslate"><span class="pre">jump</span></code></a>, <a class="reference internal" href="#pdbcommand-quit"><code class="xref std std-pdbcmd docutils literal notranslate"><span class="pre">quit</span></code></a> 及它们的缩写)将终止命令列表(就像该命令后紧跟着 end)。因为在任何时候继续运行下去(即使是简单的 next 或 step),都可能会遇到另一个断点,该断点可能具有自己的命令列表,这导致要执行的列表含糊不清。</p>
|
||
<p>如果在命令列表中加入 'silent' 命令,那么在该断点处停下时就不会打印常规信息。如果希望断点打印特定信息后继续运行,这可能是理想的。如果没有其他命令来打印一些信息,则看不到已达到断点的迹象。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="pdbcommand">
|
||
<dt id="pdbcommand-step">
|
||
<code class="sig-name descname">s(tep)</code><a class="headerlink" href="#pdbcommand-step" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>运行当前行,在第一个可以停止的位置(在被调用的函数内部或在当前函数的下一行)停下。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="pdbcommand">
|
||
<dt id="pdbcommand-next">
|
||
<code class="sig-name descname">n(ext)</code><a class="headerlink" href="#pdbcommand-next" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>继续运行,直到运行到当前函数的下一行,或当前函数返回为止。( <a class="reference internal" href="#pdbcommand-next"><code class="xref std std-pdbcmd docutils literal notranslate"><span class="pre">next</span></code></a> 和 <a class="reference internal" href="#pdbcommand-step"><code class="xref std std-pdbcmd docutils literal notranslate"><span class="pre">step</span></code></a> 之间的区别在于,<a class="reference internal" href="#pdbcommand-step"><code class="xref std std-pdbcmd docutils literal notranslate"><span class="pre">step</span></code></a> 进入被调用函数内部并停止,而 <a class="reference internal" href="#pdbcommand-next"><code class="xref std std-pdbcmd docutils literal notranslate"><span class="pre">next</span></code></a> (几乎)全速运行被调用函数,仅在当前函数的下一行停止。)</p>
|
||
</dd></dl>
|
||
|
||
<dl class="pdbcommand">
|
||
<dt id="pdbcommand-until">
|
||
<code class="sig-name descname">unt(il)</code><code class="sig-prename descclassname"> [lineno]</code><a class="headerlink" href="#pdbcommand-until" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>如果不带参数,则继续运行,直到行号比当前行大时停止。</p>
|
||
<p>如果带有行号,则继续运行,直到行号大于或等于该行号时停止。在这两种情况下,当前帧返回时也将停止。</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">在 3.2 版更改: </span>允许明确给定行号。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="pdbcommand">
|
||
<dt id="pdbcommand-return">
|
||
<code class="sig-name descname">r(eturn)</code><a class="headerlink" href="#pdbcommand-return" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>继续运行,直到当前函数返回。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="pdbcommand">
|
||
<dt id="pdbcommand-continue">
|
||
<code class="sig-name descname">c(ont(inue))</code><a class="headerlink" href="#pdbcommand-continue" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>继续运行,仅在遇到断点时停止。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="pdbcommand">
|
||
<dt id="pdbcommand-jump">
|
||
<code class="sig-name descname">j(ump)</code><code class="sig-prename descclassname"> lineno</code><a class="headerlink" href="#pdbcommand-jump" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>设置即将运行的下一行。仅可用于堆栈最底部的帧。它可以往回跳来再次运行代码,也可以往前跳来跳过不想运行的代码。</p>
|
||
<p>需要注意的是,不是所有的跳转都是允许的 -- 例如,不能跳转到 <a class="reference internal" href="../reference/compound_stmts.html#for"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">for</span></code></a> 循环的中间或跳出 <a class="reference internal" href="../reference/compound_stmts.html#finally"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">finally</span></code></a> 子句。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="pdbcommand">
|
||
<dt id="pdbcommand-list">
|
||
<code class="sig-name descname">l(ist)</code><code class="sig-prename descclassname"> [first[, last]]</code><a class="headerlink" href="#pdbcommand-list" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>列出当前文件的源代码。如果不带参数,则列出当前行周围的 11 行,或延续前一次列出。如果用 <code class="docutils literal notranslate"><span class="pre">.</span></code> 作为参数,则列出当前行周围的 11 行。如果带有一个参数,则列出那一行周围的 11 行。如果带有两个参数,则列出所给的范围中的代码;如果第二个参数小于第一个参数,则将其解释为列出行数的计数。</p>
|
||
<p>当前帧中的当前行用 <code class="docutils literal notranslate"><span class="pre">-></span></code> 标记。如果正在调试异常,且最早抛出或传递该异常的行不是当前行,则那一行用 <code class="docutils literal notranslate"><span class="pre">>></span></code> 标记。</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.2 新版功能: </span><code class="docutils literal notranslate"><span class="pre">>></span></code> 标记。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="pdbcommand">
|
||
<dt id="pdbcommand-ll">
|
||
<code class="sig-name descname">ll</code><code class="sig-prename descclassname"> | longlist</code><a class="headerlink" href="#pdbcommand-ll" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>列出当前函数或帧的所有源代码。相关行的标记与 <a class="reference internal" href="#pdbcommand-list"><code class="xref std std-pdbcmd docutils literal notranslate"><span class="pre">list</span></code></a> 相同。</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.2 新版功能.</span></p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="pdbcommand">
|
||
<dt id="pdbcommand-args">
|
||
<code class="sig-name descname">a(rgs)</code><a class="headerlink" href="#pdbcommand-args" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>打印当前函数的参数列表。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="pdbcommand">
|
||
<dt id="pdbcommand-p">
|
||
<code class="sig-name descname">p</code><code class="sig-prename descclassname"> expression</code><a class="headerlink" href="#pdbcommand-p" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>在当前上下文中运行 <em>expression</em> 并打印它的值。</p>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">注解</p>
|
||
<p><code class="docutils literal notranslate"><span class="pre">print()</span></code> 也可以使用,但它不是一个调试器命令 --- 它执行 Python <a class="reference internal" href="functions.html#print" title="print"><code class="xref py py-func docutils literal notranslate"><span class="pre">print()</span></code></a> 函数。</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="pdbcommand">
|
||
<dt id="pdbcommand-pp">
|
||
<code class="sig-name descname">pp</code><code class="sig-prename descclassname"> expression</code><a class="headerlink" href="#pdbcommand-pp" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>与 <a class="reference internal" href="#pdbcommand-p"><code class="xref std std-pdbcmd docutils literal notranslate"><span class="pre">p</span></code></a> 命令类似,但表达式的值使用 <a class="reference internal" href="pprint.html#module-pprint" title="pprint: Data pretty printer."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pprint</span></code></a> 模块美观地打印。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="pdbcommand">
|
||
<dt id="pdbcommand-whatis">
|
||
<code class="sig-name descname">whatis</code><code class="sig-prename descclassname"> expression</code><a class="headerlink" href="#pdbcommand-whatis" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>打印 <em>expression</em> 的类型。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="pdbcommand">
|
||
<dt id="pdbcommand-source">
|
||
<code class="sig-name descname">source</code><code class="sig-prename descclassname"> expression</code><a class="headerlink" href="#pdbcommand-source" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>尝试获取给定对象的源代码并显示出来。</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.2 新版功能.</span></p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="pdbcommand">
|
||
<dt id="pdbcommand-display">
|
||
<code class="sig-name descname">display</code><code class="sig-prename descclassname"> [expression]</code><a class="headerlink" href="#pdbcommand-display" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>如果表达式的值发生改变则显示它的值,每次将停止执行当前帧。</p>
|
||
<p>不带表达式则列出当前帧的所有显示表达式。</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.2 新版功能.</span></p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="pdbcommand">
|
||
<dt id="pdbcommand-undisplay">
|
||
<code class="sig-name descname">undisplay</code><code class="sig-prename descclassname"> [expression]</code><a class="headerlink" href="#pdbcommand-undisplay" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>不再显示当前帧中的表达式。 不带表达式则清除当前帧的所有显示表达式。</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.2 新版功能.</span></p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="pdbcommand">
|
||
<dt id="pdbcommand-interact">
|
||
<code class="sig-name descname">interact</code><a class="headerlink" href="#pdbcommand-interact" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>启动一个交互式解释器(使用 <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> 模块),它的全局命名空间将包含当前作用域中的所有(全局和局部)名称。</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.2 新版功能.</span></p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<span class="target" id="debugger-aliases"></span><dl class="pdbcommand">
|
||
<dt id="pdbcommand-alias">
|
||
<code class="sig-name descname">alias</code><code class="sig-prename descclassname"> [name [command]]</code><a class="headerlink" href="#pdbcommand-alias" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>创建一个标识为 <em>name</em> 的别名来执行 <em>command</em>。 执行的命令 <em>不可</em> 加上引号。 可替换形参可通过 <code class="docutils literal notranslate"><span class="pre">%1</span></code>, <code class="docutils literal notranslate"><span class="pre">%2</span></code> 等来标示,而 <code class="docutils literal notranslate"><span class="pre">%*</span></code> 会被所有形参所替换。 如果没有给出命令,则会显示 <em>name</em> 的当前别名。 如果没有给出参数,则会列出所有别名。</p>
|
||
<p>别名允许嵌套并可包含能在 pdb 提示符下合法输入的任何内容。 请注意内部 pdb 命令 <em>可以</em> 被别名所覆盖。 这样的命令将被隐藏直到别名被移除。 别名会递归地应用到命令行的第一个单词;行内的其他单词不会受影响。</p>
|
||
<p>作为示例,这里列出了两个有用的别名(特别适合放在 <code class="file docutils literal notranslate"><span class="pre">.pdbrc</span></code> 文件中):</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Print instance variables (usage "pi classInst")</span>
|
||
<span class="n">alias</span> <span class="n">pi</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="o">%</span><span class="mf">1.</span><span class="vm">__dict__</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span> <span class="nb">print</span><span class="p">(</span><span class="s2">"%1."</span><span class="p">,</span><span class="n">k</span><span class="p">,</span><span class="s2">"="</span><span class="p">,</span><span class="o">%</span><span class="mf">1.</span><span class="vm">__dict__</span><span class="p">[</span><span class="n">k</span><span class="p">])</span>
|
||
<span class="c1"># Print instance variables in self</span>
|
||
<span class="n">alias</span> <span class="n">ps</span> <span class="n">pi</span> <span class="bp">self</span>
|
||
</pre></div>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="pdbcommand">
|
||
<dt id="pdbcommand-unalias">
|
||
<code class="sig-name descname">unalias</code><code class="sig-prename descclassname"> name</code><a class="headerlink" href="#pdbcommand-unalias" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>删除指定的别名。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="pdbcommand">
|
||
<dt id="pdbcommand-!">
|
||
<code class="sig-name descname">!</code><code class="sig-prename descclassname"> statement</code><a class="headerlink" href="#pdbcommand-!" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>在当前堆栈帧的上下文中执行 (单行) <em>statement</em>。 感叹号可以被省略,除非语句的第一个单词与调试器命令重名。 要设置全局变量,你可以在同一行上为赋值命令添加前缀的 <a class="reference internal" href="../reference/simple_stmts.html#global"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">global</span></code></a> 语句,例如:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="n">Pdb</span><span class="p">)</span> <span class="k">global</span> <span class="n">list_options</span><span class="p">;</span> <span class="n">list_options</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'-l'</span><span class="p">]</span>
|
||
<span class="p">(</span><span class="n">Pdb</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="pdbcommand">
|
||
<dt id="pdbcommand-run">
|
||
<code class="sig-name descname">run</code><code class="sig-prename descclassname"> [args ...]</code><a class="headerlink" href="#pdbcommand-run" title="永久链接至目标">¶</a></dt>
|
||
<dt id="pdbcommand-restart">
|
||
<code class="sig-name descname">restart</code><code class="sig-prename descclassname"> [args ...]</code><a class="headerlink" href="#pdbcommand-restart" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>重启被调试的 Python 程序。 如果提供了参数,它会用 <a class="reference internal" href="shlex.html#module-shlex" title="shlex: Simple lexical analysis for Unix shell-like languages."><code class="xref py py-mod docutils literal notranslate"><span class="pre">shlex</span></code></a> 来拆分且拆分结果将被用作新的 <a class="reference internal" href="sys.html#sys.argv" title="sys.argv"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.argv</span></code></a>。 历史、中断点、动作和调试器选项将被保留。 <a class="reference internal" href="#pdbcommand-restart"><code class="xref std std-pdbcmd docutils literal notranslate"><span class="pre">restart</span></code></a> 是 <a class="reference internal" href="#pdbcommand-run"><code class="xref std std-pdbcmd docutils literal notranslate"><span class="pre">run</span></code></a> 的一个别名。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="pdbcommand">
|
||
<dt id="pdbcommand-quit">
|
||
<code class="sig-name descname">q(uit)</code><a class="headerlink" href="#pdbcommand-quit" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>退出调试器。 被执行的程序将被中止。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="pdbcommand">
|
||
<dt id="pdbcommand-debug">
|
||
<code class="sig-name descname">debug</code><code class="sig-prename descclassname"> code</code><a class="headerlink" href="#pdbcommand-debug" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>进入一个对代码参数执行步进的递归调试器(该参数是在当前环境中执行的任意表达式或语句)。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="pdbcommand">
|
||
<dt id="pdbcommand-retval">
|
||
<code class="sig-name descname">retval</code><a class="headerlink" href="#pdbcommand-retval" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>打印函数最后一次返回的返回值。</p>
|
||
</dd></dl>
|
||
|
||
<p class="rubric">备注</p>
|
||
<dl class="footnote brackets">
|
||
<dt class="label" id="id3"><span class="brackets"><a class="fn-backref" href="#id1">1</a></span></dt>
|
||
<dd><p>一个帧是否会被认为源自特定模块是由帧全局变量 <code class="docutils literal notranslate"><span class="pre">__name__</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">pdb</span></code> --- Python 的调试器</a><ul>
|
||
<li><a class="reference internal" href="#debugger-commands">调试器命令</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
<h4>上一个主题</h4>
|
||
<p class="topless"><a href="faulthandler.html"
|
||
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">faulthandler</span></code> —— 转储 Python 的跟踪信息</a></p>
|
||
<h4>下一个主题</h4>
|
||
<p class="topless"><a href="profile.html"
|
||
title="下一章">Python Profilers 分析器</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/pdb.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="profile.html" title="Python Profilers 分析器"
|
||
>下一页</a> |</li>
|
||
<li class="right" >
|
||
<a href="faulthandler.html" title="faulthandler —— 转储 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="debug.html" >调试和分析</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> |