VariantType
Added in version 2.24.
- class VariantType(**kwargs)
- Constructors:
new(type_string:str) -> GLib.VariantType
new_array(element:GLib.VariantType) -> GLib.VariantType
new_dict_entry(key:GLib.VariantType, value:GLib.VariantType) -> GLib.VariantType
new_maybe(element:GLib.VariantType) -> GLib.VariantType
new_tuple(items:list) -> GLib.VariantType
Constructors
- class VariantType
- classmethod new(type_string: str) VariantType
Creates a new
VariantType
corresponding to the type string given bytype_string
. It is appropriate to callfree()
on the return value.It is a programmer error to call this function with an invalid type string. Use
string_is_valid()
if you are unsure.Added in version 2.24.
- Parameters:
type_string – a valid GVariant type string
- classmethod new_array(element: VariantType) VariantType
Constructs the type corresponding to an array of elements of the type
type
.It is appropriate to call
free()
on the return value.- Parameters:
element – a
VariantType
- classmethod new_dict_entry(key: VariantType, value: VariantType) VariantType
Constructs the type corresponding to a dictionary entry with a key of type
key
and a value of typevalue
.It is appropriate to call
free()
on the return value.- Parameters:
key – a basic
VariantType
value – a
VariantType
- classmethod new_maybe(element: VariantType) VariantType
Constructs the type corresponding to a maybe instance containing type
type
or Nothing.It is appropriate to call
free()
on the return value.- Parameters:
element – a
VariantType
- classmethod new_tuple(items: Sequence[VariantType]) VariantType
Constructs a new tuple type, from
items
.length
is the number of items initems
, or -1 to indicate thatitems
isNone
-terminated.It is appropriate to call
free()
on the return value.- Parameters:
items – an array of
VariantType
, one for each item
Methods
- class VariantType
- checked_(type_string: str) VariantType
- Parameters:
type_string
- dup_string() str
Returns a newly-allocated copy of the type string corresponding to
type
. The returned string is nul-terminated. It is appropriate to callfree()
on the return value.
- element() VariantType
Determines the element type of an array or maybe type.
This function may only be used with array or maybe types.
- equal(type2: VariantType) bool
Compares
type1
andtype2
for equality.Only returns
True
if the types are exactly equal. Even if one type is an indefinite type and the other is a subtype of it,False
will be returned if they are not exactly equal. If you want to check for subtypes, useis_subtype_of()
.The argument types of
type1
andtype2
are onlygpointer
to allow use withHashTable
without function pointer casting. For both arguments, a validVariantType
must be provided.- Parameters:
type2 – a
VariantType
- first() VariantType
Determines the first item type of a tuple or dictionary entry type.
This function may only be used with tuple or dictionary entry types, but must not be used with the generic tuple type
%G_VARIANT_TYPE_TUPLE
.In the case of a dictionary entry type, this returns the type of the key.
None
is returned in case oftype
being%G_VARIANT_TYPE_UNIT
.This call, together with
next()
provides an iterator interface over tuple and dictionary entry types.
- free() None
Frees a
VariantType
that was allocated withcopy()
,new()
or one of the container type constructor functions.In the case that
type
isNone
, this function does nothing.Since 2.24
- get_string_length() int
Returns the length of the type string corresponding to the given
type
. This function must be used to determine the valid extent of the memory region returned bypeek_string()
.
- hash() int
Hashes
type
.The argument type of
type
is onlygpointer
to allow use withHashTable
without function pointer casting. A validVariantType
must be provided.
- is_array() bool
Determines if the given
type
is an array type. This is true if the type string fortype
starts with an ‘a’.This function returns
True
for any indefinite type for which every definite subtype is an array type –%G_VARIANT_TYPE_ARRAY
, for example.
- is_basic() bool
Determines if the given
type
is a basic type.Basic types are booleans, bytes, integers, doubles, strings, object paths and signatures.
Only a basic type may be used as the key of a dictionary entry.
This function returns
False
for all indefinite types except%G_VARIANT_TYPE_BASIC
.
- is_container() bool
Determines if the given
type
is a container type.Container types are any array, maybe, tuple, or dictionary entry types plus the variant type.
This function returns
True
for any indefinite type for which every definite subtype is a container –%G_VARIANT_TYPE_ARRAY
, for example.
- is_definite() bool
Determines if the given
type
is definite (ie: not indefinite).A type is definite if its type string does not contain any indefinite type characters (‘*’, ‘?’, or ‘r’).
A
Variant
instance may not have an indefinite type, so calling this function on the result ofget_type()
will always result inTrue
being returned. Calling this function on an indefinite type like%G_VARIANT_TYPE_ARRAY
, however, will result inFalse
being returned.
- is_dict_entry() bool
Determines if the given
type
is a dictionary entry type. This is true if the type string fortype
starts with a ‘{‘.This function returns
True
for any indefinite type for which every definite subtype is a dictionary entry type –%G_VARIANT_TYPE_DICT_ENTRY
, for example.
- is_maybe() bool
Determines if the given
type
is a maybe type. This is true if the type string fortype
starts with an ‘m’.This function returns
True
for any indefinite type for which every definite subtype is a maybe type –%G_VARIANT_TYPE_MAYBE
, for example.
- is_subtype_of(supertype: VariantType) bool
Checks if
type
is a subtype ofsupertype
.This function returns
True
iftype
is a subtype ofsupertype
. All types are considered to be subtypes of themselves. Aside from that, only indefinite types can have subtypes.- Parameters:
supertype – a
VariantType
- is_tuple() bool
Determines if the given
type
is a tuple type. This is true if the type string fortype
starts with a ‘(’ or iftype
is%G_VARIANT_TYPE_TUPLE
.This function returns
True
for any indefinite type for which every definite subtype is a tuple type –%G_VARIANT_TYPE_TUPLE
, for example.
- key() VariantType
Determines the key type of a dictionary entry type.
This function may only be used with a dictionary entry type. Other than the additional restriction, this call is equivalent to
first()
.
- n_items() int
Determines the number of items contained in a tuple or dictionary entry type.
This function may only be used with tuple or dictionary entry types, but must not be used with the generic tuple type
%G_VARIANT_TYPE_TUPLE
.In the case of a dictionary entry type, this function will always return 2.
- next() VariantType
Determines the next item type of a tuple or dictionary entry type.
type
must be the result of a previous call tofirst()
ornext()
.If called on the key type of a dictionary entry then this call returns the value type. If called on the value type of a dictionary entry then this call returns
None
.For tuples,
None
is returned whentype
is the last item in a tuple.
- string_is_valid(type_string: str) bool
Checks if
type_string
is a valid GVariant type string. This call is equivalent to callingstring_scan()
and confirming that the following character is a nul terminator.- Parameters:
type_string – a pointer to any string
- string_scan(string: str, limit: str | None = None) tuple[bool, str]
Scan for a single complete and valid GVariant type string in
string
. The memory pointed to bylimit
(or bytes beyond it) is never accessed.If a valid type string is found,
endptr
is updated to point to the first character past the end of the string that was found andTrue
is returned.If there is no valid type string starting at
string
, or if the type string does not end beforelimit
thenFalse
is returned.For the simple case of checking if a string is a valid type string, see
string_is_valid()
.Added in version 2.24.
- Parameters:
string – a pointer to any string
limit – the end of
string
, orNone
- value() VariantType
Determines the value type of a dictionary entry type.
This function may only be used with a dictionary entry type.