简体   繁体   English

如何将Python pdb.set_trace()与重新分配的sys.stdout一起使用?

[英]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. 我想通过命令过滤Python脚本的输出,同时能够正常使用pdb.set_trace()

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: filter脚本是这样的:

#!/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. 但是,现在,当我取消注释set_trace行时,调试器显然会中断,并且可以使用命令,但是看不到它们的输出(直到整个程序存在),因此交互式调试已中断。

How to change filter so that the interactive debugging works? 如何更改filter以使交互式调试起作用?

You could try making your own Pdb instance. 您可以尝试制作自己的Pdb实例。 eg. 例如。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM