python / python/cpython

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

オープン
#156,891 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

stdlib type-bug
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず最小の Python 再現プログラムを実行し、shlex.shlex のソースで sourcehook() 周辺のソースインクルージョンと入力終端の処理を調べます。完了条件は、インクルードされたストリームが閉じられる前に 'from-child' を返し、同時に親ストリームでのパースも正しく再開されることです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
cli
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。