Skip to content

Commit 12a7d5e

Browse files
committed
Fixed issue where constants.REDIRECTION_TOKENS was being mutated.
1 parent 581ca11 commit 12a7d5e

4 files changed

Lines changed: 20 additions & 30 deletions

File tree

cmd2/cmd2.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1976,10 +1976,8 @@ def tokens_for_completion(self, line: str, begidx: int, endidx: int) -> tuple[li
19761976
**On Failure**
19771977
- Two empty lists
19781978
"""
1979-
import copy
1980-
19811979
unclosed_quote = ""
1982-
quotes_to_try = copy.copy(constants.QUOTES)
1980+
quotes_to_try = [*constants.QUOTES]
19831981

19841982
tmp_line = line[:endidx]
19851983
tmp_endidx = endidx
@@ -3818,8 +3816,7 @@ def _alias_create(self, args: argparse.Namespace) -> None:
38183816
return
38193817

38203818
# Unquote redirection and terminator tokens
3821-
tokens_to_unquote = constants.REDIRECTION_TOKENS
3822-
tokens_to_unquote.extend(self.statement_parser.terminators)
3819+
tokens_to_unquote = (*constants.REDIRECTION_TOKENS, *self.statement_parser.terminators)
38233820
utils.unquote_specific_tokens(args.command_args, tokens_to_unquote)
38243821

38253822
# Build the alias value string
@@ -3898,8 +3895,7 @@ def _alias_list(self, args: argparse.Namespace) -> None:
38983895
"""List some or all aliases as 'alias create' commands."""
38993896
self.last_result = {} # dict[alias_name, alias_value]
39003897

3901-
tokens_to_quote = constants.REDIRECTION_TOKENS
3902-
tokens_to_quote.extend(self.statement_parser.terminators)
3898+
tokens_to_quote = (*constants.REDIRECTION_TOKENS, *self.statement_parser.terminators)
39033899

39043900
to_list = (
39053901
utils.remove_duplicates(args.names)
@@ -4065,8 +4061,7 @@ def _macro_create(self, args: argparse.Namespace) -> None:
40654061
return
40664062

40674063
# Unquote redirection and terminator tokens
4068-
tokens_to_unquote = constants.REDIRECTION_TOKENS
4069-
tokens_to_unquote.extend(self.statement_parser.terminators)
4064+
tokens_to_unquote = (*constants.REDIRECTION_TOKENS, *self.statement_parser.terminators)
40704065
utils.unquote_specific_tokens(args.command_args, tokens_to_unquote)
40714066

40724067
# Build the macro value string
@@ -4188,8 +4183,7 @@ def _macro_list(self, args: argparse.Namespace) -> None:
41884183
"""List macros."""
41894184
self.last_result = {} # dict[macro_name, macro_value]
41904185

4191-
tokens_to_quote = constants.REDIRECTION_TOKENS
4192-
tokens_to_quote.extend(self.statement_parser.terminators)
4186+
tokens_to_quote = (*constants.REDIRECTION_TOKENS, *self.statement_parser.terminators)
41934187

41944188
to_list = (
41954189
utils.remove_duplicates(args.names)
@@ -4917,9 +4911,7 @@ def py_quit() -> None:
49174911
"""Exit an interactive Python environment, callable from the interactive Python console."""
49184912
raise EmbeddedConsoleExit
49194913

4920-
from .py_bridge import (
4921-
PyBridge,
4922-
)
4914+
from .py_bridge import PyBridge
49234915

49244916
add_to_history = self.scripts_add_to_history if pyscript else True
49254917
py_bridge = PyBridge(self, add_to_history=add_to_history)

cmd2/constants.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
INFINITY = float("inf")
77

88
# Used for command parsing, output redirection, completion, and word breaks. Do not change.
9-
QUOTES = ['"', "'"]
9+
QUOTES = ('"', "'")
1010
REDIRECTION_PIPE = "|"
1111
REDIRECTION_OVERWRITE = ">"
1212
REDIRECTION_APPEND = ">>"
13-
REDIRECTION_CHARS = [REDIRECTION_PIPE, REDIRECTION_OVERWRITE]
14-
REDIRECTION_TOKENS = [REDIRECTION_PIPE, REDIRECTION_OVERWRITE, REDIRECTION_APPEND]
13+
REDIRECTION_CHARS = (REDIRECTION_PIPE, REDIRECTION_OVERWRITE)
14+
REDIRECTION_TOKENS = (REDIRECTION_PIPE, REDIRECTION_OVERWRITE, REDIRECTION_APPEND)
1515
COMMENT_CHAR = "#"
1616
MULTILINE_TERMINATOR = ";"
1717

cmd2/parsing.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,12 @@ def __init__(
330330
# the string (\Z matches the end of the string even if it
331331
# contains multiple lines)
332332
#
333-
invalid_command_chars = []
334-
invalid_command_chars.extend(constants.QUOTES)
335-
invalid_command_chars.extend(constants.REDIRECTION_CHARS)
336-
invalid_command_chars.extend(self.terminators)
333+
invalid_command_chars = (
334+
*constants.QUOTES,
335+
*constants.REDIRECTION_CHARS,
336+
*self.terminators,
337+
)
338+
337339
# escape each item so it will for sure get treated as a literal
338340
second_group_items = [re.escape(x) for x in invalid_command_chars]
339341
# add the whitespace and end of string, not escaped because they
@@ -384,9 +386,8 @@ def is_valid_command(self, word: str, *, is_subcommand: bool = False) -> tuple[b
384386
return False, errmsg
385387

386388
errmsg = "cannot contain: whitespace, quotes, "
387-
errchars = []
388-
errchars.extend(constants.REDIRECTION_CHARS)
389-
errchars.extend(self.terminators)
389+
390+
errchars = (*constants.REDIRECTION_CHARS, *self.terminators)
390391
errmsg += ", ".join([shlex.quote(x) for x in errchars])
391392

392393
match = self._command_pattern.search(word)
@@ -704,9 +705,8 @@ def split_on_punctuation(self, tokens: list[str]) -> list[str]:
704705
:param tokens: the tokens as parsed by shlex
705706
:return: a new list of tokens, further split using punctuation
706707
"""
707-
punctuation: list[str] = []
708-
punctuation.extend(self.terminators)
709-
punctuation.extend(constants.REDIRECTION_CHARS)
708+
# Using a set for faster lookups
709+
punctuation = {*self.terminators, *constants.REDIRECTION_CHARS}
710710

711711
punctuated_tokens = []
712712

cmd2/string_utils.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
full-width characters (like those used in CJK languages).
66
"""
77

8-
from collections.abc import (
9-
Sequence,
10-
)
8+
from collections.abc import Sequence
119

1210
from rich.align import AlignMethod
1311
from rich.style import StyleType

0 commit comments

Comments
 (0)