`trace` function is cleared after `RecursionError` is fired
未关闭
还没有人认领这个 Issue。
interpreter-core
pending
type-bug
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
Bug report
Bug description:
I'm using coverage and pytest-cov in CI to measure the line coverage of the unittests. It is achieved by registering a trace function with sys.settrace.
However, after binsecting my unittests, I found that when a RecursionError is raised, the system trace function will be cleared. That will cause a warning emitted by coverage:
~/Projects/cpython/venv/lib/python3.15t/site-packages/coverage/pytracer.py:355: CoverageWarning: Trace function changed, data is likely wrong: None != <bound method PyTracer._trace of <PyTracer at 0x200021dcc20: 2076 data points in 11 files>> (trace-changed)
self.warn(
Reproducible code:
import sys
def tracer(*args, **kwargs):
pass
def factorial(n: int) -> int:
"""Calculate the factorial of a number."""
if n <= 1:
return 1
return n * factorial(n - 1)
sys.settrace(tracer)
assert sys.gettrace() is tracer
sys.setrecursionlimit(64)
assert sys.gettrace() is tracer
try:
_ = factorial(100)
except RecursionError:
pass
assert sys.gettrace() is None
REPL Output:
# Add a code block here, if required
$ python3
Python 3.13.3 (main, Apr 8 2025, 13:54:08) [Clang 16.0.0 (clang-1600.0.26.6)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> def tracer(*args, **kwargs):
... pass
...
>>> def factorial(n):
... if n <= 1:
... return 1
... return n * factorial(n - 1)
...
>>> sys.settrace(tracer)
>>> sys.gettrace() is tracer
True
>>> sys.setrecursionlimit(64)
>>> _ = factorial(100)
Traceback (most recent call last):
File "<python-input-6>", line 1, in <module>
_ = factorial(100)
File "<python-input-2>", line 4, in factorial
return n * factorial(n - 1)
~~~~~~~~^^^^^^^
File "<python-input-2>", line 4, in factorial
return n * factorial(n - 1)
~~~~~~~~^^^^^^^
File "<python-input-2>", line 4, in factorial
return n * factorial(n - 1)
~~~~~~~~^^^^^^^
[Previous line repeated 51 more times]
File "<python-input-2>", line 1, in factorial
def factorial(n):
RecursionError: maximum recursion depth exceeded
>>> sys.gettrace() is None
True
CPython versions tested on:
CPython main branch
Operating systems tested on:
macOS
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
运行提供的 reproducer,并检查递归调用 factorial 附近 sys.settrace、sys.gettrace 和 RecursionError 的行为。跟踪 CPython 中涉及递归限制处理和 tracing 的实现路径;完成的标准是注册的 trace 函数在 RecursionError 之后仍然可用,并且为该 reproducer 提供回归测试覆盖。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- compilers
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 45/100