retry.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. # Copyright 2016–2021 Julien Danjou
  2. # Copyright 2016 Joshua Harlow
  3. # Copyright 2013-2014 Ray Holder
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import abc
  17. import re
  18. import typing
  19. if typing.TYPE_CHECKING:
  20. from pip._vendor.tenacity import RetryCallState
  21. class retry_base(abc.ABC):
  22. """Abstract base class for retry strategies."""
  23. @abc.abstractmethod
  24. def __call__(self, retry_state: "RetryCallState") -> bool:
  25. pass
  26. def __and__(self, other: "retry_base") -> "retry_all":
  27. return retry_all(self, other)
  28. def __or__(self, other: "retry_base") -> "retry_any":
  29. return retry_any(self, other)
  30. RetryBaseT = typing.Union[retry_base, typing.Callable[["RetryCallState"], bool]]
  31. class _retry_never(retry_base):
  32. """Retry strategy that never rejects any result."""
  33. def __call__(self, retry_state: "RetryCallState") -> bool:
  34. return False
  35. retry_never = _retry_never()
  36. class _retry_always(retry_base):
  37. """Retry strategy that always rejects any result."""
  38. def __call__(self, retry_state: "RetryCallState") -> bool:
  39. return True
  40. retry_always = _retry_always()
  41. class retry_if_exception(retry_base):
  42. """Retry strategy that retries if an exception verifies a predicate."""
  43. def __init__(self, predicate: typing.Callable[[BaseException], bool]) -> None:
  44. self.predicate = predicate
  45. def __call__(self, retry_state: "RetryCallState") -> bool:
  46. if retry_state.outcome is None:
  47. raise RuntimeError("__call__() called before outcome was set")
  48. if retry_state.outcome.failed:
  49. exception = retry_state.outcome.exception()
  50. if exception is None:
  51. raise RuntimeError("outcome failed but the exception is None")
  52. return self.predicate(exception)
  53. else:
  54. return False
  55. class retry_if_exception_type(retry_if_exception):
  56. """Retries if an exception has been raised of one or more types."""
  57. def __init__(
  58. self,
  59. exception_types: typing.Union[
  60. typing.Type[BaseException],
  61. typing.Tuple[typing.Type[BaseException], ...],
  62. ] = Exception,
  63. ) -> None:
  64. self.exception_types = exception_types
  65. super().__init__(lambda e: isinstance(e, exception_types))
  66. class retry_if_not_exception_type(retry_if_exception):
  67. """Retries except an exception has been raised of one or more types."""
  68. def __init__(
  69. self,
  70. exception_types: typing.Union[
  71. typing.Type[BaseException],
  72. typing.Tuple[typing.Type[BaseException], ...],
  73. ] = Exception,
  74. ) -> None:
  75. self.exception_types = exception_types
  76. super().__init__(lambda e: not isinstance(e, exception_types))
  77. class retry_unless_exception_type(retry_if_exception):
  78. """Retries until an exception is raised of one or more types."""
  79. def __init__(
  80. self,
  81. exception_types: typing.Union[
  82. typing.Type[BaseException],
  83. typing.Tuple[typing.Type[BaseException], ...],
  84. ] = Exception,
  85. ) -> None:
  86. self.exception_types = exception_types
  87. super().__init__(lambda e: not isinstance(e, exception_types))
  88. def __call__(self, retry_state: "RetryCallState") -> bool:
  89. if retry_state.outcome is None:
  90. raise RuntimeError("__call__() called before outcome was set")
  91. # always retry if no exception was raised
  92. if not retry_state.outcome.failed:
  93. return True
  94. exception = retry_state.outcome.exception()
  95. if exception is None:
  96. raise RuntimeError("outcome failed but the exception is None")
  97. return self.predicate(exception)
  98. class retry_if_exception_cause_type(retry_base):
  99. """Retries if any of the causes of the raised exception is of one or more types.
  100. The check on the type of the cause of the exception is done recursively (until finding
  101. an exception in the chain that has no `__cause__`)
  102. """
  103. def __init__(
  104. self,
  105. exception_types: typing.Union[
  106. typing.Type[BaseException],
  107. typing.Tuple[typing.Type[BaseException], ...],
  108. ] = Exception,
  109. ) -> None:
  110. self.exception_cause_types = exception_types
  111. def __call__(self, retry_state: "RetryCallState") -> bool:
  112. if retry_state.outcome is None:
  113. raise RuntimeError("__call__ called before outcome was set")
  114. if retry_state.outcome.failed:
  115. exc = retry_state.outcome.exception()
  116. while exc is not None:
  117. if isinstance(exc.__cause__, self.exception_cause_types):
  118. return True
  119. exc = exc.__cause__
  120. return False
  121. class retry_if_result(retry_base):
  122. """Retries if the result verifies a predicate."""
  123. def __init__(self, predicate: typing.Callable[[typing.Any], bool]) -> None:
  124. self.predicate = predicate
  125. def __call__(self, retry_state: "RetryCallState") -> bool:
  126. if retry_state.outcome is None:
  127. raise RuntimeError("__call__() called before outcome was set")
  128. if not retry_state.outcome.failed:
  129. return self.predicate(retry_state.outcome.result())
  130. else:
  131. return False
  132. class retry_if_not_result(retry_base):
  133. """Retries if the result refutes a predicate."""
  134. def __init__(self, predicate: typing.Callable[[typing.Any], bool]) -> None:
  135. self.predicate = predicate
  136. def __call__(self, retry_state: "RetryCallState") -> bool:
  137. if retry_state.outcome is None:
  138. raise RuntimeError("__call__() called before outcome was set")
  139. if not retry_state.outcome.failed:
  140. return not self.predicate(retry_state.outcome.result())
  141. else:
  142. return False
  143. class retry_if_exception_message(retry_if_exception):
  144. """Retries if an exception message equals or matches."""
  145. def __init__(
  146. self,
  147. message: typing.Optional[str] = None,
  148. match: typing.Optional[str] = None,
  149. ) -> None:
  150. if message and match:
  151. raise TypeError(f"{self.__class__.__name__}() takes either 'message' or 'match', not both")
  152. # set predicate
  153. if message:
  154. def message_fnc(exception: BaseException) -> bool:
  155. return message == str(exception)
  156. predicate = message_fnc
  157. elif match:
  158. prog = re.compile(match)
  159. def match_fnc(exception: BaseException) -> bool:
  160. return bool(prog.match(str(exception)))
  161. predicate = match_fnc
  162. else:
  163. raise TypeError(f"{self.__class__.__name__}() missing 1 required argument 'message' or 'match'")
  164. super().__init__(predicate)
  165. class retry_if_not_exception_message(retry_if_exception_message):
  166. """Retries until an exception message equals or matches."""
  167. def __init__(
  168. self,
  169. message: typing.Optional[str] = None,
  170. match: typing.Optional[str] = None,
  171. ) -> None:
  172. super().__init__(message, match)
  173. # invert predicate
  174. if_predicate = self.predicate
  175. self.predicate = lambda *args_, **kwargs_: not if_predicate(*args_, **kwargs_)
  176. def __call__(self, retry_state: "RetryCallState") -> bool:
  177. if retry_state.outcome is None:
  178. raise RuntimeError("__call__() called before outcome was set")
  179. if not retry_state.outcome.failed:
  180. return True
  181. exception = retry_state.outcome.exception()
  182. if exception is None:
  183. raise RuntimeError("outcome failed but the exception is None")
  184. return self.predicate(exception)
  185. class retry_any(retry_base):
  186. """Retries if any of the retries condition is valid."""
  187. def __init__(self, *retries: retry_base) -> None:
  188. self.retries = retries
  189. def __call__(self, retry_state: "RetryCallState") -> bool:
  190. return any(r(retry_state) for r in self.retries)
  191. class retry_all(retry_base):
  192. """Retries if all the retries condition are valid."""
  193. def __init__(self, *retries: retry_base) -> None:
  194. self.retries = retries
  195. def __call__(self, retry_state: "RetryCallState") -> bool:
  196. return all(r(retry_state) for r in self.retries)