[英]When is the attribute `isatty()` not present in sys.stderr?
我的猜测是,这只会在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.