Set the given ``exc`` as the `Future`'s exception. If the Future is already canceled, logs the exception instead. If this logging is not desired, the caller should explicitly check the state of the Future and call ``Future.set_exception`` instead of this wrapper. Avoids ``async
(
future: "Union[futures.Future[_T], Future[_T]]", exc: BaseException
)
| 187 | |
| 188 | |
| 189 | def future_set_exception_unless_cancelled( |
| 190 | future: "Union[futures.Future[_T], Future[_T]]", exc: BaseException |
| 191 | ) -> None: |
| 192 | """Set the given ``exc`` as the `Future`'s exception. |
| 193 | |
| 194 | If the Future is already canceled, logs the exception instead. If |
| 195 | this logging is not desired, the caller should explicitly check |
| 196 | the state of the Future and call ``Future.set_exception`` instead of |
| 197 | this wrapper. |
| 198 | |
| 199 | Avoids ``asyncio.InvalidStateError`` when calling ``set_exception()`` on |
| 200 | a cancelled `asyncio.Future`. |
| 201 | |
| 202 | .. versionadded:: 6.0 |
| 203 | |
| 204 | """ |
| 205 | if not future.cancelled(): |
| 206 | future.set_exception(exc) |
| 207 | else: |
| 208 | app_log.error("Exception after Future was cancelled", exc_info=exc) |
| 209 | |
| 210 | |
| 211 | def future_set_exc_info( |
no test coverage detected