繁体   English   中英

何时sys.stderr中不存在属性“ isatty()”?

[英]When is the attribute `isatty()` not present in sys.stderr?

有时,此属性不存在。 (例如thisthis )什么时候发生? 有办法解决吗? 我可以以某种方式强行添加此属性以使事情运行吗?

我的猜测是,这只会在sys.stderr被模拟或猴子修补时发生。

例如,您引用的第一个GitHub问题具有以下错误消息:

AttributeError: 'UnicodeStdout' object has no attribute 'isatty'

这个类UnicodeStdout不在标准库中,我的研究向我指出了hachoir lib的旧版本(例如, commit )。

第二个链接还有另一个可疑的错误消息,其中单独的类名称表明sys.stdout被弄乱了:

AttributeError: '_StdoutProxy' object has no attribute 'isatty'

此类似乎像一个“代理”,并未实现原始sys.stdout所有方法。 这不仅会影响isatty() ,而且可能还会影响其他属性,例如fileno (例如,请参见此其他问题 。)

您可以自己对属性进行猴子修补:

sys.stderr = SomeProxyClass()

或添加支票,例如:

if hasattr(sys.stderr, 'isatty') and sys.stderr.isatty()

但在大多数情况下,这是没有必要的。

暂无
暂无

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

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