python / python/cpython

`shlex` silently discards a sourced stream when the source filename ends at EOF

Offen
#156,891 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

stdlib type-bug
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug description:

Summary

When shlex.shlex source inclusion is enabled and the filename following the source directive is the final token in the parent input, with no trailing whitespace or newline, the stream returned by sourcehook() is opened or created normally but all of its tokens are silently discarded. The included stream is then closed.

It contradicts the documented source-inclusion semantics, under which input should be read from the included stream until EOF before parsing resumes in the parent stream.

Minimal reproducer

import io
import shlex

lexer = shlex.shlex("include child", posix=True)
lexer.source = "include"

included = io.StringIO("from-child")
lexer.sourcehook = lambda filename: (filename, included)

print(list(lexer))
print(included.closed)

Actual behavior

[]
True

The from-child token is never returned, and the included stream is closed. No exception is raised, so callers cannot detect from the token stream that included input was skipped.

Expected behavior

['from-child']
True

The included stream should be parsed completely, closed when it reaches EOF, and only then should parsing resume in the parent stream.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs
  • gh-156892

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

Führe zunächst den minimalen Python-Reproducer aus und untersuche in der Quelle von shlex.shlex die Einbindung von Quellen rund um sourcehook() sowie die Behandlung des Eingabeendes. Erledigt ist es, wenn der eingebundene Stream 'from-child' liefert, bevor er geschlossen wird, während das Parsen im übergeordneten Stream weiterhin korrekt fortgesetzt wird.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
cli
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.