Redirecting stdout failure with help function inside `__repr__`

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

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

Đánh giá

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

Hướng nghiên cứu

Bắt đầu bằng cách tái hiện ví dụ với contextlib.redirect_stdout, StringIO, help(), repr lồng nhau và các phép gán doc cho instance trên các phiên bản CPython được báo cáo. Theo dõi cách help() định dạng đối tượng và ghi đầu ra trong lần gọi thứ nhất và thứ hai. Hoàn tất khi việc tái hiện không còn làm rò rỉ đầu ra của help() hoặc phát sinh IndexError khi stdout được chuyển hướng.

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

Mô tả

interpreter-core type-bug

Bug report

Despite modifying sys.stdout, the help function appears to ignore it sometimes during nested usage within __repr__ and with instance __doc__ attributes.

Example:

from contextlib import redirect_stdout
from io import StringIO


class ReprDoc:
    """Class doc-string."""

    def __init__(self, value):
        self.__doc__ = value.__doc__
        self.value = value

    def __repr__(self):
        print("<===Starting Stdout Redirect===>")
        with redirect_stdout(StringIO()) as doc:
            help(self.value)
        print("<===Stopping Stdout Redirect===>")
        return doc.getvalue().splitlines()[2]


def foo():
    """Foo doc-string."""

help(ReprDoc(foo))
help(ReprDoc(foo))

Output:

<===Starting Stdout Redirect===>
<===Stopping Stdout Redirect===>
Help on ReprDoc in module __main__:

foo()
    Foo doc-string.

<===Starting Stdout Redirect===>
Help on function foo in module __main__:

foo()
    Foo doc-string.
<===Stopping Stdout Redirect===>
Traceback (most recent call last):
  ...
IndexError: list index out of range

Checking sys.stdout affirms it is correct at every step, and using print with the redirected stdout affirms that the problem comes from help itself.

I imagine this problem is related to the use of self.__doc__ assignments, though the behavior is very strange because it works the first time and then fails the second time.

Your environment

  • CPython versions tested on: >= 3.8
  • Operating system and architecture: Windows 11
Ngôn ngữ chính
Python
Star
77.2k
Fork
36k
Merge trung bình
1 ngày 9 giờ
Pull request đã merge (30 ngày)
558

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.

Issue khác của python/cpython

Tất cả issue của python/cpython

Issue tương tự

Thêm issue về Python

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.