Teks sumber — English
Terjemahan — Bahasa Indonesia
Bytes Objects
Bayt Nesneleri
The internal buffer of :c:type:`PyBytesObject` always includes an extra trailing null byte for compatibility with null terminated C strings. This extra byte is not counted in :c:func:`PyBytes_Size` nor in the various *length* and *size* arguments of the functions below.
This subtype of :c:type:`PyObject` represents a Python bytes object.
This instance of :c:type:`PyTypeObject` represents the Python bytes type; it is the same object as :class:`bytes` in the Python layer.
Return true if the object *o* is a bytes object or an instance of a subtype of the bytes type. This function always succeeds.
Return true if the object *o* is a bytes object, but not an instance of a subtype of the bytes type. This function always succeeds.
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.
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.
Take a C :c:func:`printf`\ -style *format* string and a variable number of arguments, calculate the size of the resulting Python bytes object and return a bytes object with the values formatted into it. The variable arguments must be C types and must correspond exactly to the format characters in the *format* string. The following format characters are allowed:
An unrecognized format character causes all the rest of the format string to be copied as-is to the result object, and any extra arguments discarded.
Return the bytes representation of object *o* that implements the buffer protocol.
If the object implements the buffer protocol, then the buffer must not be mutated while the bytes object is being created.
Return the length of the bytes in bytes object *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, :c:func:`PyBytes_AsString` returns ``NULL`` and raises :exc:`TypeError`.
Return the null-terminated contents of the object *obj* through the output variables *buffer* and *length*. Returns ``0`` on success.
If *length* is ``NULL``, the bytes object may not contain embedded null bytes; if it does, the function returns ``-1`` and a :exc:`ValueError` is raised.
The buffer refers to an internal buffer of *obj*, which includes an additional null byte at the end (not counted in *length*). 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 *obj* is not a bytes object at all, :c:func:`PyBytes_AsStringAndSize` returns ``-1`` and raises :exc:`TypeError`.
Previously, :exc:`TypeError` was raised when embedded null bytes were encountered in the bytes object.
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 ":term:`stolen <steal>`". If the new object cannot be created, the old reference to *bytes* will still be "stolen", the value of *\*bytes* will be set to ``NULL``, and the appropriate exception will be set.
If *newpart* implements the buffer protocol, then the buffer must not be mutated while the new bytes object is being created.
Create a new bytes object in *\*bytes* containing the contents of *newpart* appended to *bytes*. This version releases the :term:`strong reference` to *newpart* (i.e. decrements its reference count).
*sep* must be Python :class:`bytes` object. (Note that :c:func:`PyUnicode_Join` accepts ``NULL`` separator and treats it as a space, whereas :c:func:`PyBytes_Join` doesn't accept ``NULL`` separator.)
*iterable* must be an iterable object yielding objects that implement the :ref:`buffer protocol <bufferobjects>`.
On success, return a new :class:`bytes` object. On error, set an exception and return ``NULL``.
If *iterable* objects implement the buffer protocol, then the buffers must not be mutated while the new bytes object is being created.
Resize a bytes object. *newsize* will be the new length of the bytes object. You can think of it as creating a new bytes object and destroying the old one, only more efficiently. 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``, :exc:`MemoryError` is set, and ``-1`` is returned.
This function does not do type checking; it is undefined behavior to pass *bytes* as a non-bytes object or ``NULL``.
On success, this function returns a :term:`strong reference` to a :class:`str` object containing the representation. On failure, this returns ``NULL`` with an exception set.
On success, this function returns a :term:`strong reference` to a Python :class:`bytes` object containing the unescaped string. On failure, this function returns ``NULL`` with an exception set.
The :c:type:`PyBytesWriter` API can be used to create a Python :class:`bytes` object.
On success, return a Python :class:`bytes` object. On error, set an exception and return ``NULL``.
Similar to :c:func:`PyBytesWriter_Finish`, but resize the writer to *size* bytes before creating the :class:`bytes` object.
Similar to :c:func:`PyBytesWriter_Finish`, but resize the writer using *buf* pointer before creating the :class:`bytes` object.
PyObject* create_abc(void)
{
PyBytesWriter *writer = PyBytesWriter_Create(3);
if (writer == NULL) {
return NULL;
}
char *str = PyBytesWriter_GetData(writer);
memcpy(str, "abc", 3);
return PyBytesWriter_Finish(writer);
}
PyObject* create_abc_ptr(void)
{
PyBytesWriter *writer = PyBytesWriter_Create(3);
if (writer == NULL) {
return NULL;
}
char *str = PyBytesWriter_GetData(writer);
memcpy(str, "abc", 3);
str += 3;
return PyBytesWriter_FinishWithPointer(writer, str);
}
int copy_bytes(char *dest, const char *src, Py_ssize_t num_bytes)
{
...
}
PyObject* create_string(const char *start, Py_ssize_t num_bytes)
{
PyObject *result = PyBytes_FromStringAndSize(NULL, num_bytes);
if (result == NULL) {
return NULL;
}
if (copy_bytes(PyBytes_AS_STRING(result), start, num_bytes) < 0) {
Py_CLEAR(result);
}
return result;
}
PyObject* create_string(const char *start, Py_ssize_t num_bytes)
{
PyBytesWriter *writer = PyBytesWriter_Create(num_bytes);
if (writer == NULL) {
return NULL;
}
if (copy_bytes(PyBytesWriter_GetData(writer), start, num_bytes) < 0) {
PyBytesWriter_Discard(writer);
return NULL;
}
return PyBytesWriter_Finish(writer);
}
PyObject* create_string(Py_ssize_t size)
{
PyObject *bytes = PyBytes_FromStringAndSize(NULL, size);
if (bytes == NULL) {
return NULL;
}
char *str = PyBytes_AS_STRING(bytes);
// ... write data into str ...
if (_PyBytes_Resize(&bytes, (str - PyBytes_AS_STRING(bytes)))) {
return NULL;
}
return bytes;
}
PyObject* create_string(Py_ssize_t size)
{
PyBytesWriter *writer = PyBytesWriter_Create(size);
if (writer == NULL) {
return NULL;
}
char *str = PyBytesWriter_GetData(writer);
// ... write data into str ...
return PyBytesWriter_FinishWithPointer(writer, str);
}
object
objek