Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
user202729 opened this issue Jan 25, 2025 · 0 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@user202729
Copy link

user202729 commented Jan 25, 2025

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

@user202729 user202729 added the type-bug An unexpected behavior, bug, or error label Jan 25, 2025
@ZeroIntensity ZeroIntensity added the stdlib Python modules in the Lib dir label Jan 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants