Best way to handle python pdb flakiness re / stdout?

If I have a program that is being redirected to stdout, my pdb hints all go redirected because the library was written to write to stdout.

Often this problem is subtle, making me think that the program is hanging when it is really waiting for input.

How do people work around this? (Unfortunately, using other debuggers like winpdb is not an option.)

+5
source share
3 answers

The problem is that PDB uses the Cmd class, by default:

use_rawinput = 1

, Cmd raw_input() sys.stdout.readline() . , raw_input() ( readline) . , raw_input() , , script:

#!/usr/bin/python
name=raw_input("Enter your name: ")

> python test.py
Enter your name: Alex

,

> python test.py | tee log

, PDB . , sys.stdin.readline() script readline(), .

, , Cmd raw_input():

Cmd.use_rawinput = 0

pdb = pdb.Pdb()
pdb.use_rawinput=0
pdb.set_trace()
+3

, Ned, pdb.py main() , 40 , :

# sane_pdb.py: launch Pdb with stdout on original
import sys, pdb
def fixed_pdb(Pdb=pdb.Pdb):
    '''make Pdb() tied to original stdout'''
    return Pdb(stdout=sys.__stdout__)

if __name__ == '__main__':
    pdb.Pdb = fixed_pdb
    pdb.main()

, , , ...

+4

pdb , stdout . sys.__stdout__ .

pdb , main() pdb.py sane_pdb.py. Pdb() :

pdb = Pdb(stdout=sys.__stdout__)

sane_pdb.py pdb.py. , 40 , , .

+2

All Articles