Hi there
I’m sure you love indentation.
I think it would be nice if pdb
output is formatted with the indentation when Call/Return calls.
The test code:
def gcd(n, m):
"""Greatest common divisor.
Note: gcd(n, m) == gcd(m % n, n)
"""
return m if n == 0 else gcd(m % n, n)
if __name__ == "__main__":
breakpoint()
print(gcd(24,42))
The output would be: (click to expand)
> c:\usr\home\lib\python\test_pdb.py(23)<module>()
-> print(gcd(24,42))
(Pdb) s
--Call--
> c:\usr\home\lib\python\test_pdb.py(3)gcd()
-> def gcd(n, m):
(Pdb)
> c:\usr\home\lib\python\test_pdb.py(7)gcd()
-> return m if n == 0 else gcd(m % n, n)
(Pdb)
--Call--
> c:\usr\home\lib\python\test_pdb.py(3)gcd()
-> def gcd(n, m):
(Pdb)
> c:\usr\home\lib\python\test_pdb.py(7)gcd()
-> return m if n == 0 else gcd(m % n, n)
(Pdb)
--Call--
> c:\usr\home\lib\python\test_pdb.py(3)gcd()
-> def gcd(n, m):
(Pdb)
> c:\usr\home\lib\python\test_pdb.py(7)gcd()
-> return m if n == 0 else gcd(m % n, n)
(Pdb)
--Call--
> c:\usr\home\lib\python\test_pdb.py(3)gcd()
-> def gcd(n, m):
(Pdb)
> c:\usr\home\lib\python\test_pdb.py(7)gcd()
-> return m if n == 0 else gcd(m % n, n)
(Pdb)
--Return--
> c:\usr\home\lib\python\test_pdb.py(7)gcd()->6
-> return m if n == 0 else gcd(m % n, n)
(Pdb)
--Return--
> c:\usr\home\lib\python\test_pdb.py(7)gcd()->6
-> return m if n == 0 else gcd(m % n, n)
(Pdb)
--Return--
> c:\usr\home\lib\python\test_pdb.py(7)gcd()->6
-> return m if n == 0 else gcd(m % n, n)
(Pdb)
--Return--
> c:\usr\home\lib\python\test_pdb.py(7)gcd()->6
-> return m if n == 0 else gcd(m % n, n)
(Pdb)
6
--Return--
> c:\usr\home\lib\python\test_pdb.py(23)<module>()->None
-> print(gcd(24,42))
(Pdb)
This can be achieved by:
the monkey patch code example (click to expand)
from pdb import Pdb
class Debugger:
Pdb.indents = ''
Pdb.prompt = '(Pdb) '
def message(self, msg, indent=True):
prefix = self.indents if indent else ''
print("{}{}".format(prefix, msg), file=self.stdout)
Pdb.message = message
def print_stack_entry(self, frame_lineno):
frame, lineno = frame_lineno
if frame is self.curframe:
prefix = '> '
else:
prefix = ' '
prompt_prefix = "\n{}-> ".format(self.indents)
self.message(prefix +
self.format_stack_entry(frame_lineno, prompt_prefix))
Pdb.print_stack_entry = print_stack_entry
def user_call(self, frame, argument_list):
if len(self.indents) < 20: # indent max.
self.indents += ' '*2
self.prompt = self.indents + self.prompt.lstrip()
if self._wait_for_mainpyfile:
return
if self.stop_here(frame):
self.message('--Call--')
self.interaction(frame, None)
Pdb.user_call = user_call
def user_return(self, frame, return_value):
frame.f_locals['__return__'] = return_value
self.message('--Return--')
if len(self.indents) > 2: # indent min.
self.indents = self.indents[:-2]
self.prompt = self.indents + self.prompt.lstrip()
self.interaction(frame, None)
Pdb.user_return = user_return