Spaces:
Running on Zero
A newer version of the Gradio SDK is available: 6.20.0
Bytes Objects {#bytesobjects}
These functions raise TypeError{.interpreted-text role="exc"} when expecting a bytes parameter and called with a non-bytes parameter.
::: index pair: object; bytes :::
This subtype of
PyObject{.interpreted-text role="c:type"} represents a Python bytes object.
This instance of
PyTypeObject{.interpreted-text role="c:type"} represents the Python bytes type; it is the same object asbytes{.interpreted-text role="class"} 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
NULLon failure. The parameter v must not beNULL; 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
NULLon failure. If v isNULL, the contents of the bytes object are uninitialized.::: deprecated 3.15
PyBytes_FromStringAndSize(NULL, len)issoft deprecated{.interpreted-text role="term"}, use thePyBytesWriter{.interpreted-text role="c:type"} API instead. :::
Take a C
printf{.interpreted-text role="c:func"}-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:::: tabularcolumns |l|l|L| :::
Format Characters Type Comment
%%n/a The literal % character.
%cint A single byte, represented as a C int.
%dint Equivalent toprintf("%d").[^1]
%uunsigned int Equivalent toprintf("%u").[^2]
%ldlong Equivalent toprintf("%ld").[^3]
%luunsigned long Equivalent toprintf("%lu").[^4]
%zd:c:type:`Py_ssize_t` Equivalent toprintf("%zd").[^5]
%zusize_t Equivalent toprintf("%zu").[^6]
%iint Equivalent toprintf("%i").[^7]
%xint Equivalent toprintf("%x").[^8]
%sconst char* A null-terminated C character array.
%pconst void* The hex representation of a C pointer. Mostly equivalent toprintf("%p")except that it is guaranteed to start with the literal0xregardless of what the platform'sprintfyields.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.
Identical to
PyBytes_FromFormat{.interpreted-text role="c:func"} except that it takes exactly two arguments.
Return the bytes representation of object o that implements the buffer protocol.
Return the length of the bytes in bytes object o.
Similar to
PyBytes_Size{.interpreted-text role="c:func"}, but without error checking.
Return a pointer to the contents of o. The pointer refers to the internal buffer of o, which consists of
len(o) + 1bytes. 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 usingPyBytes_FromStringAndSize(NULL, size). It must not be deallocated. If o is not a bytes object at all,PyBytes_AsString{.interpreted-text role="c:func"} returnsNULLand raisesTypeError{.interpreted-text role="exc"}.
Similar to
PyBytes_AsString{.interpreted-text role="c:func"}, but without error checking.
Return the null-terminated contents of the object obj through the output variables buffer and length. Returns
0on success.If length is
NULL, the bytes object may not contain embedded null bytes; if it does, the function returns-1and aValueError{.interpreted-text role="exc"} 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,PyBytes_AsStringAndSize{.interpreted-text role="c:func"} returns-1and raisesTypeError{.interpreted-text role="exc"}.::: versionchanged 3.5 Previously,
TypeError{.interpreted-text role="exc"} 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 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.
Create a new bytes object in *bytes containing the contents of newpart appended to bytes. This version releases the
strong reference{.interpreted-text role="term"} to newpart (i.e. decrements its reference count).
Similar to
sep.join(iterable)in Python.sep must be Python
bytes{.interpreted-text role="class"} object. (Note thatPyUnicode_Join{.interpreted-text role="c:func"} acceptsNULLseparator and treats it as a space, whereasPyBytes_Join{.interpreted-text role="c:func"} doesn't acceptNULLseparator.)iterable must be an iterable object yielding objects that implement the
buffer protocol <bufferobjects>{.interpreted-text role="ref"}.On success, return a new
bytes{.interpreted-text role="class"} object. On error, set an exception and returnNULL.::: versionadded 3.14 :::
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
0is 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 toNULL,MemoryError{.interpreted-text role="exc"} is set, and-1is returned.::: deprecated 3.15 The function is
soft deprecated{.interpreted-text role="term"}, use thePyBytesWriter{.interpreted-text role="c:type"} API instead. :::
Get the string representation of bytes. This function is currently used to implement
!bytes.__repr__{.interpreted-text role="meth"} in Python.This function does not do type checking; it is undefined behavior to pass bytes as a non-bytes object or
NULL.If smartquotes is true, the representation will use a double-quoted string instead of single-quoted string when single-quotes are present in bytes. For example, the byte string
'Python'would be represented asb"'Python'"when smartquotes is true, orb'\'Python\''when it is false.On success, this function returns a
strong reference{.interpreted-text role="term"} to astr{.interpreted-text role="class"} object containing the representation. On failure, this returnsNULLwith an exception set.
Unescape a backslash-escaped string s. s must not be
NULL. len must be the size of s.errors must be one of
"strict","replace", or"ignore". If errors isNULL, then"strict"is used by default.On success, this function returns a
strong reference{.interpreted-text role="term"} to a Pythonbytes{.interpreted-text role="class"} object containing the unescaped string. On failure, this function returnsNULLwith an exception set.::: versionchanged 3.9 unicode and recode_encoding are now unused. :::
PyBytesWriter
The PyBytesWriter{.interpreted-text role="c:type"} API can be used to create a Python bytes{.interpreted-text role="class"} object.
::: versionadded 3.15 :::
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{.interpreted-text role="c:func"} on success, orPyBytesWriter_Discard{.interpreted-text role="c:func"} on error.
Create, Finish, Discard
Create a
PyBytesWriter{.interpreted-text role="c:type"} 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{.interpreted-text role="c:func"}. This function does not overallocate.On error, set an exception and return
NULL.size must be positive or zero.
Finish a
PyBytesWriter{.interpreted-text role="c:type"} created byPyBytesWriter_Create{.interpreted-text role="c:func"}.On success, return a Python
bytes{.interpreted-text role="class"} object. On error, set an exception and returnNULL.The writer instance is invalid after the call in any case. No API can be called on the writer after
PyBytesWriter_Finish{.interpreted-text role="c:func"}.
Similar to
PyBytesWriter_Finish{.interpreted-text role="c:func"}, but resize the writer to size bytes before creating thebytes{.interpreted-text role="class"} object.
Similar to
PyBytesWriter_Finish{.interpreted-text role="c:func"}, but resize the writer using buf pointer before creating thebytes{.interpreted-text role="class"} object.Set an exception and return
NULLif 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);
Discard a
PyBytesWriter{.interpreted-text role="c:type"} created byPyBytesWriter_Create{.interpreted-text role="c:func"}.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{.interpreted-text role="c:func"}.
High-level API
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, callstrlen(bytes)to get the string length.On success, return
0. On error, set an exception and return-1.
Similar to
PyBytes_FromFormat{.interpreted-text role="c:func"}, 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
Get the writer size.
Get the writer data: start of the internal buffer.
The pointer is valid until
PyBytesWriter_Finish{.interpreted-text role="c:func"} orPyBytesWriter_Discard{.interpreted-text role="c:func"} is called on writer.
Low-level API
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.
On success, return
0. On error, set an exception and return-1.size must be positive or zero.
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.
On success, return
0. On error, set an exception and return-1.size can be negative to shrink the writer.
Similar to
PyBytesWriter_Grow{.interpreted-text role="c:func"}, 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;
[^1]: For integer specifiers (d, u, ld, lu, zd, zu, i, x): the 0-conversion flag has effect even when a precision is given.
[^2]: For integer specifiers (d, u, ld, lu, zd, zu, i, x): the 0-conversion flag has effect even when a precision is given.
[^3]: For integer specifiers (d, u, ld, lu, zd, zu, i, x): the 0-conversion flag has effect even when a precision is given.
[^4]: For integer specifiers (d, u, ld, lu, zd, zu, i, x): the 0-conversion flag has effect even when a precision is given.
[^5]: For integer specifiers (d, u, ld, lu, zd, zu, i, x): the 0-conversion flag has effect even when a precision is given.
[^6]: For integer specifiers (d, u, ld, lu, zd, zu, i, x): the 0-conversion flag has effect even when a precision is given.
[^7]: For integer specifiers (d, u, ld, lu, zd, zu, i, x): the 0-conversion flag has effect even when a precision is given.
[^8]: For integer specifiers (d, u, ld, lu, zd, zu, i, x): the 0-conversion flag has effect even when a precision is given.