Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/exceptiongroup/_suppress.py: 40%

20 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-07 06:33 +0000

1import sys 

2from contextlib import AbstractContextManager 

3 

4if sys.version_info < (3, 11): 

5 from ._exceptions import BaseExceptionGroup 

6 

7 

8class suppress(AbstractContextManager): 

9 """Backport of :class:`contextlib.suppress` from Python 3.12.1.""" 

10 

11 def __init__(self, *exceptions): 

12 self._exceptions = exceptions 

13 

14 def __enter__(self): 

15 pass 

16 

17 def __exit__(self, exctype, excinst, exctb): 

18 # Unlike isinstance and issubclass, CPython exception handling 

19 # currently only looks at the concrete type hierarchy (ignoring 

20 # the instance and subclass checking hooks). While Guido considers 

21 # that a bug rather than a feature, it's a fairly hard one to fix 

22 # due to various internal implementation details. suppress provides 

23 # the simpler issubclass based semantics, rather than trying to 

24 # exactly reproduce the limitations of the CPython interpreter. 

25 # 

26 # See http://bugs.python.org/issue12029 for more details 

27 if exctype is None: 

28 return 

29 

30 if issubclass(exctype, self._exceptions): 

31 return True 

32 

33 if issubclass(exctype, BaseExceptionGroup): 

34 match, rest = excinst.split(self._exceptions) 

35 if rest is None: 

36 return True 

37 

38 raise rest 

39 

40 return False