我想捕获从python模块A
生成的错误消息。{< CD1> }是用C++和SWIG编写的,因此我不能捕获Python的^ {CD3}}。在
>>> import A
>>> A.func() # this prints an error message by C++ std::cerr.
this is an error message from module A
我要做的是抑制错误消息并根据错误类型更改脚本行为。但是A.func()
不返回错误号。在
假设我对下面的contextlib的使用是正确的,这并没有帮助。在
^{pr2}$
谢谢@PM2Ring
https://eli.thegreenplace.net/2015/redirecting-all-kinds-of-stdout-in-python/#id1
只需稍作修改:将所有的
stdout
替换为stderr
,并支持如下所示的macOS,它完全如预期的那样工作。在相关问题 更多 >
编程相关推荐