utils_py3.py 956 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. from collections.abc import Awaitable
  2. from inspect import CO_ITERABLE_COROUTINE
  3. from types import CoroutineType
  4. from types import GeneratorType
  5. async def do_await(obj):
  6. return await obj
  7. def do_yield_from(gen):
  8. return (yield from gen)
  9. def await_(obj):
  10. obj_type = type(obj)
  11. if (
  12. obj_type is CoroutineType or
  13. obj_type is GeneratorType and bool(obj.gi_code.co_flags & CO_ITERABLE_COROUTINE) or
  14. isinstance(obj, Awaitable)
  15. ):
  16. return do_await(obj).__await__()
  17. else:
  18. return do_yield_from(obj)
  19. def __aiter__(self):
  20. return self.__wrapped__.__aiter__()
  21. async def __anext__(self):
  22. return await self.__wrapped__.__anext__()
  23. def __await__(self):
  24. return await_(self.__wrapped__)
  25. def __aenter__(self):
  26. return self.__wrapped__.__aenter__()
  27. def __aexit__(self, *args, **kwargs):
  28. return self.__wrapped__.__aexit__(*args, **kwargs)