Mark Galeck
Mark Galeck

Reputation: 6385

how to use Python pdb.set_trace() with reassigned sys.stdout?

I want to filter the output of a Python script through a command, and at the same time be able to use pdb.set_trace() normally.

I run this script:

#!/usr/bin/python

import sys, os, pdb

print "first line"
sys.stdout = os.popen('./filter', 'w')
#pdb.set_trace()
print "second line"
print "third line"  

and the filter script is this:

#!/usr/bin/python

import subprocess, sys

subprocess.call( 'cat', stdout=sys.stdout, stderr=sys.stderr, shell=True )

Everything works fine, I see the output on the terminal. But, when I uncomment the set_trace line, now, the debugger apparently breaks and I can use commands, but I don't see their output (until the whole program exists), so interactive debugging is broken.

How to change filter so that the interactive debugging works?

Upvotes: 1

Views: 1393

Answers (1)

John La Rooy
John La Rooy

Reputation: 304137

You could try making your own Pdb instance. eg.

mypdb = pdb.Pdb(stdout=sys.__stdout__)

Upvotes: 5

Related Questions