66import _collections_abc
77from collections import deque
88from functools import wraps
9- from inspect import isasyncgenfunction as _isasyncgenfunction
10- from inspect import iscoroutinefunction as _iscoroutinefunction
11- from inspect import isgeneratorfunction as _isgeneratorfunction
9+ lazy from inspect import isasyncgenfunction as _isasyncgenfunction
10+ lazy from inspect import iscoroutinefunction as _iscoroutinefunction
11+ lazy from inspect import isgeneratorfunction as _isgeneratorfunction
1212from types import GenericAlias
1313
1414__all__ = ["asynccontextmanager" , "contextmanager" , "closing" , "nullcontext" ,
@@ -83,32 +83,36 @@ def _recreate_cm(self):
8383 return self
8484
8585 def __call__ (self , func ):
86- def inner (* args , ** kwds ):
87- with self ._recreate_cm ():
88- return func (* args , ** kwds )
89-
90- def gen_inner (* args , ** kwds ):
91- with self ._recreate_cm (), closing (func (* args , ** kwds )) as gen :
92- yield from gen
93-
94- async def async_inner (* args , ** kwds ):
95- with self ._recreate_cm ():
96- return await func (* args , ** kwds )
97-
98- async def asyncgen_inner (* args , ** kwds ):
99- with self ._recreate_cm ():
100- async with aclosing (func (* args , ** kwds )) as gen :
101- async for value in gen :
102- yield value
103-
10486 wrapper = wraps (func )
10587 if _isasyncgenfunction (func ):
88+
89+ async def asyncgen_inner (* args , ** kwds ):
90+ with self ._recreate_cm ():
91+ async with aclosing (func (* args , ** kwds )) as gen :
92+ async for value in gen :
93+ yield value
94+
10695 return wrapper (asyncgen_inner )
10796 elif _iscoroutinefunction (func ):
97+
98+ async def async_inner (* args , ** kwds ):
99+ with self ._recreate_cm ():
100+ return await func (* args , ** kwds )
101+
108102 return wrapper (async_inner )
109103 elif _isgeneratorfunction (func ):
104+
105+ def gen_inner (* args , ** kwds ):
106+ with self ._recreate_cm (), closing (func (* args , ** kwds )) as gen :
107+ yield from gen
108+
110109 return wrapper (gen_inner )
111110 else :
111+
112+ def inner (* args , ** kwds ):
113+ with self ._recreate_cm ():
114+ return func (* args , ** kwds )
115+
112116 return wrapper (inner )
113117
114118
@@ -121,36 +125,40 @@ def _recreate_cm(self):
121125 return self
122126
123127 def __call__ (self , func ):
124- async def inner (* args , ** kwds ):
125- async with self ._recreate_cm ():
126- return func (* args , ** kwds )
127-
128- async def gen_inner (* args , ** kwds ):
129- async with self ._recreate_cm ():
130- with closing (func (* args , ** kwds )) as gen :
131- for value in gen :
132- yield value
133-
134- async def async_inner (* args , ** kwds ):
135- async with self ._recreate_cm ():
136- return await func (* args , ** kwds )
137-
138- async def asyncgen_inner (* args , ** kwds ):
139- async with (
140- self ._recreate_cm (),
141- aclosing (func (* args , ** kwds )) as gen
142- ):
143- async for value in gen :
144- yield value
145-
146128 wrapper = wraps (func )
147129 if _isasyncgenfunction (func ):
130+
131+ async def asyncgen_inner (* args , ** kwds ):
132+ async with (
133+ self ._recreate_cm (),
134+ aclosing (func (* args , ** kwds )) as gen
135+ ):
136+ async for value in gen :
137+ yield value
138+
148139 return wrapper (asyncgen_inner )
149140 elif _iscoroutinefunction (func ):
141+
142+ async def async_inner (* args , ** kwds ):
143+ async with self ._recreate_cm ():
144+ return await func (* args , ** kwds )
145+
150146 return wrapper (async_inner )
151147 elif _isgeneratorfunction (func ):
148+
149+ async def gen_inner (* args , ** kwds ):
150+ async with self ._recreate_cm ():
151+ with closing (func (* args , ** kwds )) as gen :
152+ for value in gen :
153+ yield value
154+
152155 return wrapper (gen_inner )
153156 else :
157+
158+ async def inner (* args , ** kwds ):
159+ async with self ._recreate_cm ():
160+ return func (* args , ** kwds )
161+
154162 return wrapper (inner )
155163
156164
0 commit comments