python / python/cpython

`trace` function is cleared after `RecursionError` is fired

Đang mở
#134,216 5 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

interpreter-core pending type-bug
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Chạy reproducer được cung cấp và kiểm tra hành vi của sys.settrace, sys.gettrace và RecursionError xung quanh lời gọi đệ quy factorial. Truy vết các đường dẫn triển khai của CPython liên quan đến việc xử lý giới hạn đệ quy và tracing; công việc được xem là hoàn tất khi hàm trace đã đăng ký vẫn khả dụng sau RecursionError, với kiểm thử hồi quy cho reproducer.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
compilers
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
45/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.