Python 3 sys.stdout.fileno() 在 Python 2 中的等价物是什么

What is the equivalent of Python 3 sys.stdout.fileno() in Python 2

我正在编写 Python 2.7 中的代码。我使用一个非常具体的库,部分用 C 编写,带有 Python API。该库使用 printf 输出到标准输出。我想压制它。我已经找到了这个问题:How do I prevent a C shared library to print on stdout in python? 所有提供的答案都使用 sys.stdout.fileno()。当我 运行 我的代码时,我得到:

original_stdout_fd = sys.stdout.fileno()
AttributeError: 'FlushingStringIO' object has no attribute 'fileno'

我怀疑问题是我使用的是 Python 2.7。我的假设是否正确,有没有办法用 Python 2.7 实现?

问题是 sys.stdout 被库替换为 FlushingStringIO:https://github.com/flexible-atomic-code/fac。它也以某种方式依赖于这个细节,否则就会中断。由于没有合理的替代库,因此在这种情况下无法抑制标准输出输出。