Redirecting stdout failure with help function inside `__repr__`
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
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ả
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
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của python/cpython
-
docs pending
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
-
stdlib type-feature
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
-
stdlib type-feature
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
-
build type-bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
-
stdlib topic-email type-feature
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
Tất cả issue của python/cpython
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 86/100
-
🐛 Bug 🔔 Pending processing
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
jumpserver/jumpserver#17584 ·