位元組陣列物件 (Byte Array Objects)

type PyByteArrayObject

這個 PyObject 的子型別代表了 Python 的位元組陣列物件。

PyTypeObject PyByteArray_Type
穩定 ABI 的一部分.

這個 PyTypeObject 的實例代表了 Python 的位元組陣列型別;在 Python 層中的 bytearray 為同一個物件。

型別檢查巨集

int PyByteArray_Check(PyObject *o)

如果物件 o 是一個位元組陣列物件,或者是位元組陣列型別的子型別的實例,則回傳真值。此函式總是會成功執行。

int PyByteArray_CheckExact(PyObject *o)

如果物件 o 是一個位元組陣列物件,但不是位元組陣列型別的子型別的實例,則回傳真值。此函式總是會成功執行。

直接 API 函式

PyObject *PyByteArray_FromObject(PyObject *o)
回傳值:新的參照。穩定 ABI 的一部分. Thread safety: Safe for concurrent use on the same object.

由任意物件 o 回傳一個新的位元組陣列物件,並實作了緩衝協定 (buffer protocol)

在失敗時,會回傳 NULL 並設定例外。

備註

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

PyObject *PyByteArray_FromStringAndSize(const char *string, Py_ssize_t len)
回傳值:新的參照。穩定 ABI 的一部分. Thread safety: Atomic.

string 及其長度 len 建立一個新的位元組陣列物件。

在失敗時,會回傳 NULL 並設定例外。

PyObject *PyByteArray_Concat(PyObject *a, PyObject *b)
回傳值:新的參照。穩定 ABI 的一部分. Thread safety: Safe for concurrent use on the same object.

連接位元組陣列 ab,並回傳一個包含結果的新位元組陣列。

在失敗時,會回傳 NULL 並設定例外。

備註

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

Py_ssize_t PyByteArray_Size(PyObject *bytearray)
穩定 ABI 的一部分. Thread safety: Atomic.

在檢查為 NULL 指標後,回傳 bytearray 的大小。

char *PyByteArray_AsString(PyObject *bytearray)
穩定 ABI 的一部分. Thread safety: Safe to call from multiple threads with external synchronization only.

在檢查是否為 NULL 指標後,將 bytearray 的內容回傳為字元陣列。回傳的陣列總是會多附加一個空位元組。

備註

It is not thread-safe to mutate the bytearray object while using the returned char array.

int PyByteArray_Resize(PyObject *bytearray, Py_ssize_t len)
穩定 ABI 的一部分.

bytearray 的內部緩衝區大小調整為 len。失敗時會回傳 -1 並設定例外。

在 3.14 版的變更: 負的 len 現在會設定例外並回傳 -1。

巨集

這些巨集犧牲了安全性以換取速度,並且它們不會檢查指標。

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

PyByteArray_AsString() 類似,但沒有錯誤檢查。

備註

It is not thread-safe to mutate the bytearray object while using the returned char array.

Py_ssize_t PyByteArray_GET_SIZE(PyObject *bytearray)
Thread safety: Atomic.

PyByteArray_Size() 類似,但沒有錯誤檢查。