bytes 对象

这些函数在期望附带一个字节串形参但却附带了一个非字节串形参被调用时会引发 TypeError

type PyBytesObject

这种 PyObject 的子类型表示一个 Python 字节对象。

PyTypeObject PyBytes_Type
属于 稳定 ABI.

PyTypeObject 的实例代表一个 Python 字节类型,在 Python 层面它与 bytes 是相同的对象。

int PyBytes_Check(PyObject *o)

如果对象 o 是一个 bytes 对象或者 bytes 类型的子类型的实例则返回真值。 此函数总是会成功执行。

int PyBytes_CheckExact(PyObject *o)

如果对象 o 是一个 bytes 对象但不是 bytes 类型的子类型的实例则返回真值。 此函数总是会成功执行。

PyObject *PyBytes_FromString(const char *v)
返回值:新的引用。 属于 稳定 ABI.

成功时返回一个以字符串 v 的副本为值的新字节串对象,失败时返回 NULL。 形参 v 不可为 NULL;它不会被检查。

PyObject *PyBytes_FromStringAndSize(const char *v, Py_ssize_t len)
返回值:新的引用。 属于 稳定 ABI.

成功时返回一个以字符串 v 的副本为值且长度为 len 的新字节串对象,失败时返回 NULL。 如果 vNULL,则不初始化字节串对象的内容。

自 3.15 版本弃用: PyBytes_FromStringAndSize(NULL, len) is soft deprecated, use the PyBytesWriter API instead.

PyObject *PyBytes_FromFormat(const char *format, ...)
返回值:新的引用。 属于 稳定 ABI.

接受一个 C printf() 风格的 format 字符串和可变数量的参数,计算结果 Python 字节串对象的大小并返回参数值经格式化后的字节串对象。 可变数量的参数必须均为 C 类型并且必须恰好与 format 字符串中的格式字符相对应。 允许使用下列格式字符串:

格式字符

类型

注释

%%

不适用

文字%字符。

%c

int

一个字节,被表示为一个 C 语言的整型

%d

int

相当于 printf("%d"). [1]

%u

unsigned int

相当于 printf("%u"). [1]

%ld

长整型

相当于 printf("%ld"). [1]

%lu

unsigned long

相当于 printf("%lu"). [1]

%zd

Py_ssize_t

相当于 printf("%zd"). [1]

%zu

size_t

相当于 printf("%zu"). [1]

%i

int

相当于 printf("%i"). [1]

%x

int

相当于 printf("%x"). [1]

%s

const char*

以 null 为终止符的 C 字符数组。

%p

const void*

一个 C 指针的十六进制表示形式。 基本等价于 printf("%p") 但它会确保以字面值 0x 开头,不论系统平台上 printf 的输出是什么。

无法识别的格式字符会导致将格式字符串的其余所有内容原样复制到结果对象,并丢弃所有多余的参数。

PyObject *PyBytes_FromFormatV(const char *format, va_list vargs)
返回值:新的引用。 属于 稳定 ABI.

PyBytes_FromFormat() 完全相同,除了它需要两个参数。

PyObject *PyBytes_FromObject(PyObject *o)
返回值:新的引用。 属于 稳定 ABI.

返回字节表示实现缓冲区协议的对象*o*。

Py_ssize_t PyBytes_Size(PyObject *o)
属于 稳定 ABI.

返回字节对象*o*中字节的长度。

Py_ssize_t PyBytes_GET_SIZE(PyObject *o)

类似于 PyBytes_Size(),但是不带错误检测。

char *PyBytes_AsString(PyObject *o)
属于 稳定 ABI.

返回对应 o 的内容的指针。 该指针指向 o 的内部缓冲区,其中包含 len(o) + 1 个字节。 缓冲区的最后一个字节总是为空,不论是否存在其他空字节。 该数据不可通过任何形式来修改,除非是刚使用 PyBytes_FromStringAndSize(NULL, size) 创建该对象。 它不可被撤销分配。 如果 o 根本不是一个字节串对象,则 PyBytes_AsString() 将返回 NULL 并引发 TypeError

char *PyBytes_AS_STRING(PyObject *string)

类似于 PyBytes_AsString(),但是不带错误检测。

int PyBytes_AsStringAndSize(PyObject *obj, char **buffer, Py_ssize_t *length)
属于 稳定 ABI.

通过输出变量 bufferlength 返回对象 obj 以空值作为结束的内容。 成功时返回 0

如果 lengthNULL,字节串对象就不包含嵌入的空字节;如果包含,则该函数将返回 -1 并引发 ValueError

该缓冲区指向 obj 的内部缓冲,它的末尾包含一个额外的空字节(不算在 length 当中)。 该数据不可通过任何方式来修改,除非是刚使用 PyBytes_FromStringAndSize(NULL, size) 创建该对象。 它不可被撤销分配。 如果 obj 根本不是一个字节串对象,则 PyBytes_AsStringAndSize() 将返回 -1 并引发 TypeError

在 3.5 版本发生变更: 以前,当字节串对象中出现嵌入的空字节时将引发 TypeError

void PyBytes_Concat(PyObject **bytes, PyObject *newpart)
属于 稳定 ABI.

*bytes 中创建新的字节串对象,其中包含添加到 bytesnewpart 的内容;调用者将获得新的引用。 对 bytes 原值的引用将被收回。 如果无法创建新对象,对 bytes 的旧引用仍将被丢弃且 *bytes 的值将被设为 NULL;并将设置适当的异常。

void PyBytes_ConcatAndDel(PyObject **bytes, PyObject *newpart)
属于 稳定 ABI.

*bytes 中创建一个新的字节串对象,其中包含添加到 bytesnewpart 的内容。 此版本将释放对 newpartstrong reference (即递减其引用计数)。

PyObject *PyBytes_Join(PyObject *sep, PyObject *iterable)

类似于 Python 中的 sep.join(iterable)

sep 必须为 Python bytes 对象。 (请注意 PyUnicode_Join() 接受 NULL 分隔符并会将其视为空格,而 PyBytes_Join() 则不接受 NULL 分隔符。)

iterable 必须是一个产生实现了 缓冲区协议 的对象的可迭代对象。

成功时,返回一个新的 bytes 对象。 失败时,设置一个异常并返回 NULL

Added in version 3.14.

int _PyBytes_Resize(PyObject **bytes, Py_ssize_t newsize)

改变字节串对象的大小。 newsize 将为字节串对象的新长度。 你可以将它当作是创建一个新的字节串对象并销毁旧的对象,但是更为高效。 传入一个现有字节串对象的地址作为 lvalue(它可以被写入),以及想要的新大小。 当成功时,*bytes 将存放改变大小后的字节串对象并返回 0*bytes 中的地址可能与其输入值不同。 如果重新分配失败,则位于 *bytes 的原始字节串对象将被释放,*bytes 将被设为 NULL,同时设置 MemoryError,然后返回 -1

自 3.15 版本弃用: The function is soft deprecated, use the PyBytesWriter API instead.

PyBytesWriter

The PyBytesWriter API can be used to create a Python bytes object.

Added in version 3.15.0a0 (unreleased).

type PyBytesWriter

A bytes writer instance.

The API is not thread safe: a writer should only be used by a single thread at the same time.

The instance must be destroyed by PyBytesWriter_Finish() on success, or PyBytesWriter_Discard() on error.

Create, Finish, Discard

PyBytesWriter *PyBytesWriter_Create(Py_ssize_t size)

Create a PyBytesWriter to write size bytes.

If size is greater than zero, allocate size bytes, and set the writer size to size. The caller is responsible to write size bytes using PyBytesWriter_GetData().

On error, set an exception and return NULL.

size must be positive or zero.

PyObject *PyBytesWriter_Finish(PyBytesWriter *writer)

Finish a PyBytesWriter created by PyBytesWriter_Create().

On success, return a Python bytes object. On error, set an exception and return NULL.

The writer instance is invalid after the call in any case. No API can be called on the writer after PyBytesWriter_Finish().

PyObject *PyBytesWriter_FinishWithSize(PyBytesWriter *writer, Py_ssize_t size)

Similar to PyBytesWriter_Finish(), but resize the writer to size bytes before creating the bytes object.

PyObject *PyBytesWriter_FinishWithPointer(PyBytesWriter *writer, void *buf)

Similar to PyBytesWriter_Finish(), but resize the writer using buf pointer before creating the bytes object.

Set an exception and return NULL if buf pointer is outside the internal buffer bounds.

Function pseudo-code:

Py_ssize_t size = (char*)buf - (char*)PyBytesWriter_GetData(writer);
return PyBytesWriter_FinishWithSize(writer, size);
void PyBytesWriter_Discard(PyBytesWriter *writer)

Discard a PyBytesWriter created by PyBytesWriter_Create().

Do nothing if writer is NULL.

The writer instance is invalid after the call. No API can be called on the writer after PyBytesWriter_Discard().

High-level API

int PyBytesWriter_WriteBytes(PyBytesWriter *writer, const void *bytes, Py_ssize_t size)

Grow the writer internal buffer by size bytes, write size bytes of bytes at the writer end, and add size to the writer size.

If size is equal to -1, call strlen(bytes) to get the string length.

On success, return 0. On error, set an exception and return -1.

int PyBytesWriter_Format(PyBytesWriter *writer, const char *format, ...)

Similar to PyBytes_FromFormat(), but write the output directly at the writer end. Grow the writer internal buffer on demand. Then add the written size to the writer size.

On success, return 0. On error, set an exception and return -1.

Getters

Py_ssize_t PyBytesWriter_GetSize(PyBytesWriter *writer)

Get the writer size.

void *PyBytesWriter_GetData(PyBytesWriter *writer)

Get the writer data: start of the internal buffer.

The pointer is valid until PyBytesWriter_Finish() or PyBytesWriter_Discard() is called on writer.

Low-level API

int PyBytesWriter_Resize(PyBytesWriter *writer, Py_ssize_t size)

Resize the writer to size bytes. It can be used to enlarge or to shrink the writer.

Newly allocated bytes are left uninitialized.

On success, return 0. On error, set an exception and return -1.

size must be positive or zero.

int PyBytesWriter_Grow(PyBytesWriter *writer, Py_ssize_t grow)

Resize the writer by adding grow bytes to the current writer size.

Newly allocated bytes are left uninitialized.

On success, return 0. On error, set an exception and return -1.

size can be negative to shrink the writer.

void *PyBytesWriter_GrowAndUpdatePointer(PyBytesWriter *writer, Py_ssize_t size, void *buf)

Similar to PyBytesWriter_Grow(), but update also the buf pointer.

The buf pointer is moved if the internal buffer is moved in memory. The buf relative position within the internal buffer is left unchanged.

On error, set an exception and return NULL.

buf must not be NULL.

Function pseudo-code:

Py_ssize_t pos = (char*)buf - (char*)PyBytesWriter_GetData(writer);
if (PyBytesWriter_Grow(writer, size) < 0) {
    return NULL;
}
return (char*)PyBytesWriter_GetData(writer) + pos;