Fun with TextMate and PDB

TextMate, PDB, Debugging

I usually use Wing IDE to work in Python when I’m on Linux and Windows, but I just can’t get used to it on OS X. That being the case, I tend to do all of my Python work in TextMate on the Mac. One thing that’s missing from my workflow, however, is the ability to set breakpoints and step through code.

I decided to take a crack at mashing up TextMate’s ‘bookmark line’ functionality and PDB to emulate this.

First, bookmark the lines that you want to break on:

textmate_bookmark

Then, run this script, passing in the name of the file you want to debug:

 1 import xattr, zlib, sys, subprocess, os
 2 
 3 
 4 def xattr_to_list(unzipped):
 5     unwanted = ['(',')','\n',' ']
 6     for char in unwanted:
 7         unzipped = unzipped.replace(char, '')
 8     return (unzipped.split(",")
 9 
10 def main(infile):
11     pdbrc = open('~/.pdbrc','w')    
12     breakpoint_list = xattr.getxattr('%s' % infile,'com.macromates.bookmarked_lines')
13     try:
14         breakpoint_list = breakpoint_list.decode('zip')
15     except zlib.error:
16         pass
17     for breakpoint in xattr_to_list(breakpoint_list):
18         pdbrc.write("b %s:%d\n" % (infile,(int(breakpoint) + 1)))
19     pdbrc.close()
20 
21 if __name__ == "__main__":
22     try:
23         infile = sys.argv[1]
24         if os.path.exists(infile): 
25             main(infile)
26         else:
27             pass
28     except IndexError:
29         print "No file provided."

The script will create a .pdrbc in your home directory.

Lastly, run: python -m pdb name_of_your_script.py

Follow the debug steps in the docs here

Happy debugging!

Comments