Open yumasheta opened 3 years ago
Thanks @yumasheta.
Just to double check, is this only happening in the 8.0 alpha release? Or is it also an issue with 7.x
?
Yes, it does happen with the current version (7.6.3) on PYPI as well. Just verified it.
ok thanks for checking :+1:
Here is the relevant code:
Can you say more about your situation, and why you want the exception propagated out of the context handler rather than just handled and displayed, as the code above indicates?
Ok, so maybe I'm missing something. I've been struggling a bit with how the output context works and is meant to work. I'd like a way to handle exceptions that occur inside the output context in the surrounding code. But if that exception never leaves the context that's not possible. So basically:
output = ipywidgets.Output()
try:
with output:
raise Exception
except:
print("error")
But this doesn't work.
If you don't mind me talking about my implementation: I have some custom ipywidgets that use the Output context so that they can clear it and replace content in the same output. So it might not be a problem when using the jupyter notebook interactively (that is: exceptions silently fail), because you'll see the output anyway. However, it'd be nice to have an options to handle those exceptions. In particular I have unit tests that execute pre-defined notebook code (via ipython) that goes through the features of the widgets and has to fail on exceptions. But as you can imagine, this doesn't happen.
TL,DR: I want to "unit test" custom widgets for jupyter. I need a way to execute (and manipulate) my widgets programatically and test for errors. The widgets put their output in Output contexts, such that exceptions within those don't lead to failed tests.
This would be, in my opinion, a breaking change on the behavior of ipywidgets.Output()
-- so I think it would be worth considering shipping in 8.0 rather than 8.1. @vidartf, Any potential possibilities of including this earlier?
To me, the current behavior sounds really strange. It was not supposed to suppress exceptions, and the use of Output
can change the control flow of user program.
More specifically,
def foo():
with output: # block 1
print("Hello")
data = some_logic_that_throws_an_exception()
print("this will not be executed")
with output: # block 2
print("data = ", data)
This will first show a stacktrace for the Exception from the first block. The rest of the code, especially block 2 should NOT be executed, but it does, and another stacktrace will be shown for an exception thrown when accessing data
(UnboundLocalError: local variable 'data' referenced before assignment).
In addition, whether ipython is involved or not does change the behavior and flow of the same code.
Alternatively, we may have a configurable option whether to suppress exception or not if we want to keep a backward compatibility), but I suggest we should correct the behavior as we deliver a major release.
I submitted a PR #3417 that would fix this one. My proposal is to change the behavior on whether exceptions won't be suppressed by default, but this would be up to core developers' decision. Thank you for your considerations.
Thanks for the PR @wookayin . As a placeholder, you can also create a new widget by inheritance (no action in ipywidgets repo needed):
class NocatchOutput(ipywidgets.Output):
def __exit__(self, *args, **kwargs):
super().__exit__(*args, **kwargs)
@vidartf many thanks for the workaround. This seems to be printing the Traceback in red inside the Output, disregarding if the exception is later handled. Is there any way to avoid this?
So this weird behavior has not been fixed yet? I am still trying to understand why the user would expect or want an output context to essentially redirect stderr to /dev/null
. It is the definition of failing ungracefully. Any updates?
ipywidgets.Output() context manager supresses exceptions and prevents correct exit code
Description
For example take this code snippet and save it to a cell of a *.ipynb notebook.
Then execute the notebook with
ipython
:The output will be
Expected behavior
The call to ipython should fail. If it is not aborted at the time when the exception is raised, at least it should exit with exit code other than 0.
Actual Behavior
The exception raised in the output context is suppressed. Execution continues and the ipython call exits with code 0.
Also the stderr output from the exception is not displayed unless
display(o)
is called explicitly. But this I reckon is expected behavior.Context
8.0.0a4
archlinux kernel 5.12.7-arch1-1
not used
Troubleshoot Output
Command Line Output