Skip to content

pdb: calling registered alias without command raises KeyError #109375

Description

@buermarc

Bug report

Bug description:

pdb allows to register an alias without a command.
Calling that alias raises an unhandled exception.

$ /tmp/ cat bar.py
breakpoint()
$ /tmp/ python bar.py 
--Return--
> /tmp/bar.py(1)<module>()->None
-> breakpoint()
(Pdb) alias foo
(Pdb) foo
Traceback (most recent call last):
  File "/tmp/bar.py", line 1, in <module>
    breakpoint()
  File "/home/home/.pyenv/versions/3.11.1/lib/python3.11/bdb.py", line 94, in trace_dispatch
    return self.dispatch_return(frame, arg)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/home/.pyenv/versions/3.11.1/lib/python3.11/bdb.py", line 153, in dispatch_return
    self.user_return(frame, arg)
  File "/home/home/.pyenv/versions/3.11.1/lib/python3.11/pdb.py", line 372, in user_return
    self.interaction(frame, None)
  File "/home/home/.pyenv/versions/3.11.1/lib/python3.11/pdb.py", line 435, in interaction
    self._cmdloop()
  File "/home/home/.pyenv/versions/3.11.1/lib/python3.11/pdb.py", line 400, in _cmdloop
    self.cmdloop()
  File "/home/home/.pyenv/versions/3.11.1/lib/python3.11/cmd.py", line 137, in cmdloop
    line = self.precmd(line)
           ^^^^^^^^^^^^^^^^^
  File "/home/home/.pyenv/versions/3.11.1/lib/python3.11/pdb.py", line 473, in precmd
    while args[0] in self.aliases:
          ~~~~^^^
IndexError: list index out of range

The general syntax of alias foo is valid, as it allows to print the alias. However, if there is no command registered, calling it fails.

I am unsure if raising an exception is the wanted behavior here. I think providing an error message which indicates that the current alias is unknown could be more appropriate, e.g.:

$ python /tmp/bar.py
--Return--
> /tmp/bar.py(1)<module>()->None
-> breakpoint()
(Pdb) alias foo
*** Unkown alias. To create an alias see 'help alias'

A solution could be to add an additional check to catch cases where one tries to register an alias without a command.

CPython versions tested on:

3.10, 3.11, CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions