Skip to content

Honor quotes when glob-expanding %run arguments#15223

Open
Ijtihed wants to merge 1 commit into
ipython:mainfrom
Ijtihed:fix-12726-quote-glob
Open

Honor quotes when glob-expanding %run arguments#15223
Ijtihed wants to merge 1 commit into
ipython:mainfrom
Ijtihed:fix-12726-quote-glob

Conversation

@Ijtihed
Copy link
Copy Markdown

@Ijtihed Ijtihed commented May 25, 2026

Fixes #12726.

Summary

Wrapping a %run argument in single or double quotes now suppresses shell-style glob expansion of that argument matching how real shels behave. the existing backslash-escape (\*) and -G flag are unchanged. Before this with foo.txt and bar.txt in the working directory:

In [1]: %run script.py "*.txt"
# sys.argv -> ['foo.txt', 'bar.txt']

After:

In [1]: %run script.py "*.txt"
# sys.argv -> ['*.txt']

Motivation

The issue thread already has a maintainer pointer:

Looks like an oversight in IPython.core.magics.execution.ExecutionMagics.parse_options.

-- @MrMino, comment

What changed

I added a new helper arg_split_with_quotes(commandline, strict=True) in IPython/utils/_process_common.py (re-exported from IPython.utils.process). Returns [(token, was_quoted), ...]. shlex-based on two passes - posix=True and posix=False - so the quote semantics are the same on every platform

%run consults it after parse_options and skips globbing for any arg_lst entry that came from a quoted token in parameter_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 for arg_split_with_quotes covering bare, double-quoted, single-quoted, mixed-with-options and strict=False cases.
  • tests/test_run.py: new integration test test_run_quoted_glob_arg_is_not_expanded that runs %run -i against a small script in a tempdir with f les that would match *.txt, and checks sys.argv for bare double-quoted and (POSIX-only) single-quoted forms.
  • Updated doctest_run_option_parser_for_posix its 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 skipped

The POSIX-only single-quote doctest is done by CI on the Linux/macOS jobs.

Open questions

  • The Windows doctest doctest_run_option_parser_for_windows still describes the existing 'print*.py'["'print*.py'"] behavior. That case is preserved by the patch (CommandLineToArgvW keeps the literal single quotes so they never enter the quoted_remaining set) so I left it.
  • Single quotes on Windows stay documented as unsupported.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

%run incorrectly expands glob patterns when quoted

1 participant