Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/anyio/_core/_exceptions.py: 73%

26 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 07:19 +0000

1from __future__ import annotations 

2 

3from traceback import format_exception 

4 

5 

6class BrokenResourceError(Exception): 

7 """ 

8 Raised when trying to use a resource that has been rendered unusable due to external causes 

9 (e.g. a send stream whose peer has disconnected). 

10 """ 

11 

12 

13class BrokenWorkerProcess(Exception): 

14 """ 

15 Raised by :func:`run_sync_in_process` if the worker process terminates abruptly or otherwise 

16 misbehaves. 

17 """ 

18 

19 

20class BusyResourceError(Exception): 

21 """Raised when two tasks are trying to read from or write to the same resource concurrently.""" 

22 

23 def __init__(self, action: str): 

24 super().__init__(f"Another task is already {action} this resource") 

25 

26 

27class ClosedResourceError(Exception): 

28 """Raised when trying to use a resource that has been closed.""" 

29 

30 

31class DelimiterNotFound(Exception): 

32 """ 

33 Raised during :meth:`~anyio.streams.buffered.BufferedByteReceiveStream.receive_until` if the 

34 maximum number of bytes has been read without the delimiter being found. 

35 """ 

36 

37 def __init__(self, max_bytes: int) -> None: 

38 super().__init__( 

39 f"The delimiter was not found among the first {max_bytes} bytes" 

40 ) 

41 

42 

43class EndOfStream(Exception): 

44 """Raised when trying to read from a stream that has been closed from the other end.""" 

45 

46 

47class ExceptionGroup(BaseException): 

48 """ 

49 Raised when multiple exceptions have been raised in a task group. 

50 

51 :var ~typing.Sequence[BaseException] exceptions: the sequence of exceptions raised together 

52 """ 

53 

54 SEPARATOR = "----------------------------\n" 

55 

56 exceptions: list[BaseException] 

57 

58 def __str__(self) -> str: 

59 tracebacks = [ 

60 "".join(format_exception(type(exc), exc, exc.__traceback__)) 

61 for exc in self.exceptions 

62 ] 

63 return ( 

64 f"{len(self.exceptions)} exceptions were raised in the task group:\n" 

65 f"{self.SEPARATOR}{self.SEPARATOR.join(tracebacks)}" 

66 ) 

67 

68 def __repr__(self) -> str: 

69 exception_reprs = ", ".join(repr(exc) for exc in self.exceptions) 

70 return f"<{self.__class__.__name__}: {exception_reprs}>" 

71 

72 

73class IncompleteRead(Exception): 

74 """ 

75 Raised during :meth:`~anyio.streams.buffered.BufferedByteReceiveStream.receive_exactly` or 

76 :meth:`~anyio.streams.buffered.BufferedByteReceiveStream.receive_until` if the 

77 connection is closed before the requested amount of bytes has been read. 

78 """ 

79 

80 def __init__(self) -> None: 

81 super().__init__( 

82 "The stream was closed before the read operation could be completed" 

83 ) 

84 

85 

86class TypedAttributeLookupError(LookupError): 

87 """ 

88 Raised by :meth:`~anyio.TypedAttributeProvider.extra` when the given typed attribute is not 

89 found and no default value has been given. 

90 """ 

91 

92 

93class WouldBlock(Exception): 

94 """Raised by ``X_nowait`` functions if ``X()`` would block."""