python / python/cpython

_io.TextIOWrapper._CHUNK_SIZE ignores underlying buffer size, defaults 8192

未关闭
#153,656 9 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

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

描述

Bug report

Bug description:

If I try to read a line from a file containing a very long line using open(path, buffering=n).readline(), it reads in chunks of 8192 regardless of the value of n. I would like it to respect n so that I can reduce the number of syscalls.

I can manually work around this by setting the undocumented attribute _CHUNK_SIZE, but I think this should be done automatically. The number 8192 is hard-coded in _io_TextIOWrapper___init___impl; I propose automatically setting it to the buffer size of the underlying _io.BufferedReader / _io.BufferedWriter / _io.BufferedRandom (if the underlying object is one of those).

docker run --rm -it python:3.14.6 bash
apt-get update && apt-get install -y strace
python3 -c 'print("x"*100000)' > f
# f.readline ignores the buffer size
strace -e trace=read python3 -c 'open("f", buffering=100000).readline()' # calls `read(3, ..., 8192)`
# workaround: set f._CHUNK_SIZE
strace -e trace=read python3 -c 'f = open("f", buffering=100000); f._CHUNK_SIZE = 1000000; f.readline()' # calls `read(3, ..., 1000000)`
# aside: f.buffer.readline respects the buffer size
strace -e trace=read python3 -c 'f = open("f", buffering=100000); f.buffer.readline()' # calls `read(3, ..., 100000)`
CPython versions tested on:

3.14

Operating systems tested on:

Linux

贡献指南

打开贡献指南

从这里开始

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

调研方向

从 Modules/_io/textio.c 中的 _io_TextIOWrapper___init___impl 开始,issue 在这里指出了硬编码的 8192 块大小。使用提供的 open()、readline() 和 strace 命令复现该行为,然后追踪底层缓冲对象的大小是如何暴露的。完成标准是:readline() 针对相关的缓冲对象类型使用该缓冲区大小,而不需要 _CHUNK_SIZE。

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

评估

技术栈
c, python
领域
backend, performance
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
冷清
描述清晰度
描述清楚
新手友好度
58/100

把新 issue 发到你的邮箱

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