'_Stream' object has no attribute 'seekable'
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 36k
- PR 合併指標
- PR 指標待擷取
描述
Bug report
Bug description:
From a recent application, I have found tarfile._Stream doesn't have a seekable() method. A quick reproduction of the problem is as follows:
import tarfile
import io
data = b"Test data."
buf = io.BytesIO()
with tarfile.open(fileobj=buf, mode="w") as tf:
ti = tarfile.TarInfo(name="test.txt")
ti.size = len(data)
tf.addfile(tarinfo=ti, fileobj=io.BytesIO(data))
buf.seek(0)
tf = tarfile.open(fileobj=buf, mode="r|")
for entry in tf:
f = tf.extractfile(entry) # `f` is in type of `class ExFileObject(io.BufferedReader)`
f.seekable() # AttributeError
f.seek(0) # AttributeError
Output:
Traceback (most recent call last):
File "a.py", line 17, in <module>
print(f.seekable())
File "AppData\Local\Programs\Python\Python310\lib\tarfile.py", line 641, in seekable
return self.fileobj.seekable()
AttributeError: '_Stream' object has no attribute 'seekable'
When opening tarfile with r| mode, ExFileObject.fileobj would be a _Stream object. So when calling ExFileObject.seekable(), as _Stream object dun have seekable() method, it will raise AttributeError.
Also, as BufferedReader.seek() will also check its raw.seekable() (i.e. in Modules/_io/bufferedio.c, func _io__Buffered_seek_impl(), there is a call to _PyIOBase_check_seekable(state, self->raw, Py_True)), f.seek() in the above example will also raise AttributeError.
I have thought of if we can simply define seekable() in _Stream object, but as it is only "forward-seekable", I am not sure if directly returning a true/false in _Stream.seekable() is appropriate or not. However, this issue does break the IOBase interface and there should be a solution.
Thanks for everyone's time and effort for looking into this.
CPython versions tested on:
3.10, 3.11, 3.12, 3.13, CPython main branch
Operating systems tested on:
Linux, Windows
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
首先在 r| mode 下執行提供的 tarfile 重現,接著檢查 tarfile.py 中的 tarfile._Stream 和 ExFileObject,以及 Modules/_io/bufferedio.c 中的 _io__Buffered_seek_impl()。判斷僅能向前讀取的串流應採用的適當 seek 行為,並確認回報的 seekable() 和 seek() AttributeErrors 已獲解決,同時不會破壞串流處理語意。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- backend
- Issue 類型
- 缺陷
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 38/100