Kurt McKee

lessons learned in production

Archive

Hey there! This article was written in 2012.

It might not have aged well for any number of reasons, so keep that in mind when reading (or clicking outgoing links!).

pdb post-mortem debugging

Posted 13 January 2012 in pdb, programming, and python

I misread a recent feedparser bug report and thought that the author had mistyped a bbcode- or wiki-formatted link. Consequently, I wasted time debugging the code.

Turns out, I wasted a lot of time, because I spent more than zero time debugging the code. Yes, zero time. I was using a conditional breakpoint when I should have been using pdb's post_mortem() (or pm() for short). I had no idea what post-mortem debugging was until I ran across Doug Hellmann's article on pdb. I barely finished reading the following two sentences before I alt-tabbed back and found the problem:

Debugging a failure after a program terminates is called post-mortem debugging. pdb supports post-mortem debugging through the pm() and post_mortem() functions.

So my debugging session should have looked like:

import feedparser  
import pdb  
feedparser.parse('offending-url')  
# huge traceback  
pdb.pm()

Pressing u a few times took me up the call stack, and then p uri printed out the offending...wait, that's what the bug reporter already told me.

The important thing here is that I discovered a time-saving tool. (And found a performance problem with feedparser: it calls _urijoin() hundreds of times for no discernible reason. Reducing that call volume may improve performance.) Learning is fun!

☕ Like my work? I accept tips!