-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backported
contextlib.suppress
from Python 3.12.1 (#95)
- Loading branch information
Showing
5 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import sys | ||
from contextlib import AbstractContextManager | ||
|
||
if sys.version_info < (3, 11): | ||
from ._exceptions import BaseExceptionGroup | ||
|
||
|
||
class suppress(AbstractContextManager): | ||
"""Backport of :class:`contextlib.suppress` from Python 3.12.1.""" | ||
|
||
def __init__(self, *exceptions): | ||
self._exceptions = exceptions | ||
|
||
def __enter__(self): | ||
pass | ||
|
||
def __exit__(self, exctype, excinst, exctb): | ||
# Unlike isinstance and issubclass, CPython exception handling | ||
# currently only looks at the concrete type hierarchy (ignoring | ||
# the instance and subclass checking hooks). While Guido considers | ||
# that a bug rather than a feature, it's a fairly hard one to fix | ||
# due to various internal implementation details. suppress provides | ||
# the simpler issubclass based semantics, rather than trying to | ||
# exactly reproduce the limitations of the CPython interpreter. | ||
# | ||
# See http://bugs.python.org/issue12029 for more details | ||
if exctype is None: | ||
return | ||
|
||
if issubclass(exctype, self._exceptions): | ||
return True | ||
|
||
if issubclass(exctype, BaseExceptionGroup): | ||
match, rest = excinst.split(self._exceptions) | ||
if rest is None: | ||
return True | ||
|
||
raise rest | ||
|
||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import sys | ||
|
||
import pytest | ||
|
||
from exceptiongroup import suppress | ||
|
||
if sys.version_info < (3, 11): | ||
from exceptiongroup import BaseExceptionGroup, ExceptionGroup | ||
|
||
|
||
def test_suppress_exception(): | ||
with pytest.raises(ExceptionGroup) as exc, suppress(SystemExit): | ||
raise BaseExceptionGroup("", [SystemExit(1), RuntimeError("boo")]) | ||
|
||
assert len(exc.value.exceptions) == 1 | ||
assert isinstance(exc.value.exceptions[0], RuntimeError) |