python / python/cpython

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

Open
#156,891 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the minimal Python reproducer and inspect shlex.shlex source inclusion around sourcehook() and end-of-input handling. Done means the included stream yields 'from-child' before it is closed, while parsing still resumes correctly in the parent stream.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.