diff options
author | Mathieu Bridon <mathieu@hashbang.fr> | 2019-07-23 15:32:53 +0200 |
---|---|---|
committer | Mathieu Bridon <mathieu@hashbang.fr> | 2019-07-23 15:39:02 +0200 |
commit | 55fe93eb86df329ab6dbb06612dcb67330e55362 (patch) | |
tree | 4cc59a9c7a4c92487f4a04c59e72c1e611596f16 | |
parent | a7149a0ba83921591fe309a9cde0f8e43b55739c (diff) | |
download | pygobject-55fe93eb86df329ab6dbb06612dcb67330e55362.tar.gz |
variant: Define the LEAF_ACCESSORS globally
This saves a bit of work, instead of defining the dictionary every time
the method is called.
Unpacking many GVariants in a loop, this shaved roughly 17% of the total
time.
-rw-r--r-- | gi/overrides/GLib.py | 35 |
1 files changed, 18 insertions, 17 deletions
diff --git a/gi/overrides/GLib.py b/gi/overrides/GLib.py index a383f583..83cbb7fb 100644 --- a/gi/overrides/GLib.py +++ b/gi/overrides/GLib.py @@ -153,6 +153,23 @@ class _VariantCreator(object): return builder.end() +LEAF_ACCESSORS = { + 'b': 'get_boolean', + 'y': 'get_byte', + 'n': 'get_int16', + 'q': 'get_uint16', + 'i': 'get_int32', + 'u': 'get_uint32', + 'x': 'get_int64', + 't': 'get_uint64', + 'h': 'get_handle', + 'd': 'get_double', + 's': 'get_string', + 'o': 'get_string', # object path + 'g': 'get_string', # signature +} + + class Variant(GLib.Variant): def __new__(cls, format_string, value): """Create a GVariant from a native Python object. @@ -220,28 +237,12 @@ class Variant(GLib.Variant): def unpack(self): """Decompose a GVariant into a native Python object.""" - LEAF_ACCESSORS = { - 'b': self.get_boolean, - 'y': self.get_byte, - 'n': self.get_int16, - 'q': self.get_uint16, - 'i': self.get_int32, - 'u': self.get_uint32, - 'x': self.get_int64, - 't': self.get_uint64, - 'h': self.get_handle, - 'd': self.get_double, - 's': self.get_string, - 'o': self.get_string, # object path - 'g': self.get_string, # signature - } - type_string = self.get_type_string() # simple values la = LEAF_ACCESSORS.get(type_string) if la: - return la() + return getattr(self, la)() # tuple if type_string.startswith('('): |