Skip to content

Tab completion for dict key causes crash #917

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

Closed
arian-deimling opened this issue Sep 26, 2021 · 5 comments · Fixed by #920
Closed

Tab completion for dict key causes crash #917

arian-deimling opened this issue Sep 26, 2021 · 5 comments · Fixed by #920
Milestone

Comments

@arian-deimling
Copy link
Contributor

This issue is not reproducible with several different dicts that I've tried, but it can be reproduced with the dict below.

my_dict = {
'Circle': {'Ellipse',
           'Shape',
           'ShapeDriver'},

'Ellipse': {'Shape'},

'Rectangle': {'Shape'},

'Shape': {'Circle'},

'ShapeDriver': {'Circle',
                'Ellipse',
                'Shape'},

'Square': {'Rectangle'}
}

Steps to recreate:

  1. create new dict
  2. type my_dict[
  3. press key to use tab-completion
  4. crash
~ % bpython
bpython version 0.21 on top of Python 3.8.2 /Library/Developer/CommandLineTools/usr/bin/python3
>>> my_dict = {
... 'a/Circle': {'Ellipse',
...            'Shape',
...            'ShapeDriver'},
... 
... 'a/Ellipse': {'Shape'},
... 
... 'a/Rectangle': {'Shape'},
... 
... 'a/Shape': {'Circle'},
... 
... 'a/ShapeDriver': {'Circle',
...                 'Ellipse',
...                 'Shape'},
... 
... 'a/Square': {'Rectangle'}
... }
>>> my_dict['b/banana']
Traceback (most recent call last):
  File "/Users/arian/Library/Python/3.8/bin/bpython", line 8, in <module>
    sys.exit(main())
  File "/Users/arian/Library/Python/3.8/lib/python/site-packages/bpython/curtsies.py", line 201, in main
    exit_value = repl.mainloop(True, paste)
  File "/Users/arian/Library/Python/3.8/lib/python/site-packages/bpython/curtsies.py", line 121, in mainloop
    self.process_event_and_paint(e)
  File "/Users/arian/Library/Python/3.8/lib/python/site-packages/bpython/curtsies.py", line 85, in process_event_and_paint
    self.process_event(e)
  File "/Users/arian/Library/Python/3.8/lib/python/site-packages/bpython/curtsiesfrontend/repl.py", line 615, in process_event
    return self.process_key_event(e)
  File "/Users/arian/Library/Python/3.8/lib/python/site-packages/bpython/curtsiesfrontend/repl.py", line 757, in process_key_event
    self.on_tab()
  File "/Users/arian/Library/Python/3.8/lib/python/site-packages/bpython/curtsiesfrontend/repl.py", line 891, in on_tab
    cursor_and_line = self.matches_iter.substitute_cseq()
  File "/Users/arian/Library/Python/3.8/lib/python/site-packages/bpython/repl.py", line 297, in substitute_cseq
    self.update(
  File "/Users/arian/Library/Python/3.8/lib/python/site-packages/bpython/repl.py", line 318, in update
    self.start, self.end, self.current_word = self.completer.locate(
TypeError: cannot unpack non-iterable NoneType object

Image below so you can see the colors as well:
image

@thomasballinger
Copy link
Member

Thanks for reporting! @arian-deimling you mentioned wanting to work on something earlier, do you want to give this a shot?

@arian-deimling
Copy link
Contributor Author

Sure

@arian-deimling
Copy link
Contributor Author

arian-deimling commented Sep 27, 2021

Hey, I found the issue, it's this line right here

_current_dict_key_re = LazyReCompile(r"""[\w_][\w0-9._]*\[([\w0-9._(), '"]*)""")

r"""[\w_][\w0-9._]*\[([\w0-9._(), '"]*)"""
______________________^^^^^^^^^^^^^^^^ this is the cause of the bug

It's using that regex to capture the current key of the dictionary for which it is showing text completion of keys in the interactive terminal, and the key portion of the dictionary in that regex is too restrictive. The text completion works better with the following regex:
r"""[\w_][\w0-9._]*\[(.*)"""
______________________^^ something like this is how to fix the bug

Does this seem like a good adjustment to make?

Also, with this change there is still an issue; now, if you do the following in a bython interactive session, you cannot tab-complete the key for my_dict:

>>> my_dict = {'a\\a': 'Value'}
>>> my_dict['a\

I'll keep working on this to find out why the tab completion isn't working (I'm guessing it's another regex issue).

@thomasballinger
Copy link
Member

That seems like the right place to be adjusting to me. (.*) seems too general because it won't stop matching even when you've close the quotes or the square bracket of an index, something like "anything but a single or double quote" would be closer.

@arian-deimling
Copy link
Contributor Author

arian-deimling commented Sep 27, 2021

@thomasballinger Got the bug fixed and submitted pull request #920. Please let me know if there is an issue with the pull request; this is my first time contributing to an open source project!

@sebastinas sebastinas added this to the release-0.22 milestone Oct 12, 2021
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

Successfully merging a pull request may close this issue.

3 participants