◐ Shell
clean mode source ↗

Cyclic reference lead to contextlib.contextmanager exception not being deleted

Bug report

Bug description:

Consider the following code.

from contextlib import contextmanager

class MyException(Exception):
	def __del__(self):
		print("deleted")

@contextmanager
def f():
	try:
		yield
	except MyException:
		pass

with f():
	raise MyException()

print("done")

In Python 3.11 the printed value is deleted done, in Python 3.12 the printed value is done deleted. Which means the exception is kept around much longer than it need to be, this is caused by a reference cycle between the exception and the frame in contextmanager implementation.

CPython versions tested on:

3.11, 3.12

Operating systems tested on:

Linux

Linked PRs