Honor quotes when glob-expanding %run arguments#15223
Open
Ijtihed wants to merge 1 commit into
Open
Conversation
wrapping a %run argument in single or double quotes now suppresses shell-style glob expansion of that argument matching real shells. adds an arg_split_with_quotes helper (shlex-based, platform-independent) that returns (token, was_quoted) pairs which %run uses to skip globbing for quoted tokens. The pre-existing backslash-escape and -G flag still work just like before.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #12726.
Summary
Wrapping a
%runargument in single or double quotes now suppresses shell-style glob expansion of that argument matching how real shels behave. the existing backslash-escape (\*) and-Gflag are unchanged. Before this withfoo.txtandbar.txtin the working directory:After:
Motivation
The issue thread already has a maintainer pointer:
-- @MrMino, comment
What changed
I added a new helper
arg_split_with_quotes(commandline, strict=True)inIPython/utils/_process_common.py(re-exported fromIPython.utils.process). Returns[(token, was_quoted), ...]. shlex-based on two passes -posix=Trueandposix=False- so the quote semantics are the same on every platform%runconsults it afterparse_optionsand skips globbing for anyarg_lstentry that came from a quoted token inparameter_s. Docstring updated.This is a small backwards-incompatible behavior change so ofc anyone depending on the previous "quotes do not suppress globs" behavior wouldd see different output. I added
docs/source/whatsnew/pr/incompat-run-quoted-glob.rst.Tests
tests/test_process.py: parametrized unit tests forarg_split_with_quotescovering bare, double-quoted, single-quoted, mixed-with-options andstrict=Falsecases.tests/test_run.py: new integration testtest_run_quoted_glob_arg_is_not_expandedthat runs%run -iagainst a small script in a tempdir with f les that would match*.txt, and checkssys.argvfor bare double-quoted and (POSIX-only) single-quoted forms.doctest_run_option_parser_for_posixits expected output was locking in the old buggy behavior.Local run on Windows / Python 3.13:
pytest tests/test_run.py tests/test_process.py tests/test_path.py => 62 passed, 12 skippedThe POSIX-only single-quote doctest is done by CI on the Linux/macOS jobs.
Open questions
doctest_run_option_parser_for_windowsstill describes the existing'print*.py'→["'print*.py'"]behavior. That case is preserved by the patch (CommandLineToArgvWkeeps the literal single quotes so they never enter thequoted_remainingset) so I left it.