Skip to content

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

Description

@lpyu001

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions