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

Click raises RuntimeError even when exit code = 0, hence gives error in ipython #2249

Closed
antonymilne opened this issue Apr 6, 2022 · 2 comments

Comments

@antonymilne
Copy link

When click exits, it raises a RuntimeError even when everything worked ok. Hence, if I wrap a call to a click command inside a try/except, this will be picked up as an exception even though nothing went wrong.

This creates a problem with ipython, which catches all exceptions and does some custom handling of them. What happens appears to be: ipython executes the code, click raises the RuntimeError, ipython catches it but because there's no actual error message or traceback, ipython seems to get confused and generates even more exceptions.

Is there a reason why click raises an exception even when the program ran ok and code == 0? I changed the click behaviour very slightly to no longer do so and it fixes this completely, but I'm guessing there's a reason click doesn't do this already?

Steps to reproduce

  1. Put the README's simple example into hello.py
  2. Start an ipython session using ipython
  3. Enter from hello import hello; hello()
  4. Command executes ok (even with arguments, like hello(["--count", 3]) but will afterwards produce the following exception (see full traceback):
ERROR:root:Internal Python error in the inspect module.
Below is the traceback from this internal error.

Traceback (most recent call last):
  File "/Users/antony_milne/miniconda3/envs/spaceflights/lib/python3.8/site-packages/click/core.py", line 1065, in main
    ctx.exit()
  File "/Users/antony_milne/miniconda3/envs/spaceflights/lib/python3.8/site-packages/click/core.py", line 687, in exit
    raise Exit(code)
click.exceptions.Exit: 0

Note: that ipython -m hello works ok, as does the above flow when using python. It's only when running an interactive ipython session that click's RuntimeError is problematic.

What should happen

No traceback from ipython.

Environment

  • Python version: 3.8.13
  • Click version: 8.1.2
@davidism
Copy link
Member

davidism commented Apr 6, 2022

Click makes command line programs. Programs exit after they are finished. Use cli([arg, ...], standalone_mode=False) if you don't want to exit.

@davidism davidism closed this as completed Apr 6, 2022
@antonymilne
Copy link
Author

antonymilne commented Apr 6, 2022

ok, but is it normal for a program to exit by raising an exception even when nothing went wrong? Is there no other way for click to exit in the case of code == 0 in a way that wouldn't get caught by except?

Thanks for the tip about standalone_mode - I wasn't aware of that.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 21, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants