python / python/cpython

warn_explicit() discards the source line obtained from module_globals

Offen
#155,319 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

3.13 3.14 3.15 3.16 extension-modules type-bug
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

The only purpose of the module_globals argument of warnings.warn_explicit() is to get the source line of the warning from the module loader when the file cannot be read. The C implementation computes it in get_source_line(), but then throws it away.

call_show_warning() passes None in the line slot of WarningMessage and ignores its own sourceline argument:

    msg = PyObject_CallFunctionObjArgs(warnmsg_cls, message, category,
            filename, lineno_obj, Py_None, Py_None,
            source ? source : Py_None, module,
            NULL);

sourceline is only used in the fallback show_warning() path, taken when warnings._showwarnmsg is not available.

As a result the source line is lost, while the pure Python implementation, which seeds linecache instead, displays it:

$ cat > spam.py <<EOF
import warnings
def f():
    warnings.warn_explicit('eggs', UserWarning, 'bar', 1, module_globals=globals())
EOF
$ ./python -c 'import spam; spam.f()'
bar:1: UserWarning: eggs
$ ./python -c 'import sys; sys.modules["_warnings"] = None; import spam; spam.f()'
bar:1: UserWarning: eggs
  import warnings

The None was added in 914cde89d4c together with the source argument, so the C implementation has never passed the source line.

Linked PRs
  • gh-155320
  • gh-155824
  • gh-155825
  • gh-155826

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 bei den C-Funktionen get_source_line() und call_show_warning(), die das Issue als Funktionen identifiziert, die die Quellzeile berechnen und anschließend verwerfen. Reproduziere die warn_explicit()-Beispiele aus dem Issue und überprüfe, dass die Quellzeile sowohl im WarningMessage-Pfad als auch im Fallback-Pfad show_warning() erhalten bleibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
c, python
Bereich
backend
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

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