python / python/cpython

Replace context enter and exit events with single "context switched" event

未關閉
#124,872 18 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

interpreter-core type-feature
主要語言
Python
星號
77.2k
分支
35.9k
PR 合併指標
PR 指標待擷取

描述

Feature or enhancement

Proposal:

(split off of https://github.com/python/cpython/issues/99633#issuecomment-2380302948 to keep related pull requests and discussion better organized)

Starting in v3.14, C code can subscribe to "context enter" and "context exit" events:

https://github.com/python/cpython/blob/120729d862f0ef9979508899f7f63a9f3d9623cb/Include/cpython/context.h#L30-L60

See gh-119333 for some background.

I would like to replace the just-added "context enter" and "context exit" events with a single "context switched" event:

diff --git a/Include/cpython/context.h b/Include/cpython/context.h
index ec72966e82c..f9638b0abe5 100644
--- a/Include/cpython/context.h
+++ b/Include/cpython/context.h
@@ -28,8 +28,12 @@ PyAPI_FUNC(int) PyContext_Enter(PyObject *);
 PyAPI_FUNC(int) PyContext_Exit(PyObject *);
 
 typedef enum {
-   Py_CONTEXT_EVENT_ENTER,
-   Py_CONTEXT_EVENT_EXIT,
+    /*
+     * The current context has switched to a different context.  The object
+     * passed to the watch callback is the now-current contextvars.Context
+     * object.
+     */
+    Py_CONTEXT_SWITCHED = 1,
 } PyContextEvent;
 
 /*
diff --git a/Python/context.c b/Python/context.c
index ddb03555f9e..6c9f68583c2 100644
--- a/Python/context.c
+++ b/Python/context.c
@@ -192,7 +192,7 @@ _PyContext_Enter(PyThreadState *ts, PyObject *octx)
     ts->context = Py_NewRef(ctx);
     ts->context_ver++;
 
-    notify_context_watchers(Py_CONTEXT_EVENT_ENTER, ctx, ts);
+    notify_context_watchers(Py_CONTEXT_SWITCHED, ctx, ts);
     return 0;
 }
 
@@ -226,13 +226,13 @@ _PyContext_Exit(PyThreadState *ts, PyObject *octx)
         return -1;
     }
 
-    notify_context_watchers(Py_CONTEXT_EVENT_EXIT, ctx, ts);
     Py_SETREF(ts->context, (PyObject *)ctx->ctx_prev);
     ts->context_ver++;
 
     ctx->ctx_prev = NULL;
     ctx->ctx_entered = 0;
 
+    notify_context_watchers(Py_CONTEXT_SWITCHED, (PyContext *)ts->context, ts);
     return 0;
 }
 

Rationale: Users want to know when the current context switches to a different context object. Right now this happens when and only when a context is entered or exited, so the enter and exit events are synonymous with "switched". However, if the changes proposed for gh-99633 are implemented, the current context will also switch for reasons other than context enter or exit. Since users actually care about context switches and not enter or exit, replacing the enter and exit events with a single switched event would make the new feature compatible with gh-99633.

The current "context exit" event is emitted just before exiting the context. The new "context switched" event would be emitted after the context is exited to match the semantics users expect of an event with a past-tense name. If users need the ability to clean up before the switch takes effect, another "context is about to switch" event can be added in the future. I am not proposing to add such an event for this issue because no users have requested it yet (YAGNI).

I would love to get this resolved before v3.14 is released so that we don't have to worry about breaking backwards compatibility.

Has this already been discussed elsewhere?

I have already discussed this feature proposal on another bug report.

Links to previous discussion of this feature:

https://github.com/python/cpython/issues/99633#issuecomment-2380302948

Linked PRs
  • gh-124774
  • gh-124773
  • gh-124741
  • gh-124737
  • gh-124776
  • gh-125233
  • gh-125285
  • gh-125532
  • gh-125638

貢獻指南

開啟貢獻指南

從這裡開始

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

研究方向

從 Include/cpython/context.h 和 Python/context.c 開始,接著閱讀 gh-99633 中連結的討論,以及其中列出的相關 PR。完成的標準是:以提案中描述的單一 post-switch event 取代 enter 和 exit 事件,同時保留 Python 3.14 之前預期的相容性。

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

評估

技術堆疊
c, python
領域
api, backend
Issue 類型
功能
難度
5/5
預估耗時
一週以上
活躍度
停滯
描述清晰度
描述清楚
新手友好度
25/100

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

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