@@ -165,15 +165,12 @@ def __exit__(self, exctype, excinst, exctb):
165
165
# Inspired by discussions on http://bugs.python.org/issue13585
166
166
class ExitStack (object ):
167
167
"""Context manager for dynamic management of a stack of exit callbacks
168
-
169
168
For example:
170
-
171
169
with ExitStack() as stack:
172
170
files = [stack.enter_context(open(fname)) for fname in filenames]
173
171
# All opened files will automatically be closed at the end of
174
172
# the with statement, even if attempts to open files later
175
173
# in the list raise an exception
176
-
177
174
"""
178
175
def __init__ (self ):
179
176
self ._exit_callbacks = deque ()
@@ -189,14 +186,11 @@ def _push_cm_exit(self, cm, cm_exit):
189
186
"""Helper to correctly register callbacks to __exit__ methods"""
190
187
def _exit_wrapper (* exc_details ):
191
188
return cm_exit (cm , * exc_details )
192
- _exit_wrapper .__self__ = cm
193
189
self .push (_exit_wrapper )
194
190
195
191
def push (self , exit ):
196
192
"""Registers a callback with the standard __exit__ method signature
197
-
198
193
Can suppress exceptions the same way __exit__ methods can.
199
-
200
194
Also accepts any object with an __exit__ method (registering a call
201
195
to the method instead of the object itself)
202
196
"""
@@ -214,20 +208,15 @@ def push(self, exit):
214
208
215
209
def callback (self , callback , * args , ** kwds ):
216
210
"""Registers an arbitrary callback and arguments.
217
-
218
211
Cannot suppress exceptions.
219
212
"""
220
213
def _exit_wrapper (exc_type , exc , tb ):
221
214
callback (* args , ** kwds )
222
- # We changed the signature, so using @wraps is not appropriate, but
223
- # setting __wrapped__ may still help with introspection
224
- _exit_wrapper .__wrapped__ = callback
225
215
self .push (_exit_wrapper )
226
216
return callback # Allow use as a decorator
227
217
228
218
def enter_context (self , cm ):
229
219
"""Enters the supplied context manager
230
-
231
220
If successful, also pushes its __exit__ method as a callback and
232
221
returns the result of the __enter__ method.
233
222
"""
0 commit comments