From SambaWiki
Jump to: navigation, search

Debugging Python code

Debugging Python code

A commonly used trick is to import the Python debugger at some point where you would like to do debugging. Just add something like this:

import pdb

and Python will drop into the Python debugger, allowing you to inspect variables, execute code, step into/next/continue, etc. The commands are pretty similar to those of gdb.

Debugging Python extensions

As Python extensions are written in C, it is usually not sufficient to rely on pdb when debugging extensions.


There are some useful gdb macros distributed with Python that allow you to easily inspect the reference count of python objects, their string representation as used in Python and the python stack from within gdb. On Debian/Ubuntu systems the file with macros can be found in /usr/share/doc/pythonX.Y/gdbinit. Copy this file to ~/.gdbinit to use it.

See the file itself for the new commands that it provides and an explanation of their use.


There is a suppressions file for Python's memory manager (pymalloc). It is part of the Python source tree, and distributed inside of the Python package in some distributions (Debian/Ubuntu put it in the "python" package, in /usr/lib/valgrind/python.supp). With this suppressions file enabled you *should* have no valgrind warnings from Python with any of the Samba modules loaded. See also /usr/share/doc/python2.4/README.valgrind for a more detailed explanation.

Example usage:

valgrind --suppressions=/usr/lib/valgrind/python.supp python


Pyflakes is a simple static code checker for Python. It has some nice integration for emacs and vim: