新聞中心
解析參數(shù)并構(gòu)建值變量
在創(chuàng)建你自己的擴展函數(shù)和方法時,這些函數(shù)是有用的。其它的信息和樣例見 擴展和嵌入 python 解釋器 。

創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供江陽網(wǎng)站建設(shè)、江陽做網(wǎng)站、江陽網(wǎng)站設(shè)計、江陽網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、江陽企業(yè)網(wǎng)站模板建站服務(wù),10年江陽做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。
這些函數(shù)描述的前三個,PyArg_ParseTuple(),PyArg_ParseTupleAndKeywords(),以及 PyArg_Parse(),它們都使用 格式化字符串 來將函數(shù)期待的參數(shù)告知函數(shù)。這些函數(shù)都使用相同語法規(guī)則的格式化字符串。
解析參數(shù)
一個格式化字符串包含 0 或者更多的格式單元。一個格式單元用來描述一個 Python 對象;它通常是一個字符或者由括號括起來的格式單元序列。除了少數(shù)例外,一個非括號序列的格式單元通常對應(yīng)這些函數(shù)的具有單一地址的參數(shù)。在接下來的描述中,雙引號內(nèi)的表達(dá)式是格式單元;圓括號 () 內(nèi)的是對應(yīng)這個格式單元的 Python 對象類型;方括號 [] 內(nèi)的是傳遞的 C 變量(變量集)類型。
字符串和緩存區(qū)
這些格式允許將對象按照連續(xù)的內(nèi)存塊形式進行訪問。你沒必要提供返回的 unicode 字符或者字節(jié)區(qū)的原始數(shù)據(jù)存儲。
一般的,當(dāng)一個表達(dá)式設(shè)置一個指針指向一個緩沖區(qū),這個緩沖區(qū)可以被相應(yīng)的 Python 對象管理,并且這個緩沖區(qū)共享這個對象的生存周期。你不需要人為的釋放任何內(nèi)存空間。除了這些 es, es#, et and et#.
然而,當(dāng)一個 Py_buffer 結(jié)構(gòu)被賦值,其包含的緩沖區(qū)被鎖住,所以調(diào)用者在隨后使用這個緩沖區(qū),即使在 Py_BEGIN_ALLOW_THREADS 塊中,可以避免可變數(shù)據(jù)因為調(diào)整大小或者被銷毀所帶來的風(fēng)險。因此,你不得不調(diào)用 PyBuffer_Release() 在你結(jié)束數(shù)據(jù)的處理時(或者在之前任何中斷事件中)
除非另有說明,緩沖區(qū)是不會以空終止的。
某些格式需要只讀的 bytes-like object,并設(shè)置指針而不是緩沖區(qū)結(jié)構(gòu)。 他們通過檢查對象的 PyBufferProcs.bf_releasebuffer 字段是否為 NULL 來發(fā)揮作用,該字段不允許為 bytearray 這樣的可變對象。
備注
對于所有 # 格式的變體(s#、y# 等),宏 PY_SSIZE_T_CLEAN 必須在包含 Python 之前定義。 h。在 Python 3.9 及更早版本上,如果定義了 PY_SSIZE_T_CLEAN 宏,則長度參數(shù)的類型為 Py_ssize_t,否則為 int。
s (str) [const char *]
將一個 Unicode 對象轉(zhuǎn)換成一個指向字符串的 C 指針。一個指針指向一個已經(jīng)存在的字符串,這個字符串存儲的是傳如的字符指針變量。C 字符串是已空結(jié)束的。Python 字符串不能包含嵌入的無效的代碼點;如果由,一個 ValueError 異常會被引發(fā)。Unicode 對象被轉(zhuǎn)化成 'utf-8' 編碼的 C 字符串。如果轉(zhuǎn)換失敗,一個 UnicodeError 異常被引發(fā)。
備注
這個表達(dá)式不接受 bytes-like objects。如果你想接受文件系統(tǒng)路徑并將它們轉(zhuǎn)化成 C 字符串,建議使用 O& 表達(dá)式配合 PyUnicode_FSConverter() 作為 轉(zhuǎn)化函數(shù)。
在 3.5 版更改: 以前,當(dāng) Python 字符串中遇到了嵌入的 null 代碼點會引發(fā) TypeError 。
s* (str or bytes-like object) [Py_buffer]
這個表達(dá)式既接受 Unicode 對象也接受類字節(jié)類型對象。它為由調(diào)用者提供的 Py_buffer 結(jié)構(gòu)賦值。這里結(jié)果的 C 字符串可能包含嵌入的 NUL 字節(jié)。Unicode 對象通過 'utf-8' 編碼轉(zhuǎn)化成 C 字符串。
s# (str, read-only bytes-like object) [const char *, Py_ssize_t]
像 s*,除了它不接受易變的對象。結(jié)果存儲在兩個 C 變量中,第一個是指向 C 字符串的指針,第二個是它的長度。字符串可能包含嵌入的 null 字節(jié)。Unicode 對象都被通過 'utf-8' 編碼轉(zhuǎn)化成 C 字符串。
z (str or None) [const char *]
與 s 類似,但 Python 對象也可能為 None,在這種情況下,C 指針設(shè)置為 NULL。
z* (str, bytes-like object or None) [Py_buffer]
與 s* 類似,但 Python 對象也可能為 None,在這種情況下,Py_buffer 結(jié)構(gòu)的 buf 成員設(shè)置為 NULL。
z# (str, read-only bytes-like object 或者 None) [const char *, Py_ssize_t]
與 s# 類似,但 Python 對象也可能為 None,在這種情況下,C 指針設(shè)置為 NULL。
y (read-only bytes-like object) [const char *]
這個表達(dá)式將一個類字節(jié)類型對象轉(zhuǎn)化成一個指向字符串的 C 指針;它不接受 Unicode 對象。字節(jié)緩存區(qū)必須不包含嵌入的 null 字節(jié);如果包含了 null 字節(jié),會引發(fā)一個 ValueError 異常。
在 3.5 版更改: 以前,當(dāng)字節(jié)緩沖區(qū)中遇到了嵌入的 null 字節(jié)會引發(fā) TypeError 。
y* (bytes-like object) [Py_buffer]
s* 的變式,不接受 Unicode 對象,只接受類字節(jié)類型變量。這是接受二進制數(shù)據(jù)的推薦方法。
y# (read-only bytes-like object) [const char *, Py_ssize_t]
s# 的變式,不接受 Unicode 對象,只接受類字節(jié)類型變量。
S (bytes) [PyBytesObject *]
Requires that the Python object is a bytes object, without attempting any conversion. Raises TypeError if the object is not a bytes object. The C variable may also be declared as PyObject*.
Y (bytearray) [PyByteArrayObject *]
Requires that the Python object is a bytearray object, without attempting any conversion. Raises TypeError if the object is not a bytearray object. The C variable may also be declared as PyObject*.
u (str) [const Py_UNICODE *]
將一個 Python Unicode 對象轉(zhuǎn)化成指向一個以空終止的 Unicode 字符緩沖區(qū)的指針。你必須傳入一個 Py_UNICODE 指針變量的地址,存儲了一個指向已經(jīng)存在的 Unicode 緩沖區(qū)的指針。請注意一個 Py_UNICODE 類型的字符寬度取決于編譯選項(16 位或者 32 位)。Python 字符串必須不能包含嵌入的 null 代碼點;如果有,引發(fā)一個 ValueError 異常。
在 3.5 版更改: 以前,當(dāng) Python 字符串中遇到了嵌入的 null 代碼點會引發(fā) TypeError 。
從版本 3.3 開始標(biāo)記為過時,將在版本 3.12 中移除。: 這是舊版樣式 Py_UNICODE API; 請遷移至 PyUnicode_AsWideCharString().
u# (str) [const Py_UNICODE *, Py_ssize_t]
u 的變式,存儲兩個 C 變量,第一個指針指向一個 Unicode 數(shù)據(jù)緩存區(qū),第二個是它的長度。它允許 null 代碼點。
從版本 3.3 開始標(biāo)記為過時,將在版本 3.12 中移除。: 這是舊版樣式 Py_UNICODE API; 請遷移至 PyUnicode_AsWideCharString().
Z (str 或 None) [const Py_UNICODE *]
與 u 類似,但 Python 對象也可能為 None,在這種情況下 Py_UNICODE 指針設(shè)置為 NULL。
從版本 3.3 開始標(biāo)記為過時,將在版本 3.12 中移除。: 這是舊版樣式 Py_UNICODE API; 請遷移至 PyUnicode_AsWideCharString().
Z# (str 或 None) [const Py_UNICODE *, Py_ssize_t]
與 u# 類似,但 Python 對象也可能為 None,在這種情況下 Py_UNICODE 指針設(shè)置為 NULL。
從版本 3.3 開始標(biāo)記為過時,將在版本 3.12 中移除。: 這是舊版樣式 Py_UNICODE API; 請遷移至 PyUnicode_AsWideCharString().
U (str) [PyObject *]
Requires that the Python object is a Unicode object, without attempting any conversion. Raises TypeError if the object is not a Unicode object. The C variable may also be declared as PyObject*.
w* (可讀寫 bytes-like object) [Py_buffer]
這個表達(dá)式接受任何實現(xiàn)可讀寫緩存區(qū)接口的對象。它為調(diào)用者提供的 Py_buffer 結(jié)構(gòu)賦值。緩沖區(qū)可能存在嵌入的 null 字節(jié)。當(dāng)緩沖區(qū)使用完后調(diào)用者需要調(diào)用 PyBuffer_Release()。
es (str) [const char *encoding, char **buffer]
s 的變式,它將編碼后的 Unicode 字符存入字符緩沖區(qū)。它只處理沒有嵌 NUL 字節(jié)的已編碼數(shù)據(jù)。
This format requires two arguments. The first is only used as input, and must be a const char* which points to the name of an encoding as a NUL-terminated string, or NULL, in which case 'utf-8' encoding is used. An exception is raised if the named encoding is not known to Python. The second argument must be a char**; the value of the pointer it references will be set to a buffer with the contents of the argument text. The text will be encoded in the encoding specified by the first argument.
PyArg_ParseTuple() 會分配一個足夠大小的緩沖區(qū),將編碼后的數(shù)據(jù)拷貝進這個緩沖區(qū)并且設(shè)置 \buffer* 引用這個新分配的內(nèi)存空間。調(diào)用者有責(zé)任在使用后調(diào)用 PyMem_Free() 去釋放已經(jīng)分配的緩沖區(qū)。
et (str, bytes or bytearray) [const char *encoding, char **buffer]
和 es 相同,除了不用重編碼傳入的字符串對象。相反,它假設(shè)傳入的參數(shù)是編碼后的字符串類型。
es# (str) [const char *encoding, char **buffer, Py_ssize_t *buffer_length]
s# 的變式,它將已編碼的 Unicode 字符存入字符緩沖區(qū)。不像 es 表達(dá)式,它允許傳入的數(shù)據(jù)包含 NUL 字符。
It requires three arguments. The first is only used as input, and must be a const char* which points to the name of an encoding as a NUL-terminated string, or NULL, in which case 'utf-8' encoding is used. An exception is raised if the named encoding is not known to Python. The second argument must be a char**; the value of the pointer it references will be set to a buffer with the contents of the argument text. The text will be encoded in the encoding specified by the first argument. The third argument must be a pointer to an integer; the referenced integer will be set to the number of bytes in the output buffer.
有兩種操作方式:
如果 \buffer 指向 NULL 指針,則函數(shù)將分配所需大小的緩沖區(qū),將編碼的數(shù)據(jù)復(fù)制到此緩沖區(qū),并設(shè)置 *buffer* 以引用新分配的存儲。 呼叫者負(fù)責(zé)調(diào)用 PyMem_Free() 以在使用后釋放分配的緩沖區(qū)。
如果 \buffer 指向非 NULL 指針(已分配的緩沖區(qū)),則 PyArg_ParseTuple() 將使用此位置作為緩沖區(qū),并將 *buffer_length* 的初始值解釋為緩沖區(qū)大小。 然后,它將將編碼的數(shù)據(jù)復(fù)制到緩沖區(qū),并終止它。 如果緩沖區(qū)不夠大,將設(shè)置一個 ValueError。
在這兩個例子中,\buffer_length* 被設(shè)置為編碼后結(jié)尾不為 NUL 的數(shù)據(jù)的長度。
et# (str, bytes 或 bytearray) [const char *encoding, char **buffer, Py_ssize_t *buffer_length]
和 es# 相同,除了不用重編碼傳入的字符串對象。相反,它假設(shè)傳入的參數(shù)是編碼后的字符串類型。
數(shù)字
b (int) [unsigned char]
Convert a nonnegative Python integer to an unsigned tiny int, stored in a C unsigned char.
B (int) [unsigned char]
Convert a Python integer to a tiny int without overflow checking, stored in a C unsigned char.
h (int) [short int]
Convert a Python integer to a C short int.
H (int) [unsigned short int]
Convert a Python integer to a C unsigned short int, without overflow checking.
i (int) [int]
Convert a Python integer to a plain C int.
I (int) [unsigned int]
Convert a Python integer to a C unsigned int, without overflow checking.
l (int) [long int]
Convert a Python integer to a C long int.
k (int) [unsigned long]
Convert a Python integer to a C unsigned long without overflow checking.
L (int) [long long]
Convert a Python integer to a C long long.
K (int) [unsigned long long]
Convert a Python integer to a C unsigned long long without overflow checking.
n (int) [Py_ssize_t]
將一個 Python 整型轉(zhuǎn)化成一個 C Py_ssize_t Python 元大小類型。
c (bytes 或者 bytearray 長度為 1) [char]
Convert a Python byte, represented as a bytes or bytearray object of length 1, to a C char.
在 3.3 版更改: 允許 bytearray 類型的對象。
C (str 長度為 1) [int]
Convert a Python character, represented as a str object of length 1, to a C int.
f (float) [float]
Convert a Python floating point number to a C float.
d (float) [double]
Convert a Python floating point number to a C double.
D (complex) [Py_complex]
將一個 Python 復(fù)數(shù)類型轉(zhuǎn)化成一個 C Py_complex Python 復(fù)數(shù)類型。
其他對象
O (object) [PyObject *]
將 Python 對象(不進行任何轉(zhuǎn)換)存儲在 C 對象指針中。 因此,C 程序接收已傳遞的實際對象。 對象的引用計數(shù)不會增加。 存儲的指針不是 NULL。
O! (object) [typeobject, PyObject *]
Store a Python object in a C object pointer. This is similar to O, but takes two C arguments: the first is the address of a Python type object, the second is the address of the C variable (of type PyObject*) into which the object pointer is stored. If the Python object does not have the required type, TypeError is raised.
O& (object) [converter, anything]
Convert a Python object to a C variable through a converter function. This takes two arguments: the first is a function, the second is the address of a C variable (of arbitrary type), converted to void*. The converter function in turn is called as follows:
status = converter(object, address);
where object is the Python object to be converted and address is the void* argument that was passed to the PyArg_Parse* function. The returned status should be 1 for a successful conversion and 0 if the conversion has failed. When the conversion fails, the converter function should raise an exception and leave the content of address unmodified.
如果 converter 返回 Py_CLEANUP_SUPPORTED,則如果參數(shù)解析最終失敗,它可能會再次調(diào)用該函數(shù),從而使轉(zhuǎn)換器有機會釋放已分配的任何內(nèi)存。在第二個調(diào)用中,object 參數(shù)將為 NULL;因此,該參數(shù)將為 NULL;因此,該參數(shù)將為 NULL,因此,該參數(shù)將為 NULL``(如果值)為 ``NULL address 的值與原始呼叫中的值相同。
在 3.1 版更改: Py_CLEANUP_SUPPORTED 被添加。
p (bool) [int]
測試傳入的值是否為真(一個布爾判斷)并且將結(jié)果轉(zhuǎn)化為相對應(yīng)的 C true/false 整型值。如果表達(dá)式為真置 1,假則置 0。它接受任何合法的 Python 值。參見 邏輯值檢測 獲取更多關(guān)于 Python 如何測試值為真的信息。
3.3 新版功能.
(items) (tuple) [matching-items]
對象必須是 Python 序列,它的長度是 items 中格式單元的數(shù)量。C 參數(shù)必須對應(yīng) items 中每一個獨立的格式單元。序列中的格式單元可能有嵌套。
傳遞 “l(fā)ong” 整型(整型的值超過了平臺的 LONG_MAX 限制)是可能的,然而沒有進行適當(dāng)?shù)姆秶鷻z測——當(dāng)接收字段太小而接收不到值時,最重要的位被靜默地截斷(實際上,C 語言會在語義繼承的基礎(chǔ)上強制類型轉(zhuǎn)換——期望的值可能會發(fā)生變化)。
格式化字符串中還有一些其他的字符具有特殊的涵義。這些可能并不嵌套在圓括號中。它們是:
|
表明在 Python 參數(shù)列表中剩下的參數(shù)都是可選的。C 變量對應(yīng)的可選參數(shù)需要初始化為默認(rèn)值——當(dāng)一個可選參數(shù)沒有指定時, PyArg_ParseTuple() 不能訪問相應(yīng)的 C 變量(變量集)的內(nèi)容。
$
PyArg_ParseTupleAndKeywords() only:表明在 Python 參數(shù)列表中剩下的參數(shù)都是強制關(guān)鍵字參數(shù)。當(dāng)前,所有強制關(guān)鍵字參數(shù)都必須也是可選參數(shù),所以格式化字符串中 | 必須一直在 $ 前面。
3.3 新版功能.
:
格式單元的列表結(jié)束標(biāo)志;冒號后的字符串被用來作為錯誤消息中的函數(shù)名(PyArg_ParseTuple() 函數(shù)引發(fā)的“關(guān)聯(lián)值”異常)。
;
格式單元的列表結(jié)束標(biāo)志;分號后的字符串被用來作為錯誤消息取代默認(rèn)的錯誤消息。 : 和 ; 相互排斥。
注意任何由調(diào)用者提供的 Python 對象引用是 借來的 引用;不要遞減它們的引用計數(shù)!
傳遞給這些函數(shù)的附加參數(shù)必須是由格式化字符串確定的變量的地址;這些都是用來存儲輸入元組的值。有一些情況,如上面的格式單元列表中所描述的,這些參數(shù)作為輸入值使用;在這種情況下,它們應(yīng)該匹配指定的相應(yīng)的格式單元。
For the conversion to succeed, the arg object must match the format and the format must be exhausted. On success, the PyArg_Parse* functions return true, otherwise they return false and raise an appropriate exception. When the PyArg_Parse* functions fail due to conversion failure in one of the format units, the variables at the addresses corresponding to that and the following format units are left untouched.
API 函數(shù)
int PyArg_ParseTuple(PyObject *args, const char *format, …)
Part of the Stable ABI.
解析一個函數(shù)的參數(shù),表達(dá)式中的參數(shù)按參數(shù)位置順序存入局部變量中。成功返回 true;失敗返回 false 并且引發(fā)相應(yīng)的異常。
int PyArg_VaParse(PyObject *args, const char *format, va_list vargs)
Part of the Stable ABI.
和 PyArg_ParseTuple() 相同,然而它接受一個 va_list 類型的參數(shù)而不是可變數(shù)量的參數(shù)集。
int PyArg_ParseTupleAndKeywords(PyObject *args, PyObject *kw, const char *format, char *keywords[], …)
Part of the Stable ABI.
分析將位置參數(shù)和關(guān)鍵字參數(shù)同時轉(zhuǎn)換為局部變量的函數(shù)的參數(shù)。 keywords 參數(shù)是關(guān)鍵字參數(shù)名稱的 NULL 終止數(shù)組。 空名稱表示 positional-only parameters。成功時返回 true;發(fā)生故障時,它將返回 false 并引發(fā)相應(yīng)的異常。
在 3.6 版更改: 添加了 positional-only parameters 的支持。
int PyArg_VaParseTupleAndKeywords(PyObject *args, PyObject *kw, const char *format, char *keywords[], va_list vargs)
Part of the Stable ABI.
和 PyArg_ParseTupleAndKeywords() 相同,然而它接受一個va_list類型的參數(shù)而不是可變數(shù)量的參數(shù)集。
int PyArg_ValidateKeywordArguments(PyObject*)
Part of the Stable ABI.
確保字典中的關(guān)鍵字參數(shù)都是字符串。這個函數(shù)只被使用于 PyArg_ParseTupleAndKeywords() 不被使用的情況下,后者已經(jīng)不再做這樣的檢查。
3.2 新版功能.
int PyArg_Parse(PyObject *args, const char *format, …)
Part of the Stable ABI.
函數(shù)被用來析構(gòu)“舊類型”函數(shù)的參數(shù)列表——這些函數(shù)使用的 METH_OLDARGS 參數(shù)解析方法已從 Python 3 中移除。這不被推薦用于新代碼的參數(shù)解析,并且在標(biāo)準(zhǔn)解釋器中的大多數(shù)代碼已被修改,已不再用于該目的。它仍然方便于分解其他元組,然而可能因為這個目的被繼續(xù)使用。
int PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, …)
Part of the Stable ABI.
A simpler form of parameter retrieval which does not use a format string to specify the types of the arguments. Functions which use this method to retrieve their parameters should be declared as METH_VARARGS in function or method tables. The tuple containing the actual parameters should be passed as args; it must actually be a tuple. The length of the tuple must be at least min and no more than max; min and max may be equal. Additional arguments must be passed to the function, each of which should be a pointer to a PyObject* variable; these will be filled in with the values from args; they will contain borrowed references. The variables which correspond to optional parameters not given by args will not be filled in; these should be initialized by the caller. This function returns true on success and false if args is not a tuple or contains the wrong number of elements; an exception will be set if there was a failure.
這是一個使用此函數(shù)的示例,取自 _weakref 幫助模塊用來弱化引用的源代碼:
static PyObject *weakref_ref(PyObject *self, PyObject *args){PyObject *object;PyObject *callback = NULL;PyObject *result = NULL;if (PyArg_UnpackTuple(args, "ref", 1, 2, &object, &callback)) {result = PyWeakref_NewRef(object, callback);}return result;}
這個例子中調(diào)用 PyArg_UnpackTuple() 完全等價于調(diào)用 PyArg_ParseTuple():
PyArg_ParseTuple(args, "O|O:ref", &object, &callback)
創(chuàng)建變量
PyObject *Py_BuildValue(const char *format, …)
Return value: New reference. Part of the Stable ABI.
Create a new value based on a format string similar to those accepted by the PyArg_Parse* family of functions and a sequence of values. Returns the value or NULL in the case of an error; an exception will be raised if NULL is returned.
Py_BuildValue() 并不一直創(chuàng)建一個元組。只有當(dāng)它的格式化字符串包含兩個或更多的格式單元才會創(chuàng)建一個元組。如果格式化字符串是空,它返回 None;如果它包含一個格式單元,它返回由格式單元描述的的任一對象。用圓括號包裹格式化字符串可以強制它返回一個大小為 0 或者 1 的元組。
當(dāng)內(nèi)存緩存區(qū)的數(shù)據(jù)以參數(shù)形式傳遞用來構(gòu)建對象時,如 s 和 s# 格式單元,會拷貝需要的數(shù)據(jù)。調(diào)用者提供的緩沖區(qū)從來都不會被由 Py_BuildValue() 創(chuàng)建的對象來引用。換句話說,如果你的代碼調(diào)用 malloc() 并且將分配的內(nèi)存空間傳遞給 Py_BuildValue(),你的代碼就有責(zé)任在 Py_BuildValue() 返回時調(diào)用 free() 。
在下面的描述中,雙引號的表達(dá)式使格式單元;圓括號 () 內(nèi)的是格式單元將要返回的 Python 對象類型;方括號 [] 內(nèi)的是傳遞的 C 變量(變量集)的類型。
字符例如空格,制表符,冒號和逗號在格式化字符串中會被忽略(但是不包括格式單元,如 s#)。這可以使很長的格式化字符串具有更好的可讀性。
-
s(str 或None) [const char *]使用
'utf-8'編碼將空終止的 C 字符串轉(zhuǎn)換為 Python str 對象。如果 C 字符串指針為NULL,則使用None。s#(str 或None) [const char *, Py_ssize_t]使用
'utf-8'編碼將 C 字符串及其長度轉(zhuǎn)換為 Python str 對象。如果 C 字符串指針為NULL,則長度將被忽略,并返回None。y(bytes) [const char *]這將 C 字符串轉(zhuǎn)換為 Python bytes 對象。 如果 C 字符串指針為
NULL,則返回None。y#(bytes) [const char *, Py_ssize_t]這會將 C 字符串及其長度轉(zhuǎn)換為一個 Python 對象。 如果該 C 字符串指針為
NULL,則返回None。z(str orNone) [const char *]和
s一樣。z#(str 或None) [const char *, Py_ssize_t]和
s#一樣。u(str) [const wchar_t *]Convert a null-terminated wchar_t buffer of Unicode (UTF-16 or UCS-4) data to a Python Unicode object. If the Unicode buffer pointer is
NULL,Noneis returned.u#(str) [const wchar_t *, Py_ssize_t]將 Unicode (UTF-16 或 UCS-4) 數(shù)據(jù)緩沖區(qū)及其長度轉(zhuǎn)換為 Python Unicode 對象。 如果 Unicode 緩沖區(qū)指針為
NULL,則長度將被忽略,并返回None。U(str 或None) [const char *]和
s一樣。U#(str 或None) [const char *, Py_ssize_t]和
s#一樣。i(int) [int]Convert a plain C int to a Python integer object.
b(int) [char]Convert a plain C char to a Python integer object.
h(int) [short int]Convert a plain C short int to a Python integer object.
l(int) [long int]Convert a C long int to a Python integer object.
B(int) [unsigned char]Convert a C unsigned char to a Python integer object.
H(int) [unsigned short int]Convert a C unsigned short int to a Python integer object.
I(int) [unsigned int]Convert a C unsigned int to a Python integer object.
k(int) [unsigned long]Convert a C unsigned long to a Python integer object.
L(int) [long long]Convert a C long long to a Python integer object.
K(int) [unsigned long long]Convert a C unsigned long long to a Python integer object.
n(int) [Py_ssize_t]將一個 C Py_ssize_t 類型轉(zhuǎn)化為 Python 整型。
c(bytes 長度為1 ) [char]Convert a C int representing a byte to a Python bytes object of length 1.
C(str 長度為 1) [int]Convert a C int representing a character to Python str object of length 1.
d(float) [double]Convert a C double to a Python floating point number.
f(float) [float]Convert a C float to a Python floating point number.
D(complex) [Py_complex *]將一個 C Py_complex 類型的結(jié)構(gòu)轉(zhuǎn)化為 Python 復(fù)數(shù)類型。
O(object) [PyObject *]將 Python 對象傳遞不變(其引用計數(shù)除外,該計數(shù)由 1 遞增)。 如果傳入的對象是
NULL指針,則假定這是由于生成參數(shù)的調(diào)用發(fā)現(xiàn)錯誤并設(shè)置異常而引起的。因此,Py_BuildValue() 將返回NULL,但不會引發(fā)異常。 如果尚未引發(fā)異常,則設(shè)置 SystemError。S(object) [PyObject *]和
O相同。N(object) [PyObject *]和
O相同,然而它并不增加對象的引用計數(shù)。當(dāng)通過調(diào)用參數(shù)列表中的對象構(gòu)造器創(chuàng)建對象時很實用。O&(object) [converter, anything]Convert anything to a Python object through a converter function. The function is called with anything (which should be compatible with void*) as its argument and should return a “new” Python object, or
NULLif an error occurred.(items)(tuple) [matching-items]將一個 C 變量序列轉(zhuǎn)換成 Python 元組并保持相同的元素數(shù)量。
[items](list) [相關(guān)的元素]將一個 C 變量序列轉(zhuǎn)換成 Python 列表并保持相同的元素數(shù)量。
{items}(dict) [相關(guān)的元素]將一個C變量序列轉(zhuǎn)換成 Python 字典。每一對連續(xù)的 C 變量對作為一個元素插入字典中,分別作為關(guān)鍵字和值。
如果格式字符串中出現(xiàn)錯誤,則設(shè)置 SystemError 異常并返回 NULL。
PyObject *Py_VaBuildValue(const char *format, va_list vargs)
Return value: New reference. Part of the Stable ABI.
和 Py_BuildValue() 相同,然而它接受一個 va_list 類型的參數(shù)而不是可變數(shù)量的參數(shù)集。
網(wǎng)頁題目:創(chuàng)新互聯(lián)Python教程:解析參數(shù)并構(gòu)建值變量
網(wǎng)站網(wǎng)址:http://m.fisionsoft.com.cn/article/cohoioc.html


咨詢
建站咨詢
