Improve error handling for Windows and MSS context#519
Conversation
Also improve our general exception handling for MSS context object
| try: | ||
| self.close() | ||
| except Exception: | ||
| # This extra work is needed so that exceptions generated during __exit__ |
There was a problem hiding this comment.
Wouldn't these normally be chained? Is there a reason not to use chained exceptions here? I'm a little hesitant to just swallow exceptions entirely.
There was a problem hiding this comment.
No. When a with body is already failing, and __exit__ also fails during cleanup, chaining creates the wrong relationship and headline. Python/pytest reports the exception raised from __exit__ as the active failure, so the real body failure becomes secondary context. That was the ReleaseDC masking the actual BitBlt/thread failure in the PR that triggered this work.
Python 3.11 has a new feature that is designed for this case, https://docs.python.org/3/library/exceptions.html#lib-exception-groups - but we cannot use that since we support 3.10.
Make the thread helper available separately so it can be reused in the future. That will hopefully prevent this accident going forward.
|
Thanks @halldorfannar ! |
Changes proposed in this PR
We essentially had three problems:
withscope) with outer exceptions (generated during__exit__) - this led to the wrong exception being surfaced by our tests. The MSS Context object has been enhanced to not mask the inner exception with an outer exception. Once Python 3.11 becomes baseline we can useExceptionGroupsto surface both exceptions and still get the right outcome, but until then the common pattern I applied here will suffice.This relates to pull request #516. If you combine this with that you will see massive improvements in the error handling.
./check.shpassed