`trace` function is cleared after `RecursionError` is fired
未關閉
還沒有人認領這個 Issue。
interpreter-core
pending
type-bug
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 36k
- 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