PyTexas 2010 - Debugging in Python

A Hands On Exercise in Python Debugging

Before we even begin, here's how look it up for yourself:

Useful Resources

http://docs.python.org/library/pdb.html - the official pdb documentation

http://openp2p.com/pub/a/python/2005/09/01/debugger.html - lengthy O'Reilly article

http://pythonconquerstheuniverse.wordpress.com/category/the-python-debugger/ - concise blog entry about the basics

http://aymanh.com/python-debugging-techniques - broad debugging blog entry

SECRET - the long form of this presentation/tutorial! I'll provide URL at the end.

In-Talk Code

mean.py

def mean(first, second):

result = first + second / 2

return result

shouldbe = 6

m = mean(4, 8)

print("mean(4,8) =", m)

print("should be:", shouldbe)

print("correct?", m==shouldbe)

sequence.py

print(1)

print(2)

print(3)

import pdb; pdb.set_trace()

print(4)

print(5)

print(6)

print(7)

nested.py

def top():

a = 12

b = 3

print("Hello")

middle()

print("done!")

def middle():

a = 0

c = 9

print("world")

bottom()

def bottom():

d = 88

print("!")

top()

divide (for mean.py)

def divide(x, y):

a = 3 / x

b = 4 / y

return a + b

callself.py

import logging

def callself(a=0):

logging.debug(a)

a = a + 1

print(a)

if a < 10:

callself(a)

Outline

    1. How are you debugging?

        1. Staring at it?

        2. Print?

            1. watch out for Python 3

        3. Interactive Console (if you didn't know...)

    2. A better way: pdb

      1. Get some code to debug

      2. pdb reference online, and other good articles

      3. Get into pdb session the easiest way: pdb.set_trace()

        1. how it works and what we can do

        2. interactive help with h(elp)

        3. leaving with q(uit)

        4. repeating with blank lines

      4. Examine the environment

        1. show location with l(ist)

        2. see args with a(rgs)

        3. see return value with retval

        4. look at stack with w(here)

        5. execute statements with ! or p(rint)

      1. Move around

        1. go forward with s(tep), n(ext)

        2. fast-forward with r(eturn), c(ontinue), and unt(il)

        3. go back with j(ump)

      1. The stack

        1. printing the stack with w(here)... and what it means

        2. navigating the stack with u(p) and d(own)

      1. Other ways into pdb

        1. multiple 'set_trace'

        2. after a crash: post-mortem with 'pm' and 'post_mortem'

          1. interactive

          2. as exception handling

        3. without error or source changes: 'run', 'runeval', and 'runcall'

          1. starting pdb without source changes

          2. setting breakpoints with 'break' and 'tbreak'

          3. managing breakpoints with 'clear', 'disable', 'enable'

          4. skipping breakpoints with 'condition' and 'ignore'

          5. hooking into breakpoints with 'commands'

          6. restarting with 'run'

    1. Logging, briefly