Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reverse Search History Not Functioning Correctly on Windows #1315

Open
Chris3606 opened this issue Jul 31, 2024 · 0 comments
Open

Reverse Search History Not Functioning Correctly on Windows #1315

Chris3606 opened this issue Jul 31, 2024 · 0 comments

Comments

@Chris3606
Copy link

Chris3606 commented Jul 31, 2024

Steps to Reproduce

  1. Run the following cmd2 application:
import cmd2

class FirstApp(cmd2.Cmd):
    """A simple cmd2 application."""

    def do_hello_world(self, _: cmd2.Statement):
        self.poutput('Hello World')

if __name__ == '__main__':
    import sys
    c = FirstApp()
    sys.exit(c.cmdloop())
  1. Type "hello" at the console and press enter

  2. Press Ctrl + R.

    • Note: this loads into forward search, whereas on Linux this loads to reverse search
  3. Press Ctrl + R again to load into reverse search

    • Note that "hello" is selected.
  4. Type something not in the search history (for example, 'p'), and press enter

    • Note that "hello" is run. On Linux, this results in a "failed reverse search" and nothing is run.
  5. Press Ctrl + R again to re-enter reverse search, and type another character not in the search history (for example, 'f'). I receive a stack trace similar to the following on that key press, and any subsequent key presses:

Traceback (most recent call last):
  File "C:\path_to_proj\.venv\Lib\site-packages\pyreadline3\console\console.py", line 804, in hook_wrapper_23
    res = ensure_str(readline_hook(prompt))
                     ^^^^^^^^^^^^^^^^^^^^^
  File "C:\path_to_proj\.venv\Lib\site-packages\pyreadline3\rlmain.py", line 589, in readline
    self._readline_from_keyboard()
  File "C:\path_to_proj\.venv\Lib\site-packages\pyreadline3\rlmain.py", line 553, in _readline_from_keyboard
    if self._readline_from_keyboard_poll():
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\path_to_proj\.venv\Lib\site-packages\pyreadline3\rlmain.py", line 574, in _readline_from_keyboard_poll
    result = self.mode.process_keyevent(event.keyinfo)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\path_to_proj\.venv\Lib\site-packages\pyreadline3\modes\emacs.py", line 243, in process_keyevent
    r = self.process_keyevent_queue[-1](keyinfo)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\path_to_proj\.venv\Lib\site-packages\pyreadline3\modes\emacs.py", line 74, in _process_incremental_search_keyevent
    self.line = self.subsearch_fun(self.subsearch_query)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\path_to_proj\.venv\Lib\site-packages\pyreadline3\lineeditor\history.py", line 170, in reverse_search_history
    result = self.history[startpos].get_line_text()
             ~~~~~~~~~~~~^^^^^^^^^^
IndexError: list index out of range

Notes

There appear to be a fairly substantial number of bugs in the history implementation in pyreadline3. For starters, I believe the bounds of slicing are wrong here; the upper bound is exclusive so the first item is cut off.

However, even aside from that, based on what I'm seeing with this example, there appear to be a number of other bugs that more pertain to the state machine they use to track history; I haven't been able to track those down, thus far.

FWIW, pyreadline3 seems somewhat unmaintained, and at least as I've experienced it, has a number of shortcomings beyond this issue (see #1313, for example). Not sure if an alternative is available or if you have the capacity to fork and fix some of these issue (I'm willing to help there, as I'm able, though not super familiar with their code base), but it definitely is impactful to the cmd2 experience on Windows, unless I'm way off about what's going on in this case. I'm actually going to have to either fork pyreadline3 or monkey patch their code to get around this one, I think, for my use case (or just disable the history features, which is less than ideal).

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

No branches or pull requests

1 participant