11"""Utilities for with-statement contexts. See PEP 343."""
2+ from inspect import isasyncgenfunction , iscoroutinefunction , \
3+ isgeneratorfunction
4+
25import abc
36import os
47import sys
@@ -79,11 +82,32 @@ def _recreate_cm(self):
7982 return self
8083
8184 def __call__ (self , func ):
82- @wraps (func )
8385 def inner (* args , ** kwds ):
8486 with self ._recreate_cm ():
8587 return func (* args , ** kwds )
86- return inner
88+
89+ def gen_inner (* args , ** kwds ):
90+ with self ._recreate_cm ():
91+ yield from func (* args , ** kwds )
92+
93+ async def async_inner (* args , ** kwds ):
94+ with self ._recreate_cm ():
95+ return await func (* args , ** kwds )
96+
97+ async def asyncgen_inner (* args , ** kwds ):
98+ with self ._recreate_cm ():
99+ async for value in func (* args , ** kwds ):
100+ yield value
101+
102+ wrapper = wraps (func )
103+ if isasyncgenfunction (func ):
104+ return wrapper (asyncgen_inner )
105+ elif iscoroutinefunction (func ):
106+ return wrapper (async_inner )
107+ elif isgeneratorfunction (func ):
108+ return wrapper (gen_inner )
109+ else :
110+ return wrapper (inner )
87111
88112
89113class AsyncContextDecorator (object ):
@@ -95,11 +119,33 @@ def _recreate_cm(self):
95119 return self
96120
97121 def __call__ (self , func ):
98- @wraps (func )
99122 async def inner (* args , ** kwds ):
123+ async with self ._recreate_cm ():
124+ return func (* args , ** kwds )
125+
126+ async def gen_inner (* args , ** kwds ):
127+ async with self ._recreate_cm ():
128+ for value in func (* args , ** kwds ):
129+ yield value
130+
131+ async def async_inner (* args , ** kwds ):
100132 async with self ._recreate_cm ():
101133 return await func (* args , ** kwds )
102- return inner
134+
135+ async def asyncgen_inner (* args , ** kwds ):
136+ async with self ._recreate_cm ():
137+ async for value in func (* args , ** kwds ):
138+ yield value
139+
140+ wrapper = wraps (func )
141+ if isasyncgenfunction (func ):
142+ return wrapper (asyncgen_inner )
143+ elif iscoroutinefunction (func ):
144+ return wrapper (async_inner )
145+ elif isgeneratorfunction (func ):
146+ return wrapper (gen_inner )
147+ else :
148+ return wrapper (inner )
103149
104150
105151class _GeneratorContextManagerBase :
0 commit comments