-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path07-debugging-post-mortem.html
227 lines (216 loc) · 16.3 KB
/
07-debugging-post-mortem.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="generator" content="pandoc">
<title>Software Carpentry: Debugging</title>
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="css/bootstrap/bootstrap.css" />
<link rel="stylesheet" type="text/css" href="css/bootstrap/bootstrap-theme.css" />
<link rel="stylesheet" type="text/css" href="css/swc.css" />
<link rel="alternate" type="application/rss+xml" title="Software Carpentry Blog" href="http://software-carpentry.org/feed.xml"/>
<meta charset="UTF-8" />
<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body class="lesson">
<div class="container card">
<div class="banner">
<a href="http://software-carpentry.org" title="Software Carpentry">
<img alt="Software Carpentry banner" src="img/software-carpentry-banner.png" />
</a>
</div>
<article>
<div class="row">
<div class="col-md-10 col-md-offset-1">
<a href="index.html"><h1 class="title">Debugging</h1></a>
<h2 class="subtitle">Post-mortem debugging</h2>
<section class="objectives panel panel-warning">
<div class="panel-heading">
<h2 id="learning-objectives"><span class="glyphicon glyphicon-certificate"></span>Learning Objectives</h2>
</div>
<div class="panel-body">
<ul>
<li>Understand what is meant by “post-mortem debugging”</li>
<li>Understand how to call Python’s debugger after an exception was raised</li>
</ul>
</div>
</section>
<p>One of the main reasons to start debugging is that the program exited with an error message. Ideally, all the necessary information to find the cause of the problem is in the error message but often this is not the case. In that situation, it might be of interest to have access to the program state at the point <em>where the exception was raised</em>. A common technique to investigate this kind of problem is to edit the code, put <code>print</code> statements at the point where the error was raised and then run the code again. This approach however has several drawbacks:</p>
<ul>
<li>The code in question might be some library code that you cannot easily edit (e.g. you need administrator privileges to edit code in a system-wide installation of a library).</li>
<li>You have to know in advance which variables are of interest to you which means that potentially you’ll have to repeat the process several times.</li>
<li>You’ll have to clean up afterwards and make sure not to forget any <code>print</code> statements in the code.</li>
<li>Depending on the program you are debugging, running everything again might take a long time.</li>
</ul>
<p>There is an alternative approach using Python’s built-in symbolic debugger. When an exception is raised, its full context is stored and can be investigated. This kind of debugging is called “post-mortem” (i.e. “after death”), because you only use the debugger after the program has crashed as opposed to running it under the control of the debugger from the start (as we’ll do later). Let’s have a look at some erroneous code:</p>
<pre class="sourceCode python"><code class="sourceCode python"><span class="ch">import</span> numpy
<span class="kw">def</span> find_first(data, element):
<span class="co">"""</span>
<span class="co"> Return the index of the first appearance of ``element`` in</span>
<span class="co"> ``data`` (or -1 if ``data`` does not contain ``element``).</span>
<span class="co"> """</span>
counter = <span class="dv">0</span>
<span class="kw">while</span> counter <= <span class="dt">len</span>(data):
<span class="kw">if</span> data[counter] == element:
<span class="kw">return</span> counter
counter += <span class="dv">1</span>
<span class="kw">return</span> -<span class="dv">1</span>
<span class="kw">def</span> check_data(target):
test_data = [<span class="dv">3</span>, <span class="dv">2</span>, <span class="dv">8</span>, <span class="dv">9</span>, <span class="dv">3</span>, numpy.nan, <span class="dv">4</span>, <span class="dv">7</span>, <span class="dv">5</span>]
<span class="co"># We look for a zero in the data</span>
index = find_first(test_data, target)
<span class="kw">if</span> index != -<span class="dv">1</span>:
<span class="dt">print</span>(<span class="st">'Data until first occurrence of'</span>, target, <span class="st">':'</span>, test_data[:index])
<span class="kw">else</span>:
<span class="dt">print</span>(<span class="st">'No occurrence of'</span>, target, <span class="st">'in the data'</span>)</code></pre>
<p>The <code>find_first</code> function finds the first occurrence of an element in a list (or an array) of elements and returns its index. The <code>check_data</code> function uses this function to find a certain value in the data and prints the data until that point. It uses some fake “data” and takes an <code>target</code> argument to specify what value to look for.</p>
<p>If we run the <code>check_data</code> function, all looks fine:</p>
<pre class="sourceCode python"><code class="sourceCode python">check_data(<span class="dv">5</span>)</code></pre>
<pre class="output"><code>Data until first occurrence of 5 : [3, 2, 8, 9, 3, nan, 4, 7]</code></pre>
<p>Now let’s run the function again but use a different target value:</p>
<pre class="sourceCode python"><code class="sourceCode python">check_data(<span class="dv">1</span>)</code></pre>
<pre class="output"><code>---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-58-cb57f256f664> in <module>()
----> 1 check_data(1)
<ipython-input-56-610c1db03285> in check_data(target)
16 test_data = [3, 2, 8, 9, 3, numpy.nan, 4, 7, 5]
17 # We look for a zero in the data
---> 18 index = find_first(test_data, target)
19 if index != -1:
20 print('Data until first occurrence of', target, ':', test_data[:index])
<ipython-input-56-610c1db03285> in find_first(data, element)
8 counter = 0
9 while counter <= len(data):
---> 10 if data[counter] == element:
11 return counter
12 counter += 1
IndexError: list index out of range</code></pre>
<p>There seems to be a problem and unfortunately the <code>IndexError</code> is not very precise (note that the error message would have been more helpful if we had used a numpy array instead of a list for the “data”). Let’s investigate what caused it by using the symbolic debugger (note that the following command only works in ipython or the jupyter notebook):</p>
<pre class="sourceCode python"><code class="sourceCode python">% debug</code></pre>
<pre class="output"><code><ipython-input-56-610c1db03285>(10)find_first()
8 counter = 0
9 while counter <= len(data):
---> 10 if data[counter] == element:
11 return counter
12 counter += 1
ipdb> </code></pre>
<p>We are now in an <code>ipdb</code> console (an improved interface to Python’s built-in <code>pdb</code> debugger) and can issue <code>ipdb</code> commands and investigate the state of variables and expressions in the context pointed to by the arrow. To distinguish the two, we can add a <code>!</code> to the start of the line when we are interested in variables/expressions:</p>
<pre class="output"><code>ipdb> !counter
8
ipdb> !len(data)
8</code></pre>
<p>This is not necessary when there is no possible confusion between <code>ipdb</code> commands and Python expression, though:</p>
<pre class="output"><code>ipdb> counter
8</code></pre>
<p>Sometimes, the values we are interested in are not accessible at the exact point where the exception was raised. For example, we cannot access <code>test_data</code> and <code>target</code> used in <code>check_data</code> (in the current example, this is of course not an actual problem, since they are handed over to <code>find_first</code> as <code>data</code> and <code>element</code>):</p>
<pre class="output"><code>ipdb> test_data
*** NameError: name 'test_data' is not defined</code></pre>
<p>To access the data, we can move “up” in the exception stack and investigate the variables there:</p>
<pre class="output"><code>ipdb> up
> <ipython-input-56-610c1db03285>(18)check_data()
16 test_data = [3, 2, 8, 9, 3, numpy.nan, 4, 7, 5]
17 # We look for a zero in the data
---> 18 index = find_first(test_data, target)
19 if index != -1:
20 print('Data until first occurrence of', target, ':', test_data[:index])
ipdb> test_data
[3, 2, 8, 9, 3, nan, 4, 7, 5]</code></pre>
<p>To get back to where we were before, we use <code>down</code> or alternatively <code>d</code> (instead of <code>up</code> we can also use <code>u</code>):</p>
<pre class="output"><code>ipdb> d
> <ipython-input-56-610c1db03285>(10)find_first()
8 counter = 0
9 while counter <= len(data):
---> 10 if data[counter] == element:
11 return counter
12 counter += 1</code></pre>
<p>We finished our debugging because we figured out that the problem lies in the <code>counter</code> variable going one step too far – instead of <code>counter <= len(data)</code> the comparison should read <code>counter < len(data)</code>. We can therefore quit the debugger with <code>q</code> (short for <code>quit</code>):</p>
<pre class="output"><code>ipdb> q</code></pre>
<p>Now, if this were a real-life situation we would now take the time to add a test to our test suite, checking for this condition (searching for an element that is not present in the list). We’d make sure that this test fails and then go on to fix the test. This way, we’d be sure not to re-introduce the same error in future versions of our code (e.g. because of a code re-organization) without noticing. Incidentally, there <em>is</em> a way to re-organize the code and make it less error-prone:</p>
<pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> find_first(data, element):
<span class="co">"""</span>
<span class="co"> Return the index of the first appearance of ``element`` in</span>
<span class="co"> ``data`` (or -1 if ``data`` does not contain ``element``).</span>
<span class="co"> """</span>
<span class="kw">for</span> index, data_element in <span class="dt">enumerate</span>(data):
<span class="kw">if</span> data_element == element:
<span class="kw">return</span> index
<span class="kw">return</span> -<span class="dv">1</span></code></pre>
<p>Using a counter and indexing into a list at every iteration of a loop is something that might feel natural when you have experience in another programming language but in Python it is often considered to be a so-called “<a href="https://en.wikipedia.org/wiki/Anti-pattern">anti-pattern</a>”.</p>
<aside class="callout panel panel-info">
<div class="panel-heading">
<h2 id="post-mortem-debugging-with-pytest"><span class="glyphicon glyphicon-pushpin"></span>Post-mortem debugging with pytest</h2>
</div>
<div class="panel-body">
<p>You can run your test suite with pytest and make pytest open a debugger session for you as soon as an error occurs:</p>
<pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">py.test</span> --pdb</code></pre>
<pre class="output"><code>============================= test session starts ==============================
platform linux -- Python 3.5.1, pytest-2.8.5, py-1.4.31, pluggy-0.3.1
rootdir: [...], inifile:
collected 2 items
test_whiten.py .F
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> traceback >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
def test_2d():
test_data = numpy.array([[1, 3, 5, 7],
[2, 3, 4, 1]])
whitened = whiten(test_data)
> assert_allclose(whitened.mean(), 0)
E AssertionError:
E Not equal to tolerance rtol=1e-07, atol=0
E
E (mismatch 100.0%)
E x: array(-2.7755575615628914e-17)
E y: array(0)
test_whiten.py:17: AssertionError
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> entering PDB >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
> [...]/test_whiten.py(17)test_2d()
-> assert_allclose(whitened.mean(), 0)
(Pdb)</code></pre>
</div>
</aside>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2 id="error-debugging"><span class="glyphicon glyphicon-pencil"></span>Error debugging</h2>
</div>
<div class="panel-body">
<p>Copy & paste the following code and run <code>test_code_array</code>. It will fail with an error, use the debugger to find out why.</p>
<pre class="sourceCode python"><code class="sourceCode python"><span class="ch">import</span> numpy
<span class="ch">from</span> numpy.testing.utils <span class="ch">import</span> assert_equal
<span class="kw">def</span> code_array(codes):
<span class="co">'''Store airport codes in an array in numerical form'''</span>
to_numerical_code = numpy.vectorize(<span class="kw">lambda</span> code: numpy.array([<span class="dt">ord</span>(c) <span class="kw">for</span> c in code]))
<span class="kw">return</span> numpy.vstack([to_numerical_code(c) <span class="kw">for</span> c in codes])
<span class="kw">def</span> test_code_array():
airport_codes = [<span class="st">'TXL'</span>, <span class="st">'LAX'</span>, <span class="st">'PHX'</span>, <span class="st">'CDG'</span> <span class="st">'ORY'</span>, <span class="st">'JFK'</span>, <span class="st">'JNB'</span>, <span class="st">'WAW'</span>]
expected = numpy.array([[<span class="dv">84</span>, <span class="dv">88</span>, <span class="dv">76</span>],
[<span class="dv">76</span>, <span class="dv">65</span>, <span class="dv">88</span>],
[<span class="dv">80</span>, <span class="dv">72</span>, <span class="dv">88</span>],
[<span class="dv">67</span>, <span class="dv">68</span>, <span class="dv">71</span>],
[<span class="dv">79</span>, <span class="dv">82</span>, <span class="dv">89</span>],
[<span class="dv">74</span>, <span class="dv">70</span>, <span class="dv">75</span>],
[<span class="dv">74</span>, <span class="dv">78</span>, <span class="dv">66</span>],
[<span class="dv">87</span>, <span class="dv">65</span>, <span class="dv">87</span>]])
assert_equal(code_array(airport_codes), expected)</code></pre>
</div>
</section>
<p>Not every error in the code leads to an exception – the ones that are most difficult to debug usually don’t! We will therefore next look at using the debugger right from the start of a program.</p>
</div>
</div>
</article>
<div class="footer">
<a class="label swc-blue-bg" href="http://software-carpentry.org">Software Carpentry</a>
<a class="label swc-blue-bg" href="https://github.com/paris-swc/python-testing-debugging-profiling">Source</a>
<a class="label swc-blue-bg" href="mailto:[email protected]">Contact</a>
<a class="label swc-blue-bg" href="LICENSE.html">License</a>
</div>
</div>
<!-- Javascript placed at the end of the document so the pages load faster -->
<script src="http://software-carpentry.org/v5/js/jquery-1.9.1.min.js"></script>
<script src="css/bootstrap/bootstrap-js/bootstrap.js"></script>
<script src='https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'></script>
</body>
</html>