diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2021-10-07 22:06:21 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2021-10-07 22:10:05 -0400 |
commit | 18271699055acbc65a5d6956606b392c6e146f1e (patch) | |
tree | 487d462c2dd41cf9e0f829dc4a1a31160edf3f36 | |
parent | c7aae956989949065ccfaccca27dcdec38224dcd (diff) | |
download | cmd2-git-prompt_error.tar.gz |
Fixed AttributeError in rl_get_prompt() when prompt is None.prompt_error
-rw-r--r-- | CHANGELOG.md | 4 | ||||
-rw-r--r-- | cmd2/cmd2.py | 38 | ||||
-rw-r--r-- | cmd2/rl_utils.py | 6 |
3 files changed, 22 insertions, 26 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a8f4456..c372f74b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.3.0 (TBD, 2021) +* Bug Fixes + * Fixed `AttributeError` in `rl_get_prompt()` when prompt is `None`. + ## 2.2.0 (September 14, 2021) * Bug Fixes * Fixed extra space appended to each alias by "alias list" command diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index b787bb18..0f7e1055 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -5011,8 +5011,6 @@ class Cmd(cmd.Cmd): To the user it appears as if an alert message is printed above the prompt and their current input text and cursor location is left alone. - Raises a `RuntimeError` if called while another thread holds `terminal_lock`. - IMPORTANT: This function will not print an alert unless it can acquire self.terminal_lock to ensure a prompt is onscreen. Therefore it is best to acquire the lock before calling this function to guarantee the alert prints and to avoid raising a RuntimeError. @@ -5020,6 +5018,7 @@ class Cmd(cmd.Cmd): :param alert_msg: the message to display to the user :param new_prompt: If you also want to change the prompt that is displayed, then include it here. See async_update_prompt() docstring for guidance on updating a prompt. + :raises RuntimeError: if called while another thread holds `terminal_lock` """ if not (vt100_support and self.use_rawinput): return @@ -5082,8 +5081,6 @@ class Cmd(cmd.Cmd): prompt, it is best to keep the prompt the same width as what's on screen. Otherwise the user's input text will be shifted and the update will not be seamless. - Raises a `RuntimeError` if called while another thread holds `terminal_lock`. - IMPORTANT: This function will not update the prompt unless it can acquire self.terminal_lock to ensure a prompt is onscreen. Therefore it is best to acquire the lock before calling this function to guarantee the prompt changes and to avoid raising a RuntimeError. @@ -5093,36 +5090,29 @@ class Cmd(cmd.Cmd): line command completes. :param new_prompt: what to change the prompt to + :raises RuntimeError: if called while another thread holds `terminal_lock` """ self.async_alert('', new_prompt) - def set_window_title(self, title: str) -> None: # pragma: no cover - """Set the terminal window title. - - Raises a `RuntimeError` if called while another thread holds `terminal_lock`. + @staticmethod + def set_window_title(title: str) -> None: # pragma: no cover + """ + Set the terminal window title. - IMPORTANT: This function will not set the title unless it can acquire self.terminal_lock to avoid writing - to stderr while a command is running. Therefore it is best to acquire the lock before calling - this function to guarantee the title changes and to avoid raising a RuntimeError. + NOTE: This function writes to stderr. Therefore if you call this during a command run by a pyscript, + the string which updates the title will appear in that command's CommandResult.stderr data. :param title: the new window title """ if not vt100_support: return - # Sanity check that can't fail if self.terminal_lock was acquired before calling this function - if self.terminal_lock.acquire(blocking=False): - try: - sys.stderr.write(ansi.set_title_str(title)) - sys.stderr.flush() - except AttributeError: - # Debugging in Pycharm has issues with setting terminal title - pass - finally: - self.terminal_lock.release() - - else: - raise RuntimeError("another thread holds terminal_lock") + try: + sys.stderr.write(ansi.set_title_str(title)) + sys.stderr.flush() + except AttributeError: + # Debugging in Pycharm has issues with setting terminal title + pass def enable_command(self, command: str) -> None: """ diff --git a/cmd2/rl_utils.py b/cmd2/rl_utils.py index b2dc7649..d2a7c54b 100644 --- a/cmd2/rl_utils.py +++ b/cmd2/rl_utils.py @@ -8,7 +8,6 @@ from enum import ( ) from typing import ( Union, - cast, ) # Prefer statically linked gnureadline if available (for macOS compatibility due to issues with libedit) @@ -197,7 +196,10 @@ def rl_get_prompt() -> str: # pragma: no cover """Gets Readline's current prompt""" if rl_type == RlType.GNU: encoded_prompt = ctypes.c_char_p.in_dll(readline_lib, "rl_prompt").value - prompt = cast(bytes, encoded_prompt).decode(encoding='utf-8') + if encoded_prompt is None: + prompt = '' + else: + prompt = encoded_prompt.decode(encoding='utf-8') elif rl_type == RlType.PYREADLINE: prompt_data: Union[str, bytes] = readline.rl.prompt |