Skip to content

Commit 2e066b7

Browse files
committed
Stop resolving PATH entries and fold shell marking into one helper
os.path.realpath() can raise for a PATH entry which can't be reached, such as one on an unavailable network share on Windows. Since find_editor() runs when cmd2 is imported, that could keep cmd2 from being imported. The PATH entries are now used as written. Executable names were already de-duplicated, so the only cost is searching a directory twice when it is listed under two names. The places which mark completions as being passed to a shell now share one helper, Cmd._for_shell(). Corrected the docs for how the quote character is chosen when several matches are offered, and documented that a macro which runs a shell command is not quoted for the shell.
1 parent 19d9a46 commit 2e066b7

4 files changed

Lines changed: 48 additions & 30 deletions

File tree

‎cmd2/cmd2.py‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2581,13 +2581,10 @@ def _redirect_complete(self, text: str, line: str, begidx: int, endidx: int, com
25812581

25822582
prior_token = cur_token
25832583

2584-
if do_shell_completion:
2585-
completions = self.shell_cmd_complete(text, line, begidx, endidx)
2586-
return dataclasses.replace(completions, _quote_for_shell=piped_to_shell)
2587-
2588-
if do_path_completion:
2589-
completions = self.path_complete(text, line, begidx, endidx)
2590-
return dataclasses.replace(completions, _quote_for_shell=piped_to_shell)
2584+
if do_shell_completion or do_path_completion:
2585+
completer = self.shell_cmd_complete if do_shell_completion else self.path_complete
2586+
completions = completer(text, line, begidx, endidx)
2587+
return self._for_shell(completions) if piped_to_shell else completions
25912588

25922589
# If there were redirection strings anywhere on the command line, then we
25932590
# are no longer completing for the current command
@@ -5054,15 +5051,21 @@ def _build_shell_parser(cls) -> Cmd2ArgumentParser:
50545051

50555052
return shell_parser
50565053

5054+
@staticmethod
5055+
def _for_shell(completions: Completions) -> Completions:
5056+
"""Mark completions as being for part of the command line which is passed to a shell.
5057+
5058+
This is what gets them quoted when one of them has a character which is special to that shell.
5059+
"""
5060+
return dataclasses.replace(completions, _quote_for_shell=True)
5061+
50575062
def _complete_shell_command(self, text: str, line: str, begidx: int, endidx: int) -> Completions:
50585063
"""Complete the command which do_shell passes to a shell."""
5059-
completions = self.shell_cmd_complete(text, line, begidx, endidx)
5060-
return dataclasses.replace(completions, _quote_for_shell=True)
5064+
return self._for_shell(self.shell_cmd_complete(text, line, begidx, endidx))
50615065

50625066
def _complete_shell_arg(self, text: str, line: str, begidx: int, endidx: int) -> Completions:
50635067
"""Complete an argument of the command which do_shell passes to a shell."""
5064-
completions = self.path_complete(text, line, begidx, endidx)
5065-
return dataclasses.replace(completions, _quote_for_shell=True)
5068+
return self._for_shell(self.path_complete(text, line, begidx, endidx))
50665069

50675070
# Preserve quotes since we are passing these strings to the shell
50685071
@with_argparser(_build_shell_parser, preserve_quotes=True)

‎cmd2/utils.py‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -379,16 +379,13 @@ def _quote_shell_arg(arg: str) -> str:
379379
def _get_path_dirs() -> set[str]:
380380
"""Return the directories listed in the PATH environment variable.
381381
382-
Symbolic links are resolved so a directory which is reachable through more than one entry is only
383-
returned once. Relative entries are returned as they are since resolving one raises an exception if
384-
the current working directory no longer exists.
382+
The entries are returned as they are written. Resolving them would avoid searching a directory twice
383+
when it is listed under two names, but doing so can raise an exception for an entry which can't be reached.
385384
386385
:return: the directories, which is an empty set if PATH is not set
387386
"""
388387
env_path = os.getenv("PATH")
389-
if env_path is None:
390-
return set()
391-
return {os.path.realpath(p) if os.path.isabs(p) else p for p in env_path.split(os.path.pathsep)}
388+
return set() if env_path is None else set(env_path.split(os.path.pathsep))
392389

393390

394391
def find_editor() -> str | None:

‎docs/features/completion.md‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,24 @@ completing `!ls my*` gives:
5858

5959
On POSIX systems the characters which get a match quoted are a space, a quote character, `*`, `?`,
6060
`[`, `{`, `}`, `(`, `)`, `<`, `>`, `|`, `&`, `;`, `$`, a backtick, a backslash, and a leading `#` or
61-
`=`. Single quotes are used when a match has a `$`, a backtick, or a backslash, since a shell still
62-
interprets those within double quotes. On Windows the characters are the ones `cmd.exe` documents as
63-
requiring quotes, other than `~`, and the quotes are always double quotes.
61+
`=`. Single quotes are used when a match has a `$`, a backtick, a backslash, or a double quote in
62+
it, since a shell still interprets those within double quotes. On Windows the characters are the
63+
ones `cmd.exe` documents as requiring quotes, other than `~`, and the quotes are always double
64+
quotes.
6465

6566
There are limits to this:
6667

67-
- The quotes go around the whole argument, so when several matches are offered and only one of them
68-
needs quotes, they all get them. An executable named `[` is common, so completing a shell command
69-
which could match it quotes every match.
70-
- A match with a single quote in it is wrapped in double quotes, so a `$`, a backtick, or a
71-
backslash in that same match is still interpreted by a shell.
68+
- One pair of quotes goes around the whole argument, and it is chosen before you pick a match. So
69+
when several matches are offered and only one of them needs quotes, they all get them. An
70+
executable named `[` is common, so completing a shell command which could match it quotes every
71+
match.
72+
- For the same reason, one quote character is used for all the matches being offered. If any of them
73+
has a single quote in it, double quotes are used, and a `$`, a backtick, or a backslash in any of
74+
the matches is then still interpreted by a shell.
7275
- A `%` in a match is still interpreted by `cmd.exe` on Windows.
76+
- A [macro](./shortcuts_aliases_macros.md#macros) which runs a shell command is not covered. Its
77+
arguments are completed as paths, but they are only quoted for a space. An alias for the `shell`
78+
command is covered.
7379
- Your own commands are not covered, even if they pass their arguments to a shell. Quote or escape
7480
those arguments in the command.
7581

‎tests/test_utils.py‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2094,11 +2094,23 @@ def test_get_exes_in_path_symlinked_directory(tmp_path, monkeypatch) -> None:
20942094
monkeypatch.setenv("PATH", str(link_dir))
20952095
assert cu.get_exes_in_path("my") == ["mytool"]
20962096

2097-
# A directory reachable through more than one PATH entry is only searched once
2098-
monkeypatch.setenv("PATH", os.pathsep.join([str(real_dir), str(link_dir)]))
2099-
with mock.patch("cmd2.utils.files_from_glob_pattern", wraps=cu.files_from_glob_pattern) as glob_mock:
2100-
assert cu.get_exes_in_path("my") == ["mytool"]
2101-
glob_mock.assert_called_once()
2097+
# An executable reachable through more than one PATH entry is only returned once
2098+
monkeypatch.setenv("PATH", os.pathsep.join([str(real_dir), str(link_dir), str(real_dir)]))
2099+
assert cu.get_exes_in_path("my") == ["mytool"]
2100+
2101+
2102+
def test_get_path_dirs_does_not_resolve_entries(monkeypatch) -> None:
2103+
# Resolving an entry can raise an exception, such as for one on a network share which can't be reached
2104+
# on Windows. Since find_editor() runs when cmd2 is imported, that would keep cmd2 from being imported.
2105+
monkeypatch.setenv("PATH", os.pathsep.join(["/unreachable/bin", "relative_bin", "/unreachable/bin"]))
2106+
with mock.patch("os.path.realpath", side_effect=OSError("network path was not found")):
2107+
assert cu._get_path_dirs() == {"/unreachable/bin", "relative_bin"}
2108+
assert cu.get_exes_in_path("my") == []
2109+
monkeypatch.delenv("EDITOR", raising=False)
2110+
assert cu.find_editor() is None
2111+
2112+
monkeypatch.delenv("PATH")
2113+
assert cu._get_path_dirs() == set()
21022114

21032115

21042116
@pytest.mark.skipif(sys.platform == "win32", reason="Windows does not allow the current working directory to be removed")

0 commit comments

Comments
 (0)