parthtamu/rag-code-assistant
0
1<!DOCTYPE html>2 3<html lang="en" data-content_root="../">4 <head>5 <meta charset="utf-8" />6 <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />7<meta property="og:title" content="traceback — Print or retrieve a stack traceback" />8<meta property="og:type" content="website" />9<meta property="og:url" content="https://docs.python.org/3/library/traceback.html" />10<meta property="og:site_name" content="Python documentation" />11<meta property="og:description" content="Source code: Lib/traceback.py This module provides a standard interface to extract, format and print stack traces of Python programs. It is more flexible than the interpreter’s default traceback di..." />12<meta property="og:image:width" content="1146" />13<meta property="og:image:height" content="600" />14<meta property="og:image" content="https://docs.python.org/3.15/_images/social_previews/summary_library_traceback_b416843f.png" />15<meta property="og:image:alt" content="Source code: Lib/traceback.py This module provides a standard interface to extract, format and print stack traces of Python programs. It is more flexible than the interpreter’s default traceback di..." />16<meta name="description" content="Source code: Lib/traceback.py This module provides a standard interface to extract, format and print stack traces of Python programs. It is more flexible than the interpreter’s default traceback di..." />17<meta name="twitter:card" content="summary_large_image" />18<meta name="theme-color" content="#3776ab">19 20 <title>traceback — Print or retrieve a stack traceback — Python 3.15.0a6 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">21 22 <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />23 <link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />24 <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=89a2f22a" />25 <link rel="stylesheet" type="text/css" href="../_static/profiling-sampling-visualization.css?v=0c2600ae" />26 <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />27 28 <script src="../_static/documentation_options.js?v=6b7c9ff5"></script>29 <script src="../_static/doctools.js?v=9bcbadda"></script>30 <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>31 <script src="../_static/profiling-sampling-visualization.js?v=9811ed04"></script>32 33 <script src="../_static/sidebar.js"></script>34 35 <link rel="search" type="application/opensearchdescription+xml"36 title="Search within Python 3.15.0a6 documentation"37 href="../_static/opensearch.xml"/>38 <link rel="author" title="About these documents" href="../about.html" />39 <link rel="index" title="Index" href="../genindex.html" />40 <link rel="search" title="Search" href="../search.html" />41 <link rel="copyright" title="Copyright" href="../copyright.html" />42 <link rel="next" title="__future__ — Future statement definitions" href="__future__.html" />43 <link rel="prev" title="atexit — Exit handlers" href="atexit.html" />44 45 46 <script defer file-types="bz2,epub,zip" data-domain="docs.python.org" src="https://analytics.python.org/js/script.file-downloads.outbound-links.js"></script>47 48 <link rel="canonical" href="https://docs.python.org/3/library/traceback.html">49 50 51 52 53 <style>54 @media only screen {55 table.full-width-table {56 width: 100%;57 }58 }59 </style>60<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">61 <link rel="shortcut icon" type="image/png" href="../_static/py.svg">62 <script type="text/javascript" src="../_static/copybutton.js"></script>63 <script type="text/javascript" src="../_static/menu.js"></script>64 <script type="text/javascript" src="../_static/search-focus.js"></script>65 <script type="text/javascript" src="../_static/themetoggle.js"></script> 66 <script type="text/javascript" src="../_static/rtd_switcher.js"></script>67 <meta name="readthedocs-addons-api-version" content="1">68 69 </head>70<body>71<div class="mobile-nav">72 <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"73 aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu">74 <nav class="nav-content" role="navigation">75 <label for="menuToggler" class="toggler__label">76 <span></span>77 </label>78 <span class="nav-items-wrapper">79 <a href="https://www.python.org/" class="nav-logo">80 <img src="../_static/py.svg" alt="Python logo">81 </a>82 <span class="version_switcher_placeholder"></span>83 <form role="search" class="search" action="../search.html" method="get">84 <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">85 <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>86 </svg>87 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q">88 <input type="submit" value="Go">89 </form>90 </span>91 </nav>92 <div class="menu-wrapper">93 <nav class="menu" role="navigation" aria-label="main navigation">94 <div class="language_switcher_placeholder"></div>95 96<label class="theme-selector-label">97 Theme98 <select class="theme-selector" oninput="activateTheme(this.value)">99 <option value="auto" selected>Auto</option>100 <option value="light">Light</option>101 <option value="dark">Dark</option>102 </select>103</label>104 <div>105 <h3><a href="../contents.html">Table of Contents</a></h3>106 <ul>107<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">traceback</span></code> — Print or retrieve a stack traceback</a><ul>108<li><a class="reference internal" href="#module-level-functions">Module-Level Functions</a></li>109<li><a class="reference internal" href="#tracebackexception-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">TracebackException</span></code> Objects</a></li>110<li><a class="reference internal" href="#stacksummary-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">StackSummary</span></code> Objects</a></li>111<li><a class="reference internal" href="#framesummary-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">FrameSummary</span></code> Objects</a></li>112<li><a class="reference internal" href="#examples-of-using-the-module-level-functions">Examples of Using the Module-Level Functions</a></li>113<li><a class="reference internal" href="#examples-of-using-tracebackexception">Examples of Using <code class="xref py py-class docutils literal notranslate"><span class="pre">TracebackException</span></code></a></li>114</ul>115</li>116</ul>117 118 </div>119 <div>120 <h4>Previous topic</h4>121 <p class="topless"><a href="atexit.html"122 title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">atexit</span></code> — Exit handlers</a></p>123 </div>124 <div>125 <h4>Next topic</h4>126 <p class="topless"><a href="__future__.html"127 title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">__future__</span></code> — Future statement definitions</a></p>128 </div>129 <script>130 document.addEventListener('DOMContentLoaded', () => {131 const title = document.querySelector('meta[property="og:title"]').content;132 const elements = document.querySelectorAll('.improvepage');133 const pageurl = window.location.href.split('?')[0];134 elements.forEach(element => {135 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));136 url.searchParams.set('pagetitle', title);137 url.searchParams.set('pageurl', pageurl);138 url.searchParams.set('pagesource', "library/traceback.rst");139 element.href = url.toString();140 });141 });142 </script>143 <div role="note" aria-label="source link">144 <h3>This page</h3>145 <ul class="this-page-menu">146 <li><a href="../bugs.html">Report a bug</a></li>147 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>148 <li>149 <a href="https://github.com/python/cpython/blob/main/Doc/library/traceback.rst?plain=1"150 rel="nofollow">Show source151 </a>152 </li>153 154 </ul>155 </div>156 </nav>157 </div>158</div>159 160 161 <div class="related" role="navigation" aria-label="Related">162 <h3>Navigation</h3>163 <ul>164 <li class="right" style="margin-right: 10px">165 <a href="../genindex.html" title="General Index"166 accesskey="I">index</a></li>167 <li class="right" >168 <a href="../py-modindex.html" title="Python Module Index"169 >modules</a> |</li>170 <li class="right" >171 <a href="__future__.html" title="__future__ — Future statement definitions"172 accesskey="N">next</a> |</li>173 <li class="right" >174 <a href="atexit.html" title="atexit — Exit handlers"175 accesskey="P">previous</a> |</li>176 177 <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>178 <li><a href="https://www.python.org/">Python</a> »</li>179 <li class="switchers">180 <div class="language_switcher_placeholder"></div>181 <div class="version_switcher_placeholder"></div>182 </li>183 <li>184 185 </li>186 <li id="cpython-language-and-version">187 <a href="../index.html">3.15.0a6 Documentation</a> »188 </li>189 190 <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>191 <li class="nav-item nav-item-2"><a href="python.html" accesskey="U">Python Runtime Services</a> »</li>192 <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">traceback</span></code> — Print or retrieve a stack traceback</a></li>193 <li class="right">194 195 196 <div class="inline-search" role="search">197 <form class="inline-search" action="../search.html" method="get">198 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">199 <input type="submit" value="Go">200 </form>201 </div>202 |203 </li>204 <li class="right">205<label class="theme-selector-label">206 Theme207 <select class="theme-selector" oninput="activateTheme(this.value)">208 <option value="auto" selected>Auto</option>209 <option value="light">Light</option>210 <option value="dark">Dark</option>211 </select>212</label> |</li>213 214 </ul>215 </div> 216 217 <div class="document">218 <div class="documentwrapper">219 <div class="bodywrapper">220 <div class="body" role="main">221 222 <section id="module-traceback">223<span id="traceback-print-or-retrieve-a-stack-traceback"></span><h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">traceback</span></code> — Print or retrieve a stack traceback<a class="headerlink" href="#module-traceback" title="Link to this heading">¶</a></h1>224<p><strong>Source code:</strong> <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/main/Lib/traceback.py">Lib/traceback.py</a></p>225<hr class="docutils" />226<p>This module provides a standard interface to extract, format and print227stack traces of Python programs. It is more flexible than the228interpreter’s default traceback display, and therefore makes it229possible to configure certain aspects of the output. Finally,230it contains a utility for capturing enough information about an231exception to print it later, without the need to save a reference232to the actual exception. Since exceptions can be the roots of large233objects graph, this utility can significantly improve234memory management.</p>235<p id="index-0">The module uses <a class="reference internal" href="../reference/datamodel.html#traceback-objects"><span class="std std-ref">traceback objects</span></a> — these are236objects of type <a class="reference internal" href="types.html#types.TracebackType" title="types.TracebackType"><code class="xref py py-class docutils literal notranslate"><span class="pre">types.TracebackType</span></code></a>,237which are assigned to the <a class="reference internal" href="exceptions.html#BaseException.__traceback__" title="BaseException.__traceback__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__traceback__</span></code></a> field of238<a class="reference internal" href="exceptions.html#BaseException" title="BaseException"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseException</span></code></a> instances.</p>239<div class="admonition seealso">240<p class="admonition-title">See also</p>241<dl class="simple">242<dt>Module <a class="reference internal" href="faulthandler.html#module-faulthandler" title="faulthandler: Dump the Python traceback."><code class="xref py py-mod docutils literal notranslate"><span class="pre">faulthandler</span></code></a></dt><dd><p>Used to dump Python tracebacks explicitly, on a fault, after a timeout, or on a user signal.</p>243</dd>244<dt>Module <a class="reference internal" href="pdb.html#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></dt><dd><p>Interactive source code debugger for Python programs.</p>245</dd>246</dl>247</div>248<p>The module’s API can be divided into two parts:</p>249<ul class="simple">250<li><p>Module-level functions offering basic functionality, which are useful for interactive251inspection of exceptions and tracebacks.</p></li>252<li><p><a class="reference internal" href="#traceback.TracebackException" title="traceback.TracebackException"><code class="xref py py-class docutils literal notranslate"><span class="pre">TracebackException</span></code></a> class and its helper classes253<a class="reference internal" href="#traceback.StackSummary" title="traceback.StackSummary"><code class="xref py py-class docutils literal notranslate"><span class="pre">StackSummary</span></code></a> and <a class="reference internal" href="#traceback.FrameSummary" title="traceback.FrameSummary"><code class="xref py py-class docutils literal notranslate"><span class="pre">FrameSummary</span></code></a>. These offer both more254flexibility in the output generated and the ability to store the information255necessary for later formatting without holding references to actual exception256and traceback objects.</p></li>257</ul>258<div class="versionadded">259<p><span class="versionmodified added">Added in version 3.13: </span>Output is colorized by default and can be260<a class="reference internal" href="../using/cmdline.html#using-on-controlling-color"><span class="std std-ref">controlled using environment variables</span></a>.</p>261</div>262<section id="module-level-functions">263<h2>Module-Level Functions<a class="headerlink" href="#module-level-functions" title="Link to this heading">¶</a></h2>264<dl class="py function">265<dt class="sig sig-object py" id="traceback.print_tb">266<span class="sig-prename descclassname"><span class="pre">traceback.</span></span><span class="sig-name descname"><span class="pre">print_tb</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">tb</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">limit</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">file</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="#traceback.print_tb" title="Link to this definition">¶</a></dt>267<dd><p>Print up to <em>limit</em> stack trace entries from268<a class="reference internal" href="../reference/datamodel.html#traceback-objects"><span class="std std-ref">traceback object</span></a> <em>tb</em> (starting269from the caller’s frame) if <em>limit</em> is positive. Otherwise, print the last270<code class="docutils literal notranslate"><span class="pre">abs(limit)</span></code> entries. If <em>limit</em> is omitted or <code class="docutils literal notranslate"><span class="pre">None</span></code>, all entries are271printed. If <em>file</em> is omitted or <code class="docutils literal notranslate"><span class="pre">None</span></code>, the output goes to272<a class="reference internal" href="sys.html#sys.stderr" title="sys.stderr"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stderr</span></code></a>; otherwise it should be an open273<a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file</span></a> or <a class="reference internal" href="../glossary.html#term-file-like-object"><span class="xref std std-term">file-like object</span></a> to274receive the output.</p>275<div class="admonition note">276<p class="admonition-title">Note</p>277<p>The meaning of the <em>limit</em> parameter is different than the meaning278of <a class="reference internal" href="sys.html#sys.tracebacklimit" title="sys.tracebacklimit"><code class="xref py py-const docutils literal notranslate"><span class="pre">sys.tracebacklimit</span></code></a>. A negative <em>limit</em> value corresponds to279a positive value of <code class="xref py py-const docutils literal notranslate"><span class="pre">sys.tracebacklimit</span></code>, whereas the behaviour of280a positive <em>limit</em> value cannot be achieved with281<code class="xref py py-const docutils literal notranslate"><span class="pre">sys.tracebacklimit</span></code>.</p>282</div>283<div class="versionchanged">284<p><span class="versionmodified changed">Changed in version 3.5: </span>Added negative <em>limit</em> support.</p>285</div>286</dd></dl>287 288<dl class="py function">289<dt class="sig sig-object py" id="traceback.print_exception">290<span class="sig-prename descclassname"><span class="pre">traceback.</span></span><span class="sig-name descname"><span class="pre">print_exception</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">exc</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">/</span></span></em>, <span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">value</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">tb</span></span></em>, <span class="optional">]</span><em class="sig-param"><span class="n"><span class="pre">limit=None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">file=None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">chain=True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#traceback.print_exception" title="Link to this definition">¶</a></dt>291<dd><p>Print exception information and stack trace entries from292<a class="reference internal" href="../reference/datamodel.html#traceback-objects"><span class="std std-ref">traceback object</span></a>293<em>tb</em> to <em>file</em>. This differs from <a class="reference internal" href="#traceback.print_tb" title="traceback.print_tb"><code class="xref py py-func docutils literal notranslate"><span class="pre">print_tb()</span></code></a> in the following294ways:</p>295<ul class="simple">296<li><p>if <em>tb</em> is not <code class="docutils literal notranslate"><span class="pre">None</span></code>, it prints a header <code class="docutils literal notranslate"><span class="pre">Traceback</span> <span class="pre">(most</span> <span class="pre">recent</span>297<span class="pre">call</span> <span class="pre">last):</span></code></p></li>298<li><p>it prints the exception type and <em>value</em> after the stack trace</p></li>299</ul>300<ul class="simple" id="index-1">301<li><p>if <em>type(value)</em> is <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> and <em>value</em> has the appropriate302format, it prints the line where the syntax error occurred with a caret303indicating the approximate position of the error.</p></li>304</ul>305<p>Since Python 3.10, instead of passing <em>value</em> and <em>tb</em>, an exception object306can be passed as the first argument. If <em>value</em> and <em>tb</em> are provided, the307first argument is ignored in order to provide backwards compatibility.</p>308<p>The optional <em>limit</em> argument has the same meaning as for <a class="reference internal" href="#traceback.print_tb" title="traceback.print_tb"><code class="xref py py-func docutils literal notranslate"><span class="pre">print_tb()</span></code></a>.309If <em>chain</em> is true (the default), then chained exceptions (the310<a class="reference internal" href="exceptions.html#BaseException.__cause__" title="BaseException.__cause__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__cause__</span></code></a> or <a class="reference internal" href="exceptions.html#BaseException.__context__" title="BaseException.__context__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__context__</span></code></a>311attributes of the exception) will be312printed as well, like the interpreter itself does when printing an unhandled313exception.</p>314<div class="versionchanged">315<p><span class="versionmodified changed">Changed in version 3.5: </span>The <em>etype</em> argument is ignored and inferred from the type of <em>value</em>.</p>316</div>317<div class="versionchanged">318<p><span class="versionmodified changed">Changed in version 3.10: </span>The <em>etype</em> parameter has been renamed to <em>exc</em> and is now319positional-only.</p>320</div>321</dd></dl>322 323<dl class="py function">324<dt class="sig sig-object py" id="traceback.print_exc">325<span class="sig-prename descclassname"><span class="pre">traceback.</span></span><span class="sig-name descname"><span class="pre">print_exc</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">limit</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">file</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">chain</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#traceback.print_exc" title="Link to this definition">¶</a></dt>326<dd><p>This is a shorthand for <code class="docutils literal notranslate"><span class="pre">print_exception(sys.exception(),</span> <span class="pre">limit=limit,</span> <span class="pre">file=file,</span>327<span class="pre">chain=chain)</span></code>.</p>328</dd></dl>329 330<dl class="py function">331<dt class="sig sig-object py" id="traceback.print_last">332<span class="sig-prename descclassname"><span class="pre">traceback.</span></span><span class="sig-name descname"><span class="pre">print_last</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">limit</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">file</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">chain</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#traceback.print_last" title="Link to this definition">¶</a></dt>333<dd><p>This is a shorthand for <code class="docutils literal notranslate"><span class="pre">print_exception(sys.last_exc,</span> <span class="pre">limit=limit,</span> <span class="pre">file=file,</span>334<span class="pre">chain=chain)</span></code>. In general it will work only after an exception has reached335an interactive prompt (see <a class="reference internal" href="sys.html#sys.last_exc" title="sys.last_exc"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.last_exc</span></code></a>).</p>336</dd></dl>337 338<dl class="py function">339<dt class="sig sig-object py" id="traceback.print_stack">340<span class="sig-prename descclassname"><span class="pre">traceback.</span></span><span class="sig-name descname"><span class="pre">print_stack</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">f</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">limit</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">file</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="#traceback.print_stack" title="Link to this definition">¶</a></dt>341<dd><p>Print up to <em>limit</em> stack trace entries (starting from the invocation342point) if <em>limit</em> is positive. Otherwise, print the last <code class="docutils literal notranslate"><span class="pre">abs(limit)</span></code>343entries. If <em>limit</em> is omitted or <code class="docutils literal notranslate"><span class="pre">None</span></code>, all entries are printed.344The optional <em>f</em> argument can be used to specify an alternate345<a class="reference internal" href="../reference/datamodel.html#frame-objects"><span class="std std-ref">stack frame</span></a>346to start. The optional <em>file</em> argument has the same meaning as for347<a class="reference internal" href="#traceback.print_tb" title="traceback.print_tb"><code class="xref py py-func docutils literal notranslate"><span class="pre">print_tb()</span></code></a>.</p>348<div class="versionchanged">349<p><span class="versionmodified changed">Changed in version 3.5: </span>Added negative <em>limit</em> support.</p>350</div>351</dd></dl>352 353<dl class="py function">354<dt class="sig sig-object py" id="traceback.extract_tb">355<span class="sig-prename descclassname"><span class="pre">traceback.</span></span><span class="sig-name descname"><span class="pre">extract_tb</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">tb</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">limit</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="#traceback.extract_tb" title="Link to this definition">¶</a></dt>356<dd><p>Return a <a class="reference internal" href="#traceback.StackSummary" title="traceback.StackSummary"><code class="xref py py-class docutils literal notranslate"><span class="pre">StackSummary</span></code></a> object representing a list of “pre-processed”357stack trace entries extracted from the358<a class="reference internal" href="../reference/datamodel.html#traceback-objects"><span class="std std-ref">traceback object</span></a> <em>tb</em>. It is useful359for alternate formatting of stack traces. The optional <em>limit</em> argument has360the same meaning as for <a class="reference internal" href="#traceback.print_tb" title="traceback.print_tb"><code class="xref py py-func docutils literal notranslate"><span class="pre">print_tb()</span></code></a>. A “pre-processed” stack trace361entry is a <a class="reference internal" href="#traceback.FrameSummary" title="traceback.FrameSummary"><code class="xref py py-class docutils literal notranslate"><span class="pre">FrameSummary</span></code></a> object containing attributes362<a class="reference internal" href="#traceback.FrameSummary.filename" title="traceback.FrameSummary.filename"><code class="xref py py-attr docutils literal notranslate"><span class="pre">filename</span></code></a>, <a class="reference internal" href="#traceback.FrameSummary.lineno" title="traceback.FrameSummary.lineno"><code class="xref py py-attr docutils literal notranslate"><span class="pre">lineno</span></code></a>,363<a class="reference internal" href="#traceback.FrameSummary.name" title="traceback.FrameSummary.name"><code class="xref py py-attr docutils literal notranslate"><span class="pre">name</span></code></a>, and <a class="reference internal" href="#traceback.FrameSummary.line" title="traceback.FrameSummary.line"><code class="xref py py-attr docutils literal notranslate"><span class="pre">line</span></code></a> representing the364information that is usually printed for a stack trace.</p>365</dd></dl>366 367<dl class="py function">368<dt class="sig sig-object py" id="traceback.extract_stack">369<span class="sig-prename descclassname"><span class="pre">traceback.</span></span><span class="sig-name descname"><span class="pre">extract_stack</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">f</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">limit</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="#traceback.extract_stack" title="Link to this definition">¶</a></dt>370<dd><p>Extract the raw traceback from the current371<a class="reference internal" href="../reference/datamodel.html#frame-objects"><span class="std std-ref">stack frame</span></a>. The return value has372the same format as for <a class="reference internal" href="#traceback.extract_tb" title="traceback.extract_tb"><code class="xref py py-func docutils literal notranslate"><span class="pre">extract_tb()</span></code></a>. The optional <em>f</em> and <em>limit</em>373arguments have the same meaning as for <a class="reference internal" href="#traceback.print_stack" title="traceback.print_stack"><code class="xref py py-func docutils literal notranslate"><span class="pre">print_stack()</span></code></a>.</p>374</dd></dl>375 376<dl class="py function">377<dt class="sig sig-object py" id="traceback.print_list">378<span class="sig-prename descclassname"><span class="pre">traceback.</span></span><span class="sig-name descname"><span class="pre">print_list</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">extracted_list</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">file</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="#traceback.print_list" title="Link to this definition">¶</a></dt>379<dd><p>Print the list of tuples as returned by <a class="reference internal" href="#traceback.extract_tb" title="traceback.extract_tb"><code class="xref py py-func docutils literal notranslate"><span class="pre">extract_tb()</span></code></a> or380<a class="reference internal" href="#traceback.extract_stack" title="traceback.extract_stack"><code class="xref py py-func docutils literal notranslate"><span class="pre">extract_stack()</span></code></a> as a formatted stack trace to the given file.381If <em>file</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, the output is written to <a class="reference internal" href="sys.html#sys.stderr" title="sys.stderr"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stderr</span></code></a>.</p>382</dd></dl>383 384<dl class="py function">385<dt class="sig sig-object py" id="traceback.format_list">386<span class="sig-prename descclassname"><span class="pre">traceback.</span></span><span class="sig-name descname"><span class="pre">format_list</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">extracted_list</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#traceback.format_list" title="Link to this definition">¶</a></dt>387<dd><p>Given a list of tuples or <a class="reference internal" href="#traceback.FrameSummary" title="traceback.FrameSummary"><code class="xref py py-class docutils literal notranslate"><span class="pre">FrameSummary</span></code></a> objects as returned by388<a class="reference internal" href="#traceback.extract_tb" title="traceback.extract_tb"><code class="xref py py-func docutils literal notranslate"><span class="pre">extract_tb()</span></code></a> or <a class="reference internal" href="#traceback.extract_stack" title="traceback.extract_stack"><code class="xref py py-func docutils literal notranslate"><span class="pre">extract_stack()</span></code></a>, return a list of strings ready389for printing. Each string in the resulting list corresponds to the item with390the same index in the argument list. Each string ends in a newline; the391strings may contain internal newlines as well, for those items whose source392text line is not <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>393</dd></dl>394 395<dl class="py function">396<dt class="sig sig-object py" id="traceback.format_exception_only">397<span class="sig-prename descclassname"><span class="pre">traceback.</span></span><span class="sig-name descname"><span class="pre">format_exception_only</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">exc</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">/</span></span></em>, <span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">value</span></span></em>, <span class="optional">]</span><em class="sig-param"><span class="n"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">show_group=False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#traceback.format_exception_only" title="Link to this definition">¶</a></dt>398<dd><p>Format the exception part of a traceback using an exception value such as399given by <a class="reference internal" href="sys.html#sys.last_value" title="sys.last_value"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.last_value</span></code></a>. The return value is a list of strings, each400ending in a newline. The list contains the exception’s message, which is401normally a single string; however, for <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> exceptions, it402contains several lines that (when printed) display detailed information403about where the syntax error occurred. Following the message, the list404contains the exception’s <a class="reference internal" href="exceptions.html#BaseException.__notes__" title="BaseException.__notes__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">notes</span></code></a>.</p>405<p>Since Python 3.10, instead of passing <em>value</em>, an exception object406can be passed as the first argument. If <em>value</em> is provided, the first407argument is ignored in order to provide backwards compatibility.</p>408<p>When <em>show_group</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, and the exception is an instance of409<a class="reference internal" href="exceptions.html#BaseExceptionGroup" title="BaseExceptionGroup"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BaseExceptionGroup</span></code></a>, the nested exceptions are included as410well, recursively, with indentation relative to their nesting depth.</p>411<div class="versionchanged">412<p><span class="versionmodified changed">Changed in version 3.10: </span>The <em>etype</em> parameter has been renamed to <em>exc</em> and is now413positional-only.</p>414</div>415<div class="versionchanged">416<p><span class="versionmodified changed">Changed in version 3.11: </span>The returned list now includes any417<a class="reference internal" href="exceptions.html#BaseException.__notes__" title="BaseException.__notes__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">notes</span></code></a> attached to the exception.</p>418</div>419<div class="versionchanged">420<p><span class="versionmodified changed">Changed in version 3.13: </span><em>show_group</em> parameter was added.</p>421</div>422</dd></dl>423 424<dl class="py function">425<dt class="sig sig-object py" id="traceback.format_exception">426<span class="sig-prename descclassname"><span class="pre">traceback.</span></span><span class="sig-name descname"><span class="pre">format_exception</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">exc</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">/</span></span></em>, <span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">value</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">tb</span></span></em>, <span class="optional">]</span><em class="sig-param"><span class="n"><span class="pre">limit=None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">chain=True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#traceback.format_exception" title="Link to this definition">¶</a></dt>427<dd><p>Format a stack trace and the exception information. The arguments have the428same meaning as the corresponding arguments to <a class="reference internal" href="#traceback.print_exception" title="traceback.print_exception"><code class="xref py py-func docutils literal notranslate"><span class="pre">print_exception()</span></code></a>. The429return value is a list of strings, each ending in a newline and some430containing internal newlines. When these lines are concatenated and printed,431exactly the same text is printed as does <code class="xref py py-func docutils literal notranslate"><span class="pre">print_exception()</span></code>.</p>432<div class="versionchanged">433<p><span class="versionmodified changed">Changed in version 3.5: </span>The <em>etype</em> argument is ignored and inferred from the type of <em>value</em>.</p>434</div>435<div class="versionchanged">436<p><span class="versionmodified changed">Changed in version 3.10: </span>This function’s behavior and signature were modified to match437<a class="reference internal" href="#traceback.print_exception" title="traceback.print_exception"><code class="xref py py-func docutils literal notranslate"><span class="pre">print_exception()</span></code></a>.</p>438</div>439</dd></dl>440 441<dl class="py function">442<dt class="sig sig-object py" id="traceback.format_exc">443<span class="sig-prename descclassname"><span class="pre">traceback.</span></span><span class="sig-name descname"><span class="pre">format_exc</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">limit</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">chain</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#traceback.format_exc" title="Link to this definition">¶</a></dt>444<dd><p>This is like <code class="docutils literal notranslate"><span class="pre">print_exc(limit)</span></code> but returns a string instead of printing to445a file.</p>446</dd></dl>447 448<dl class="py function">449<dt class="sig sig-object py" id="traceback.format_tb">450<span class="sig-prename descclassname"><span class="pre">traceback.</span></span><span class="sig-name descname"><span class="pre">format_tb</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">tb</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">limit</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="#traceback.format_tb" title="Link to this definition">¶</a></dt>451<dd><p>A shorthand for <code class="docutils literal notranslate"><span class="pre">format_list(extract_tb(tb,</span> <span class="pre">limit))</span></code>.</p>452</dd></dl>453 454<dl class="py function">455<dt class="sig sig-object py" id="traceback.format_stack">456<span class="sig-prename descclassname"><span class="pre">traceback.</span></span><span class="sig-name descname"><span class="pre">format_stack</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">f</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">limit</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="#traceback.format_stack" title="Link to this definition">¶</a></dt>457<dd><p>A shorthand for <code class="docutils literal notranslate"><span class="pre">format_list(extract_stack(f,</span> <span class="pre">limit))</span></code>.</p>458</dd></dl>459 460<dl class="py function">461<dt class="sig sig-object py" id="traceback.clear_frames">462<span class="sig-prename descclassname"><span class="pre">traceback.</span></span><span class="sig-name descname"><span class="pre">clear_frames</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">tb</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#traceback.clear_frames" title="Link to this definition">¶</a></dt>463<dd><p>Clears the local variables of all the stack frames in a464<a class="reference internal" href="../reference/datamodel.html#traceback-objects"><span class="std std-ref">traceback</span></a> <em>tb</em>465by calling the <a class="reference internal" href="../reference/datamodel.html#frame.clear" title="frame.clear"><code class="xref py py-meth docutils literal notranslate"><span class="pre">clear()</span></code></a> method of each466<a class="reference internal" href="../reference/datamodel.html#frame-objects"><span class="std std-ref">frame object</span></a>.</p>467<div class="versionadded">468<p><span class="versionmodified added">Added in version 3.4.</span></p>469</div>470</dd></dl>471 472<dl class="py function">473<dt class="sig sig-object py" id="traceback.walk_stack">474<span class="sig-prename descclassname"><span class="pre">traceback.</span></span><span class="sig-name descname"><span class="pre">walk_stack</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">f</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#traceback.walk_stack" title="Link to this definition">¶</a></dt>475<dd><p>Walk a stack following <a class="reference internal" href="../reference/datamodel.html#frame.f_back" title="frame.f_back"><code class="xref py py-attr docutils literal notranslate"><span class="pre">f.f_back</span></code></a> from the given frame,476yielding the frame477and line number for each frame. If <em>f</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, the current stack is478used. This helper is used with <a class="reference internal" href="#traceback.StackSummary.extract" title="traceback.StackSummary.extract"><code class="xref py py-meth docutils literal notranslate"><span class="pre">StackSummary.extract()</span></code></a>.</p>479<div class="versionadded">480<p><span class="versionmodified added">Added in version 3.5.</span></p>481</div>482<div class="versionchanged">483<p><span class="versionmodified changed">Changed in version 3.14: </span>This function previously returned a generator that would walk the stack484when first iterated over. The generator returned now is the state of the485stack when <code class="docutils literal notranslate"><span class="pre">walk_stack</span></code> is called.</p>486</div>487</dd></dl>488 489<dl class="py function">490<dt class="sig sig-object py" id="traceback.walk_tb">491<span class="sig-prename descclassname"><span class="pre">traceback.</span></span><span class="sig-name descname"><span class="pre">walk_tb</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">tb</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#traceback.walk_tb" title="Link to this definition">¶</a></dt>492<dd><p>Walk a traceback following <a class="reference internal" href="../reference/datamodel.html#traceback.tb_next" title="traceback.tb_next"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tb_next</span></code></a> yielding the frame and493line number494for each frame. This helper is used with <a class="reference internal" href="#traceback.StackSummary.extract" title="traceback.StackSummary.extract"><code class="xref py py-meth docutils literal notranslate"><span class="pre">StackSummary.extract()</span></code></a>.</p>495<div class="versionadded">496<p><span class="versionmodified added">Added in version 3.5.</span></p>497</div>498</dd></dl>499 500</section>501<section id="tracebackexception-objects">502<h2><code class="xref py py-class docutils literal notranslate"><span class="pre">TracebackException</span></code> Objects<a class="headerlink" href="#tracebackexception-objects" title="Link to this heading">¶</a></h2>503<div class="versionadded">504<p><span class="versionmodified added">Added in version 3.5.</span></p>505</div>506<p><code class="xref py py-class docutils literal notranslate"><span class="pre">TracebackException</span></code> objects are created from actual exceptions to507capture data for later printing. They offer a more lightweight method of508storing this information by avoiding holding references to509<a class="reference internal" href="../reference/datamodel.html#traceback-objects"><span class="std std-ref">traceback</span></a> and <a class="reference internal" href="../reference/datamodel.html#frame-objects"><span class="std std-ref">frame</span></a> objects.510In addition, they expose more options to configure the output compared to511the module-level functions described above.</p>512<dl class="py class">513<dt class="sig sig-object py" id="traceback.TracebackException">514<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">traceback.</span></span><span class="sig-name descname"><span class="pre">TracebackException</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">exc_type</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">exc_value</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">exc_traceback</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">limit</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">lookup_lines</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">capture_locals</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">compact</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">max_group_width</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">15</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">max_group_depth</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">10</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#traceback.TracebackException" title="Link to this definition">¶</a></dt>515<dd><p>Capture an exception for later rendering. The meaning of <em>limit</em>,516<em>lookup_lines</em> and <em>capture_locals</em> are as for the <a class="reference internal" href="#traceback.StackSummary" title="traceback.StackSummary"><code class="xref py py-class docutils literal notranslate"><span class="pre">StackSummary</span></code></a>517class.</p>518<p>If <em>compact</em> is true, only data that is required by519<code class="xref py py-class docutils literal notranslate"><span class="pre">TracebackException</span></code>’s <a class="reference internal" href="functions.html#format" title="format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">format()</span></code></a> method520is saved in the class attributes. In particular, the521<a class="reference internal" href="#traceback.TracebackException.__context__" title="traceback.TracebackException.__context__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__context__</span></code></a> field is calculated only if <a class="reference internal" href="#traceback.TracebackException.__cause__" title="traceback.TracebackException.__cause__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__cause__</span></code></a> is522<code class="docutils literal notranslate"><span class="pre">None</span></code> and <a class="reference internal" href="#traceback.TracebackException.__suppress_context__" title="traceback.TracebackException.__suppress_context__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__suppress_context__</span></code></a> is false.</p>523<p>Note that when locals are captured, they are also shown in the traceback.</p>524<p><em>max_group_width</em> and <em>max_group_depth</em> control the formatting of exception525groups (see <a class="reference internal" href="exceptions.html#BaseExceptionGroup" title="BaseExceptionGroup"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BaseExceptionGroup</span></code></a>). The depth refers to the nesting526level of the group, and the width refers to the size of a single exception527group’s exceptions array. The formatted output is truncated when either528limit is exceeded.</p>529<div class="versionchanged">530<p><span class="versionmodified changed">Changed in version 3.10: </span>Added the <em>compact</em> parameter.</p>531</div>532<div class="versionchanged">533<p><span class="versionmodified changed">Changed in version 3.11: </span>Added the <em>max_group_width</em> and <em>max_group_depth</em> parameters.</p>534</div>535<dl class="py attribute">536<dt class="sig sig-object py" id="traceback.TracebackException.__cause__">537<span class="sig-name descname"><span class="pre">__cause__</span></span><a class="headerlink" href="#traceback.TracebackException.__cause__" title="Link to this definition">¶</a></dt>538<dd><p>A <code class="xref py py-class docutils literal notranslate"><span class="pre">TracebackException</span></code> of the original539<a class="reference internal" href="exceptions.html#BaseException.__cause__" title="BaseException.__cause__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__cause__</span></code></a>.</p>540</dd></dl>541 542<dl class="py attribute">543<dt class="sig sig-object py" id="traceback.TracebackException.__context__">544<span class="sig-name descname"><span class="pre">__context__</span></span><a class="headerlink" href="#traceback.TracebackException.__context__" title="Link to this definition">¶</a></dt>545<dd><p>A <code class="xref py py-class docutils literal notranslate"><span class="pre">TracebackException</span></code> of the original546<a class="reference internal" href="exceptions.html#BaseException.__context__" title="BaseException.__context__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__context__</span></code></a>.</p>547</dd></dl>548 549<dl class="py attribute">550<dt class="sig sig-object py" id="traceback.TracebackException.exceptions">551<span class="sig-name descname"><span class="pre">exceptions</span></span><a class="headerlink" href="#traceback.TracebackException.exceptions" title="Link to this definition">¶</a></dt>552<dd><p>If <code class="docutils literal notranslate"><span class="pre">self</span></code> represents an <a class="reference internal" href="exceptions.html#ExceptionGroup" title="ExceptionGroup"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ExceptionGroup</span></code></a>, this field holds a list of553<code class="xref py py-class docutils literal notranslate"><span class="pre">TracebackException</span></code> instances representing the nested exceptions.554Otherwise it is <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>555<div class="versionadded">556<p><span class="versionmodified added">Added in version 3.11.</span></p>557</div>558</dd></dl>559 560<dl class="py attribute">561<dt class="sig sig-object py" id="traceback.TracebackException.__suppress_context__">562<span class="sig-name descname"><span class="pre">__suppress_context__</span></span><a class="headerlink" href="#traceback.TracebackException.__suppress_context__" title="Link to this definition">¶</a></dt>563<dd><p>The <a class="reference internal" href="exceptions.html#BaseException.__suppress_context__" title="BaseException.__suppress_context__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__suppress_context__</span></code></a> value from the original564exception.</p>565</dd></dl>566 567<dl class="py attribute">568<dt class="sig sig-object py" id="traceback.TracebackException.__notes__">569<span class="sig-name descname"><span class="pre">__notes__</span></span><a class="headerlink" href="#traceback.TracebackException.__notes__" title="Link to this definition">¶</a></dt>570<dd><p>The <a class="reference internal" href="exceptions.html#BaseException.__notes__" title="BaseException.__notes__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__notes__</span></code></a> value from the original exception,571or <code class="docutils literal notranslate"><span class="pre">None</span></code>572if the exception does not have any notes. If it is not <code class="docutils literal notranslate"><span class="pre">None</span></code>573is it formatted in the traceback after the exception string.</p>574<div class="versionadded">575<p><span class="versionmodified added">Added in version 3.11.</span></p>576</div>577</dd></dl>578 579<dl class="py attribute">580<dt class="sig sig-object py" id="traceback.TracebackException.stack">581<span class="sig-name descname"><span class="pre">stack</span></span><a class="headerlink" href="#traceback.TracebackException.stack" title="Link to this definition">¶</a></dt>582<dd><p>A <a class="reference internal" href="#traceback.StackSummary" title="traceback.StackSummary"><code class="xref py py-class docutils literal notranslate"><span class="pre">StackSummary</span></code></a> representing the traceback.</p>583</dd></dl>584 585<dl class="py attribute">586<dt class="sig sig-object py" id="traceback.TracebackException.exc_type">587<span class="sig-name descname"><span class="pre">exc_type</span></span><a class="headerlink" href="#traceback.TracebackException.exc_type" title="Link to this definition">¶</a></dt>588<dd><p>The class of the original traceback.</p>589<div class="deprecated">590<p><span class="versionmodified deprecated">Deprecated since version 3.13.</span></p>591</div>592</dd></dl>593 594<dl class="py attribute">595<dt class="sig sig-object py" id="traceback.TracebackException.exc_type_str">596<span class="sig-name descname"><span class="pre">exc_type_str</span></span><a class="headerlink" href="#traceback.TracebackException.exc_type_str" title="Link to this definition">¶</a></dt>597<dd><p>String display of the class of the original exception.</p>598<div class="versionadded">599<p><span class="versionmodified added">Added in version 3.13.</span></p>600</div>601</dd></dl>602 603<dl class="py attribute">604<dt class="sig sig-object py" id="traceback.TracebackException.filename">605<span class="sig-name descname"><span class="pre">filename</span></span><a class="headerlink" href="#traceback.TracebackException.filename" title="Link to this definition">¶</a></dt>606<dd><p>For syntax errors - the file name where the error occurred.</p>607</dd></dl>608 609<dl class="py attribute">610<dt class="sig sig-object py" id="traceback.TracebackException.lineno">611<span class="sig-name descname"><span class="pre">lineno</span></span><a class="headerlink" href="#traceback.TracebackException.lineno" title="Link to this definition">¶</a></dt>612<dd><p>For syntax errors - the line number where the error occurred.</p>613</dd></dl>614 615<dl class="py attribute">616<dt class="sig sig-object py" id="traceback.TracebackException.end_lineno">617<span class="sig-name descname"><span class="pre">end_lineno</span></span><a class="headerlink" href="#traceback.TracebackException.end_lineno" title="Link to this definition">¶</a></dt>618<dd><p>For syntax errors - the end line number where the error occurred.619Can be <code class="docutils literal notranslate"><span class="pre">None</span></code> if not present.</p>620<div class="versionadded">621<p><span class="versionmodified added">Added in version 3.10.</span></p>622</div>623</dd></dl>624 625<dl class="py attribute">626<dt class="sig sig-object py" id="traceback.TracebackException.text">627<span class="sig-name descname"><span class="pre">text</span></span><a class="headerlink" href="#traceback.TracebackException.text" title="Link to this definition">¶</a></dt>628<dd><p>For syntax errors - the text where the error occurred.</p>629</dd></dl>630 631<dl class="py attribute">632<dt class="sig sig-object py" id="traceback.TracebackException.offset">633<span class="sig-name descname"><span class="pre">offset</span></span><a class="headerlink" href="#traceback.TracebackException.offset" title="Link to this definition">¶</a></dt>634<dd><p>For syntax errors - the offset into the text where the error occurred.</p>635</dd></dl>636 637<dl class="py attribute">638<dt class="sig sig-object py" id="traceback.TracebackException.end_offset">639<span class="sig-name descname"><span class="pre">end_offset</span></span><a class="headerlink" href="#traceback.TracebackException.end_offset" title="Link to this definition">¶</a></dt>640<dd><p>For syntax errors - the end offset into the text where the error occurred.641Can be <code class="docutils literal notranslate"><span class="pre">None</span></code> if not present.</p>642<div class="versionadded">643<p><span class="versionmodified added">Added in version 3.10.</span></p>644</div>645</dd></dl>646 647<dl class="py attribute">648<dt class="sig sig-object py" id="traceback.TracebackException.msg">649<span class="sig-name descname"><span class="pre">msg</span></span><a class="headerlink" href="#traceback.TracebackException.msg" title="Link to this definition">¶</a></dt>650<dd><p>For syntax errors - the compiler error message.</p>651</dd></dl>652 653<dl class="py method">654<dt class="sig sig-object py" id="traceback.TracebackException.from_exception">655<em class="property"><span class="k"><span class="pre">classmethod</span></span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">from_exception</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">exc</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">limit</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">lookup_lines</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">capture_locals</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#traceback.TracebackException.from_exception" title="Link to this definition">¶</a></dt>656<dd><p>Capture an exception for later rendering. <em>limit</em>, <em>lookup_lines</em> and657<em>capture_locals</em> are as for the <a class="reference internal" href="#traceback.StackSummary" title="traceback.StackSummary"><code class="xref py py-class docutils literal notranslate"><span class="pre">StackSummary</span></code></a> class.</p>658<p>Note that when locals are captured, they are also shown in the traceback.</p>659</dd></dl>660 661<dl class="py method">662<dt class="sig sig-object py" id="traceback.TracebackException.print">663<span class="sig-name descname"><span class="pre">print</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">file</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">chain</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#traceback.TracebackException.print" title="Link to this definition">¶</a></dt>664<dd><p>Print to <em>file</em> (default <code class="docutils literal notranslate"><span class="pre">sys.stderr</span></code>) the exception information returned by665<a class="reference internal" href="functions.html#format" title="format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">format()</span></code></a>.</p>666<div class="versionadded">667<p><span class="versionmodified added">Added in version 3.11.</span></p>668</div>669</dd></dl>670 671<dl class="py method">672<dt class="sig sig-object py" id="traceback.TracebackException.format">673<span class="sig-name descname"><span class="pre">format</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">chain</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#traceback.TracebackException.format" title="Link to this definition">¶</a></dt>674<dd><p>Format the exception.</p>675<p>If <em>chain</em> is not <code class="docutils literal notranslate"><span class="pre">True</span></code>, <a class="reference internal" href="#traceback.TracebackException.__cause__" title="traceback.TracebackException.__cause__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__cause__</span></code></a> and <a class="reference internal" href="#traceback.TracebackException.__context__" title="traceback.TracebackException.__context__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__context__</span></code></a>676will not be formatted.</p>677<p>The return value is a generator of strings, each ending in a newline and678some containing internal newlines. <a class="reference internal" href="#traceback.print_exception" title="traceback.print_exception"><code class="xref py py-func docutils literal notranslate"><span class="pre">print_exception()</span></code></a>679is a wrapper around this method which just prints the lines to a file.</p>680</dd></dl>681 682<dl class="py method">683<dt class="sig sig-object py" id="traceback.TracebackException.format_exception_only">684<span class="sig-name descname"><span class="pre">format_exception_only</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">show_group</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#traceback.TracebackException.format_exception_only" title="Link to this definition">¶</a></dt>685<dd><p>Format the exception part of the traceback.</p>686<p>The return value is a generator of strings, each ending in a newline.</p>687<p>When <em>show_group</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code>, the generator emits the exception’s688message followed by its notes (if it has any). The exception message689is normally a single string; however, for <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> exceptions,690it consists of several lines that (when printed) display detailed691information about where the syntax error occurred.</p>692<p>When <em>show_group</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, and the exception is an instance of693<a class="reference internal" href="exceptions.html#BaseExceptionGroup" title="BaseExceptionGroup"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BaseExceptionGroup</span></code></a>, the nested exceptions are included as694well, recursively, with indentation relative to their nesting depth.</p>695<div class="versionchanged">696<p><span class="versionmodified changed">Changed in version 3.11: </span>The exception’s <a class="reference internal" href="exceptions.html#BaseException.__notes__" title="BaseException.__notes__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">notes</span></code></a> are now697included in the output.</p>698</div>699<div class="versionchanged">700<p><span class="versionmodified changed">Changed in version 3.13: </span>Added the <em>show_group</em> parameter.</p>701</div>702</dd></dl>703 704</dd></dl>705 706</section>707<section id="stacksummary-objects">708<h2><code class="xref py py-class docutils literal notranslate"><span class="pre">StackSummary</span></code> Objects<a class="headerlink" href="#stacksummary-objects" title="Link to this heading">¶</a></h2>709<div class="versionadded">710<p><span class="versionmodified added">Added in version 3.5.</span></p>711</div>712<p><code class="xref py py-class docutils literal notranslate"><span class="pre">StackSummary</span></code> objects represent a call stack ready for formatting.</p>713<dl class="py class">714<dt class="sig sig-object py" id="traceback.StackSummary">715<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">traceback.</span></span><span class="sig-name descname"><span class="pre">StackSummary</span></span><a class="headerlink" href="#traceback.StackSummary" title="Link to this definition">¶</a></dt>716<dd><dl class="py method">717<dt class="sig sig-object py" id="traceback.StackSummary.extract">718<em class="property"><span class="k"><span class="pre">classmethod</span></span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">extract</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">frame_gen</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">limit</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">lookup_lines</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">capture_locals</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#traceback.StackSummary.extract" title="Link to this definition">¶</a></dt>719<dd><p>Construct a <code class="xref py py-class docutils literal notranslate"><span class="pre">StackSummary</span></code> object from a frame generator (such as720is returned by <a class="reference internal" href="#traceback.walk_stack" title="traceback.walk_stack"><code class="xref py py-func docutils literal notranslate"><span class="pre">walk_stack()</span></code></a> or721<a class="reference internal" href="#traceback.walk_tb" title="traceback.walk_tb"><code class="xref py py-func docutils literal notranslate"><span class="pre">walk_tb()</span></code></a>).</p>722<p>If <em>limit</em> is supplied, only this many frames are taken from <em>frame_gen</em>.723If <em>lookup_lines</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code>, the returned <a class="reference internal" href="#traceback.FrameSummary" title="traceback.FrameSummary"><code class="xref py py-class docutils literal notranslate"><span class="pre">FrameSummary</span></code></a>724objects will not have read their lines in yet, making the cost of725creating the <code class="xref py py-class docutils literal notranslate"><span class="pre">StackSummary</span></code> cheaper (which may be valuable if it726may not actually get formatted). If <em>capture_locals</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code> the727local variables in each <code class="xref py py-class docutils literal notranslate"><span class="pre">FrameSummary</span></code> are captured as object728representations.</p>729<div class="versionchanged">730<p><span class="versionmodified changed">Changed in version 3.12: </span>Exceptions raised from <a class="reference internal" href="functions.html#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a> on a local variable (when731<em>capture_locals</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>) are no longer propagated to the caller.</p>732</div>733</dd></dl>734 735<dl class="py method">736<dt class="sig sig-object py" id="traceback.StackSummary.from_list">737<em class="property"><span class="k"><span class="pre">classmethod</span></span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">from_list</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">a_list</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#traceback.StackSummary.from_list" title="Link to this definition">¶</a></dt>738<dd><p>Construct a <code class="xref py py-class docutils literal notranslate"><span class="pre">StackSummary</span></code> object from a supplied list of739<a class="reference internal" href="#traceback.FrameSummary" title="traceback.FrameSummary"><code class="xref py py-class docutils literal notranslate"><span class="pre">FrameSummary</span></code></a> objects or old-style list of tuples. Each tuple740should be a 4-tuple with <em>filename</em>, <em>lineno</em>, <em>name</em>, <em>line</em> as the741elements.</p>742</dd></dl>743 744<dl class="py method">745<dt class="sig sig-object py" id="traceback.StackSummary.format">746<span class="sig-name descname"><span class="pre">format</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#traceback.StackSummary.format" title="Link to this definition">¶</a></dt>747<dd><p>Returns a list of strings ready for printing. Each string in the748resulting list corresponds to a single <a class="reference internal" href="../reference/datamodel.html#frame-objects"><span class="std std-ref">frame</span></a> from749the stack.750Each string ends in a newline; the strings may contain internal751newlines as well, for those items with source text lines.</p>752<p>For long sequences of the same frame and line, the first few753repetitions are shown, followed by a summary line stating the exact754number of further repetitions.</p>755<div class="versionchanged">756<p><span class="versionmodified changed">Changed in version 3.6: </span>Long sequences of repeated frames are now abbreviated.</p>757</div>758</dd></dl>759 760<dl class="py method">761<dt class="sig sig-object py" id="traceback.StackSummary.format_frame_summary">762<span class="sig-name descname"><span class="pre">format_frame_summary</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">frame_summary</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#traceback.StackSummary.format_frame_summary" title="Link to this definition">¶</a></dt>763<dd><p>Returns a string for printing one of the <a class="reference internal" href="../reference/datamodel.html#frame-objects"><span class="std std-ref">frames</span></a>764involved in the stack.765This method is called for each <a class="reference internal" href="#traceback.FrameSummary" title="traceback.FrameSummary"><code class="xref py py-class docutils literal notranslate"><span class="pre">FrameSummary</span></code></a> object to be766printed by <a class="reference internal" href="#traceback.StackSummary.format" title="traceback.StackSummary.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">StackSummary.format()</span></code></a>. If it returns <code class="docutils literal notranslate"><span class="pre">None</span></code>, the767frame is omitted from the output.</p>768<div class="versionadded">769<p><span class="versionmodified added">Added in version 3.11.</span></p>770</div>771</dd></dl>772 773</dd></dl>774 775</section>776<section id="framesummary-objects">777<h2><code class="xref py py-class docutils literal notranslate"><span class="pre">FrameSummary</span></code> Objects<a class="headerlink" href="#framesummary-objects" title="Link to this heading">¶</a></h2>778<div class="versionadded">779<p><span class="versionmodified added">Added in version 3.5.</span></p>780</div>781<p>A <code class="xref py py-class docutils literal notranslate"><span class="pre">FrameSummary</span></code> object represents a single <a class="reference internal" href="../reference/datamodel.html#frame-objects"><span class="std std-ref">frame</span></a>782in a <a class="reference internal" href="../reference/datamodel.html#traceback-objects"><span class="std std-ref">traceback</span></a>.</p>783<dl class="py class">784<dt class="sig sig-object py" id="traceback.FrameSummary">785<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">traceback.</span></span><span class="sig-name descname"><span class="pre">FrameSummary</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">filename</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">lineno</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">lookup_line</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <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">line</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">end_lineno</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">colno</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">end_colno</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="#traceback.FrameSummary" title="Link to this definition">¶</a></dt>786<dd><p>Represents a single <a class="reference internal" href="../reference/datamodel.html#frame-objects"><span class="std std-ref">frame</span></a> in the787<a class="reference internal" href="../reference/datamodel.html#traceback-objects"><span class="std std-ref">traceback</span></a> or stack that is being formatted788or printed. It may optionally have a stringified version of the frame’s789locals included in it. If <em>lookup_line</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code>, the source code is not790looked up until the <code class="xref py py-class docutils literal notranslate"><span class="pre">FrameSummary</span></code> has the <a class="reference internal" href="#traceback.FrameSummary.line" title="traceback.FrameSummary.line"><code class="xref py py-attr docutils literal notranslate"><span class="pre">line</span></code></a>791attribute accessed (which also happens when casting it to a <a class="reference internal" href="stdtypes.html#tuple" title="tuple"><code class="xref py py-class docutils literal notranslate"><span class="pre">tuple</span></code></a>).792<code class="xref py py-attr docutils literal notranslate"><span class="pre">line</span></code> may be directly provided, and will prevent line793lookups happening at all. <em>locals</em> is an optional local variable794mapping, and if supplied the variable representations are stored in the795summary for later display.</p>796<p><code class="xref py py-class docutils literal notranslate"><span class="pre">FrameSummary</span></code> instances have the following attributes:</p>797<dl class="py attribute">798<dt class="sig sig-object py" id="traceback.FrameSummary.filename">799<span class="sig-name descname"><span class="pre">filename</span></span><a class="headerlink" href="#traceback.FrameSummary.filename" title="Link to this definition">¶</a></dt>800<dd><p>The filename of the source code for this frame. Equivalent to accessing801<a class="reference internal" href="../reference/datamodel.html#codeobject.co_filename" title="codeobject.co_filename"><code class="xref py py-attr docutils literal notranslate"><span class="pre">f.f_code.co_filename</span></code></a> on a802<a class="reference internal" href="../reference/datamodel.html#frame-objects"><span class="std std-ref">frame object</span></a> <em>f</em>.</p>803</dd></dl>804 805<dl class="py attribute">806<dt class="sig sig-object py" id="traceback.FrameSummary.lineno">807<span class="sig-name descname"><span class="pre">lineno</span></span><a class="headerlink" href="#traceback.FrameSummary.lineno" title="Link to this definition">¶</a></dt>808<dd><p>The line number of the source code for this frame.</p>809</dd></dl>810 811<dl class="py attribute">812<dt class="sig sig-object py" id="traceback.FrameSummary.name">813<span class="sig-name descname"><span class="pre">name</span></span><a class="headerlink" href="#traceback.FrameSummary.name" title="Link to this definition">¶</a></dt>814<dd><p>Equivalent to accessing <a class="reference internal" href="../reference/datamodel.html#codeobject.co_name" title="codeobject.co_name"><code class="xref py py-attr docutils literal notranslate"><span class="pre">f.f_code.co_name</span></code></a> on815a <a class="reference internal" href="../reference/datamodel.html#frame-objects"><span class="std std-ref">frame object</span></a> <em>f</em>.</p>816</dd></dl>817 818<dl class="py attribute">819<dt class="sig sig-object py" id="traceback.FrameSummary.line">820<span class="sig-name descname"><span class="pre">line</span></span><a class="headerlink" href="#traceback.FrameSummary.line" title="Link to this definition">¶</a></dt>821<dd><p>A string representing the source code for this frame, with leading and822trailing whitespace stripped.823If the source is not available, it is <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>824</dd></dl>825 826<dl class="py attribute">827<dt class="sig sig-object py" id="traceback.FrameSummary.end_lineno">828<span class="sig-name descname"><span class="pre">end_lineno</span></span><a class="headerlink" href="#traceback.FrameSummary.end_lineno" title="Link to this definition">¶</a></dt>829<dd><p>The last line number of the source code for this frame.830By default, it is set to <code class="docutils literal notranslate"><span class="pre">lineno</span></code> and indexation starts from 1.</p>831<div class="versionchanged">832<p><span class="versionmodified changed">Changed in version 3.13: </span>The default value changed from <code class="docutils literal notranslate"><span class="pre">None</span></code> to <code class="docutils literal notranslate"><span class="pre">lineno</span></code>.</p>833</div>834</dd></dl>835 836<dl class="py attribute">837<dt class="sig sig-object py" id="traceback.FrameSummary.colno">838<span class="sig-name descname"><span class="pre">colno</span></span><a class="headerlink" href="#traceback.FrameSummary.colno" title="Link to this definition">¶</a></dt>839<dd><p>The column number of the source code for this frame.840By default, it is <code class="docutils literal notranslate"><span class="pre">None</span></code> and indexation starts from 0.</p>841</dd></dl>842 843<dl class="py attribute">844<dt class="sig sig-object py" id="traceback.FrameSummary.end_colno">845<span class="sig-name descname"><span class="pre">end_colno</span></span><a class="headerlink" href="#traceback.FrameSummary.end_colno" title="Link to this definition">¶</a></dt>846<dd><p>The last column number of the source code for this frame.847By default, it is <code class="docutils literal notranslate"><span class="pre">None</span></code> and indexation starts from 0.</p>848</dd></dl>849 850</dd></dl>851 852</section>853<section id="examples-of-using-the-module-level-functions">854<span id="traceback-example"></span><h2>Examples of Using the Module-Level Functions<a class="headerlink" href="#examples-of-using-the-module-level-functions" title="Link to this heading">¶</a></h2>855<p>This simple example implements a basic read-eval-print loop, similar to (but856less useful than) the standard Python interactive interpreter loop. For a more857complete implementation of the interpreter loop, refer to the <a class="reference internal" href="code.html#module-code" title="code: Facilities to implement read-eval-print loops."><code class="xref py py-mod docutils literal notranslate"><span class="pre">code</span></code></a>858module.</p>859<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">sys</span><span class="o">,</span><span class="w"> </span><span class="nn">traceback</span>860 861<span class="k">def</span><span class="w"> </span><span class="nf">run_user_code</span><span class="p">(</span><span class="n">envdir</span><span class="p">):</span>862 <span class="n">source</span> <span class="o">=</span> <span class="nb">input</span><span class="p">(</span><span class="s2">">>> "</span><span class="p">)</span>863 <span class="k">try</span><span class="p">:</span>864 <span class="n">exec</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">envdir</span><span class="p">)</span>865 <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>866 <span class="nb">print</span><span class="p">(</span><span class="s2">"Exception in user code:"</span><span class="p">)</span>867 <span class="nb">print</span><span class="p">(</span><span class="s2">"-"</span><span class="o">*</span><span class="mi">60</span><span class="p">)</span>868 <span class="n">traceback</span><span class="o">.</span><span class="n">print_exc</span><span class="p">(</span><span class="n">file</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="p">)</span>869 <span class="nb">print</span><span class="p">(</span><span class="s2">"-"</span><span class="o">*</span><span class="mi">60</span><span class="p">)</span>870 871<span class="n">envdir</span> <span class="o">=</span> <span class="p">{}</span>872<span class="k">while</span> <span class="kc">True</span><span class="p">:</span>873 <span class="n">run_user_code</span><span class="p">(</span><span class="n">envdir</span><span class="p">)</span>874</pre></div>875</div>876<p>The following example demonstrates the different ways to print and format the877exception and traceback:</p>878<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">sys</span><span class="o">,</span><span class="w"> </span><span class="nn">traceback</span>879 880<span class="k">def</span><span class="w"> </span><span class="nf">lumberjack</span><span class="p">():</span>881 <span class="n">bright_side_of_life</span><span class="p">()</span>882 883<span class="k">def</span><span class="w"> </span><span class="nf">bright_side_of_life</span><span class="p">():</span>884 <span class="k">return</span> <span class="nb">tuple</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span>885 886<span class="k">try</span><span class="p">:</span>887 <span class="n">lumberjack</span><span class="p">()</span>888<span class="k">except</span> <span class="ne">IndexError</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>889 <span class="nb">print</span><span class="p">(</span><span class="s2">"*** print_tb:"</span><span class="p">)</span>890 <span class="n">traceback</span><span class="o">.</span><span class="n">print_tb</span><span class="p">(</span><span class="n">exc</span><span class="o">.</span><span class="n">__traceback__</span><span class="p">,</span> <span class="n">limit</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">file</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="p">)</span>891 <span class="nb">print</span><span class="p">(</span><span class="s2">"*** print_exception:"</span><span class="p">)</span>892 <span class="n">traceback</span><span class="o">.</span><span class="n">print_exception</span><span class="p">(</span><span class="n">exc</span><span class="p">,</span> <span class="n">limit</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">file</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="p">)</span>893 <span class="nb">print</span><span class="p">(</span><span class="s2">"*** print_exc:"</span><span class="p">)</span>894 <span class="n">traceback</span><span class="o">.</span><span class="n">print_exc</span><span class="p">(</span><span class="n">limit</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">file</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="p">)</span>895 <span class="nb">print</span><span class="p">(</span><span class="s2">"*** format_exc, first and last line:"</span><span class="p">)</span>896 <span class="n">formatted_lines</span> <span class="o">=</span> <span class="n">traceback</span><span class="o">.</span><span class="n">format_exc</span><span class="p">()</span><span class="o">.</span><span class="n">splitlines</span><span class="p">()</span>897 <span class="nb">print</span><span class="p">(</span><span class="n">formatted_lines</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>898 <span class="nb">print</span><span class="p">(</span><span class="n">formatted_lines</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>899 <span class="nb">print</span><span class="p">(</span><span class="s2">"*** format_exception:"</span><span class="p">)</span>900 <span class="nb">print</span><span class="p">(</span><span class="nb">repr</span><span class="p">(</span><span class="n">traceback</span><span class="o">.</span><span class="n">format_exception</span><span class="p">(</span><span class="n">exc</span><span class="p">)))</span>901 <span class="nb">print</span><span class="p">(</span><span class="s2">"*** extract_tb:"</span><span class="p">)</span>902 <span class="nb">print</span><span class="p">(</span><span class="nb">repr</span><span class="p">(</span><span class="n">traceback</span><span class="o">.</span><span class="n">extract_tb</span><span class="p">(</span><span class="n">exc</span><span class="o">.</span><span class="n">__traceback__</span><span class="p">)))</span>903 <span class="nb">print</span><span class="p">(</span><span class="s2">"*** format_tb:"</span><span class="p">)</span>904 <span class="nb">print</span><span class="p">(</span><span class="nb">repr</span><span class="p">(</span><span class="n">traceback</span><span class="o">.</span><span class="n">format_tb</span><span class="p">(</span><span class="n">exc</span><span class="o">.</span><span class="n">__traceback__</span><span class="p">)))</span>905 <span class="nb">print</span><span class="p">(</span><span class="s2">"*** tb_lineno:"</span><span class="p">,</span> <span class="n">exc</span><span class="o">.</span><span class="n">__traceback__</span><span class="o">.</span><span class="n">tb_lineno</span><span class="p">)</span>906</pre></div>907</div>908<p>The output for the example would look similar to this:</p>909<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>*** print_tb:910 File "<doctest...>", line 10, in <module>911 lumberjack()912 ~~~~~~~~~~^^913*** print_exception:914Traceback (most recent call last):915 File "<doctest...>", line 10, in <module>916 lumberjack()917 ~~~~~~~~~~^^918 File "<doctest...>", line 4, in lumberjack919 bright_side_of_life()920 ~~~~~~~~~~~~~~~~~~~^^921IndexError: tuple index out of range922*** print_exc:923Traceback (most recent call last):924 File "<doctest...>", line 10, in <module>925 lumberjack()926 ~~~~~~~~~~^^927 File "<doctest...>", line 4, in lumberjack928 bright_side_of_life()929 ~~~~~~~~~~~~~~~~~~~^^930IndexError: tuple index out of range931*** format_exc, first and last line:932Traceback (most recent call last):933IndexError: tuple index out of range934*** format_exception:935['Traceback (most recent call last):\n',936 ' File "<doctest default[0]>", line 10, in <module>\n lumberjack()\n ~~~~~~~~~~^^\n',937 ' File "<doctest default[0]>", line 4, in lumberjack\n bright_side_of_life()\n ~~~~~~~~~~~~~~~~~~~^^\n',938 ' File "<doctest default[0]>", line 7, in bright_side_of_life\n return tuple()[0]\n ~~~~~~~^^^\n',939 'IndexError: tuple index out of range\n']940*** extract_tb:941[<FrameSummary file <doctest...>, line 10 in <module>>,942 <FrameSummary file <doctest...>, line 4 in lumberjack>,943 <FrameSummary file <doctest...>, line 7 in bright_side_of_life>]944*** format_tb:945[' File "<doctest default[0]>", line 10, in <module>\n lumberjack()\n ~~~~~~~~~~^^\n',946 ' File "<doctest default[0]>", line 4, in lumberjack\n bright_side_of_life()\n ~~~~~~~~~~~~~~~~~~~^^\n',947 ' File "<doctest default[0]>", line 7, in bright_side_of_life\n return tuple()[0]\n ~~~~~~~^^^\n']948*** tb_lineno: 10949</pre></div>950</div>951<p>The following example shows the different ways to print and format the stack:</p>952<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span><span class="w"> </span><span class="nn">traceback</span>953<span class="gp">>>> </span><span class="k">def</span><span class="w"> </span><span class="nf">another_function</span><span class="p">():</span>954<span class="gp">... </span> <span class="n">lumberstack</span><span class="p">()</span>955<span class="gp">...</span>956<span class="gp">>>> </span><span class="k">def</span><span class="w"> </span><span class="nf">lumberstack</span><span class="p">():</span>957<span class="gp">... </span> <span class="n">traceback</span><span class="o">.</span><span class="n">print_stack</span><span class="p">()</span>958<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="nb">repr</span><span class="p">(</span><span class="n">traceback</span><span class="o">.</span><span class="n">extract_stack</span><span class="p">()))</span>959<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="nb">repr</span><span class="p">(</span><span class="n">traceback</span><span class="o">.</span><span class="n">format_stack</span><span class="p">()))</span>960<span class="gp">...</span>961<span class="gp">>>> </span><span class="n">another_function</span><span class="p">()</span>962 File <span class="nb">"<doctest>"</span>, line <span class="m">10</span>, in <span class="n"><module></span>963<span class="w"> </span><span class="n">another_function</span><span class="p">()</span>964 File <span class="nb">"<doctest>"</span>, line <span class="m">3</span>, in <span class="n">another_function</span>965<span class="w"> </span><span class="n">lumberstack</span><span class="p">()</span>966 File <span class="nb">"<doctest>"</span>, line <span class="m">6</span>, in <span class="n">lumberstack</span>967<span class="w"> </span><span class="n">traceback</span><span class="o">.</span><span class="n">print_stack</span><span class="p">()</span>968<span class="x">[('<doctest>', 10, '<module>', 'another_function()'),</span>969<span class="x"> ('<doctest>', 3, 'another_function', 'lumberstack()'),</span>970<span class="x"> ('<doctest>', 7, 'lumberstack', 'print(repr(traceback.extract_stack()))')]</span>971<span class="x">[' File "<doctest>", line 10, in <module>\n another_function()\n',</span>972<span class="x"> ' File "<doctest>", line 3, in another_function\n lumberstack()\n',</span>973<span class="x"> ' File "<doctest>", line 8, in lumberstack\n print(repr(traceback.format_stack()))\n']</span>974</pre></div>975</div>976<p>This last example demonstrates the final few formatting functions:</p>977<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span><span class="w"> </span><span class="nn">traceback</span>978<span class="gp">>>> </span><span class="n">traceback</span><span class="o">.</span><span class="n">format_list</span><span class="p">([(</span><span class="s1">'spam.py'</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="s1">'<module>'</span><span class="p">,</span> <span class="s1">'spam.eggs()'</span><span class="p">),</span>979<span class="gp">... </span> <span class="p">(</span><span class="s1">'eggs.py'</span><span class="p">,</span> <span class="mi">42</span><span class="p">,</span> <span class="s1">'eggs'</span><span class="p">,</span> <span class="s1">'return "bacon"'</span><span class="p">)])</span>980<span class="go">[' File "spam.py", line 3, in <module>\n spam.eggs()\n',</span>981<span class="go"> ' File "eggs.py", line 42, in eggs\n return "bacon"\n']</span>982<span class="gp">>>> </span><span class="n">an_error</span> <span class="o">=</span> <span class="ne">IndexError</span><span class="p">(</span><span class="s1">'tuple index out of range'</span><span class="p">)</span>983<span class="gp">>>> </span><span class="n">traceback</span><span class="o">.</span><span class="n">format_exception_only</span><span class="p">(</span><span class="n">an_error</span><span class="p">)</span>984<span class="go">['IndexError: tuple index out of range\n']</span>985</pre></div>986</div>987</section>988<section id="examples-of-using-tracebackexception">989<h2>Examples of Using <a class="reference internal" href="#traceback.TracebackException" title="traceback.TracebackException"><code class="xref py py-class docutils literal notranslate"><span class="pre">TracebackException</span></code></a><a class="headerlink" href="#examples-of-using-tracebackexception" title="Link to this heading">¶</a></h2>990<p>With the helper class, we have more options:</p>991<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span><span class="w"> </span><span class="nn">sys</span>992<span class="gp">>>> </span><span class="kn">from</span><span class="w"> </span><span class="nn">traceback</span><span class="w"> </span><span class="kn">import</span> <span class="n">TracebackException</span>993<span class="gp">>>></span>994<span class="gp">>>> </span><span class="k">def</span><span class="w"> </span><span class="nf">lumberjack</span><span class="p">():</span>995<span class="gp">... </span> <span class="n">bright_side_of_life</span><span class="p">()</span>996<span class="gp">...</span>997<span class="gp">>>> </span><span class="k">def</span><span class="w"> </span><span class="nf">bright_side_of_life</span><span class="p">():</span>998<span class="gp">... </span> <span class="n">t</span> <span class="o">=</span> <span class="s2">"bright"</span><span class="p">,</span> <span class="s2">"side"</span><span class="p">,</span> <span class="s2">"of"</span><span class="p">,</span> <span class="s2">"life"</span>999<span class="gp">... </span> <span class="k">return</span> <span class="n">t</span><span class="p">[</span><span class="mi">5</span><span class="p">]</span>1000<span class="gp">...</span>1001<span class="gp">>>> </span><span class="k">try</span><span class="p">:</span>1002<span class="gp">... </span> <span class="n">lumberjack</span><span class="p">()</span>1003<span class="gp">... </span><span class="k">except</span> <span class="ne">IndexError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>1004<span class="gp">... </span> <span class="n">exc</span> <span class="o">=</span> <span class="n">e</span>1005<span class="gp">...</span>1006<span class="gp">>>> </span><span class="k">try</span><span class="p">:</span>1007<span class="gp">... </span> <span class="k">try</span><span class="p">:</span>1008<span class="gp">... </span> <span class="n">lumberjack</span><span class="p">()</span>1009<span class="gp">... </span> <span class="k">except</span><span class="p">:</span>1010<span class="gp">... </span> <span class="mi">1</span><span class="o">/</span><span class="mi">0</span>1011<span class="gp">... </span><span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>1012<span class="gp">... </span> <span class="n">chained_exc</span> <span class="o">=</span> <span class="n">e</span>1013<span class="gp">...</span>1014<span class="gp">>>> </span><span class="c1"># limit works as with the module-level functions</span>1015<span class="gp">>>> </span><span class="n">TracebackException</span><span class="o">.</span><span class="n">from_exception</span><span class="p">(</span><span class="n">exc</span><span class="p">,</span> <span class="n">limit</span><span class="o">=-</span><span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">print</span><span class="p">()</span>1016<span class="gt">Traceback (most recent call last):</span>1017 File <span class="nb">"<python-input-1>"</span>, line <span class="m">6</span>, in <span class="n">lumberjack</span>1018<span class="w"> </span><span class="n">bright_side_of_life</span><span class="p">()</span>1019<span class="w"> </span><span class="pm">~~~~~~~~~~~~~~~~~~~^^</span>1020 File <span class="nb">"<python-input-1>"</span>, line <span class="m">10</span>, in <span class="n">bright_side_of_life</span>1021<span class="w"> </span><span class="k">return</span> <span class="n">t</span><span class="p">[</span><span class="mi">5</span><span class="p">]</span>1022<span class="w"> </span><span class="pm">~^^^</span>1023<span class="gr">IndexError</span>: <span class="n">tuple index out of range</span>1024 1025<span class="gp">>>> </span><span class="c1"># capture_locals adds local variables in frames</span>1026<span class="gp">>>> </span><span class="n">TracebackException</span><span class="o">.</span><span class="n">from_exception</span><span class="p">(</span><span class="n">exc</span><span class="p">,</span> <span class="n">limit</span><span class="o">=-</span><span class="mi">2</span><span class="p">,</span> <span class="n">capture_locals</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span><span class="o">.</span><span class="n">print</span><span class="p">()</span>1027<span class="gt">Traceback (most recent call last):</span>1028 File <span class="nb">"<python-input-1>"</span>, line <span class="m">6</span>, in <span class="n">lumberjack</span>1029<span class="w"> </span><span class="n">bright_side_of_life</span><span class="p">()</span>1030<span class="w"> </span><span class="pm">~~~~~~~~~~~~~~~~~~~^^</span>1031 File <span class="nb">"<python-input-1>"</span>, line <span class="m">10</span>, in <span class="n">bright_side_of_life</span>1032<span class="w"> </span><span class="k">return</span> <span class="n">t</span><span class="p">[</span><span class="mi">5</span><span class="p">]</span>1033<span class="w"> </span><span class="pm">~^^^</span>1034<span class="w"> </span><span class="n">t</span> <span class="o">=</span> <span class="p">(</span><span class="s2">"bright"</span><span class="p">,</span> <span class="s2">"side"</span><span class="p">,</span> <span class="s2">"of"</span><span class="p">,</span> <span class="s2">"life"</span><span class="p">)</span>1035<span class="gr">IndexError</span>: <span class="n">tuple index out of range</span>1036 1037<span class="gp">>>> </span><span class="c1"># The *chain* kwarg to print() controls whether chained</span>1038<span class="gp">>>> </span><span class="c1"># exceptions are displayed</span>1039<span class="gp">>>> </span><span class="n">TracebackException</span><span class="o">.</span><span class="n">from_exception</span><span class="p">(</span><span class="n">chained_exc</span><span class="p">)</span><span class="o">.</span><span class="n">print</span><span class="p">()</span>1040<span class="gt">Traceback (most recent call last):</span>1041 File <span class="nb">"<python-input-19>"</span>, line <span class="m">4</span>, in <span class="n"><module></span>1042<span class="w"> </span><span class="n">lumberjack</span><span class="p">()</span>1043<span class="w"> </span><span class="pm">~~~~~~~~~~^^</span>1044 File <span class="nb">"<python-input-8>"</span>, line <span class="m">7</span>, in <span class="n">lumberjack</span>1045<span class="w"> </span><span class="n">bright_side_of_life</span><span class="p">()</span>1046<span class="w"> </span><span class="pm">~~~~~~~~~~~~~~~~~~~^^</span>1047 File <span class="nb">"<python-input-8>"</span>, line <span class="m">11</span>, in <span class="n">bright_side_of_life</span>1048<span class="w"> </span><span class="k">return</span> <span class="n">t</span><span class="p">[</span><span class="mi">5</span><span class="p">]</span>1049<span class="w"> </span><span class="pm">~^^^</span>1050<span class="gr">IndexError</span>: <span class="n">tuple index out of range</span>1051 1052<span class="gt">During handling of the above exception, another exception occurred:</span>1053 1054<span class="gt">Traceback (most recent call last):</span>1055 File <span class="nb">"<python-input-19>"</span>, line <span class="m">6</span>, in <span class="n"><module></span>1056<span class="w"> </span><span class="mi">1</span><span class="o">/</span><span class="mi">0</span>1057<span class="w"> </span><span class="pm">~^~</span>1058<span class="gr">ZeroDivisionError</span>: <span class="n">division by zero</span>1059 1060<span class="gp">>>> </span><span class="n">TracebackException</span><span class="o">.</span><span class="n">from_exception</span><span class="p">(</span><span class="n">chained_exc</span><span class="p">)</span><span class="o">.</span><span class="n">print</span><span class="p">(</span><span class="n">chain</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>1061<span class="gt">Traceback (most recent call last):</span>1062 File <span class="nb">"<python-input-19>"</span>, line <span class="m">6</span>, in <span class="n"><module></span>1063<span class="w"> </span><span class="mi">1</span><span class="o">/</span><span class="mi">0</span>1064<span class="w"> </span><span class="pm">~^~</span>1065<span class="gr">ZeroDivisionError</span>: <span class="n">division by zero</span>1066</pre></div>1067</div>1068</section>1069</section>1070 1071 1072 <div class="clearer"></div>1073 </div>1074 </div>1075 </div>1076 <div class="sphinxsidebar" role="navigation" aria-label="Main">1077 <div class="sphinxsidebarwrapper">1078 <div>1079 <h3><a href="../contents.html">Table of Contents</a></h3>1080 <ul>1081<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">traceback</span></code> — Print or retrieve a stack traceback</a><ul>1082<li><a class="reference internal" href="#module-level-functions">Module-Level Functions</a></li>1083<li><a class="reference internal" href="#tracebackexception-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">TracebackException</span></code> Objects</a></li>1084<li><a class="reference internal" href="#stacksummary-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">StackSummary</span></code> Objects</a></li>1085<li><a class="reference internal" href="#framesummary-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">FrameSummary</span></code> Objects</a></li>1086<li><a class="reference internal" href="#examples-of-using-the-module-level-functions">Examples of Using the Module-Level Functions</a></li>1087<li><a class="reference internal" href="#examples-of-using-tracebackexception">Examples of Using <code class="xref py py-class docutils literal notranslate"><span class="pre">TracebackException</span></code></a></li>1088</ul>1089</li>1090</ul>1091 1092 </div>1093 <div>1094 <h4>Previous topic</h4>1095 <p class="topless"><a href="atexit.html"1096 title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">atexit</span></code> — Exit handlers</a></p>1097 </div>1098 <div>1099 <h4>Next topic</h4>1100 <p class="topless"><a href="__future__.html"1101 title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">__future__</span></code> — Future statement definitions</a></p>1102 </div>1103 <script>1104 document.addEventListener('DOMContentLoaded', () => {1105 const title = document.querySelector('meta[property="og:title"]').content;1106 const elements = document.querySelectorAll('.improvepage');1107 const pageurl = window.location.href.split('?')[0];1108 elements.forEach(element => {1109 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));1110 url.searchParams.set('pagetitle', title);1111 url.searchParams.set('pageurl', pageurl);1112 url.searchParams.set('pagesource', "library/traceback.rst");1113 element.href = url.toString();1114 });1115 });1116 </script>1117 <div role="note" aria-label="source link">1118 <h3>This page</h3>1119 <ul class="this-page-menu">1120 <li><a href="../bugs.html">Report a bug</a></li>1121 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>1122 <li>1123 <a href="https://github.com/python/cpython/blob/main/Doc/library/traceback.rst?plain=1"1124 rel="nofollow">Show source1125 </a>1126 </li>1127 1128 </ul>1129 </div>1130 </div>1131<div id="sidebarbutton" title="Collapse sidebar">1132<span>«</span>1133</div>1134 1135 </div>1136 <div class="clearer"></div>1137 </div> 1138 <div class="related" role="navigation" aria-label="Related">1139 <h3>Navigation</h3>1140 <ul>1141 <li class="right" style="margin-right: 10px">1142 <a href="../genindex.html" title="General Index"1143 >index</a></li>1144 <li class="right" >1145 <a href="../py-modindex.html" title="Python Module Index"1146 >modules</a> |</li>1147 <li class="right" >1148 <a href="__future__.html" title="__future__ — Future statement definitions"1149 >next</a> |</li>1150 <li class="right" >1151 <a href="atexit.html" title="atexit — Exit handlers"1152 >previous</a> |</li>1153 1154 <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>1155 <li><a href="https://www.python.org/">Python</a> »</li>1156 <li class="switchers">1157 <div class="language_switcher_placeholder"></div>1158 <div class="version_switcher_placeholder"></div>1159 </li>1160 <li>1161 1162 </li>1163 <li id="cpython-language-and-version">1164 <a href="../index.html">3.15.0a6 Documentation</a> »1165 </li>1166 1167 <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>1168 <li class="nav-item nav-item-2"><a href="python.html" >Python Runtime Services</a> »</li>1169 <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">traceback</span></code> — Print or retrieve a stack traceback</a></li>1170 <li class="right">1171 1172 1173 <div class="inline-search" role="search">1174 <form class="inline-search" action="../search.html" method="get">1175 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">1176 <input type="submit" value="Go">1177 </form>1178 </div>1179 |1180 </li>1181 <li class="right">1182<label class="theme-selector-label">1183 Theme1184 <select class="theme-selector" oninput="activateTheme(this.value)">1185 <option value="auto" selected>Auto</option>1186 <option value="light">Light</option>1187 <option value="dark">Dark</option>1188 </select>1189</label> |</li>1190 1191 </ul>1192 </div> 1193 <div class="footer">1194 © <a href="../copyright.html">Copyright</a> 2001 Python Software Foundation.1195 <br>1196 This page is licensed under the Python Software Foundation License Version 2.1197 <br>1198 Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.1199 <br>1200 