bytes 对象¶
当期望带一个字节串形参但却带一个非字节串形参被调用时,这些函数会引发 TypeError
。
-
PyTypeObject
PyBytes_Type
¶ PyTypeObject
的实例代表一个 Python 字节类型,在 Python 层面它与bytes
是相同的对象。
-
PyObject*
PyBytes_FromString
(const char *v)¶ Return a new bytes object with a copy of the string v as value on success, and NULL on failure. The parameter v must not be NULL; it will not be checked.
-
PyObject*
PyBytes_FromStringAndSize
(const char *v, Py_ssize_t len)¶ Return a new bytes object with a copy of the string v as value and length len on success, and NULL on failure. If v is NULL, the contents of the bytes object are uninitialized.
-
PyObject*
PyBytes_FromFormat
(const char *format, ...)¶ 接受一个 C
printf()
风格的 format 字符串和可变数量的参数,计算结果 Python 字节串对象的大小并返回参数值经格式化后的字节串对象。 可变数量的参数必须均为 C 类型并且必须恰好与 format 字符串中的格式字符相对应。 允许使用下列格式字符串:格式字符
类型
注释
%%
不适用
文字%字符。
%c
整型
一个字节,被表示为一个 C 语言的整型
%d
整型
Exactly equivalent to
printf("%d")
.%u
无符号整型
Exactly equivalent to
printf("%u")
.%ld
长整型
Exactly equivalent to
printf("%ld")
.%lu
无符号长整型
Exactly equivalent to
printf("%lu")
.%zd
Py_ssize_t
Exactly equivalent to
printf("%zd")
.%zu
size_t
Exactly equivalent to
printf("%zu")
.%i
整型
Exactly equivalent to
printf("%i")
.%x
整型
Exactly equivalent to
printf("%x")
.%s
char*
以 null 为终止符的 C 字符数组。
%p
void*
一个 C 指针的十六进制表示形式。 基本等价于
printf("%p")
但它会确保以字面值0x
开头,不论系统平台上printf
的输出是什么。无法识别的格式字符会导致将格式字符串的其余所有内容原样复制到结果对象,并丢弃所有多余的参数。
-
PyObject*
PyBytes_FromFormatV
(const char *format, va_list vargs)¶ 与
PyBytes_FromFormat()
完全相同,除了它需要两个参数。
-
Py_ssize_t
PyBytes_GET_SIZE
(PyObject *o)¶ 宏版本的
PyBytes_Size()
但是不带错误检查。
-
char*
PyBytes_AsString
(PyObject *o)¶ Return a pointer to the contents of o. The pointer refers to the internal buffer of o, which consists of
len(o) + 1
bytes. The last byte in the buffer is always null, regardless of whether there are any other null bytes. The data must not be modified in any way, unless the object was just created usingPyBytes_FromStringAndSize(NULL, size)
. It must not be deallocated. If o is not a bytes object at all,PyBytes_AsString()
returns NULL and raisesTypeError
.
-
char*
PyBytes_AS_STRING
(PyObject *string)¶ 宏版本的
PyBytes_AsString()
但是不带错误检测。
-
int
PyBytes_AsStringAndSize
(PyObject *obj, char **buffer, Py_ssize_t *length)¶ 通过输出变量 buffer 和 length 返回以 null 为终止符的对象 obj 的内容。
If length is NULL, the bytes object may not contain embedded null bytes; if it does, the function returns
-1
and aValueError
is raised.该缓冲区指向 obj 的内部缓冲,它的末尾包含一个额外的空字节(不算在 length 当中)。 该数据不可通过任何方式来修改,除非是刚使用
PyBytes_FromStringAndSize(NULL, size)
创建该对象。 它不可被撤销分配。 如果 obj 根本不是一个字节串对象,则PyBytes_AsStringAndSize()
将返回-1
并引发TypeError
。在 3.5 版更改: 以前,当字节串对象中出现嵌入的空字节时将引发
TypeError
。
-
void
PyBytes_Concat
(PyObject **bytes, PyObject *newpart)¶ Create a new bytes object in *bytes containing the contents of newpart appended to bytes; the caller will own the new reference. The reference to the old value of bytes will be stolen. If the new object cannot be created, the old reference to bytes will still be discarded and the value of *bytes will be set to NULL; the appropriate exception will be set.
-
void
PyBytes_ConcatAndDel
(PyObject **bytes, PyObject *newpart)¶ 在 *bytes 中创建新的字节串对象,其中包含添加到 bytes 的 newpart 的内容。 此版本会减少 newpart 的引用计数。
-
int
_PyBytes_Resize
(PyObject **bytes, Py_ssize_t newsize)¶ A way to resize a bytes object even though it is “immutable”. Only use this to build up a brand new bytes object; don’t use this if the bytes may already be known in other parts of the code. It is an error to call this function if the refcount on the input bytes object is not one. Pass the address of an existing bytes object as an lvalue (it may be written into), and the new size desired. On success, *bytes holds the resized bytes object and
0
is returned; the address in *bytes may differ from its input value. If the reallocation fails, the original bytes object at *bytes is deallocated, *bytes is set to NULL,MemoryError
is set, and-1
is returned.