情境變數物件(Context Variables Objects)¶
在 3.7 版被加入.
在 3.7.1 版的變更:
備註
在 Python 3.7.1 中所有情境變數 C API 的簽名都被改為使用PyObject 指標,而不是PyContext、PyContextVar 或PyContextToken,例如:
// 在 3.7.0:PyContext*PyContext_New(void);// 在 3.7.1+:PyObject*PyContext_New(void);
更多細節請見bpo-34762。
本節詳述contextvars 模組的公開 C API。
- typePyContext¶
用來代表
contextvars.Context物件的 C 結構。
- typePyContextVar¶
用來代表
contextvars.ContextVar物件的 C 結構。
- typePyContextToken¶
用來代表
contextvars.Token物件的 C 結構。
- PyTypeObjectPyContext_Type¶
代表context 型別的型別物件。
- PyTypeObjectPyContextVar_Type¶
代表情境變數 型別的型別物件。
- PyTypeObjectPyContextToken_Type¶
代表情境變數 token 型別的型別物件。
型別檢查巨集:
- intPyContext_CheckExact(PyObject*o)¶
如果o 的型別為
PyContext_Type則回傳 true。o 不得為NULL。此函式一定會成功回傳。
- intPyContextVar_CheckExact(PyObject*o)¶
如果o 的類型為
PyContextVar_Type則回傳 true。o 不得為NULL。此函式一定會成功回傳。
- intPyContextToken_CheckExact(PyObject*o)¶
如果o 的類型為
PyContextToken_Type則回傳 true。o 不得為NULL。此函式一定會成功回傳。
情境物件管理函式:
- intPyContext_AddWatcher(PyContext_WatchCallbackcallback)¶
Registercallback as a context object watcher for the current interpreter.Return an ID which may be passed to
PyContext_ClearWatcher().In case of error (e.g. no more watcher IDs available),return-1and set an exception.在 3.14 版被加入.
- intPyContext_ClearWatcher(intwatcher_id)¶
Clear watcher identified bywatcher_id previously returned from
PyContext_AddWatcher()for the current interpreter.Return0on success, or-1and set an exception on error(e.g. if the givenwatcher_id was never registered.)在 3.14 版被加入.
- typePyContextEvent¶
Enumeration of possible context object watcher events:
Py_CONTEXT_SWITCHED: Thecurrent context has switched to adifferent context. The object passed to the watch callback is thenow-currentcontextvars.Contextobject, or None if no context iscurrent.
在 3.14 版被加入.
- typedefint(*PyContext_WatchCallback)(PyContextEventevent,PyObject*obj)¶
Context object watcher callback function. The object passed to the callbackis event-specific; see
PyContextEventfor details.If the callback returns with an exception set, it must return
-1; thisexception will be printed as an unraisable exception usingPyErr_FormatUnraisable(). Otherwise it should return0.There may already be a pending exception set on entry to the callback. Inthis case, the callback should return
0with the same exception stillset. This means the callback may not call any other API that can set anexception unless it saves and clears the exception state first, and restoresit before returning.在 3.14 版被加入.
情境變數函式:
- PyObject*PyContextVar_New(constchar*name,PyObject*def)¶
- 回傳值:新的參照。
建立一個新的
ContextVar物件。name 參數用於自我檢查(introspection)和除錯目的。def 參數指定情境變數的預設值,亦可用NULL表示沒有預設值。 如果發生錯誤,此函式會回傳NULL。
- intPyContextVar_Get(PyObject*var,PyObject*default_value,PyObject**value)¶
取得情境變數的值。如果在查找過程中發生錯誤則回傳
-1,如果沒有發生錯誤則無論是否找到值都會回傳0。如果找到情境變數,value 將會是指向它的指標。如果沒有 找到情境變數,value 將會指到:
default_value 預設值,但前提是預設值不是
NULL;var 的預設值,但前提是預設值不是
NULL;NULL
除了
NULL之外,此函式會回傳一個新的參照(reference)。
- PyObject*PyContextVar_Set(PyObject*var,PyObject*value)¶
- 回傳值:新的參照。
在目前的情境中將var 的值設為value。會回傳一個用來代表此變更的新 token 物件,如果發生錯誤則回傳
NULL。
- intPyContextVar_Reset(PyObject*var,PyObject*token)¶
將var 情境變數的狀態設回之前的狀態,亦即上一次回傳 token 的
PyContextVar_Set()被呼叫前的狀態。此函式在成功時回傳0,錯誤時回傳-1。