Code object comparison could lead to monitoring issue
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
Bug report
Bug description:
Currently we compare code objects with a lot of its features, but not the file it is defined in - that might make some sense because the two code objects are "equivalent" as long as all their features match. However, the two code objects could still be two different ones - for example, if they are two exact same functions defined in two different files (this is a real case for me and it took me a while to figure it out).
The problem is, code objects can be used as keys. When we have "equal but not the same" code objects, it could affect monitoring. We thought we were monitoring one code object while we are actually monitoring another one.
The code was like this:
@cache
def get_code_lineno(code):
...
return {code: [...]}
assert code1 == code2 # two code objects in two different files
for c, linenos in get_code_lineno(code1):
sys.monitoring.set_local_events(tool, c, E.LINE)
for c, linenos in get_code_lineno(code2):
# This does not instrument code2! This instruments code1 because of the cache!
sys.monitoring.set_local_events(tool, c, E.LINE)
Do we consider this an expected behavior? Should we just "live with it" as it's not that common? (Again, this is a real issue I had when I was working on a project).
@markshannon
CPython versions tested on:
3.12, CPython main branch, 3.13, 3.14
Operating systems tested on:
No response
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
该报告没有提到源文件或测试。首先,使用来自不同文件的等效代码对象重现缓存查找,然后检查代码对象比较以及 sys.monitoring.set_local_events 入口点。在回归测试能够验证监控不会针对错误的代码对象之前,必须先确定预期行为。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- devtools
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 需要澄清
- 新手友好度
- 30/100