Python Debugging Fundamentals
description
Transcript of Python Debugging Fundamentals
![Page 1: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/1.jpg)
magePython Debugging Fundamentals
Saturday, October 20, 2012
PyCarolinas
UNC School of Pharmacy
Chapel Hill, NC
Chris Calloway
University of North Carolina
Department of Marine Sciences
Python DEBUGGING Fundamentals
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 2: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/2.jpg)
mage
A method for isolating
program errors
What Is Debugging
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 3: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/3.jpg)
mage
•Execute program one statement at
a time
•Inspect the state of objects bound
to identifiers
•Lather, rinse, repeat
What Is Debugging
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 4: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/4.jpg)
mage
Python's pdb module to
the rescue!
pdb Module
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 5: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/5.jpg)
mage
$ python -m pdb fizzbuzz.py
Invoking Python's Debugger
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 6: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/6.jpg)
mage
$ python -m pdb fizzbuzz.py
Invoking Python's Debugger
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
Run a module as a script
![Page 7: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/7.jpg)
mage
$ python -m pdb fizzbuzz.py
Invoking Python's Debugger
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
Module to run as a script
![Page 8: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/8.jpg)
mage
$ python -m pdb fizzbuzz.py
Invoking Python's Debugger
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
Argument to pdb:script to debug
![Page 9: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/9.jpg)
mage
$ python -m pdb fizzbuzz.py
> /Users/cbc/pycarolinas/fizzbuzz.py(7)<module>()
-> """
(Pdb)
Invoking Python's Debugger
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 10: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/10.jpg)
mage
$ python -m pdb fizzbuzz.py
> /Users/cbc/pycarolinas/fizzbuzz.py(7)<module>()
-> """
(Pdb)
Invoking Python's Debugger
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
Full path to scriptbeing debugged
![Page 11: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/11.jpg)
mage
$ python -m pdb fizzbuzz.py
> /Users/cbc/pycarolinas/fizzbuzz.py(7)<module>()
-> """
(Pdb)
Invoking Python's Debugger
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
Line number of nextstatement to execute
![Page 12: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/12.jpg)
mage
$ python -m pdb fizzbuzz.py
> /Users/cbc/pycarolinas/fizzbuzz.py(7)<module>()
-> """
(Pdb)
Invoking Python's Debugger
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
Type of objectlast evaluated
![Page 13: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/13.jpg)
mage
$ python -m pdb fizzbuzz.py
> /Users/cbc/pycarolinas/fizzbuzz.py(7)<module>()
-> """
(Pdb)
Invoking Python's Debugger
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
Instruction pointer tonext statement to execute
![Page 14: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/14.jpg)
mage
$ python -m pdb fizzbuzz.py
> /Users/cbc/pycarolinas/fizzbuzz.py(7)<module>()
-> """
(Pdb)
Invoking Python's Debugger
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
Debugger prompt
![Page 15: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/15.jpg)
mage(Pdb) help
Documented commands (type help <topic>):
========================================
EOF cl disable interact next return u where
a clear display j p retval unalias
alias commands down jump pp run undisplay
args condition enable l print rv unt
b cont exit list q s until
break continue h ll quit source up
bt d help longlist r step w
c debug ignore n restart tbreak whatis
Miscellaneous help topics:
==========================
exec pdb
(Pdb)
Python's Debugger Prompt
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 16: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/16.jpg)
mage(Pdb) help list
l(ist) [first [,last] | .]
List source code for the current file. Without arguments,
list 11 lines around the current line or continue the previous
listing. With . as argument, list 11 lines around the current
line. With one argument, list 11 lines starting at that line.
With two arguments, list the given range; if the second
argument is less than the first, it is a count.
The current line in the current frame is indicated by "->".
If an exception is being debugged, the line where the
exception was originally raised or propagated is indicated by
">>", if it differs from the current line.
(Pdb)
Python's Debugger Prompt
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 17: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/17.jpg)
mage(Pdb) h l
l(ist) [first [,last] | .]
List source code for the current file. Without arguments,
list 11 lines around the current line or continue the previous
listing. With . as argument, list 11 lines around the current
line. With one argument, list 11 lines starting at that line.
With two arguments, list the given range; if the second
argument is less than the first, it is a count.
The current line in the current frame is indicated by "->".
If an exception is being debugged, the line where the
exception was originally raised or propagated is indicated by
">>", if it differs from the current line.
(Pdb)
Python's Debugger Prompt
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 18: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/18.jpg)
mage(Pdb) l
2 Generate the first n Fizz Buzz answers.
3
4 Usage:
5
6 > python fizzbuzz.py n
7 -> """
8
9 import sys
10
11 def fizzbuzz(n):
12 """
(Pdb)
Python's Debugger Prompt
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 19: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/19.jpg)
mage(Pdb) l 6
1 """
2 Generate the first n Fizz Buzz answers.
3
4 Usage:
5
6 > python fizzbuzz.py n
7 -> """
8
9 import sys
10
11 def fizzbuzz(n):
(Pdb)
Python's Debugger Prompt
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 20: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/20.jpg)
mage(Pdb) l .
2 Generate the first n Fizz Buzz answers.
3
4 Usage:
5
6 > python fizzbuzz.py n
7 -> """
8
9 import sys
10
11 def fizzbuzz(n):
12 """
(Pdb)
Python's Debugger Prompt
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 21: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/21.jpg)
mage
(Pdb) 3 ** (1 / 2)
1.7320508075688772
(Pdb) dir()
['__builtins__', '__file__', '__name__']
(Pdb) print(__name__)
'__main__'
(Pdb)
Python's Debugger Prompt
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 22: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/22.jpg)
mage
(Pdb) !list
<class 'list'>
(Pdb)
Python's Debugger Prompt
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 23: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/23.jpg)
mage
•Execute program one statement at
a time
•Inspect the state of objects bound
to identifiers
•Lather, rinse, repeat
What Is Debugging
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 24: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/24.jpg)
mage
(Pdb) s
> /Users/cbc/pycarolinas/fizzbuzz.py(9)<module>()
-> import sys
(Pdb)
Single Stepping
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 25: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/25.jpg)
mage(Pdb) l
4 Usage:
5
6 > python fizzbuzz.py n
7 """
8
9 -> import sys
10
11 def fizzbuzz(n):
12 """
13 fizzbuzz(n) -> [first n Fizz Buzz answers]
14 """
(Pdb)
Single Stepping
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 26: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/26.jpg)
mage(Pdb) dir()
['__builtins__', '__doc__', '__file__', '__name__']
(Pdb) !print(__doc__)
Generate the first n Fizz Buzz answers.
Usage:
> python fizzbuzz.py n
(Pdb)
Single Stepping
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 27: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/27.jpg)
mage
(Pdb) s
> /Users/cbc/pycarolinas/fizzbuzz.py(11)<module>()
-> def fizzbuzz(n):
(Pdb) dir()
['__builtins__', '__doc__', '__file__', '__name__', 'sys']
(Pdb)
Single Stepping
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 28: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/28.jpg)
mage(Pdb) l
6 > python fizzbuzz.py n
7 """
8
9 import sys
10
11 -> def fizzbuzz(n):
12 """
13 fizzbuzz(n) -> [first n Fizz Buzz answers]
14 """
15
16 answers = []
(Pdb)
Single Stepping
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 29: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/29.jpg)
mage(Pdb) l
17 for x in range(1,n+1):
18 answer = ""
19 if not x%3:
20 answer += "Fizz"
21 if not x%5:
22 answer += "Buzz"
23 if not answer:
24 answer = x
25 answers.append(answer)
26 return answers
27
(Pdb)
Single Stepping
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 30: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/30.jpg)
mage(Pdb) l .
6 > python fizzbuzz.py n
7 """
8
9 import sys
10
11 -> def fizzbuzz(n):
12 """
13 fizzbuzz(n) -> [first n Fizz Buzz answers]
14 """
15
16 answers = []
(Pdb)
Single Stepping
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 31: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/31.jpg)
mage
(Pdb) s
> /Users/cbc/pycarolinas/fizzbuzz.py(28)<module>()
-> if __name__ == '__main__':
(Pdb) dir()
['__builtins__', '__doc__', '__file__',
'__name__', 'fizzbuzz', 'sys']
(Pdb)
Single Stepping
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 32: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/32.jpg)
mage(Pdb) l
23 if not answer:
24 answer = x
25 answers.append(answer)
26 return answers
27
28 -> if __name__ == '__main__':
29 try:
30 if len(sys.argv) != 2:
31 raise ValueError("Incorrect number of arguments")
32 answers = fizzbuzz(int(sys.argv[1]))
33 print(" ".join([str(answer) for answer in answers]))
(Pdb)
Single Stepping
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 33: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/33.jpg)
mage(Pdb) s
> /Users/cbc/pycarolinas/fizzbuzz.py(29)<module>()
-> try:
(Pdb) s
> /Users/cbc/pycarolinas/fizzbuzz.py(30)<module>()
-> if len(sys.argv) != 2:
(Pdb) s
> /Users/cbc/pycarolinas/fizzbuzz.py(31)<module>()
-> raise ValueError("Incorrect number of arguments")
(Pdb) s
ValueError: Incorrect number of arguments
> /Users/cbc/pycarolinas/fizzbuzz.py(31)<module>()
-> raise ValueError("Incorrect number of arguments")
(Pdb)
Single Stepping
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 34: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/34.jpg)
mage
(Pdb) s
> /Users/cbc/pycarolinas/fizzbuzz.py(34)<module>()
-> except:
(Pdb) s
> /Users/cbc/pycarolinas/fizzbuzz.py(35)<module>()
-> print(__doc__)
(Pdb)
Single Stepping
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 35: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/35.jpg)
mage(Pdb) s
Generate the first n Fizz Buzz answers.
Usage:
> python fizzbuzz.py n
--Return--
> /Users/cbc/pycarolinas/fizzbuzz.py(35)<module>()->None
-> print(__doc__)
(Pdb)
Single Stepping
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 36: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/36.jpg)
mage
(Pdb) s
--Return--
> <string>(1)<module>()->None
(Pdb) l
[EOF]
(Pdb)
Single Stepping
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 37: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/37.jpg)
mage
(Pdb) s
> /opt/python330/lib/python3.3/bdb.py(409)run()
-> self.quitting = True
(Pdb) s
The program finished and will be restarted
> /Users/cbc/pycarolinas/fizzbuzz.py(7)<module>()
-> """
(Pdb) dir()
['__builtins__', '__file__', '__name__']
(Pdb)
Single Stepping
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 38: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/38.jpg)
mage
(Pdb) q
$
Single Stepping
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 39: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/39.jpg)
mage
$ python -m pdb fizzbuzz.py 100
Invoking Python's Debugger
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 40: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/40.jpg)
mage
$ python -m pdb fizzbuzz.py 100
> /Users/cbc/pycarolinas/fizzbuzz.py(7)<module>()
-> """
(Pdb)
Invoking Python's Debugger
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 41: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/41.jpg)
mage(Pdb) l
2 Generate the first n Fizz Buzz answers.
3
4 Usage:
5
6 > python fizzbuzz.py n
7 -> """
8
9 import sys
10
11 def fizzbuzz(n):
12 """
(Pdb)
Python's Debugger Prompt
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 42: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/42.jpg)
mage(Pdb) l
13 fizzbuzz(n) -> [first n Fizz Buzz answers]
14 """
15
16 answers = []
17 for x in range(1,n+1):
18 answer = ""
19 if not x%3:
20 answer += "Fizz"
21 if not x%5:
22 answer += "Buzz"
23 if not answer:
(Pdb)
Python's Debugger Prompt
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 43: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/43.jpg)
mage(Pdb) l
24 answer = x
25 answers.append(answer)
26 return answers
27
28 if __name__ == '__main__':
29 try:
30 if len(sys.argv) != 2:
31 raise ValueError("Incorrect number of arguments")
32 answers = fizzbuzz(int(sys.argv[1]))
33 print(" ".join([str(answer) for answer in answers]))
34 except:
(Pdb)
Python's Debugger Prompt
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 44: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/44.jpg)
mage(Pdb) b 30
Breakpoint 1 at /Users/cbc/pycarolinas/fizzbuzz.py:30
(Pdb) l 30
25 answers.append(answer)
26 return answers
27
28 if __name__ == '__main__':
29 try:
30 B if len(sys.argv) != 2:
31 raise ValueError("Incorrect number of arguments")
32 answers = fizzbuzz(int(sys.argv[1]))
33 print(" ".join([str(answer) for answer in answers]))
34 except:
35 print(__doc__)
(Pdb)
Setting a Breakpoint
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 45: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/45.jpg)
mage(Pdb) l .
2 Generate the first n Fizz Buzz answers.
3
4 Usage:
5
6 > python fizzbuzz.py n
7 -> """
8
9 import sys
10
11 def fizzbuzz(n):
12 """
(Pdb)
Setting a Breakpoint
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 46: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/46.jpg)
mage(Pdb) c
> /Users/cbc/pycarolinas/fizzbuzz.py(30)<module>()
-> if len(sys.argv) != 2:
(Pdb) l
25 answers.append(answer)
26 return answers
27
28 if __name__ == '__main__':
29 try:
30 B-> if len(sys.argv) != 2:
31 raise ValueError("Incorrect number of arguments")
32 answers = fizzbuzz(int(sys.argv[1]))
33 print(" ".join([str(answer) for answer in answers]))
34 except:
35 print(__doc__)
(Pdb)
Setting a Breakpoint
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 47: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/47.jpg)
mage
(Pdb) len(sys.argv)
2
(Pdb) s
> /Users/cbc/pycarolinas/fizzbuzz.py(32)<module>()
-> answers = fizzbuzz(int(sys.argv[1]))
(Pdb)
Stepping Into a Function
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 48: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/48.jpg)
mage(Pdb) s
--Call--
> /Users/cbc/pycarolinas/fizzbuzz.py(11)fizzbuzz()
-> def fizzbuzz(n):
(Pdb) l
6 > python fizzbuzz.py n
7 """
8
9 import sys
10
11 -> def fizzbuzz(n):
12 """
13 fizzbuzz(n) -> [first n Fizz Buzz answers]
14 """
15
16 answers = []
(Pdb)
Stepping Into a Function
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 49: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/49.jpg)
mage(Pdb) p n
100
(Pdb) s
> /Users/cbc/pycarolinas/fizzbuzz.py(16)fizzbuzz()
-> answers = []
(Pdb) s
> /Users/cbc/pycarolinas/fizzbuzz.py(17)fizzbuzz()
-> for x in range(1,n+1):
(Pdb) s
> /Users/cbc/pycarolinas/fizzbuzz.py(18)fizzbuzz()
-> answer = ""
(Pdb)
Stepping Into a Function
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 50: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/50.jpg)
mage(Pdb) l
13 fizzbuzz(n) -> [first n Fizz Buzz answers]
14 """
15
16 answers = []
17 for x in range(1,n+1):
18 -> answer = ""
19 if not x%3:
20 answer += "Fizz"
21 if not x%5:
22 answer += "Buzz"
23 if not answer:
(Pdb)
Stepping Into a Function
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 51: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/51.jpg)
mage(Pdb) dir()
['answers', 'n', 'x']
(Pdb) where
/opt/python330/lib/python3.3/bdb.py(405)run()
-> exec(cmd, globals, locals)
<string>(1)<module>()
/Users/cbc/pycarolinas/fizzbuzz.py(32)<module>()
-> answers = fizzbuzz(int(sys.argv[1]))
> /Users/cbc/pycarolinas/fizzbuzz.py(18)fizzbuzz()
-> answer = ""
(Pdb)
Stepping Into a Function
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 52: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/52.jpg)
mage(Pdb) up
> /Users/cbc/pycarolinas/fizzbuzz.py(32)<module>()
-> answers = fizzbuzz(int(sys.argv[1]))
(Pdb) dir()
['__builtins__', '__doc__', '__file__',
'__name__', 'fizzbuzz', 'sys']
(Pdb) down
> /Users/cbc/pycarolinas/fizzbuzz.py(18)fizzbuzz()
-> answer = ""
(Pdb) down
*** Newest frame
(Pdb)
Stack Frames
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 53: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/53.jpg)
mage(Pdb) l
13 fizzbuzz(n) -> [first n Fizz Buzz answers]
14 """
15
16 answers = []
17 for x in range(1,n+1):
18 -> answer = ""
19 if not x%3:
20 answer += "Fizz"
21 if not x%5:
22 answer += "Buzz"
23 if not answer:
(Pdb)
Stepping Into a Function
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 54: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/54.jpg)
mage(Pdb) c
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17
Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz 31 32
Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44 FizzBuzz 46 47
Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56 Fizz 58 59 FizzBuzz 61 62
Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 FizzBuzz 76 77
Fizz 79 Buzz Fizz 82 83 Fizz Buzz 86 Fizz 88 89 FizzBuzz 91 92
Fizz 94 Buzz Fizz 97 98 Fizz Buzz
The program finished and will be restarted
> /Users/cbc/pycarolinas/fizzbuzz.py(7)<module>()
-> """
(Pdb)
Stepping Into a Function
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 55: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/55.jpg)
mage
(Pdb) b
Num Type Disp Enb Where
1 breakpoint keep yes at /Users/cbc/pycarolinas/fizzbuzz.py:30
breakpoint already hit 1 time
(Pdb) c
> /Users/cbc/pycarolinas/fizzbuzz.py(30)<module>()
-> if len(sys.argv) != 2:
(Pdb) s
> /Users/cbc/pycarolinas/fizzbuzz.py(32)<module>()
-> answers = fizzbuzz(int(sys.argv[1]))
(Pdb)
Listing Breakpoints
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 56: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/56.jpg)
mage
(Pdb) n
> /Users/cbc/pycarolinas/fizzbuzz.py(33)<module>()
-> print(" ".join([str(answer) for answer in answers]))
(Pdb) pp answers
[1,
2,
'Fizz',
...
98,
'Fizz',
'Buzz']
(Pdb)
Stepping Over a Function
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 57: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/57.jpg)
mage(Pdb) c
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17
Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz 31 32
Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44 FizzBuzz 46 47
Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56 Fizz 58 59 FizzBuzz 61 62
Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 FizzBuzz 76 77
Fizz 79 Buzz Fizz 82 83 Fizz Buzz 86 Fizz 88 89 FizzBuzz 91 92
Fizz 94 Buzz Fizz 97 98 Fizz Buzz
The program finished and will be restarted
> /Users/cbc/pycarolinas/fizzbuzz.py(7)<module>()
-> """
(Pdb)
Stepping Over a Function
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 58: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/58.jpg)
mage
(Pdb) c
> /Users/cbc/pycarolinas/fizzbuzz.py(30)<module>()
-> if len(sys.argv) != 2:
(Pdb) b
Num Type Disp Enb Where
1 breakpoint keep yes at /Users/cbc/pycarolinas/fizzbuzz.py:30
breakpoint already hit 3 times
(Pdb) s
> /Users/cbc/pycarolinas/fizzbuzz.py(32)<module>()
-> answers = fizzbuzz(int(sys.argv[1]))
(Pdb)
Stepping Over a Function
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 59: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/59.jpg)
mage(Pdb) s
--Call--
> /Users/cbc/pycarolinas/fizzbuzz.py(11)fizzbuzz()
-> def fizzbuzz(n):
(Pdb) s
> /Users/cbc/pycarolinas/fizzbuzz.py(16)fizzbuzz()
-> answers = []
(Pdb) s
> /Users/cbc/pycarolinas/fizzbuzz.py(17)fizzbuzz()
-> for x in range(1,n+1):
(Pdb) s
> /Users/cbc/pycarolinas/fizzbuzz.py(18)fizzbuzz()
-> answer = ""
(Pdb)
Stepping Over a Function
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 60: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/60.jpg)
mage
(Pdb) r
--Return--
> /Users/cbc/pycarolinas/fizzbuzz.py(26)fizzbuzz()->
[1, 2, 'Fizz', 4, 'Buzz', 'Fizz', ...]
-> return answers
(Pdb)
Stepping Over a Function
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 61: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/61.jpg)
mage(Pdb) s
> /Users/cbc/pycarolinas/fizzbuzz.py(33)<module>()
-> print(" ".join([str(answer) for answer in answers]))
(Pdb) s
--Call--
> /Users/cbc/pycarolinas/fizzbuzz.py(33)<listcomp>()
-> print(" ".join([str(answer) for answer in answers]))
(Pdb) s
> /Users/cbc/pycarolinas/fizzbuzz.py(33)<listcomp>()
-> print(" ".join([str(answer) for answer in answers]))
(Pdb)
Stepping Over a List Comprehension
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 62: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/62.jpg)
mage
(Pdb) r
--Return--
> /Users/cbc/pycarolinas/fizzbuzz.py(33)<listcomp>()->
['1', '2', 'Fizz', '4', 'Buzz', 'Fizz', ...]
-> print(" ".join([str(answer) for answer in answers]))
(Pdb)
Stepping Over a List Comprehension
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 63: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/63.jpg)
mage
(Pdb) h clear
cl(ear) filename:lineno
cl(ear) [bpnumber [bpnumber...]]
With a space separated list of breakpoint numbers, clear
those breakpoints. Without argument, clear all breaks (but
first ask confirmation). With a filename:lineno argument,
clear all breaks at that line in that file.
(Pdb)
Clear Breakpoints
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 64: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/64.jpg)
mage
(Pdb) h disable
disable bpnumber [bpnumber ...]
Disables the breakpoints given as a space separated list of
breakpoint numbers. Disabling a breakpoint means it cannot
cause the program to stop execution, but unlike clearing a
breakpoint, it remains in the list of breakpoints and can be
(re-)enabled.
(Pdb)
Disable Breakpoints
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 65: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/65.jpg)
mage
(Pdb) h enable
enable bpnumber [bpnumber ...]
Enables the breakpoints given as a space separated list of
breakpoint numbers.
(Pdb)
Enable Breakpoints
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 66: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/66.jpg)
mage
(Pdb) h display
display [expression]
Display the value of the expression if it changed, each time execution
stops in the current frame.
Without expression, list all display expressions for the current frame.
(Pdb)
Monitor Objects for Changes
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 67: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/67.jpg)
mage
(Pdb) h condition
condition bpnumber [condition]
Set a new condition for the breakpoint, an expression which
must evaluate to true before the breakpoint is honored. If
condition is absent, any existing condition is removed; i.e.,
the breakpoint is made unconditional.
(Pdb)
Conditional Breakpoints
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 68: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/68.jpg)
mage
(Pdb) h ignore
ignore bpnumber [count]
Set the ignore count for the given breakpoint number. If
count is omitted, the ignore count is set to 0. A breakpoint
becomes active when the ignore count is zero. When non-zero,
the count is decremented each time the breakpoint is reached
and the breakpoint is not disabled and any associated
condition evaluates to true.
(Pdb)
Skip Over Breakpoints
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 69: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/69.jpg)
mage
(Pdb) help alias
alias [name [command [parameter parameter ...] ]]
Create an alias called 'name' that executes 'command'. The
command must *not* be enclosed in quotes. Replaceable
parameters can be indicated by %1, %2, and so on, while %* is
replaced by all the parameters. If no command is given, the
current alias for name is shown. If no name is given, all
aliases are listed.
Debugger Macros
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 70: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/70.jpg)
mage
(Pdb) h tbreak
tbreak [ ([filename:]lineno | function) [, condition] ]
Same arguments as break, but sets a temporary breakpoint: it
is automatically deleted when first hit.
(Pdb)
One Time Breakpoints
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 71: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/71.jpg)
mage
•Execute entire program one step at
the time
•Execute only suspect portions of
program
•Isolate suspect portions of program
What Is Debugging
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 72: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/72.jpg)
mage
if __name__ == '__main__':
try:
if len(sys.argv) != 2:
import pdb; pdb.set_trace()
raise ValueError("Incorrect number of arguments")
answers = fizzbuzz(int(sys.argv[1]))
print(" ".join([str(answer) for answer in answers]))
except:
print(__doc__)
Setting a Trace
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 73: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/73.jpg)
mage
$ python fizzbuzzNG.py
> /Users/cbc/pycarolinas/fizzbuzzNG.py(32)<module>()
-> raise ValueError("Incorrect number of arguments")
(Pdb)
Setting a Trace
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 74: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/74.jpg)
mage
if __name__ == '__main__':
if len(sys.argv) != 2:
raise ValueError("Incorrect number of arguments")
answers = fizzbuzz(int(sys.argv[1]))
print(" ".join([str(answer) for answer in answers]))
Post-mortem Debugging
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 75: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/75.jpg)
mage
$ python -i fizzbuzzNG2.py
Traceback (most recent call last):
File "fizzbuzzNG2.py", line 30, in <module>
raise ValueError("Incorrect number of arguments")
ValueError: Incorrect number of arguments
>>>
Post-mortem Debugging
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 76: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/76.jpg)
mage
>>> import pdb; pdb.pm()
> /Users/cbc/pycarolinas/fizzbuzzNG2.py(30)<module>()
-> raise ValueError("Incorrect number of arguments")
(Pdb)
Post-mortem Debugging
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 77: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/77.jpg)
mage>>> import fizzbuzz
>>> import pdb
>>> pdb.run('fizzbuzz.fizzbuzz(100)')
> <string>(1)<module>()
(Pdb) s
--Call--
> /Users/cbc/pycarolinas/fizzbuzz.py(11)fizzbuzz()
-> def fizzbuzz(n):
(Pdb)
Debugger Runner
PyCamp™ Programming For The People
Copyright © 2012
Trizpug
![Page 78: Python Debugging Fundamentals](https://reader031.fdocuments.in/reader031/viewer/2022020110/554a1e2cb4c9058c5d8b5651/html5/thumbnails/78.jpg)
mage
http://drunkenpython.org/pycarolinas.ziphttp://docs.python.org/py3k/library/pdb.html#module-pdb
Python DEBUGGING Fundamentals
PyCamp™ Programming For The People
Copyright © 2012
Trizpug