Skip to content

Cyclic reference lead to contextlib.contextmanager exception not being deleted #129280

Open
@user202729

Description

@user202729

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions