[pygobject/bochecha/gvariant-unpack: 2/2] variant: Define the LEAF_ACCESSORS globally



commit 55fe93eb86df329ab6dbb06612dcb67330e55362
Author: Mathieu Bridon <mathieu hashbang fr>
Date:   Tue Jul 23 15:32:53 2019 +0200

    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.

 gi/overrides/GLib.py | 35 ++++++++++++++++++-----------------
 1 file 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('('):


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]