Bytes#
Added in version 2.32.
- class Bytes(**kwargs)#
A simple refcounted data type representing an immutable sequence of zero or more bytes from an unspecified origin.
The purpose of a Bytes
is to keep the memory region that it holds
alive for as long as anyone holds a reference to the bytes. When
the last reference count is dropped, the memory is released. Multiple
unrelated callers can use byte data in the Bytes
without coordinating
their activities, resting assured that the byte data will not change or
move while they hold a reference.
A Bytes
can come from many different origins that may have
different procedures for freeing the memory region. Examples are
memory from malloc()
, from memory slices, from a MappedFile
or
memory from other allocators.
Bytes
work well as keys in HashTable
. Use equal()
and
hash()
as parameters to new()
or new_full()
.
Bytes
can also be used as keys in a Tree
by passing the compare()
function to new()
.
The data pointed to by this bytes must not be modified. For a mutable
array of bytes see GByteArray
. Use unref_to_array()
to create a
mutable array for a Bytes
sequence. To create an immutable Bytes
from
a mutable GByteArray
, use the free_to_bytes()
function.
Constructors#
- class Bytes
- classmethod new(data: Sequence[int] | None = None) Bytes #
Creates a new
Bytes
fromdata
.data
is copied. Ifsize
is 0,data
may beNone
.Added in version 2.32.
- Parameters:
data – the data to be used for the bytes
- classmethod new_take(data: Sequence[int] | None = None) Bytes #
Creates a new
Bytes
fromdata
.After this call,
data
belongs to theBytes
and may no longer be modified by the caller. The memory ofdata
has to be dynamically allocated and will eventually be freed withfree()
.For creating
Bytes
with memory from other allocators, seenew_with_free_func()
.data
may beNone
ifsize
is 0.Added in version 2.32.
- Parameters:
data – the data to be used for the bytes
Methods#
- class Bytes
- compare(bytes2: Bytes) int #
Compares the two
Bytes
values.This function can be used to sort GBytes instances in lexicographical order.
If
bytes1
andbytes2
have different length but the shorter one is a prefix of the longer one then the shorter one is considered to be less than the longer one. Otherwise the first byte where both differ is used for comparison. Ifbytes1
has a smaller value at that position it is considered less, otherwise greater thanbytes2
.Added in version 2.32.
- Parameters:
bytes2 – a pointer to a
Bytes
to compare withbytes1
- equal(bytes2: Bytes) bool #
Compares the two
Bytes
values being pointed to and returnsTrue
if they are equal.This function can be passed to
new()
as thekey_equal_func
parameter, when using non-None
Bytes
pointers as keys in aHashTable
.Added in version 2.32.
- Parameters:
bytes2 – a pointer to a
Bytes
to compare withbytes1
- get_data() bytes | None #
Get the byte data in the
Bytes
. This data should not be modified.This function will always return the same pointer for a given
Bytes
.None
may be returned ifsize
is 0. This is not guaranteed, as theBytes
may represent an empty string withdata
non-None
andsize
as 0.None
will not be returned ifsize
is non-zero.Added in version 2.32.
- get_region(element_size: int, offset: int, n_elements: int) None #
Gets a pointer to a region in
bytes
.The region starts at
offset
many bytes from the start of the data and containsn_elements
many elements ofelement_size
size.n_elements
may be zero, butelement_size
must always be non-zero. Ideally,element_size
is a static constant (eg: sizeof a struct).This function does careful bounds checking (including checking for arithmetic overflows) and returns a non-
None
pointer if the specified region lies entirely within thebytes
. If the region is in some way out of range, or if an overflow has occurred, thenNone
is returned.Note: it is possible to have a valid zero-size region. In this case, the returned pointer will be equal to the base pointer of the data of
bytes
, plusoffset
. This will be non-None
except for the case wherebytes
itself was a zero-sized region. Since it is unlikely that you will be using this function to check for a zero-sized region in a zero-sizedbytes
,None
effectively always means “error”.Added in version 2.70.
- Parameters:
element_size – a non-zero element size
offset – an offset to the start of the region within the
bytes
n_elements – the number of elements in the region
- get_size() int #
Get the size of the byte data in the
Bytes
.This function will always return the same value for a given
Bytes
.Added in version 2.32.
- hash() int #
Creates an integer hash code for the byte data in the
Bytes
.This function can be passed to
new()
as thekey_hash_func
parameter, when using non-None
Bytes
pointers as keys in aHashTable
.Added in version 2.32.
- new_from_bytes(offset: int, length: int) Bytes #
Creates a
Bytes
which is a subsection of anotherBytes
. Theoffset
+length
may not be longer than the size ofbytes
.A reference to
bytes
will be held by the newly createdBytes
until the byte data is no longer needed.Since 2.56, if
offset
is 0 andlength
matches the size ofbytes
, thenbytes
will be returned with the reference count incremented by 1. Ifbytes
is a slice of anotherBytes
, then the resultingBytes
will reference the sameBytes
instead ofbytes
. This allows consumers to simplify the usage ofBytes
when asynchronously writing to streams.Added in version 2.32.
- Parameters:
offset – offset which subsection starts at
length – length of subsection