Skip to content

Conversation

@gaogaotiantian
Copy link
Member

@gaogaotiantian gaogaotiantian commented Nov 4, 2023

Currently cmd.Cmd executes code in an except block if no command is found. This is not ideal because exception info like sys.exc_info() would break. Specifically, in pdb, the users won't be able to see the exception they want, instead they get an AttributeError from cmd.Cmd.

The fix is pretty trivial - just don't do it in the except block. Here I used default value for getattr, which is probably cleaner. It's equivalent to moving the self.default() function call outside of the except block.

@iritkatriel
Copy link
Member

How does this PR compare with #4666?

@gaogaotiantian
Copy link
Member Author

How does this PR compare with #4666?

Oh I did not see that PR. I believe the purpose is the same. I don't think we need to "avoid raising any exception", because it seems like as long as we are not doing the work in the except block, it should be fine (Python will remember the last exception raised).

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change can also be tested without debugger.

Add a test in test_cmd for default() raising an exception and check __context__ and __cause__ of the exception.

@gaogaotiantian
Copy link
Member Author

This change can also be tested without debugger.

Add a test in test_cmd for default() raising an exception and check __context__ and __cause__ of the exception.

Sure, I added a test to check the correct exception can be retrieved in default() function.

@serhiy-storchaka serhiy-storchaka enabled auto-merge (squash) November 10, 2023 20:50
@serhiy-storchaka
Copy link
Member

Thank you for your contribution @gaogaotiantian.

The advantage of this PR is that it contains tests. But #4666 has also changes in the pdb module, so it is not completely superseded.

@serhiy-storchaka serhiy-storchaka merged commit 148af38 into python:main Nov 10, 2023
@gaogaotiantian
Copy link
Member Author

Thank you for your contribution @gaogaotiantian.

The advantage of this PR is that it contains tests. But #4666 has also changes in the pdb module, so it is not completely superseded.

The pdb changes in #4666 is not needed (not that it's wrong). Like I mentioned above, this is only an issue if the pdb cmdloop executes in an except block - that's the case for the current cmd. However, simply getting an attribute and falling back to another solution in an except block works fine. That's why I did not change pdb. It may make the code slightly cleaner if I have to admit.

So in my opinion, #4666 can be closed because the problem it tries to solve is solved.

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 this pull request may close these issues.

3 participants