Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/sqlalchemy/util/_compat_py3k.py: 21%
43 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:35 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:35 +0000
1# util/_compat_py3k.py
2# Copyright (C) 2005-2023 the SQLAlchemy authors and contributors
3# <see AUTHORS file>
4#
5# This module is part of SQLAlchemy and is released under
6# the MIT License: https://www.opensource.org/licenses/mit-license.php
8from functools import wraps
10# vendored from py3.7
13class _AsyncGeneratorContextManager:
14 """Helper for @asynccontextmanager."""
16 def __init__(self, func, args, kwds):
17 self.gen = func(*args, **kwds)
18 self.func, self.args, self.kwds = func, args, kwds
19 doc = getattr(func, "__doc__", None)
20 if doc is None:
21 doc = type(self).__doc__
22 self.__doc__ = doc
24 async def __aenter__(self):
25 try:
26 return await self.gen.__anext__()
27 except StopAsyncIteration:
28 raise RuntimeError("generator didn't yield") from None
30 async def __aexit__(self, typ, value, traceback):
31 if typ is None:
32 try:
33 await self.gen.__anext__()
34 except StopAsyncIteration:
35 return
36 else:
37 raise RuntimeError("generator didn't stop")
38 else:
39 if value is None:
40 value = typ()
41 # See _GeneratorContextManager.__exit__ for comments on subtleties
42 # in this implementation
43 try:
44 await self.gen.athrow(typ, value, traceback)
45 raise RuntimeError("generator didn't stop after athrow()")
46 except StopAsyncIteration as exc:
47 return exc is not value
48 except RuntimeError as exc:
49 if exc is value:
50 return False
51 if isinstance(value, (StopIteration, StopAsyncIteration)):
52 if exc.__cause__ is value:
53 return False
54 raise
55 except BaseException as exc:
56 if exc is not value:
57 raise
60# using the vendored version in all cases at the moment to establish
61# full test coverage
62def asynccontextmanager(func):
63 @wraps(func)
64 def helper(*args, **kwds):
65 return _AsyncGeneratorContextManager(func, args, kwds)
67 return helper