python / python/cpython

Inconsistant behavior of .readable() and .writable() on io.IOBase

未关闭
#96,780 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

extension-modules topic-IO type-bug
主要语言
Python
星标
77.2k
派生
36k
PR 合并指标
PR 指标待抓取

描述

Bug report

The methods .readble() and .writable() on io.IOBase should return a boolean representing if the file can be read from/written to. However, calling .readable() on a closed IO file object causes a ValueError, whereas calling .writable() on a closed file returns False if the file was opened with just read permissions, but raises ValueError if it was opened with read and write permissions.

Make a folder with a text file in, then run:

f = open("hello.txt")
print(f.writable())
print(f.readable())
f.close()
print(f.writable())
print(f.readable())

This will output:

False
True
False
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file

Then doing a similar thing, but with w+ permissions

f = open("hello.txt", "w+")
print(f.writable())
print(f.readable())
f.close()
print(f.writable())
print(f.readable())

Will output

True
True
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file

Your environment

  • CPython versions tested on: 3.10.4
  • Operating system and architecture: Windows 64bit

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先复现 issue 中关于已关闭文件的两个示例,然后检查 io.IOBase.readable() 和 writable() 入口点及其相关测试。完成的标准是,这两个方法对已关闭文件具有一致且有文档记录的行为,同时保留对打开文件的预期结果。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
operating-systems
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
38/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。