You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 111_debugging.html
+139-7Lines changed: 139 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -376,7 +376,8 @@ <h2 id="toc-title">Table of contents</h2>
376
376
<li><ahref="#what-is-a-debugger" id="toc-what-is-a-debugger" class="nav-link active" data-scroll-target="#what-is-a-debugger"><spanclass="header-section-number">13.1</span> What is a debugger?</a></li>
<h2data-number="13.1" class="anchored" data-anchor-id="what-is-a-debugger"><spanclass="header-section-number">13.1</span> What is a debugger?</h2>
409
410
<p>A debugger is a device that allows us to interrupt code execution and jump into the execution context in a interactive mode, so that we can inspect and run code to find out what’s going on.<br>
410
411
We called that to “set a breakpoint” or “set a trace”.</p>
411
-
<p>There are a few options to do that natively in python.</p>
412
+
<p>There are a few options to do that in python.</p>
<p>The python standard library includes <code>pdb</code> module. If you call this function in your code you will be put into an interactive session exactly at that point.</p>
417
+
<p>Any of the following options would work:</p>
418
+
<divclass="code-copy-outer-scaffold"><divclass="sourceCode" id="cb1"><preclass="sourceCode python code-with-copy"><codeclass="sourceCode python"><spanid="cb1-1"><ahref="#cb1-1" aria-hidden="true" tabindex="-1"></a><spanclass="co"># your code</span></span>
419
+
<spanid="cb1-2"><ahref="#cb1-2" aria-hidden="true" tabindex="-1"></a><spanclass="co"># your code</span></span>
<spanid="cb1-8"><ahref="#cb1-8" aria-hidden="true" tabindex="-1"></a><spanclass="co"># your code</span></span>
426
+
<spanid="cb1-9"><ahref="#cb1-9" aria-hidden="true" tabindex="-1"></a><spanclass="co"># your code</span></span>
427
+
<spanid="cb1-10"><ahref="#cb1-10" aria-hidden="true" tabindex="-1"></a><spanclass="co"># your code</span></span></code></pre></div><buttontitle="Copy to Clipboard" class="code-copy-button"><iclass="bi"></i></button></div>
428
+
<p>Once in the debugger, we have several functions to proceed with code execution with more granular control.</p>
429
+
<p>There are several options, but the most commonly used methods are:</p>
430
+
<ul>
431
+
<li>n (next): Continue execution one line, stay in the current function (step over)</li>
432
+
<li>s (step): Execute current line and stop in a foreign function if one is called (step into)</li>
433
+
<li>c (continue): Continue whole code execution until a new breakpoint is found</li>
<spanid="cb5-2"><ahref="#cb5-2" aria-hidden="true" tabindex="-1"></a> a <spanclass="op">=</span><spanclass="dv">1</span></span>
453
+
<spanid="cb5-3"><ahref="#cb5-3" aria-hidden="true" tabindex="-1"></a><spanclass="cf">assert</span> a <spanclass="op">==</span><spanclass="dv">0</span></span>
<spanclass="ansi-cyan-fg">Cell</span><spanclass="ansi-cyan-fg"></span><spanclass="ansi-green-fg">In[1]</span><spanclass="ansi-green-fg">, line 5</span>
461
+
<spanclass="ansi-green-fg"> 2</span> a = <spanclass="ansi-green-fg">1</span>
462
+
<spanclass="ansi-green-fg"> 3</span><spanstyle="font-weight:bold;color:rgb(0,135,0)">assert</span> a == <spanclass="ansi-green-fg">0</span>
<spanclass="ansi-cyan-fg">Cell</span><spanclass="ansi-cyan-fg"></span><spanclass="ansi-green-fg">In[1]</span><spanclass="ansi-green-fg">, line 3</span>, in <spanclass="ansi-cyan-fg">wrong_func</span><spanclass="ansi-blue-fg">()</span>
<spanclass="ansi-green-fg"> 2</span> a = <spanclass="ansi-green-fg">1</span>
468
+
<spanclass="ansi-green-fg">----> </span><spanclass="ansi-green-fg">3</span><spanstyle="font-weight:bold;color:rgb(0,135,0)">assert</span> a == <spanclass="ansi-green-fg">0</span>
<spanid="cb10-2"><ahref="#cb10-2" aria-hidden="true" tabindex="-1"></a><spanclass="cf">assert</span> p <spanclass="op">==</span><spanclass="dv">0</span></span>
<spanid="cb10-9"><ahref="#cb10-9" aria-hidden="true" tabindex="-1"></a> c <spanclass="op">=</span><spanclass="dv">0</span></span>
519
+
<spanid="cb10-10"><ahref="#cb10-10" aria-hidden="true" tabindex="-1"></a> f(c)</span></code></pre></div><buttontitle="Copy to Clipboard" class="code-copy-button"><iclass="bi"></i></button></div>
520
+
</div>
521
+
<oltype="1">
522
+
<li>Run the code to see the error</li>
523
+
<li>Set a breakpoint inside <code>main</code> to use the debugger</li>
524
+
<li>Step through the code using <code>n (next)</code> and another time using <code>s (step)</code></li>
525
+
<li>Set a second breakpoint inside <code>main</code>and run again the code but this time use <code>c (continue)</code></li>
526
+
<li>Download <ahref="https://github.com/fabridamicelli/ds005588/archive/refs/heads/broken-data.zip">this public dataset</a> into the folder <code>/pycourse/data/</code> (create it if you don’t yet have it).</li>
527
+
</ol>
528
+
<p>This dataset was modified and has some problems apparently. Here’s a bit of code to unzip it and read through the files.</p>
<spanclass="ansi-cyan-fg">Cell</span><spanclass="ansi-cyan-fg"></span><spanclass="ansi-green-fg">In[15]</span><spanclass="ansi-green-fg">, line 1</span>
0 commit comments