Skip to content

Commit c80e42b

Browse files
committed
Fix method of dropping the reference to the exception
1 parent eb113cc commit c80e42b

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

‎Lib/contextlib.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ def __enter__(self):
155155
raise RuntimeError("generator didn't yield") from None
156156

157157
def __exit__(self, typ, value, traceback):
158+
# don't keep the stored exception alive unnecessarily
159+
self.exc_context, exc_context = None, self.exc_context
158160
if typ is None:
159161
try:
160162
next(self.gen)
@@ -164,7 +166,6 @@ def __exit__(self, typ, value, traceback):
164166
try:
165167
raise RuntimeError("generator didn't stop")
166168
finally:
167-
del self.exc_context
168169
self.gen.close()
169170
else:
170171
if value is None:
@@ -177,7 +178,7 @@ def __exit__(self, typ, value, traceback):
177178
# context here. In order to make the context manager behave
178179
# like a normal function we set the current exception context
179180
# to what it was during the context manager's __enter__
180-
_set_current_exception(self.exc_context)
181+
_set_current_exception(exc_context)
181182
self.gen.throw(value)
182183
except StopIteration as exc:
183184
# Suppress StopIteration *unless* it's the same exception that
@@ -216,7 +217,6 @@ def __exit__(self, typ, value, traceback):
216217
try:
217218
raise RuntimeError("generator didn't stop after throw()")
218219
finally:
219-
del self.exc_context
220220
self.gen.close()
221221

222222
class _AsyncGeneratorContextManager(

0 commit comments

Comments
 (0)