How to do it...

To interact with pdb, you need to use its language, which allows you to move around the code, examine and modify the values of the variables, insert breakpoints, or move through stack calls:

  1. Use the where command (or alternatively, the compact form, w) to view which line of code is running and the call stack. In this case, this is on line 17 in the go() method of the pdb_test.py module:
> python -m pdb pdb_test.py
-> class Pdb_test(object):
(Pdb) where
c:python35libdb.py(431)run()
-> exec(cmd, globals, locals)
<string>(1)<module>()
(Pdb)
  1. Inspect the lines of code near the current location (indicated by an arrow) by using list. In the default mode, 11 rows are listed around the current one (five before and five after):
 (Pdb) list
1 -> class Pdb_test(object):
2 def __init__(self, parameter):
3 self.counter = parameter
4
5 def go(self):
6 for j in range(self.counter):
7 print ("--->",j)
8 return
9
10 if __name__ == '__main__':
11 Pdb_test(10).go()
  1. If list receives two parameters, then they are interpreted as the first and last lines to be displayed:
 (Pdb) list 3,9
3 self.counter = parameter
4
5 def go(self):
6 for j in range(self.counter):
7 print ("--->",j)
8 return
9
  1. Use up (or u) to move to older frames on the stack and down (or d) to move to more recent stack frames:
(Pdb) up
> <string>(1)<module>()
(Pdb) up
> c:python35libdb.py(431)run()
-> exec(cmd, globals, locals)
(Pdb) down
> <string>(1)<module>()
(Pdb) down
>....pdb_test.py(1)<module>()
-> class Pdb_test(object):
(Pdb)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset