python / python/cpython

Give PyErr_CheckSignals guide-level documentation

Offen
#134,075 3 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

Documentation

Compiled extensions that "want to be interruptible by user requests" need to call PyErr_CheckSignals periodically; when it returns -1, they should stop work, clean up after themselves, and return to the interpreter.

This function is not mentioned at all in the extending and embedding guide, and its reference documentation makes it sound unimportant. As a result, many extension authors are (it seems to me) not aware that it exists or that they ought to be calling it, and so many extensions that should be interruptible by user requests aren't. For example, if you execute this code

>>> import numpy as np
>>> rng = np.random.default_rng()
>>> x = rng.random(1000000000)

and then immediately hit control-C, you'll be waiting at least a few seconds before you get the prompt back.

PyErr_CheckSignals also has the unusual property of potentially executing arbitrary Python code before returning to its caller, which makes it easy to use unsafely. Making an extension interruptible throughout may necessitate calling PyErr_CheckSignals from innermost loops where one would ideally release the GIL, which makes that worse (see #133465).

Thus, I suggest that a guide to correct use of PyErr_CheckSignals should be added to the extending and embedding guide.

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 damit, den Leitfaden zu Erweiterung und Einbettung zusammen mit der Referenzdokumentation zu PyErr_CheckSignals zu lesen. Füge einen Leitfaden zur sicheren Verwendung von PyErr_CheckSignals hinzu, einschließlich des Umgangs mit einer Rückgabe von -1, der Bereinigung und der Möglichkeit, dass beliebiger Python-Code ausgeführt wird; die Dokumentation sollte deutlich machen, wie Erweiterungen unterbrechbar bleiben können.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
documentation
Issue-Typ
Dokumentation
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Ruhig
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
65/100

Neue Issues direkt in Ihr Postfach

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