Skip to content

Fix %edit failing when path contains spaces (editor hook double-quoting)#15315

Open
shreyabyte wants to merge 1 commit into
ipython:mainfrom
shreyabyte:fix-editorhook-path-spaces
Open

Fix %edit failing when path contains spaces (editor hook double-quoting)#15315
shreyabyte wants to merge 1 commit into
ipython:mainfrom
shreyabyte:fix-editorhook-path-spaces

Conversation

@shreyabyte

@shreyabyte shreyabyte commented Jul 16, 2026

Copy link
Copy Markdown

Fixes #14961

This PR standardises filename handling by moving quoting responsibility into the editor hook.

Problem

%edit single-quoted the target filename before passing it to self.shell.hooks.editor() if the path contained spaces. The default editor hook (IPython/core/hooks.py) then built a shell command by string-formatting editor, linemark, and filename together, without quoting filename at all — relying on %edit having already done it. Custom hooks installed via IPython/lib/editorhooks.py also call shlex.quote(filename) on their own, assuming a raw filename — so any path with spaces reaching those hooks got double-quoted and broken (e.g. the Notepad++ hook).

In short, callers and hooks disagreed about who owns quoting, and only one side actually did it.

Fix

Established a clear contract: hooks.editor() always receives a raw, unquoted filename. Quoting/escaping for shell execution is the responsibility of the hook implementation.

  • IPython/core/magics/code.py: removed the pre-quoting logic in %edit (quoted = "'%s'" % quoted); now passes the raw filename to self.shell.hooks.editor(), matching what _edit_macro already did. Added comments at both call sites documenting the contract.
  • IPython/core/hooks.py: added a small _quote_if_needed() helper, applied consistently to both editor and filename before building the Popen command string, so the default hook now correctly quotes filenames with spaces (previously only the editor binary path was quoted).
  • IPython/lib/editorhooks.py: unchanged — its shlex.quote(filename) calls already assumed a raw filename; this was previously being fed an already-quoted string, causing the double-quoting bug. No code change needed there, it now works as originally intended.

Tests

  • IPython/core/tests/test_hooks.py: added tests for the default editor() hook verifying filenames with spaces are quoted in the resulting command, filenames without spaces are left untouched, the editor binary path quoting behavior is preserved, and the full command (with a line number) is assembled correctly.
  • IPython/core/tests/test_magics_code.py: fixed test_edit_filename_with_space_is_quoted (renamed to test_edit_filename_with_space_passed_raw), which previously asserted the quoted form was received and stripped quotes in the EditorStub fixture — masking the real bug. It now asserts %edit passes the hook a fully raw filename with no surrounding quote characters. Removed the now-unneeded filename.strip("'") in EditorStub and the @skip_win32 marker, since the test no longer depends on shell-quoting semantics.

Compatibility note

This changes the editor hook contract so that hooks.editor() now receives a raw filename instead of a pre-quoted one.

Manual verification

Verified %edit on a path containing spaces opens correctly via the default hook and via a custom hook (Notepad++), with no stray quote characters and no "file not found" errors.

@Carreau
Carreau force-pushed the fix-editorhook-path-spaces branch from 4e167ff to e6b4fba Compare July 22, 2026 16:46
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.

%edit editorhook fails if path has spaces

1 participant