Python获取封闭文件描述符的内容

问题描述

我正在为程序编写一堆单元测试。函数之一写入特定的文件描述符,然后关闭文件描述符。我正在尝试寻找一种针对此特定功能进行单元测试的最佳方法,而无需更改相关功能

有没有办法获取关闭文件描述符的内容?还是以某种方式复制了该文件描述符,即使关闭了该文件描述符,重复项仍然可以读取内容

示例:

def writer(self):
    print("testing only",file=self.some_file_descriptor)
    self.some_file_descriptor.close()

单元测试将类似于:

def test_writer():
    ...
    a.writer()
    assert fd == "testing only"  ## where fd is the duplicate file descriptor

我尝试将sys.stdout作为self.some_file_descriptor传递,但是当那关闭时,我会得到一堆ValueError: I/O operation on closed file.,因为其他人会在上面写东西。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)