Tuples should be immutable and safe in C, as well as in Python.
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
Bug report
Bug description:
[Apologies if this sounds a bit like a rant. I'm not blaming anyone. Just because something is the wrong choice now, doesn't mean it wasn't the right choice historically]
Tuples are immutable in Python, but we play all sorts of games in C with tuples, filling them will NULLs, mutating them and reusing them.
We do this in the mistaken belief that it improves performance.
But it doesn't. It makes the code base more complicated and fragile as we need to work around tuples that misbehave and do strange things. Any local performance gain is overwhelmed by slowdowns caused by the extra complexity in tuple code, the garbage collector and a few other places.
So let's fix this.
We need to:
- Provide a new C API
PyTuple_MakePair(). Pairs are by far the most common type of tuple that we play games with. By providing a fast way to create pairs, we can provide an upgrade path for C code that creates tuples in unsafe ways to do so safely and quickly. - Deprecate
PyTuple_New. I don't know when we'll be able to remove it, but we should deprecate it ASAP. - Change
PyTuple_Newto fill the tuple with pointers toNoneinstead ofNULL. This doesn't fix the mutability issue, but it at least means the GC will only see valid objects. (This might break too much code, so we might just have to clearly document that tuples should be fully initialized in one go, before the tuple escapes the function it was created in) - Fix our own code to not use
PyTuple_New()or perform tuple shenanigans. We can't reasonably expect third-party package authors to follow the rules if we don't.
CPython versions tested on:
CPython main branch
Operating systems tested on:
No response
Linked PRs
- gh-127758
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先检查提议的 C API PyTuple_New 和 PyTuple_MakePair,然后查看关联的 PR gh-127758,以了解已经在进行的工作。完成意味着处理所请求的元组安全性更改,包括 API 迁移、初始化行为以及移除不安全的内部元组使用。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- c, python
- 领域
- backend
- Issue 类型
- 缺陷
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 描述清楚
- 新手友好度
- 25/100