Table des matières

Python - Debug

Voir :

pdb

Voir https://docs.python.org/fr/3/library/pdb.html

Insérer

import pdb; pdb.set_trace()

Ou lancer le programme en mode debug sans modifier le code

python2 -m pdb /usr/local/bin/plop.py

Syntax

b pour Breakpoint
Ajout un point d'arrêt ou liste les points d'arrêt

l ou ll
Affiche une portion du code

cl(ear)
Clear breakpoint

p ou pp
Affiche la valeur d'une variable

! statement
Executer du code Python. Dans bien des cas le ! pourra être omis

s(tep)
Exécute la ligne en cours, s'arrête à la première occasion possible (soit dans une fonction qui est appelée, soit sur la ligne suivante de la fonction courante).

n(ext)
La différence entre next et step est que step s'arrête dans une fonction appelée, tandis que next exécute les fonctions appelées à (presque) pleine vitesse

restart
Redémarrer le programe Python débugé.

quit

Exemple

(Pdb) b plop:49
Breakpoint 1 at /usr/local/bin/plop.py:49
(Pdb) r
> /usr/local/bin/plop.py(49)__init__()
-> self.root_wrappers = root_wrappers
(Pdb) p self.svcid
'event-abcd'
(Pdb) self.svcid='event-abcdef'
(Pdb) p self.svcid
'event-abcdef'
(Pdb) r
--Return--
> /usr/local/bin/plop.py(50)__init__()->None
-> self.pwd = pwd.getpwnam(self.svcid)
(Pdb) p self.svcid
'event-abcdef'
(Pdb) r
KeyError: 'getpwnam(): name not found: event-abcdef'
> /usr/local/bin/plop.py(283)fetch_svcids()
-> return [CronTab(site, svcid['nom'], svcid['active'], options, env, root, template_wrapper, root_wrappers) for svcid in svcids]
(Pdb) p [CronTab(site, svcid['nom'], svcid['active'], options, env, root, template_wrapper, root_wrappers) for svcid in svcids]
*** KeyError: KeyError('getpwnam(): name not found: event-abcdef',)
(Pdb) self.svcid='event-abcd'
*** NameError: name 'self' is not defined
(Pdb) b
Num Type         Disp Enb   Where
1   breakpoint   keep yes   at /usr/local/bin/plop.py:49
        breakpoint already hit 1 time
(Pdb) restart
Restarting /usr/local/bin/plop.py with arguments:

> /usr/local/bin/plop.py(6)<module>()
-> """

Ipdb

pip install ipdb
import ipdb; ipdb.set_trace()

trace

python -m trace --trace somefile.py