pickle does not work with unbuffered streams
还没有人认领这个 Issue。
评估
调研方向
首先阅读 pickle.load()、pickle.dump()、marshal.load() 和 marshal.dump() 入口,以及 read() 和 write() 有文档记录的流契约。使用无缓冲流重现报告的案例,然后确定应更改实现还是文档;完成的标准是大型读取和写入不会截断数据或在不知情的情况下损坏数据。
由索引模型根据 Issue 内容生成。
描述
The documentation for pickle.load() says:
The argument file must have two methods, a read() method that takes an integer argument, and a readline() method that requires no arguments. Both methods should return bytes. Thus file can be an on-disk file opened for binary reading, an io.BytesIO object, or any other custom object that meets this interface.
However, the following code doesn’t work:
import pickle
large_bytes = b'x' * (1 << 31)
with open('test.pickle', 'wb') as w:
pickle.dump(large_bytes, w)
with open('test.pickle', 'rb', 0) as r:
assert pickle.load(r) == large_bytes
It fails with:
Traceback (most recent call last):
File "test_pickle.py", line 9, in <module>
assert pickle.load(r) == large_bytes
_pickle.UnpicklingError: pickle data was truncated
Contrary to the documentation, pickle.load() requires that the file’s read() method returns as many bytes as requested. This is the case for buffered binary streams unless the underlying raw stream is interactive (source). However, it is not the case for unbuffered binary streams if the operating system can’t read enough bytes at once. On my system this is the case for bytestrings longer than (1 << 31) - 4096 bytes. For pipes, the limit is 1 << 16 bytes on my system.
pickle.dump() has a similar problem. Its documentation says:
The file argument must have a write() method that accepts a single bytes argument. It can thus be an on-disk file opened for binary writing, an io.BytesIO instance, or any other custom object that meets this interface.
The above code with an unbuffered writer and buffered reader results in the same exception.
If the bytestring is one byte longer that what the operating system can write at once, loading it works but returns a wrong result.
import pickle
large_bytes = b'x' * ((1 << 31) - 4095)
with open('test.pickle', 'wb', 0) as w:
pickle.dump(large_bytes, w)
with open('test.pickle', 'rb') as r:
assert pickle.load(r) == large_bytes
fails with:
Traceback (most recent call last):
File "test_pickle.py", line 9, in <module>
assert pickle.load(r) == large_bytes
AssertionError
because the last byte of the unpickled bytestring is b'\x94' (MEMOIZE opcode).
marshal.load() / marshal.dump() have a similar problem, except that I couldn’t find an example like the previous in which the data was corrupted in-between, as marshal creates a buffer for the whole output and writes it to the stream at once. Also marshal’s maximum supported bytes length is (1 << 31) - 1, so the above example has to be adapted.
Possible solutions
The documentation should match the actual requirements of the implementation. The documentation could be changed to mention the additional restrictions, or the implementation could be changed to call read() / write() multiple times if necessary.
If it is decided that the implementation should not call write() multiple times, I think that at least an exception should be thrown to avoid silent data corruption.
Environment
- CPython versions tested on: 3.10.4
- Operating system and architecture: Linux x86_64
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 36k
- 平均合并
- 1 天 9 小时
- 30 天内合并 PR
- 558
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
python/cpython 的其他 Issue
-
docs pending
难度 2/5 1-3 小时 新手友好度 78/100
-
stdlib type-feature
难度 2/5 1-3 小时 新手友好度 78/100
-
stdlib type-feature
难度 2/5 1-3 小时 新手友好度 72/100
-
build type-bug
难度 2/5 1-3 小时 新手友好度 76/100
-
stdlib topic-email type-feature
难度 2/5 1-3 小时 新手友好度 70/100
相似的 Issue
-
难度 1/5 1 小时以内 新手友好度 90/100
-
bug
难度 2/5 1-3 小时 新手友好度 86/100
zostera/django-bootstrap4#894 ·
-
难度 2/5 1-3 小时 新手友好度 78/100
use-agent-os/agent-os#3276 ·
-
难度 2/5 1-3 小时 新手友好度 88/100
zephyrproject-rtos/zephyr#119726 ·
-
area/auth bug comp/agent P3 platform/discord type/security
难度 2/5 1-3 小时 新手友好度 88/100
NousResearch/hermes-agent#117848 ·