Skip to content

Commit

Permalink
Fixed AttributeError in rl_get_prompt() when prompt is None.
Browse files Browse the repository at this point in the history
  • Loading branch information
kmvanbrunt committed Oct 11, 2021
1 parent c7aae95 commit c5aaee5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 26 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
38 changes: 14 additions & 24 deletions cmd2/cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5011,15 +5011,14 @@ def async_alert(self, alert_msg: str, new_prompt: Optional[str] = None) -> None:
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.
: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
Expand Down Expand Up @@ -5082,8 +5081,6 @@ def async_update_prompt(self, new_prompt: str) -> None: # pragma: no cover
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.
Expand All @@ -5093,36 +5090,29 @@ def async_update_prompt(self, new_prompt: str) -> None: # pragma: no cover
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:
"""
Expand Down
6 changes: 4 additions & 2 deletions cmd2/rl_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
)
from typing import (
Union,
cast,
)

# Prefer statically linked gnureadline if available (for macOS compatibility due to issues with libedit)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c5aaee5

Please sign in to comment.