bytes 对象

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

    PyBytes_Type

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

    int PyBytes_Check(PyObject *o)

    如果对象 o 是字节对象或字节类型的子类型的实例,则返回 true。

    int PyBytes_CheckExact( *o)

    如果对象 o 是字节对象,但不是字节类型子类型的实例,则返回 true。

    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.

    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 字符串中的格式字符相对应。 允许使用下列格式字符串:

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

    PyBytes_FromFormatV(const char **format, va_list vargs)

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

    (PyObject **o)

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

    Py_ssize_t PyBytes_Size( *o)

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

    Py_ssize_t PyBytes_GET_SIZE(PyObject *o)

    宏版本的 但是不带错误检查。

    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 using PyBytes_FromStringAndSize(NULL, size). It must not be deallocated. If o is not a bytes object at all, returns NULL and raises TypeError.

    char PyBytes_AS_STRING( **string)

    宏版本的 PyBytes_AsString() 但是不带错误检测。

    int PyBytes_AsStringAndSize( obj, char buffer, Py_ssize_t **length)

    通过输出变量 bufferlength 返回以 null 为终止符的对象 obj 的内容。

    If length is NULL, the bytes object may not contain embedded null bytes; if it does, the function returns -1 and a ValueError is raised.

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

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

    void PyBytes_Concat(PyObject bytes, 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, newpart*)

    *bytes 中创建新的字节串对象,其中包含添加到 bytesnewpart 的内容。 此版本会减少 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, is set, and -1 is returned.