python / python/cpython

Document how import chains are affected by `sys.setrecursionlimit`

Offen
#128,544 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

docs
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

While working on a very large Python codebase, I ran into some undocumented behaviour.

A chain of imports (i.e. one module imports another, which imports another, which imports another) will cause a RecursionError to be raised if the Python recursion limit is not high enough. (Note, there is no recursion involved.)

Longer chains of imports can be enabled by increasing the recursion limit. But the limit has no effect beyond a certain point (around 10,000). (I believe this is due to Py_C_RECURSION_LIMIT, mentioned in by Serhiy Storchaka.)

A workaround is available: you can import the modules in a different order.

I've created a repository with more analysis and scripts to reproduce the problem. I also started a Discuss topic on the subject.

From what I understand, this is a complex problem that is unlikely to be fixed. But I think it would be helpful to document this, probably in the docs for sys.setrecursionlimit.

I would be happy to try to do a pull request, assuming the core team agree that it should be added.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit der Dokumentation zu sys.setrecursionlimit und verwende das verlinkte Repository import-recursion-demo sowie dessen Reproduktionsskripte, um das Verhalten zu überprüfen. Als erledigt gilt die Aufgabe, wenn dokumentiert ist, wie lange Importketten RecursionError auslösen, wo ungefähr die obere Grenze liegt und welcher alternative Workaround zur Import-Reihenfolge möglich ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
documentation
Issue-Typ
Dokumentation
Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
58/100

Neue Issues direkt in Ihr Postfach

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