剖析引數與建置數值¶
These functions are useful when creating your own extension functions and methods. Additional information and examples are available in 擴充和嵌入 Python 直譯器.
The first three of these functions described, PyArg_ParseTuple(),
PyArg_ParseTupleAndKeywords(), and PyArg_Parse(), all use format
strings which are used to tell the function about the expected arguments. The
format strings use the same syntax for each of these functions.
剖析引數¶
A format string consists of zero or more "format units." A format unit describes one Python object; it is usually a single character or a parenthesized sequence of format units. With a few exceptions, a format unit that is not a parenthesized sequence normally corresponds to a single address argument to these functions. In the following description, the quoted form is the format unit; the entry in (round) parentheses is the Python object type that matches the format unit; and the entry in [square] brackets is the type of the C variable(s) whose address should be passed.
字串與緩衝區¶
備註
On Python 3.12 and older, the macro PY_SSIZE_T_CLEAN must be
defined before including Python.h to use all # variants of
formats (s#, y#, etc.) explained below.
This is not necessary on Python 3.13 and later.
These formats allow accessing an object as a contiguous chunk of memory. You don't have to provide raw storage for the returned unicode or bytes area.
Unless otherwise stated, buffers are not NUL-terminated.
There are three ways strings and buffers can be converted to C:
Formats such as
y*ands*fill aPy_bufferstructure. This locks the underlying buffer so that the caller can subsequently use the buffer even inside aPy_BEGIN_ALLOW_THREADSblock without the risk of mutable data being resized or destroyed. As a result, you have to callPyBuffer_Release()after you have finished processing the data (or in any early abort case).The
es,es#,etandet#formats allocate the result buffer. You have to callPyMem_Free()after you have finished processing the data (or in any early abort case).Other formats take a
stror a read-only bytes-like object, such asbytes, and provide aconst char *pointer to its buffer. In this case the buffer is "borrowed": it is managed by the corresponding Python object, and shares the lifetime of this object. You won't have to release any memory yourself.To ensure that the underlying buffer may be safely borrowed, the object's
PyBufferProcs.bf_releasebufferfield must beNULL. This disallows common mutable objects such asbytearray, but also some read-only objects such asmemoryviewofbytes.Besides this
bf_releasebufferrequirement, there is no check to verify whether the input object is immutable (e.g. whether it would honor a request for a writable buffer, or whether another thread can mutate the data).
s(str) [const char *]Convert a Unicode object to a C pointer to a character string. A pointer to an existing string is stored in the character pointer variable whose address you pass. The C string is NUL-terminated. The Python string must not contain embedded null code points; if it does, a
ValueErrorexception is raised. Unicode objects are converted to C strings using'utf-8'encoding. If this conversion fails, aUnicodeErroris raised.備註
This format does not accept bytes-like objects. If you want to accept filesystem paths and convert them to C character strings, it is preferable to use the
O&format withPyUnicode_FSConverter()as converter.在 3.5 版的變更: Previously,
TypeErrorwas raised when embedded null code points were encountered in the Python string.s*(str或 bytes-like object) [Py_buffer]This format accepts Unicode objects as well as bytes-like objects. It fills a
Py_bufferstructure provided by the caller. In this case the resulting C string may contain embedded NUL bytes. Unicode objects are converted to C strings using'utf-8'encoding.s#(str、唯讀的 bytes-like object) [const char *,Py_ssize_t]Like
s*, except that it provides a borrowed buffer. The result is stored into two C variables, the first one a pointer to a C string, the second one its length. The string may contain embedded null bytes. Unicode objects are converted to C strings using'utf-8'encoding.z(str或None) [const char *]Like
s, but the Python object may also beNone, in which case the C pointer is set toNULL.z*(str、bytes-like object 或None) [Py_buffer]Like
s*, but the Python object may also beNone, in which case thebufmember of thePy_bufferstructure is set toNULL.z#(str、唯讀的 bytes-like object 或None) [const char *,Py_ssize_t]Like
s#, but the Python object may also beNone, in which case the C pointer is set toNULL.y(唯讀的 bytes-like object) [const char *]This format converts a bytes-like object to a C pointer to a borrowed character string; it does not accept Unicode objects. The bytes buffer must not contain embedded null bytes; if it does, a
ValueErrorexception is raised.在 3.5 版的變更: Previously,
TypeErrorwas raised when embedded null bytes were encountered in the bytes buffer.y*(bytes-like object) [Py_buffer]This variant on
s*doesn't accept Unicode objects, only bytes-like objects. This is the recommended way to accept binary data.y#(唯讀的 bytes-like object) [const char *,Py_ssize_t]This variant on
s#doesn't accept Unicode objects, only bytes-like objects.S(bytes) [PyBytesObject *]Requires that the Python object is a
bytesobject, without attempting any conversion. RaisesTypeErrorif 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
bytearrayobject, without attempting any conversion. RaisesTypeErrorif the object is not abytearrayobject. The C variable may also be declared as PyObject*.U(str) [PyObject *]