Difference between revisions of "Python/Debugging"

From SambaWiki
(fix formatting)
(Add note about using PYTHONMALLOC=malloc)
 
(2 intermediate revisions by one other user not shown)
Line 18: Line 18:
   
 
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.
 
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.
   
 
=== Valgrind ===
 
=== Valgrind ===
   
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.
+
Running Python with PYTHONMALLOC=malloc will have it use malloc() as its memory allocator, avoiding most warnings emitted by Valgrind. There is also 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:
 
Example usage:
Line 28: Line 30:
 
valgrind --suppressions=/usr/lib/valgrind/python.supp python foo.py
 
valgrind --suppressions=/usr/lib/valgrind/python.supp python foo.py
 
</pre>
 
</pre>
  +
  +
  +
== Pyflakes ==
  +
  +
Pyflakes is a simple static code checker for Python. It has some nice integration for emacs and vim:
  +
  +
* emacs: http://plope.com/Members/chrism/flymake-mode
  +
* vim: http://www.vim.org/scripts/script.php?script_id=2441

Latest revision as of 23:38, 9 September 2021

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
pdb.set_trace()

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.

GDB

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.

Valgrind

Running Python with PYTHONMALLOC=malloc will have it use malloc() as its memory allocator, avoiding most warnings emitted by Valgrind. There is also 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 foo.py


Pyflakes

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