Use `PyObject_GetTypeData` without the GIL/attached thread state
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
Feature or enhancement
Proposal:
In Cython we've historically let people do things like:
cdef class C:
cdef int non_object_attribute
def f(C c):
with nogil:
value = c.non_object_attribute
...
i.e. we've allowed them to get direct access into extension types without the GIL (providing they aren't manipulating Python objects).
When using PEP 697 lookup this becomes a little trickier because access to the attribute has to go through PyObject_GetTypeData. Looking at the implementation of PyObject_GetTypeData, it doesn't appear to need the GIL (except in debug mode where it sanity-checks the type).
I think I can work around this by calculating the offset of the struct myself once at startup (using PyObject_GetTypeData) and then doing all the lookups manually via the offset. It also looks like nanobind use a similar trick in places so I wouldn't be alone in this abuse of the system.
However, it'd be nice not to have to do this and if PyObject_GetTypeData could somehow be exempted from the "C API needs the GIL" rule.
I do understand that people would probably prefer not to do this, so I'm happy to look at other options.
Has this already been discussed elsewhere?
I have already discussed this feature proposal on Discourse
Links to previous discussion of this feature:
I asked about this in https://discuss.python.org/t/does-pyobject-gettypedata-need-the-gil/104637/4 - it only generated a small amount of discussion.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先阅读 PyObject_GetTypeData 的实现,以及关于其是否需要 GIL 的链接 Discourse 讨论。确定对于 PEP 697 属性访问,是否可以采用受支持的无 GIL 方案,并为 Cython 用户记录明确的结果或替代方案。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- c, python
- 领域
- api
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 32/100