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.** Thread safety: Atomic.*

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

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

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

   自 3.15 版起已处于 Soft deprecated 状态: Use the "PyBytesWriter"
   API instead of "PyBytes_FromStringAndSize(NULL, len)".

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

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

   +---------------------+-----------------+----------------------------------+
   | 格式字符            | 类型            | 注释                             |
   |=====================|=================|==================================|
   | "%%"                | *不适用*        | 文字%字符。                      |
   +---------------------+-----------------+----------------------------------+
   | "%c"                | int             | 一个字节，被表示为一个 C int。   |
   +---------------------+-----------------+----------------------------------+
   | "%d"                | int             | 相当于 "printf("%d")"。[1]       |
   +---------------------+-----------------+----------------------------------+
   | "%u"                | unsigned int    | 相当于 "printf("%u")"。[1]       |
   +---------------------+-----------------+----------------------------------+
   | "%ld"               | long            | 相当于 "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" 的输出是什么。 |
   +---------------------+-----------------+----------------------------------+

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

   [1] 对于整数说明符（d，u，ld，lu，zd，zu，i，x）：当给出精度时，0
       转换标志是有效的。

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

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

PyObject *PyBytes_FromObject(PyObject *o)
    *返回值：新的引用。** 属于 稳定 ABI.** Thread safety: Safe for
   concurrent use on the same object.*

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

   备注:

     If the object implements the buffer protocol, then the buffer
     must not be mutated while the bytes object is being created.

Py_ssize_t PyBytes_Size(PyObject *o)
    * 属于 稳定 ABI.** Thread safety: Atomic.*

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

Py_ssize_t PyBytes_GET_SIZE(PyObject *o)
    * Thread safety: Atomic.*

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

char *PyBytes_AsString(PyObject *o)
    * 属于 稳定 ABI.** Thread safety: Safe to call from multiple
   threads with external synchronization only.*

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

char *PyBytes_AS_STRING(PyObject *string)
    * Thread safety: Safe to call from multiple threads with external
   synchronization only.*

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

int PyBytes_AsStringAndSize(PyObject *obj, char **buffer, Py_ssize_t *length)
    * 属于 稳定 ABI.** Thread safety: Safe to call from multiple
   threads with external synchronization only.*

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

   如果 *length* 为 "NULL"，字节串对象不可包含嵌入的空字节；如果包含，
   则该函数将返回 "-1" 并引发 "ValueError" 异常。

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

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

void PyBytes_Concat(PyObject **bytes, PyObject *newpart)
    * 属于 稳定 ABI.** Thread safety: Safe for concurrent use on the
   same object.*

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

   备注:

     If *newpart* implements the buffer protocol, then the buffer must
     not be mutated while the new bytes object is being created.

void PyBytes_ConcatAndDel(PyObject **bytes, PyObject *newpart)
    * 属于 稳定 ABI.** Thread safety: Safe for concurrent use on the
   same object.*

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

   备注:

     If *newpart* implements the buffer protocol, then the buffer must
     not be mutated while the new bytes object is being created.

PyObject *PyBytes_Join(PyObject *sep, PyObject *iterable)
    * Thread safety: Safe for concurrent use on the same object.*

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

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

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

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

   Added in version 3.14.

   备注:

     If *iterable* objects implement the buffer protocol, then the
     buffers must not be mutated while the new bytes object is being
     created.

int _PyBytes_Resize(PyObject **bytes, Py_ssize_t newsize)
    * Thread safety: Safe to call without external synchronization on
   distinct objects.*

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

   自 3.15 版起已处于 Soft deprecated 状态: Use the "PyBytesWriter"
   API instead.

PyObject *PyBytes_Repr(PyObject *bytes, int smartquotes)
    * 属于 稳定 ABI.** Thread safety: Atomic.*

   获取 *bytes* 的字符串表示形式。此函数目前被用于实现 Python 中的
   "bytes.__repr__()"。

   此函数不会进行类型检测；向 *bytes* 传入非字节对象或 "NULL" 是未定义
   的行为。

   如果 *smartquotes* 为真值，表示形式将在 *bytes* 中存在单引号时使用
   带双引号的字符串而不是带单引号的字符串。例如，当 *smartquotes* 为真
   值时字节串 "'Python'" 将被表示为 "b"'Python'""，当其为假值时则为
   "b'\'Python\''" 这种形式。

   成功时，此函数将返回一个指向包含该表示形式的 "str" 对象的 *strong
   reference*。失败时，此函数将返回 "NULL" 并设置一个异常。

PyObject *PyBytes_DecodeEscape(const char *s, Py_ssize_t len, const char *errors, Py_ssize_t unicode, const char *recode_encoding)
    * 属于 稳定 ABI.** Thread safety: Atomic.*

   取消带反斜杠转义的字符串 *s* 的转义。 *s* 必须不为 "NULL"。 *len*
   必须为 *s* 的长度。

   *errors* 必须为 ""strict"", ""replace"" 或 ""ignore"" 之一。如果
   *errors* 为 "NULL"，则默认使用 ""strict""。

   成功时，此函数将返回一个指向包含未转义文本的 Python "bytes" 对象的
   *strong reference*。 失败时，此函数将返回 "NULL" 并设置一个异常。

   在 3.9 版本发生变更: *unicode* 和 *recode_encoding* 现在未被使用。


PyBytesWriter
*************

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

Added in version 3.15.

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()". This function does not
   overallocate.

   发生错误时，将设置异常并返回 "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()".


高阶 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.

   如果成功，返回 "0"。如果发生错误，设置异常并返回 "-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.

   如果成功，返回 "0"。如果发生错误，设置异常并返回 "-1"。


Getters
=======

Py_ssize_t PyBytesWriter_GetSize(PyBytesWriter *writer)

   Get the writer size.

   The function cannot fail.

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*.

   The function cannot fail.


底层级 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. This function typically overallocates to achieve
   amortized performance when resizing multiple times.

   Newly allocated bytes are left uninitialized.

   如果成功，返回 "0"。如果发生错误，设置异常并返回 "-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. This function typically overallocates to achieve amortized
   performance when resizing multiple times.

   Newly allocated bytes are left uninitialized.

   如果成功，返回 "0"。如果发生错误，设置异常并返回 "-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.

   发生错误时，将设置异常并返回 "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;
