Capsule 對象?
有關(guān)使用這些對象的更多信息請參閱 給擴展模塊提供C API。
3.1 新版功能.
-
type PyCapsule?
這個(gè)
PyObject的子類(lèi)型代表一個(gè)隱藏的值,適用于需要將隱藏值(作為 void* 指針)通過(guò) Python 代碼傳遞到其他 C 代碼的 C 擴展模塊。 它常常被用來(lái)讓在一個(gè)模塊中定義的 C 函數指針在其他模塊中可用,這樣就可以使用常規導入機制來(lái)訪(fǎng)問(wèn)在動(dòng)態(tài)加載的模塊中定義的 C API。
-
type PyCapsule_Destructor?
- Part of the Stable ABI.
Capsule 的析構器回調的類(lèi)型。 定義如下:
typedef void (*PyCapsule_Destructor)(PyObject *);
參閱
PyCapsule_New()來(lái)獲取 PyCapsule_Destructor 返回值的語(yǔ)義。
-
PyObject *PyCapsule_New(void *pointer, const char *name, PyCapsule_Destructor destructor)?
- Return value: New reference. Part of the Stable ABI.
創(chuàng )建一個(gè)封裝了 pointer 的
PyCapsule。 pointer 參考可以不為NULL。在失敗時(shí)設置一個(gè)異常并返回
NULL。字符串 name 可以是
NULL或是一個(gè)指向有效的 C 字符串的指針。 如果不為NULL,則此字符串必須比 capsule 長(cháng)(雖然也允許在 destructor 中釋放它。)如果 destructor 參數不為
NULL,則當它被銷(xiāo)毀時(shí)將附帶 capsule 作為參數來(lái)調用。如果此 capsule 將被保存為一個(gè)模塊的屬性,則 name 應當被指定為
modulename.attributename。 這將允許其他模塊使用PyCapsule_Import()來(lái)導入此 capsule。
-
void *PyCapsule_GetPointer(PyObject *capsule, const char *name)?
- Part of the Stable ABI.
提取保存在 capsule 中的 pointer。 在失敗時(shí)設置一個(gè)異常并返回
NULL。name 形參必須與保存在 capsule 中的名稱(chēng)進(jìn)行精確比較。 如果保存在 capsule 中的名稱(chēng)為
NULL,則傳入的 name 也必須為NULL。 Python 會(huì )使用 C 函數strcmp()來(lái)比較 capsule 名稱(chēng)。
-
PyCapsule_Destructor PyCapsule_GetDestructor(PyObject *capsule)?
- Part of the Stable ABI.
返回保存在 capsule 中的當前析構器。 在失敗時(shí)設置一個(gè)異常并返回
NULL。capsule 具有
NULL析構器是合法的。 這會(huì )使得NULL返回碼有些歧義;請使用PyCapsule_IsValid()或PyErr_Occurred()來(lái)消除歧義。
-
void *PyCapsule_GetContext(PyObject *capsule)?
- Part of the Stable ABI.
返回保存在 capsule 中的當前上下文。 在失敗時(shí)設置一個(gè)異常并返回
NULL。capsule 具有
NULL上下文是全法的。 這會(huì )使得NULL返回碼有些歧義;請使用PyCapsule_IsValid()或PyErr_Occurred()來(lái)消除歧義。
-
const char *PyCapsule_GetName(PyObject *capsule)?
- Part of the Stable ABI.
返回保存在 capsule 中的當前名稱(chēng)。 在失敗時(shí)設置一個(gè)異常并返回
NULL。capsule 具有
NULL名稱(chēng)是合法的。 這會(huì )使得NULL返回碼有些歧義;請使用PyCapsule_IsValid()或PyErr_Occurred()來(lái)消除歧義。
-
void *PyCapsule_Import(const char *name, int no_block)?
- Part of the Stable ABI.
Import a pointer to a C object from a capsule attribute in a module. The name parameter should specify the full name to the attribute, as in
module.attribute. The name stored in the capsule must match this string exactly.成功時(shí)返回 capsule 的內部 指針。 在失敗時(shí)設置一個(gè)異常并返回
NULL。在 3.3 版更改: no_block has no effect anymore.
-
int PyCapsule_IsValid(PyObject *capsule, const char *name)?
- Part of the Stable ABI.
確定 capsule 是否是一個(gè)有效的。 有效的 capsule 必須不為
NULL,傳遞PyCapsule_CheckExact(),在其中存儲一個(gè)不為NULL的指針,并且其內部名稱(chēng)與 name 形參相匹配。 (請參閱PyCapsule_GetPointer()了解如何對 capsule 名稱(chēng)進(jìn)行比較的有關(guān)信息。)換句話(huà)說(shuō),如果
PyCapsule_IsValid()返回真值,則任何對訪(fǎng)問(wèn)器(以PyCapsule_Get()開(kāi)頭的任何函數)的調用都保證會(huì )成功。如果對象有效并且匹配傳入的名稱(chēng)則返回非零值。 否則返回
0。 此函數一定不會(huì )失敗。
-
int PyCapsule_SetContext(PyObject *capsule, void *context)?
- Part of the Stable ABI.
將 capsule 內部的上下文指針設為 context。
成功時(shí)返回
0。 失敗時(shí)返回非零值并設置一個(gè)異常。
-
int PyCapsule_SetDestructor(PyObject *capsule, PyCapsule_Destructor destructor)?
- Part of the Stable ABI.
將 capsule 內部的析構器設為 destructor。
成功時(shí)返回
0。 失敗時(shí)返回非零值并設置一個(gè)異常。
-
int PyCapsule_SetName(PyObject *capsule, const char *name)?
- Part of the Stable ABI.
將 capsule 內部的名稱(chēng)設為 name。 如果不為
NULL,則名稱(chēng)的存在期必須比 capsule 更長(cháng)。 如果之前保存在 capsule 中的 name 不為NULL,則不會(huì )嘗試釋放它。成功時(shí)返回
0。 失敗時(shí)返回非零值并設置一個(gè)異常。
-
int PyCapsule_SetPointer(PyObject *capsule, void *pointer)?
- Part of the Stable ABI.
將 capsule 內部的空指針設為 pointer。 指針不可為
NULL。成功時(shí)返回
0。 失敗時(shí)返回非零值并設置一個(gè)異常。