Redirecting stdout failure with help function inside `__repr__`

Offen
#101,624 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Anfängerfreundlichkeit
35/100
Issue-Typ
Bug
Klarheit
Größtenteils klar
Aktivitätsstatus
Veraltet
Tech-Stack
python
Bereich
tooling

Rechercherichtung

Beginne damit, das Beispiel mit contextlib.redirect_stdout, StringIO, help(), verschachteltem repr und doc-Zuweisungen an Instanzen auf den gemeldeten CPython-Versionen zu reproduzieren. Verfolge, wie help() das Objekt formatiert und während des ersten und zweiten Aufrufs Ausgabe schreibt. Erledigt ist es, wenn die Reproduktion bei umgeleitetem stdout keine help-Ausgabe mehr nach außen dringen lässt und keinen IndexError auslöst.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
36k
Ø Merge
1 T. 9 Std.
Gemergte PRs (30 T.)
558

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus python/cpython

Alle Issues in python/cpython

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.