python / python/cpython

Cyclic reference lead to contextlib.contextmanager exception not being deleted

未關閉
#129,280 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

stdlib type-bug
主要語言
Python
星號
77.2k
分支
36k
PR 合併指標
PR 指標待擷取

描述

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
  • gh-129276

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

在 Python 3.11 和 3.12 上重現所提供的 contextlib.contextmanager 範例,然後檢查 contextlib.contextmanager 的實作,以追蹤 exception 和 frame 參照。完成的標準是 exception 被釋放,且不會出現 Python 3.12 中觀察到的延遲刪除;關聯的 PR gh-129276 表示相關工作已經在進行中。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
backend
Issue 類型
缺陷
難度
3/5
預估耗時
1-2 天
活躍度
停滯
描述清晰度
描述清楚
新手友好度
25/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。