簡體   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