Friday, May 23, 2008

Debugging Python Extension Module

There's some useful technique for debugging Python extension module, gathered during these days work.

  • Clewn is a way to combine VIM and GDB.
  • Set break on PyErr_Format or PyErr_SetString so you can trace a Python exception raised from the extension module
  • Use 'make EXTEA_CFLAGS="-ggdb3 -O0"' to compile Python so you can have debugging symbol in the compiled executable. (Thanks my mentor Richard told me that.)
  • There is a Misc/gdbinit in Python's code base. It defined many useful macro for GDB. Put the file in your working directory and rename it to .gdbinit to use it. The most useful macro is "pyo", which prints many useful information of a PyObject.
  • Valgrind is also a good debugging tool. (Btw, it has won the Trolltech Open Source Development Award recently.) There's a Misc/valgrind-python.supp for it.
  • [Python-Dev] disappearing exceptions explained some odd behavior I've ever encountered. Seems even the print() statement also swallow exception. (At least in Python 3.0)
Anyway, happy hacking!

No comments: